zoukankan      html  css  js  c++  java
  • 文章搜素—最终版

    顺序有些乱,是我没来的及更新。写完初始版本后感觉速度实在是慢,就把开始版本中复杂的数据交换

    temp=p1->num;
     p1->num=p->num;
    p->num=temp;
     s=p1->word;
     p1->word=p->word;
    p->word=s;

    去掉了,新的代码是

    //删除了复杂的数据交换
     #include<fstream>
    #include<iostream>
    #include<string>
    using namespace std;
    typedef struct words{
     string word;
     int num;
     struct words *next;
    }words,*Linklist;
     int InitList_L(Linklist &L)
    {
        L=new words;
        L->next=NULL;
        return 1;
    }
     void Paixu(Linklist L)
     { cout.width(10); 
     cout<<"单词"<<"   "<<"次数"<<endl;
         string s;
         int temp;
         Linklist p,p1;
         
         for(int i=0;i<10;i++){
             p1=L->next;
             p=p1->next;
             while(p)
             {
         if(p->num>p1->num)
         {   
             p1=p;
     
         }
         p=p->next;}
             cout.width(10);
             cout<<p1->word<<"   "<<p1->num<<endl;
             p1->num=0;
             
         }
     }
    int main()
    {  
         cout<<"请输入文件的名称:"<<endl;
         char filename[30];
         cin>>filename;
        Linklist L;
       InitList_L(L);
        string w;
        ifstream fin;
        fin.open(filename,ios::in);
        if(!fin)
        {cout<<"打开错误!"<<endl;
        return 0;}
        Linklist p,s,q;
           while(fin>>w)
           { int a=0;
               q=L;
               p=L->next;
               while(p){
            if(p->word==w)
            {p->num+=1;
            a++;}
             p=p->next;
             q=q->next;}
               if(a==0)
               {       
      s=new words;
      s->word=w;
      s->num=1;
      q->next=s;
      s->next=NULL;}
           }
    
      Paixu(L);
     return 0;
        
    }

    即,采用冒泡法,找出前十名,无需复杂数据交换,另外还增加了按文件名读文件以及读取错误处理等功能

  • 相关阅读:
    lookup:ID列
    分享几篇文章
    怎样无限制使用smartgit ?
    C++ Win32控制台应用程序捕捉关闭事件
    mt4 在K线上 放文字
    变色指标
    用windows 打包 证书
    监管fca asic nfa 啥啥啥
    sublime 3 build 3126 code ,压缩包在我的360企业云盘里,搜sublime
    个人作业收官——软件工程实践总结
  • 原文地址:https://www.cnblogs.com/caoyusongnet/p/3576779.html
Copyright © 2011-2022 走看看