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

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

  • 相关阅读:
    AngularJS:模块
    AngularJS:事件
    AngularJS:HTML DOM
    AngularJS:SQL
    AngularJS:表格
    AngularJS:Select
    shell之数组
    java实现串中找数字
    java实现串的反转
    java实现串的反转
  • 原文地址:https://www.cnblogs.com/caoyusongnet/p/3576779.html
Copyright © 2011-2022 走看看