#include
#include
#include
#include
//#include
#include <time.h>
//#include <Windows.h>
using namespace std;
struct node
{
string word;
int num;
};
int main()
{
long t1 = clock();
vector<node> v;
node tempstr;
ifstream in("data.dat");
string temp; //缓冲区
string str;
int count=0;
int j=0;
while(getline(in,temp)) //文件读取结束时退出循环
{
//transform(temp.begin(), temp.end(), temp.begin(), tolower); // 转换为小写,头文件冲突?
for(int i=0; i<temp.length(); i++)
{
for(; i<temp.length(); i++)
{
if( (temp[i]>='a'&&temp[i]<='z') || (temp[i]>='A'&&temp[i]<='Z') )
{
temp[i]=tolower(temp[i]); //转换成小写
count++;
}else
break;//规避一些标点符号
}
if(count)
{
str=temp.substr(i-count,count);
//cout<<str<<endl;
if(v.size())//如果链表里有重复的就个数++
{
for(j=0; j<v.size(); j++)
{
if(str.compare(v[j].word)==0)
{
v[j].num++;
count=0;
break;
}
}
}
if(j>=v.size())//没有就加到链表尾部
{
tempstr.word = str;
tempstr.num = 1;
v.push_back(tempstr);
count=0;
}
}
}
}
int glass=0,n=v.size();
string glastr;
for(int i=0; i<3; i++)//冒泡排序
{
for(int k=0; k<=n-2-i; k++)
{
if(v[k].num>v[k+1].num)
{
glass=v[k].num;//交换值和个数
v[k].num=v[k+1].num;
v[k+1].num=glass;
glastr=v[k].word;
v[k].word=v[k+1].word;
v[k+1].word=glastr;
}
}
}
for(int i=1; i<=3; i++)
{
cout<<v[n-i].word<<" 出现"<<v[n-i].num<<"次"<<endl;
}
long t2 = clock();
cout<<"总用时:"<<t2-t1<<"ms"<<endl;
// getch();
return 0;
}