第四次作业

作业要求:

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

结对对象:

#include <iostream>

#include <vector>

#include <algorithm>
#include <string>
#include <fstream>

using namespace std;struct WORD

{
string word;
int num;
};

vector<WORD> a; 

int&value(const string&s)
{
for(int i=0;i<a.size();i++)
if(s==a[i].word)
return a[i].num;
WORD p;
p.word=s;
p.num=0;
a.push_back(p); 
return a[a.size()-1].num;
}

int main()
{
string str;
cout << "输入字符串:\n";
char c;
while(c=cin.get())
{
if((c>='a' && c<='z') || (c>='A' && c<='Z') || c==' ' || c=='\n')
str+=c;

if(c=='\n')
break;
}

for(int j=0;str[j]!='\0';j++)
{
if(str[j]>='A'&&str[j]<='Z')
{
str[j]+= 32; 
}
}

string buf;
ofstream fout("D:\\123.txt"); 
fout<<str;
fout.close ();
ifstream fin("D:\\123.txt"); 
while(fin>>buf){
value(buf)++;
}
vector<WORD>::const_iterator p; 
ofstream output("D:\\Result1.txt"); 
for(p=a.begin();p!=a.end();++p)
output<<p->word<<":"<<p->num<<'\n'; 
return 0;

}

部分结果:
}

posted @ 2016-03-22 23:43  朱瑾  阅读(112)  评论(1编辑  收藏  举报