您的位置:首页 > 编程语言 > C语言/C++

c++ 程序设计实践指导1.1

2009-02-24 22:55 441 查看
1.1程序改写

要求以数组为结构的程序改写为以指针为数据结构

解:

//2009 Tonee hannsoft@163.com

#include <iostream>

using namespace std;

class ARP
{
int m;
int* p;
public:
ARP(int x[],int size)
{
m = size;
p = new int [m];
for (int i =0;i<m;i++)
{
p[i]=x[i];
}
}
void delsame();
void show();
~ARP()
{
delete [] p;// delete p???
}
};
void ARP::show()
{
for(int i=0;i<m;i++)
{
cout<<p[i]<<"/t";
if((i + 1 ) % 5 == 0 )
cout<<endl;
}
cout<<endl;
}
void ARP::delsame()
{
int i,j;
for(i=0;i<m-1;i++)
{
if(p[i]==p[i+1])
{
for(j=i+1;j<m-1;j++)
{
p[j]=p[j+1];
}
m --;
i --;
}
}
}
int main()
{
int b[16] = {1,2,2,3,4,4,5,6,6,7,8,8,8,9,10,10};
ARP temp(b,sizeof(b)/sizeof(b[0]));
temp.show();
temp.delsame();
temp.show();
return 0;
}


我的目标是Cpp enginerr what about you?
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: