编程:实现读取一篇英文文本文件in.txt,统计其中各个单词出现的频率,把统计结果输出到out.txt文件中。
展开全部
#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)
{
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++;
}
};
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);
}
#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)
{
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++;
}
};
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);
}
参考资料: 随手写的应该符合你的要求
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询