![]() |
#2
joker392008-09-09 22:02
|

#include <iostream>
#include <malloc.h>
using namespace std;
class Array
{
int num;
int *pt;
public:
Array(int n,int *p)
{
num=n;
pt=p=(int*)malloc(sizeof(int));//这个好像怪怪的,但没有又不行
}
void create();
void show();
void delSame();
void showNum();
};
void Array::create()
{
int x;
int *p;
p=pt;
int n=num;
cout<<"Please input the member of the array"<<endl;
do
{
cin>>x;
*p=x;
p++;
n--;
}while(n);
}
void Array::show()
{
int *p=pt,n=num;
while(n)
{
n--;
cout<<*p<<" ";
p++;
}
cout<<endl;
}
void Array::delSame()
{
int *p=pt;
for(int i=1;i<num;i++)
{
if(*(p+i-1)==*(p+i))
{
for(int j=i;j<num-1;j++)
*(p+j)=*(p+j+1);
num--;
i--;
}
}
}
void Array::showNum()
{
int *p=pt;
int k=1;
for(int i=0;i<num;i++)
{
for(int j=i+1;j<num;j++)
if(*(p+i)==*(p+j))
k++;
cout<<k<<" ";
i=i+k-1;
k=1;
}
cout<<endl;
}
int main()
{
int n;
int *p;
cout<<"Input the length of the array"<<endl;
cin>>n;
Array a(n,p);
a.create();
a.showNum();
a.delSame();
a.show();
system("pause");
return 0;
}
#include <malloc.h>
using namespace std;
class Array
{
int num;
int *pt;
public:
Array(int n,int *p)
{
num=n;
pt=p=(int*)malloc(sizeof(int));//这个好像怪怪的,但没有又不行
}
void create();
void show();
void delSame();
void showNum();
};
void Array::create()
{
int x;
int *p;
p=pt;
int n=num;
cout<<"Please input the member of the array"<<endl;
do
{
cin>>x;
*p=x;
p++;
n--;
}while(n);
}
void Array::show()
{
int *p=pt,n=num;
while(n)
{
n--;
cout<<*p<<" ";
p++;
}
cout<<endl;
}
void Array::delSame()
{
int *p=pt;
for(int i=1;i<num;i++)
{
if(*(p+i-1)==*(p+i))
{
for(int j=i;j<num-1;j++)
*(p+j)=*(p+j+1);
num--;
i--;
}
}
}
void Array::showNum()
{
int *p=pt;
int k=1;
for(int i=0;i<num;i++)
{
for(int j=i+1;j<num;j++)
if(*(p+i)==*(p+j))
k++;
cout<<k<<" ";
i=i+k-1;
k=1;
}
cout<<endl;
}
int main()
{
int n;
int *p;
cout<<"Input the length of the array"<<endl;
cin>>n;
Array a(n,p);
a.create();
a.showNum();
a.delSame();
a.show();
system("pause");
return 0;
}
程序是删除一个升序数组中相同的项,然后还要统计相同项个数,要求用指针动态开辟,不是链表。我用devcpp都调试好了,都没问题,就是如果数字大于26的话,程序就会自动关闭,25或者更小就完全没问题。实在看不出哪里问题啊!估计是指针使用不当,麻烦哪位高手帮忙解释解释!刚学c++不久,以前接触过学过c,但是连入门都不算,指针很多的有问题。