您的位置:首页 > 其它

作业4

2016-03-22 23:42 225 查看
基于作业3的结果,读取一个较小的文本文件A_Tale_of_Two_Cities.txt,统计该文件中的单词的频率,并将统计结果输出到当前目录下的 Result1.txt 文件。 (第一阶段初稿完成该要求)

命令行格式: 提示符> Myapp.exe -f filename.txt > Result.txt (PS:C++ 程序,Java 程序输出方式类似) filename.txt 为前面下载的文件名。

解释:

选项 -f 表示后面跟文件名

输出格式规定(参考作业3中的示例):

首先按照频率由高到低排序

频率一样的词, 按照字典顺序排序

此外, 读取一个较大的文本文件Gone_with_the_wind.txt实验对比程序执行效率,做如下改进,比较改进前后程序执行时间。

结对对象:侯清原http://www.cnblogs.com/sunshine2/

周侃http://home.cnblogs.com/u/zhoukan/

结对照片:


相关程序:

#include<iostream>
#include<fstream>
#include<cstring>
#include<string>
using namespace std;
struct Word{
int Count;
Word() : Str(""), Count(0) {}
string Str;
char *p;
};
void exchange(Word &word)
{
string tStr = word.Str;
int tCount = word.Count;
word.Str = Str;
word.count =count ;
Str = tStr;
Count = tCount;
}

Words test[100];

void lwr(char x[])
{   int k = 0;
while (x[k] != '\0')
{
if (x[k] >= 'A'&&x[k] <= 'Z')
x[k] = x[k] + 32;
k++;
}
}

int identify(char a[])
{   int m=(strlen(a)>=4)?1:0;
int n=(a[0]>='a'&&a[0]<='z')?1:0;
if(!m||!n)
return 0;
else
while(a)
{   for(int i=1;;i++)
{
if(!(a[i]>='a'&&a[i]<='z')||!(a[i]>='0'&&a[i]<='9'))
return 0;
else
return 1;
}
}
}

void SortWordDown(Word * words, int size)
{
for(int i=0;i<size;i++)
{
for(int j=0;j <size-1;j++)
{
if(words[j].Count<words[j+1].Count)
{
words[j].exchange(words[j+1]);
}
}
}
}

int counting(char b[],int num)
{   for(int j=0;j<num;j++)
{if(!strcmp(b,test[j].p))
test[j].count++;
else
return 0;
}
}
int main()
{   char c[200];
ifstream fin("D:/A_Tale_of_Two_Cities.txt");
for(int f=0;;f++)
fin>>c[f];
fin.close();
cin.get();
lwr(c);
const char *delim = ",”“.''!?";
char *q;
int n=0;
q = strtok(c, delim);
SortWordDown(words, wCount);
while (q)
{
if (identify(q))
{ strcpy(test
.p,q);
n++;}
ofstream Result1;
Result1.open("Result1.txt");
Result1<< test
.p << ":" << test
.count << '\n';
q=strtok(NULL,delim);
}
return 0;
}


结果:



Github的链接:https://github.com/sunshine111/sunshine111.git

小结:虽然作业3不是很会,但通过这次合作,还是顺利的完成了作业4,以后一定多多结对合作。积极探讨学习,相互提高。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: