上周四我们老师留了一个作业,让我们都申请一个自己的博客,把自己写的程序都在上面发表出来。我才发现博客真是个好东西,上面有好多软件人自己写的东西,也有很多该专业方面的介绍,真是非常感谢王建民老师。
这是我们的第一个作业。编写一个程序,实现分析一个文本文件(英文文章)中各个词出现的频率,并且把频率最高的10个词打印出来。这个项目对我们来说不是很陌生,以前也接触过统计单词个数的程序。我认为编写这个程序要完成以下几个关键:
1.怎样实现读文件、写文件。
2.实现完成识别单词的过程并记录单词出现的次数。
3.通过排序算法找出频率最多的10个。
这个作业是22号布置得,觉得还不着急就等了两天才写。
模块一
文件读写
2.25日 星期二
这一天的课程比较少,中午放学我就正式开始了这个程序的编写。我先选定要编写的语言--C++,编程通过VC++6.0实现,呵呵,这也是我最熟悉的语言之一了。通过编写io输入输出流很轻松的实现了读文件。刚开始只能实现读一个指定的文件,后经过改进实现了自己键盘输入文件名的读写。具体代码如下:
ifstream mf;
char f[30];
cout<<"请输入文件名(如:c:/a.txt)"<<endl;
cin>>f;
mf.open(f);
if(mf==NULL)
{
cout<<"此文件不存在!,请重新输入"<<endl;
cin>>f;
mf.open(f);
}
在这个过程中也遇到了问题,在刚开始编译的时候会提示
e:\san.cpp(60) : error C2079: 'mf' uses undefined class 'basic_ifstream<char,struct std::char_traits<char> >'
e:\san.cpp(64) : error C2228: left of '.open' must have class/struct/union type
e:\san.cpp(70) : error C2228: left of '.open' must have class/struct/union type
e:\san.cpp(76) : error C2228: left of '.eof' must have class/struct/union type
怎么这么多错误呢,但是我发现这些问题根源都是一个,没有添加输入输出流的头文件fstream,在加入这个头文件之后就一个错误都没有了。
模块二
读字母识别单词
2.26日 星期三 下午
这一天我们一下午都没有课,又打开了我的小程序,继续作业。
我利用结构体实现对单词的个数,字符串表示。
struct Word
{
char s[30]; //单词
int n; //单词出现的次数
}A[1000];
最开始想建立一个缓冲区buf,在缓冲区内进行比对。但是在编写的过程中发现并不用,只需要通过while(!mf.eof())就可以实现读文件到文件的结尾。在建立一个char 数组ch,ch=mf.get().然后把这个值赋给结构体的w值。判断是不是单词就更简单了,只需要通过ch和a,z,A,Z的ascll码进行比较,是字母就计入s数组中,不是则跳出循环。具体代码实现如下:
sum=0;
ch=NULL;
for(i=0;i<1000;i++)
A[i].n=0;
while(!mf.eof())
{
for(i=0;i<30;i++)
s[i]='\0';
ch=mf.get();
if(('a'<=ch&&ch<='z')||(ch>='A'&&ch<='Z'))
{
for(i=0;;i++)
{
s[i]=ch;
ch=mf.get();
if(('a'<=ch&&ch<='z')||(ch>='A'&&ch<='Z'))continue;
else break;
}
handle(s);
}
}
2.26 星期三 晚上 程序还是没有眉目,有点着急 。在处理怎样查单词上又是下了一番苦功,实现了一个函数handle(),在函数中设置标志flag.flag=0,则代表没有重复的单词。
void handle(char s[])
{
int i,j;
int flag=0; //flag为零时没有重复的
for(i=0;i<=sum;i++)
{
if(strcmp(A[i].s,s)==0)
{
A[i].n++;
flag=1;
sum++;
}
}
if(flag==0)
{
for(j=0;j<30;j++)
A[sum].s[j]=s[j];
A[sum].n++;
sum++;
}
}
这个查单词功能功能实现了可是了却了我的一块心病。下面只需要进行一个简单的排序就ok了。犒劳一下自己,放一天假哈。
2.28 星期五
2月的最后一天,我也该做些有意义的事呢,努力把这个程序弄完吧。本想着用冒泡排序法,可是在有些单词比较少的文章里就有问题,最终选择了选择排序法,另外对程序进行了整理。一个能够实现功能的程序就完成了,下面是选择排序法的代码:
void sort()
{
int i,j;
struct Word a;
for(i=0;i<sum;i++)
{
for(j=i+1;j<sum;j++)
if(A[i].n<A[j].n)
{
a=A[j];
A[j]=A[i];
A[i]=a;
}
}
}
实验结果:
文件内容:
Ambition
It is not difficult to imagine a world short of ambition. It would probably be a kinder world: with out demands, without abrasions, without disappointments. People would have time for reflection. Such work as they did would not be for themselves but for the collectivity. Competition would never enter in. conflict would be eliminated, tension become a thing of the past. The stress of creation would be at an end. Art would no longer be troubling, but purely celebratory in its functions. Longevity would be increased, for fewer people would die of heart attack or stroke caused by tumultuous endeavor. Anxiety would be extinct. Time would stretch on and on, with ambition long departed from the human heart.
Ah, how unrelieved boring life would be!
There is a strong view that holds that success is a myth, and ambition therefore a sham. Does this mean that success does not really exist? That achievement is at bottom empty? That the efforts of men and women are of no significance alongside the force of movements and events now not all success, obviously, is worth esteeming, nor all ambition worth cultivating. Which are and which are not is something one soon enough learns on one’s own. But even the most cynical secretly admit that success exists; that achievement counts for a great deal; and that the true myth is that the actions of men and women are useless. To believe otherwise is to take on a point of view that is likely to be deranging. It is, in its implications, to remove all motives for competence, interest in attainment, and regard for posterity.
We do not choose to be born. We do not choose our parents. We do not choose our historical epoch, the country of our birth, or the immediate circumstances of our upbringing. We do not, most of us, choose to die; nor do we choose the time or conditions of our death. But within all this realm of choicelessness, we do choose how we shall live: courageously or in cowardice, honorably or dishonorably, with purpose or in drift. We decide what is important and what is trivial in life. We decide that what makes us significant is either what we do or what we refuse to do. But no matter how indifferent the universe may be to our choices and decisions, these choices and decisions are ours to make. We decide. We choose. And as we decide and choose, so are our lives formed. In the end, forming our own destiny is what ambition is about.
请输入文件名(如:c:/a.txt)
e:/ambition.txt
文章中单词出现频率最高的10个:
is 15
of 14
the 13
would 12
and 12
be 11
to 9
not 9
that 9
a 8
Press any key to continue
通过完成这个程序,我不仅对C++文件读写更加了解,而且学会了相对的思考问题。比如查单词,我最初的想法是排除各种符号,数字等不是单词的字符,这样想就会好麻烦,后来终于想到了解决办法,只需要对a-z和A-Z进行排查,就可以了,所以说任何事情都有两面性,都是相对的。 编写这个博客,让我认识了一个新的领域,在博客上真的有有很多很牛很牛的人而且他们都是非常热心,比如我发表了一个动态就有好多人鼓励我给我支持,让我看到我不是自己在战斗。而且更加领悟到人外有人天外有天,看他们的博客有很多专业的知识,令我受益匪浅。