建立一个类NUM,并统计特定序列中相同的字符的个数。
具体要求如下:
(1)私有数据成员
l char data[25]:随机生成25 个字符。
l int num[128]:储存每个字符出现的个数。
(2)公有数据成员
l NUM(int data):构造函数,同时初始化数组data。
l void process( ):统计数组data 中每个字符出现的个数,并保存到数组
num 中。
l void print( ):输出每个出现过的字符及其出现的个数,每行输出5 个,
没有出现过的字符不显示。
(3)在主程序中定义一个对象,对该类进行测试。
个人表示有点困难啊
#include<iostream.h>
#include<stdlib.h>
class NUM
{
private:
char data[25];
int num[128];
public:
NUM(int data)
{
for(int i=0;i<25;i++)
this->data=rand()%data;//随机生成25个字符;
}
void process();
void print();
};
void NUM::process()
{
for(int i=0;i<128;i++)
{
int k=0;
for(int j=0;j<25;j++)
{
if(data[j]==i)
k++;
}
num=k;
}
}
void NUM::print()
{
for(int i=0;i<128;i++)
{
int k=0;
if(num)
{
cout<<char(i)<<"出现个数"<<num<<'\t';
k++;
}
if((k+1)%5==0)cout<<endl;
}
cout<<endl;
}
void main()
{
NUM test(128);
test.process();
test.print();
}
请大神看看有没有错误 我朋友写的
[查看全文]
