• 第二周作业二


    博客:

    功能一:

              把text.txt文本放在d盘下,并打开

    #include <stdio.h>
    #include <string.h>
    
    struct word
    {    char str[30]; 
         int num;       
    }A[1000];
    
    int sum;    
    
    void chuli(char s[])
    { 
          int i,j;  
          int flag=0;  //flag为零时没有重复的
          for(i=0;i<=sum;i++)  
        {   
           if(strcmp(A[i].str,s)==0)   
           {             
                A[i].num++;    
                flag=1;    
                sum++;      
             }     
         }  
          if(flag==0)  
           {   
             for(j=0;j<30;j++)       
                A[sum].str[j]=s[j];  
                A[sum].num++;   
                sum++;  
           }       
       }
      void paixu()
    {    
          int i,j;    
          struct word a;  
          for(i=0;i<sum;i++)  
        {   
            for(j=i+1;j<sum;j++)      
              if(A[i].num<A[j].num)    
             {     
                  a=A[j];          
                  A[j]=A[i];
                  A[i]=a; 
               }
          }
    }
    
    int main()
    {  
          char ch,s[30];  
          int i,flag=0;
           FILE *fp;
           fp=fopen("d:\text.txt","r");
          if(fp==NULL)
     {
            printf("此文件不存在!
    ");
      }
    	   sum=0;
           ch=NULL;
          for(i=0;i<1000;i++)
           A[i].num=0;
       while(ch!=-1)
     {
         for(i=0;i<30;i++)
         s[i]='';
         ch=fgetc(fp);
        if((65<=ch&&ch<=90)||(ch>=97&&ch<=122))
      {
          for(i=0;;i++)
          {
             s[i]=ch;
             ch=fgetc(fp);
             if((65<=ch&&ch<=90)||(ch>=97&&ch<=122))continue;
             else break;
           }
           chuli(s);
          }
        }
           paixu();
           printf("total%6d
    ",sum);
            for(i=0;i<5;i++)
            printf("%s  %4d
    ",A[i].str,A[i].num);
           return 0;
    }
    

      

    功能二:

     

    #include<stdio.h>
    #include<string.h>
    #include<iostream>
    #include<fstream>
    #include<string>
    #include<map>
    #include <iomanip>
    #include<vector>
    #include<algorithm>
    #include<fstream>
    using namespace std;
    FILE *fp1;
    map<string, int>my_map;
    vector<pair<string, int> > v_result;
    int cmp(const pair<string, int> &x, const pair<string, int> &y)
    {
        return x.second>y.second;
    }
    void sortMapbyValue(map<string, int>&my_map, vector<pair<string, int> >&t_vec)
    {
        for (map<string, int>::iterator iter = my_map.begin(); iter != my_map.end(); iter++)
        {
            t_vec.push_back(make_pair(iter->first, iter->second));
        }
        sort(t_vec.begin(), t_vec.end(), cmp);
    }
    int sum()
    {
        char text[1024];
        int n = 0;
        int i;
        while (fgets(text, 1024, fp1) != NULL)
        {
            i = 0;
            while (text[i] != '')
            {
                char s[30];
                int j = 0;
                while ((text[i] >= 'a'&&text[i] <= 'z') || (text[i] >= 'A'&&text[i] <= 'Z') || text[i] == '-')
                {
                    if (text[i] >= 'A'&&text[i] <= 'Z')
                        text[i] += 'a' - 'A';
                    s[j++] = text[i++];
                }
                s[j] = '';
                if (my_map[s] == 0)
                    n++;
                my_map[s]++;
                if (text[i] == '') break;
                else
                    i++;
            }
        }
        fclose(fp1);
        return n;
    }
    void paixu()
    {
        sortMapbyValue(my_map, v_result);
    }
    void out(int n)
    {
        cout << "total" << "  " << n << "
    " << "
    ";
        for (int i = 0; i<10; i++)
        {
            if (v_result[i].first != "")
            {
                cout << left;
    			cout<<setw(10) << v_result[i].first << setw(10) << v_result[i].second<<"
    ";
                cout << '
    ';
            }
        }
    }
    int main(int argc,char* argv[])
    {
    int i=0;
    	for(i=1;i<argc;i++)
    	{
    		if(strcmp(argv[i],"gone_with_the_wand")==0)
    		{
    			fp1 = fopen("c:\2.txt", "r");
    			int n = sum();
        		paixu();
        		out(n);
    		}
    		if(strcmp(argv[i],"test.txt")==0)
    		{
    			fp1 = fopen("c:\test.txt", "r");
    			int n = sum();
        		paixu();
        		out(n);
    		}
    		if(strcmp(argv[i],"name3")==0)
    		{
    			fp1 = fopen("c:\2.txt", "r");
    			int n = sum();
        		paixu();
        		out(n);
    		}	
    	}
    return 0;
    }
    

      

    功能三:

     

    #include<stdio.h>
    #include<string.h>
    #include<iostream>
    #include<fstream>
    #include<string>
    #include<map>
    #include <iomanip>
    #include<vector>
    #include<algorithm>
    #include<fstream>
    using namespace std;
    FILE *fp1;
    map<string, int>my_map;
    vector<pair<string, int> > v_result;
    int cmp(const pair<string, int> &x, const pair<string, int> &y)
    {
        return x.second>y.second;
    }
    void sortMapbyValue(map<string, int>&my_map, vector<pair<string, int> >&t_vec)
    {
        for (map<string, int>::iterator iter = my_map.begin(); iter != my_map.end(); iter++)
        {
            t_vec.push_back(make_pair(iter->first, iter->second));
        }
        sort(t_vec.begin(), t_vec.end(), cmp);
    }
    int sum()
    {
        char text[1024];
        int n = 0;
        int i;
        while (fgets(text, 1024, fp1) != NULL)
        {
            i = 0;
            while (text[i] != '')
            {
                char s[30];
                int j = 0;
                while ((text[i] >= 'a'&&text[i] <= 'z') || (text[i] >= 'A'&&text[i] <= 'Z') || text[i] == '-')
                {
                    if (text[i] >= 'A'&&text[i] <= 'Z')
                        text[i] += 'a' - 'A';
                    s[j++] = text[i++];
                }
                s[j] = '';
                if (my_map[s] == 0)
                    n++;
                my_map[s]++;
                if (text[i] == '') break;
                else
                    i++;
            }
        }
        fclose(fp1);
        return n;
    }
    void paixu()
    {
        sortMapbyValue(my_map, v_result);
    }
    void out(int n)
    {
        cout << "total" << " " << n << "
    ";
        for (int i = 0; i<10; i++)
        {
            if (v_result[i].first != "")
            {
                cout << left;
                cout<<setw(10) << v_result[i].first << setw(10) << v_result[i].second<<"
    ";
            }
        }
        cout<<"----
    runbinson
    total "<<n<<" words";
    }
    int main(int argc,char* argv[])
    {
        int i=0;
        for(i=1;i<argc;i++)
        {
            if(strcmp(argv[i],"folder")==0)
            {
                char str[100];
                scanf("%s",str);
                if(strcmp(str,"gone_with_the_wand")==0)
                {
                    fp1 = fopen("c:\2.txt", "r");
                    int n = sum();
                    paixu();
                    out(n);
                }
            }
            
        }
        return 0;
    }

      

    PSP:

    代码及版本控制:

    HTTPS:https://git.coding.net/lynlynyess/Wordcount.git

    SHH:git@git.coding.net:lynlynyess/Wordcount.git

    GIT:git://git.coding.net/lynlynyess/Wordcount.git

     

  • 相关阅读:
    google说 老子 = I !
    昨日种种死 今日种种生
    设定Access数据库自增长字段初始值
    [c路历程]品品数组这杯茶
    爱上位运算
    .Net中的委托
    Jquery css函数用法(判断标签是否拥有某属性)
    记录几条简单的正则表达式
    ASP.NET MVC中DropDownList的使用
    C#如何获取object对象的属性值
  • 原文地址:https://www.cnblogs.com/lynlyn/p/7545530.html
Copyright © 2020-2023  润新知