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

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

2009-02-25 21:05 288 查看
统计指针数组中每个元素出现的个数

#include <iostream>

using namespace std;

class ARP
{
int m;
int* pa; //array *P
int* ps; // sum of same;
public:
ARP(int x[],int size)
{
m = size;
pa = new int[m];
ps = new int[m];
for(int i = 0;i<m;i++)
{
pa[i] = x[i];
ps[i] = 1;
}
}
void show();
void delsame();
void showcount()
{
for(int i = 0;i<m;i++)
{
cout<<ps[i]<<"  ";
}
cout<<endl;
}
~ARP()
{
delete [] ps;
delete [] pa;
}
};

void ARP::show()
{
for(int i = 0;i<m;i++)
{
cout<<pa[i]<<"  ";
}
cout<<endl;
}
void ARP::delsame()
{
int i,j;

for(i = 0;i < m-1;i++ )
{
if (pa[i]==pa[i+1])
{
for(j=i+1;j<m-1;j++)
{
pa[j] = pa[j+1];
}

ps[i] = ps[i] + 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 v(b,sizeof(b)/sizeof(b[0]));
v.show();
v.delsame();
v.show();
v.showcount();
return 0 ;
}


g++产生以下的警告:
a.h:1:2: warning: no newline at end of file
后来发现解决这个问题产生的原因是源文件的最后一行没有回车符造成的; 解决的办法很简单, 在最后一行敲一个回车, 然后保存, 重新编译.

文件尾,不是函数尾,呵呵
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: