test00

#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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值