防霉剂和抗菌剂:C++统计字符串

来源:百度文库 编辑:杭州交通信息网 时间:2024/05/06 16:55:59
统计出文档中出现次数最多和最少的字符串

好象要用结构体,帮忙写个程序
PS:不是从键盘输入句子进行处理
是打开文档,读取文档中的数据进行处理

编译环境 DEV C++
#include<iostream>
using namespace std;
struct S
{
int p;//position
int v;//value
};

int main()
{
int asc[128]={0};
int i=0;
S max,min;
max.v=0,min.v=1000;
char temp[1000];
cin.getline(temp,1000,'\n');

while(temp[i]!=0)
asc[temp[i++]]++;
for (i=0;i<128;i++)
if (asc[i]!=0)
{
if (max.v<asc[i]) { max.v=asc[i]; max.p=i;}
else if (min.v>asc[i]) { min.v=asc[i]; min.p=i;}
}
cout << "the max charactor is:"<<char(max.p)<<endl;
cout << "the min charactor is:"<<char(min.p)<<endl;
system("pause");
return 0;
}