VC++课程实践第9题

  • 格式:docx
  • 大小:24.74 KB
  • 文档页数:4

江苏科技大学
课程实践报告
设计题目: 计算机程序设计实践(VC++)
设计时间: 2016. 2.22 至2016. 2.28 学院: 能源与动力工程学院
专业班级: 14级建环2班
学生姓名: 姜宇阳学号1442105220 指导老师: 於跃成
(备注:红色部分根据自己的实际情况修改)
2016年2月23日
一、实践任务
9.定义一个字符串类CString,并设计一个算法对该串中各个不同字符出现的频率进行统计。

二、详细设计
1.累的描述与定义
(1)私有数据成员。

①char *str:指向要统计的字符串。

②char(*p)[2]:动态分配二维空间,用以存放str所指字符串中出现的字符及其出现的次数(次数在存放时,用该数字对应的ASCII编码值存放;在输出次数时,输出该ASCII字符对应的ASCII编码值即可)。

③int size:存放字符串中出现的所有不同的字符的个数。

(2)公有成员函数。

①CString(char*s)
②void Count()
③void Show()
④~CString()
2.主要函数设计
在主程序中定义字符串char s[]=“abdabcdesffffd”。

定义一个CString类对象test,用s以初始化test,完成对该类的测试。

三、源程序
#include<iostream.h>
#include<string.h>
class cstring{
char*str;
char (*p)[2];
int size;
public:
cstring(char *s);
void count();
void show();
~cstring();
};
cstring::cstring(char*s)
{
p=0;
size=0;
str=s;
}
void cstring::count()
{
p=new char[strlen(str)][2];
char n;
char *p1,*p2;
for(int i=0;str[i];i++)
{
n='\0';
p1=&str[i];
for(int m=0;str[m];m++)
{
p2=&str[m];
if(*p1==*p2)n++;
p2++;
}
p[i][0]=str[i];
p[i][1]=n;
}
}
void cstring::show()
{
for(int i=0;i<strlen(str);i++)
{
int m,x=1;
for(m=0;m<i;m++)
{
if(p[m][0]==p[i][0])
{
x=0;
break;
}
}
if(x==1)cout<<p[i][0]<<'\t'<<(int)p[i][1]<<endl;
}
}
cstring::~cstring()
{
delete []p;
}
void main()
{
char s[]="abdabcdesffffd";
cstring test(s);
test.count();
test.show();
}
四、测试和运行。