C++STL 第十次实验(词频统计)

来源:互联网 发布:java用ftp上传文件 编辑:程序博客网 时间:2024/05/24 06:47

【题目】
  考察string,file i/o, map的操作
  
  将样例内容按单词的词频从高到低排序
  过滤数字与符号
  不区分大小写

【样例】
An Uninterruptible Power Supply (UPS) is an important thing to have if you
live in an area where power outages are at all common, especially if you run
a mail/DNS/Web server that must be up 24/7. The aging power grid in the U.S.
has made this a more urgent issue than it used to be even for American
hackers, but everyone is vulnerable to outages caused by storms and other
natural phenomena. This document covers both the software and hardware
aspects of protecting yourself.

The advice in this document is aimed primarily at small installations ?? one
computer and one UPS. Thus we’ll focus on consumer-grade UPSes, especially
those designed for home and small-business use. If you are a data center
administrator running a big server farm, there is a whole different (and much
more expensive) range of technologies we’ll do no more than hint at here.

The people who contribute to this document can speak only about equipment
they have experience with. This may reflect a bias toward or against certain
brands, features, functions, etc. Please keep in mind that the suggestions,
brand names and functions here are by no means exhaustive, or even
necessarily applicable to your situation. Also, if you have information that
is not in this document, please submit it to the maintainer listed above. If
you submit information, please say whether you’d like it to be attributed to
you or not. We are more than glad to give credit to the fine people who
helped with this document, but we want to respect the anonymity of those
people who would prefer it.

【代码】
写了一部分注释,后面不想写了。。。嗯,自己看代码实验哇

#include <iostream>#include <fstream>#include <algorithm>#include <map>#include <vector>using namespace std;//定义一个manager类,方便定义和插入class manager{public:    //键:string字符串    //值:字符串的数量    map<string,int> mymap;    //向mymap中添加字符串的方法    void add(string letter)    {        //定义迭代器找mymap中是否已经存在要插入的元素        //存在返回位置,不存在返回尾迭代器        map<string,int>::iterator mit = mymap.find(letter);        //如果不存在        if(mit==mymap.end())        {            pair<string,int> s(letter,1);            mymap.insert(s);            cout << letter << "插入到mymap中,数量:1" << endl;        }        //如果存在,将之前的数量调出来,并加一        else        {            //提取k记录数量            int k = (*mit).second;            //k+1            k++;            mymap.erase(mit);            pair<string,int> s(letter,k);            //将字符串和加一后的k插进去            mymap.insert(s);            cout << letter << "插入到mymap中,数量:" << k << endl;        }    }};//函数对象struct sortByValue{    bool operator()(const pair<string,int> p1,const pair<string,int> p2)    {        return p1.second > p2.second;    }};int main(){    ifstream in;    string letter;    manager mmp;    in.open("sample.txt");    if(!in)    {        cout << "打开文件失败..." << endl;    }    while(!in.eof())    {        in >> letter;        cout << "提取之前的letter是:" << letter << endl;        transform(letter.begin(),letter.end(),letter.begin(),::tolower);        int a;        //以下,去掉'/'        while((a = letter.find_first_of('/',0))!=string::npos)        {            string s1;            s1=letter.substr(0,a);            //提取出第一个单词的字符,如果是数字,不插入到mymap中            if((s1.at(0)-'0')<10)            {                break;            }            //............下面的不想说了...........有问题直接问我哇...........            else            {                map<string,int>::iterator mit = mmp.mymap.find(letter);                if(mit==mmp.mymap.end())                {                    pair<string,int> s(s1,1);                    mmp.mymap.insert(s);                    letter=letter.substr(a+1,(letter.length()-a-1));                    cout << s1 << "这儿插入到mymap中,数量:1" << endl;                }                else                {                    int k = (*mit).second;                    k++;                    mmp.mymap.erase(mit);                    pair<string,int> s(s1,k);                    mmp.mymap.insert(s);                    letter=letter.substr(a+1,(letter.length()-a-1));                    cout << s1 << "插入到mymap中,数量:1" << endl;                }            }        }        if((a=letter.find_first_of('\'',0))!=string::npos)        {            cout << "我进来了。。。。。。。。。。。。。。" << endl;            string s1;            s1=letter.substr(0,a);            mmp.add(s1);            letter=letter.substr(a+1,(letter.length()-a-1));            switch (letter[0])            {                case 'l':letter="will";cout<<"will替换成功"<<endl<<endl<<endl<<endl;mmp.add(letter);break;                case 're':letter="are";cout<<"are替换成功"<<endl<<endl<<endl<<endl;mmp.add(letter);break;                case 's':letter="is";cout<<"is替换成功"<<endl<<endl<<endl<<endl;mmp.add(letter);break;                case 'm':letter="am";cout<<"am替换成功"<<endl<<endl<<endl<<endl;mmp.add(letter);break;                case 'd':letter="would";cout<<"would替换成功"<<endl<<endl<<endl<<endl;mmp.add(letter);break;                default:cout << "没找到......................." << endl; break;            }            continue;        }        string::iterator sit = letter.begin();  //sit指向每个单词的第一个字符;        if((a = letter.find_first_of('-',0))!=string::npos)        {            mmp.add(letter);            continue;        }        else        {            while(sit!=letter.end())            {                if(((*sit)>='a'&&(*sit)<='z') || ((*sit)>='A'&&(*sit)<='Z') )                {                    sit++;                }                else                {                    letter.erase(sit);                }            }        }        cout << "提取出来的letter是:" << letter << endl;        if(letter!="")        {            mmp.add(letter);        }    }    vector<pair<string,int>> v;    for(map<string,int>::iterator iter = mmp.mymap.begin();iter!=mmp.mymap.end();iter++)    {        v.push_back(make_pair((*iter).first,(*iter).second));    }    sort(v.begin(),v.end(),sortByValue());    for(int i=0;i<v.size();i++)        cout << v[i].first << ": " << v[i].second << endl;    return 0;}