还有怎么把一篇实实在在的TXT文档读进来?

想要实现读取一篇英文文本文件in.txt,统计其中各个单词出现的频率,把统计结果输出到out.txt文件中。

实现方法:用C或C++编程语言实现,可以采用顺序表、链表、二叉排序树三种结构实现。

慕娘9325324
浏览 142回答 2
2回答

RISEBY

#include<iostream>#include<fstream>#include<string>using namespace std;class danci{public:string name;int num;danci(){num=0;name="";};};void readfile(danci*&inchar,int &counter){ifstream infile("in.txt");if(!infile) {cout<<"cannot open!"<<endl;return;}while(infile){&nbsp;&nbsp;string temp;infile>>temp;int i=0;for( ;i<counter;i++){if(temp==inchar[i].name) { inchar[i].num++;break;}}if(i==counter&&inchar[i].name!=temp){inchar[counter].name=temp;inchar[counter].num++;counter++;}&nbsp;};infile.close();}void outfile(danci*inchar,int counter){ofstream outfile("out.txt");for(int i=0;i<counter;i++)outfile<<"单词"<<inchar[i].name<<endl<<"出现次数"<<inchar[i].num<<endl;}void main(){danci*inchar=new danci[1000];int counter=0;readfile(inchar,counter);outfile(inchar,counter);}把这个文件放在与debug相同的目录下,程序自己就能找到了

慕桂英3389331

这个要用到哈希表
打开App,查看更多内容
随时随地看视频慕课网APP