我弱弱的问一句:大神在哪?这是一个能读取英文文本文件统计单词的源代码,思路是在网上找的,不知道有没有更好的。。

我们软工老师布置的作业 在网上找了好多也有自己的思路 发现这个不错、当时自己想的好像比较麻烦:先读取文件用数组存储单词,循环统计出有几个单词,比较长度将长度一样的数组比较,统计出有多少相同的 把最多的前十个输出来。后来自己编实在是太乱实现不了、 后来在网上搜了下发现这个思路。创建两个char型指针valueA,valueB,valueA记录挨个记录每个单词,用valueB来搜寻,最后可得到每个单词出现的次数。再将无重复的单词写入文件,然后读入string类型的动态数组,而且用int型动态数组记录相应的每个单词出现的次数,最最后10次循环可得到出现频数最高的10个单词.下面是源码,有一些不足的地方,在对符号判读的时候有一些bug,没有能将所有出现的符号涵盖,是否有好的方法能解决这个问题还请大神指导下,小弟在此先谢过了。

#include<iostream>

#include<string>
#include<string>
#include<fstream>
using namespace std;
int i=0;
char *creatArray()//创建字符数组将文件内容写入字符数组中
{
FILE*fp;
if((fp=fopen("ss.txt","r+"))==NULL)
{
cout<<"can't open the file"<<endl;
exit(0);
}
int sum=0;//sum统计共有多少个字符,包含空格等字符
while(fgetc(fp)!=EOF)
sum++;
char*array=new char[sum+1];//多创建一个字符空间来存储“\0”,以表示结尾
fp=fopen("ss.txt","r+");
array[0]=fgetc(fp);
while(!feof(fp))
{

i++;
array[i]=fgetc(fp);


}
array[sum]='\0';
return array;//返回包含文件所有内容的数组
}
void print()
{
char*destr=creatArray();

int num=1;//num用来存储共多少个单词
int sum=0;//存储共多少个单词,与print()函数中的sum不同,它不包含空格等字符
char *p=destr;
while(*p!='\0')//统计num
{
if(*p>='A'&&*p<='Z')
{
*p=*p-'A'+'a';
}
if(*p==' '||*p==','||*p=='.'||*p=='\"'||*p==':'||*p=='!'||*p=='\''||*p=='?')
{
num++;
*p='\0';
}
p++;
}
int *account=new int[num];//用来统计每个单词的出现次数
account[0]=1;
for(int i=1;i<num;i++)
account[i]=0;
char *valueA=destr;//valueA从第二个单词开始记录
for(i=0;i<num-1;i++)
{
valueA+=(strlen(valueA)+1);
char *valueB=destr;//valueB查看是否与valueA相等
for(int j=0;j<num;j++)
{
if(strcmp(valueA,valueB)==0)//若相等则对应次数增加,且break再次搜寻,若还相等则对应次数再次增加......
{
account[j]++;
break;
}
else
valueB+=(strlen(valueB)+1);//指向下一个
}

}

valueA=destr;
ofstream outfile("ww.txt",ios::out);
if(!outfile)
{
cout<<"open error!"<<endl;
exit(1);
}
for(i=0;i<num;i++)
{
if(account[i]&&*valueA!='\0')
{

outfile<<valueA;//将不再有重复的单词写入文件
outfile<<" ";
cout<<valueA<<" 出现的次数:"<<account[i]<<endl;

sum++;
}
valueA+=(strlen(valueA)+1);//指向下一个
}
outfile.close();
cout<<endl<<"出现频率最高的10个英语单词以及各自出现的次数是:"<<endl;

/*此时每个单词不再重复*/
/*从现在开始搜寻出现频率出现最高次数的10个单词,具体思路是两个数组分别储存各个单词和分别对应的次数,其中储存单词的数组类型是string,进行10次循环,
每次可以找出一个符合要求的单词并输出,期间可用m记录是第几个单词,然后下一次循环开始之前sum--(单词总数减一),单词数组中被输出单词后的每个单词向前
移动一位(总数组长度减一),记录次数的数组采取同样方法,最后通过10次循环可找出10个单词*/

int*amount=new int[sum];//用来统计每个单词出现的次数
string*value=new string[sum];//用来存放单词
ifstream infile("ww.txt",ios::in);
if(!infile)
{
cout<<"open error!"<<endl;
exit(1);
}
for(i=0;i<sum;i++)
{
infile>>value[i];//将无重复单词的文件中的每个单词写入数组
}
int n=0;
valueA=destr;
for(i=0;i<num;i++)//记录单词出现次数
{
if(account[i]&&*valueA!='\0')
{

amount[n]=account[i];
n++;
}
valueA+=(strlen(valueA)+1);
}
int m,k;
for(k=0;k<10;k++)
{
int max=amount[0];
for(i=0;i<sum;i++)
{
if(max<amount[i])
{
max=amount[i];
m=i;
}
}
sum--;
cout<<value[m]<<"出现次数"<<max<<endl;
for(i=m;i<sum;i++)//缩减数组
{
amount[i]=amount[i+1];
value[i]=value[i+1];
}
}

}
int main()
{
print();
return 0;
}

后面是执行的文章:I name take you, name to be my wife, knowing in my heart that you will always be my best friend, my partner in life and my one true love. On this special day, I give to you in the presence of God my promise to stay by your side as your husband,in sickness and in health, in joy and in sorrow, as well as through the good times and the bad.I promise to love you without reservation, to honor and respect you, to provide for your needs the best thatI can,to protect you from harm, to comfort you in times of distress,to grow with you in mind and spirit, to always be open and honestwith you,  and to cherish you for as long as we both shall live?

然后执行出的dos桌面:

还有一点遗憾是该程序好像不能统计有几个单词只能输出有几个字符,不知道哪里出了问题,有能一眼看出的还望指导!

posted @ 2014-03-03 13:12  Cinderella丶  阅读(167)  评论(0编辑  收藏  举报