zoukankan      html  css  js  c++  java
  • C++统计一段文字中各单词出现的频率

    #include <iostream>

    using namespace std;

    /* run this program using the console pauser or add your own getch, system("pause") or input loop */

    class SqString
    {
    private:
    char * base;
    int length;
    public:
    SqString()
    {
    }
    SqString(char * s)
    {
    length=0;
    base=s;
    int i=0;
    while(s[i]!='')
    {
    ++i;
    ++length;
    }
    }
    char * getBase()
    {
    return base;
    }
    int getLength()
    {
    return length;
    }
    void StrConcat(SqString ss)
    {
    char * newbase=new char[ss.getLength()+length+1];
    for(int i=0;i<length;i++)
    {
    newbase[i]=base[i];
    }
    for(int j=0;j<=ss.getLength();j++)
    {
    newbase[j+length]=ss.base[j];
    }
    base=newbase;
    length=ss.getLength()+length;
    }
    int getNumOfSonSqString(SqString son)
    {
    int Num=0;
    for(int i=0;i<=length-son.getLength();i++)
    {
    int j=0;
    for(j=0;j<son.getLength();j++)
    {
    if(son.base[j]!=base[i+j])
    {
    break;
    }
    }
    if(j==son.getLength())
    {
    Num++;
    }
    }
    return Num;
    }
    bool isEqual(SqString s)
    {
    int i=0;
    while(s.base[i]!=''&&s.base[i]==base[i]&&base[i]!='')
    {
    ++i;
    }
    if(i==length&&i==s.length)
    {
    return true;
    }
    else
    {
    return false;
    }
    }
    };


    class SqStringNode
    {
    public:
    SqStringNode * pNext;
    SqString data;
    };


    class SqStringList
    {
    public:
    SqStringNode * pHead;
    SqStringNode * pTail;
    int length;
    public:
    SqStringList()
    {
    pHead=new SqStringNode;
    pTail=pHead;
    pTail->pNext=NULL;
    length=0;
    }
    void insertIntoList(SqString s)
    {
    SqStringNode * sNode=new SqStringNode;
    sNode->data=s;
    sNode->pNext=NULL;
    pTail->pNext=sNode;
    pTail=sNode;
    length++;
    }
    void show()
    {
    SqStringNode * temp=pHead->pNext;
    while(temp)
    {
    cout<<temp->data.getBase()<<endl;
    temp=temp->pNext;
    }
    }
    int getListLength()
    {
    return length;
    }
    bool isHave(SqString s)
    {
    SqStringNode * temp=pHead->pNext;
    int i=0;
    while(temp)
    {
    if(temp->data.isEqual(s))
    {
    break;
    }
    temp=temp->pNext;
    i++;
    }
    if(i>=length)
    {
    return false;
    }
    else
    {
    return true;
    }
    }
    };

    class Text
    {
    private:
    SqString content;
    char * inside;
    int length;
    public:
    Text(SqString & s)
    {
    content=s;
    length=s.getLength();
    inside=content.getBase();
    }
    char * getInside()
    {
    return inside;
    }
    SqString getContent()
    {
    return content;
    }
    int getTextLength()
    {
    return length;
    }
    SqStringList getAllWordsFromText()
    {
    char * temp=new char[20];
    SqStringList list;
    int j=0;
    for(int i=0;i<length;i++)
    {
    if(inside[i]==' '||inside[i]==','||inside[i]=='.')
    {
    temp[j]='';
    if(temp[0]!='')
    {
    SqString sqstring(temp);
    if(!list.isHave(temp))
    {
    list.insertIntoList(sqstring);
    }
    }
    temp=new char[20];
    j=0;
    while(inside[i]==' '||inside[i]==','||inside[i]=='.')
    {
    ++i;
    }
    }
    temp[j]=inside[i];
    j++;
    }
    //最后一个单词的判断
    temp[j]='';
    SqString sqstring(temp);
    if(!list.isHave(temp))
    {
    list.insertIntoList(sqstring);
    }
    return list;
    }
    };


    int main(int argc, char *argv[]) {
    SqString sqstring("flypie is good,flypie is better,flypie is best,I love flypie,flypie is good,flypie is better,flypie is best,I love flypie,flypie is good,flypie is better,flypie is best,I love flypie,flypie is good,flypie is better,flypie is best,I love flypie,flypie is good,flypie is better,flypie is best,I love flypie,flypie is good,flypie is better,flypie is best,I love flypie,flypie is good,flypie is better,flypie is best,I love flypie,flypie is good,flypie is better,flypie is best,I love flypie");
    Text text(sqstring);
    cout<<"这段文字为:"<<endl;
    cout<<text.getInside()<<endl;
    cout<<"----------------------------------"<<endl;
    cout<<"统计结果为:"<<endl;
    SqStringList list=text.getAllWordsFromText();
    //list.show();
    SqStringNode * temp=list.pHead->pNext;
    while(temp)
    {
    cout<<temp->data.getBase()<<" : "<<sqstring.getNumOfSonSqString(temp->data)<<endl;
    temp=temp->pNext;
    }
    return 0;
    }

  • 相关阅读:
    品Spring:真没想到,三十步才能完成一个bean实例的创建
    品Spring:对@Autowired和@Value注解的处理方法
    品Spring:对@Resource注解的处理方法
    品Spring:对@PostConstruct和@PreDestroy注解的处理方法
    品Spring:详细解说bean后处理器
    品Spring:bean工厂后处理器的调用规则
    品Spring:注解之王@Configuration和它的一众“小弟们”
    品Spring:SpringBoot发起bean定义注册的“二次攻坚战”
    品Spring:SpringBoot轻松取胜bean定义注册的“第一阶段”
    .net的retrofit--WebApiClient底层篇
  • 原文地址:https://www.cnblogs.com/flypie/p/4918875.html
Copyright © 2011-2022 走看看