在上周一软件工程的王老师给我们布置了作业,由于我们课程比较满,所以只能把敲代码的时间挪到了周末。但自己在敲代码之前利用零散的时间构造自己程序的结构算法。
1.分析文本文档,首先要成功的打开文本文档。即要用到文件流的输入输出。头文件#include<fstream>和函数fopen(),getc()等
int wenjianopen(FILE *fp)
{
fp=fopen("yingyu.txt","r");
if(fp==NULL)
cout<<"文件打开错误,请检验。"<<endl;
return 0;
}
2.文件创建以后需要读入文件中的英文单词。在读入过程中如果是单词就录入结构体ciyu[30]中,方便以后进行比较和统计。
struct yingyu
{
string danci;
int count;
}ciyu[40],d;
3。文本文档的内容:
My father was a self-taught mandolin player. He was one of the best string instrument players in our town. He could not read music, but if he heard a tune a few times, he could play it. When he was younger, he was a member of a small country music band. They would play at local dances and on a few occasions would play for the local radio station. He often told us how he had auditioned and earned a position in a band that featured Patsy Cline as their lead singer. He told the family that after he was hired he never went back. Dad was a very religious man. He stated that there was a lot of drinking and cursing the day of his audition and he did not want to be around that type of environment.
4.在比较过程中利用选择比较法进行比较:
for(int p=0;p<N;p++)
{
q=p;
for(int r=p+1;r<N;r++)
if(ciyu[q].count<ciyu[r].count)
q=r;
if(q!=p)
{
d.count=ciyu[q].count;
ciyu[q].count=ciyu[p].count;
ciyu[p].count=d.count;
d.danci=ciyu[q].danci;
ciyu[q].danci=ciyu[p].danci;
ciyu[p].danci=d.danci;
}
5.运行结果: