#include<iostream>
#include<fstream>
#include<string>
#include <iomanip>
using namespace std;
string stringtrim(
string s)
{
int i=
0;
while (s[i]==
'\"'||s[i]==
'\''||s[i]==
','||s[i]==
'.'||s[i]==
'?')
{
i++;
}
s=s.substr(i);
i=s.size()-
1;
while(s[i]==
'\"'||s[i]==
'\''||s[i]==
','||s[i]==
'.'||s[i]==
'?')
{
i--;
}
s=s.substr(
0,i+
1);
return s;
}
void main()
{
ifstream fin;
fin.open(
"in.txt",ios::in);
if(!fin.is_open())
{
cout <<
"打开输入文件错误";
exit (
1);
}
else {}
ofstream fout;
fout.open(
"out.txt",ios::out | ios::trunc);
if(!fout.is_open())
{
cout <<
"打开输出文件错误"<<endl;
exit (
0);
}
else {}
string words[
1000];
int num[
1000];
string temps;
int tempn;
int i,j;
i=
0;
int sign;
while (!fin.eof())
{
fin>>temps;
temps=stringtrim(temps);
sign=
0;
for(j=
0;j<=i;j++)
{
if(temps==words[j])
{
num[j]++;
sign=
1;
break;
}
}
if(sign==
0)
{
words[i]=temps;
num[i]=
1;
i++;
}
}
fin.close();
for(
int m=
1;m<i;++m)
{
for(
int n=
0;n<m;++n)
{
if(words[m]<words[n])
{
temps=words[n];
words[n]=words[m];
words[m]=temps;
tempn=num[n];
num[n]=num[m];
num[m]=tempn;
}
}
}
for(j=
0;j<i;j++)
{
cout<<setw(
20)<<setiosflags(ios::left)<<words[j]<<num[j]<<endl;
fout<<setw(
20)<<setiosflags(ios::left)<<words[j]<<num[j]<<endl;
}
fout.close();
}
转载请注明原文地址: https://ju.6miu.com/read-200217.html