zoukankan      html  css  js  c++  java
  • 20200917-2 词频统计

    此作业的要求参见:https://edu.cnblogs.com/campus/nenu/2020Fall/homework/11206

    词频统计 SPEC

    功能一:本题我所用的语言是C语言,在本功能中我认为难点在于如何在控制台输入打开一个文本文件并且统计其中的单词内容。本题我运用了结构体、文件操作、指针等一系列C语言的知识编写了这个程序。

    功能一关键代码:

    void Fuzhi(struct Word *Danci, FILE *read,int length) //把文件中单词复制到数组中
    {
    
        char word[100000];
        int i = 0, j=0;
    
        while (fscanf(read, "%s", &word) != EOF)
        {
            strcpy(Danci[i].word, word); 
            ++i;                         
        }
        fclose(read);                  
        Word_count(Danci, length); 
    }
    void Word_count(struct Word *Danci,int length) //统计单词的个数
    {
        int i, j,cishu=0;
        for (i = 0; i < length; i++)
        {
            Danci[i].time = 1;
            for (j = i + 1; j < length; j++)
            {
                if (strcmp(Danci[i].word, Danci[j].word) == 0)
                {
                    ++Danci[i].time;            
                    strcpy(Danci[j].word, " "); 
                }
            }
        }
        for (int index = 0; index < length; index++) 
        {
            for (int temp = 0; temp < length - index-1; temp++)
            {
                if (Danci[temp].time < Danci[temp + 1].time)
                {
                    struct Word word = Danci[temp];
                    Danci[temp] = Danci[temp + 1];
                    Danci[temp + 1] = word;
                }
            }
        }
    
        for (i = 0; i < length; i++)
            if (strcmp(Danci[i].word, " ") != 0)
            {  
                cishu++;
            }
        printf("total %d
    
    ", cishu);
        for (i = 0; i < length; i++)
            if (strcmp(Danci[i].word, " ") != 0)
            { 
                printf("%-5s:%-3d
    ", Danci[i].word, Danci[i].time);
            }
    }

    功能一运行截图:

     功能2 支持命令行输入英文作品的文件名,请老五亲自录入。

    功能二:此题的重点在于支持命令行输入英文作品的文件名,与功能一最本质的特征就是在控制台不需要输入.txt就可以读入文件并统计单词。此时需要在输入拼接一个.txt字符串就可以完成此功能。

    功能二关键代码:

    void Fuzhi(struct Word *Danci, FILE *read,int length) //把文件中单词复制到数组中
    {
    
        char word[100000];
        int i = 0, j=0;
    
        while (fscanf(read, "%s", &word) != EOF)
        {
            strcpy(Danci[i].word, word); 
            ++i;                         
        }
        fclose(read);                  
        Word_count(Danci, length); 
    }
    void Word_count(struct Word *Danci,int length) //统计单词的个数
    {
        int i, j,cishu=0;
        for (i = 0; i < length; i++)
        {
            Danci[i].time = 1;
            for (j = i + 1; j < length; j++)
            {
                if (strcmp(Danci[i].word, Danci[j].word) == 0)
                {
                    ++Danci[i].time;            
                    strcpy(Danci[j].word, " "); 
                }
            }
        }
        for (int index = 0; index < length; index++) 
        {
            for (int temp = 0; temp < length - index-1; temp++)
            {
                if (Danci[temp].time < Danci[temp + 1].time)
                {
                    struct Word word = Danci[temp];
                    Danci[temp] = Danci[temp + 1];
                    Danci[temp + 1] = word;
                }
            }
        }
    
        for (i = 0; i < length; i++)
            if (strcmp(Danci[i].word, " ") != 0)
            {  
                cishu++;
            }
        printf("total %d
    
    ", cishu);
        for (i = 0; i < length; i++)
            if (strcmp(Danci[i].word, " ") != 0)
            { 
                printf("%-5s:%-3d
    ", Danci[i].word, Danci[i].time);
            }
    }

    功能二运行截图:

    功能3 支持命令行输入存储有英文作品文件的目录名,批量统计。难点在于代码设计要涉及到批量统计,这是重点也是难点!

    >dir folder
    gone_with_the_wand
    runbinson
    janelove
    >wf folder
    gone_with_the_wand
    total 1234567 words
    the 5023
    a 4783
    love 4572
    fire 4322
    run 3822
    cheat 3023
    girls 2783
    girl 2572
    slave 1322
    buy 822
    ----
    runbinson
    total 1234567 words

    功能4 从控制台读入英文单篇作品,这不是为了打脸老五,而是为了向你女朋
    友炫酷,表明你能提供更适合嵌入脚本中的作品(或者如她所说,不过是更灵活
    的接口)。如果读不懂需求,请教师兄师姐,或者 bing: linux 重定向,尽管
    这个功能在windows下也有,搜索关键词中加入linux有利于迅速找到。

    功能4:由于能力不足,导致功能四花费了很多时间,也请教了学长,但还是无法做出来,功能四没有完成,希望以后能学到更多的东西来完成它。

     程序主函数:

    int main(int argc, char *argv[])
    {
        extern int cishu;
        char word[100000];
        int length = 0;
        FILE *read;
        struct Word *Danci;
        if(argc==3){
        if (argc < 3 || argc > 3)
        {
            printf("找不到%s该文件
    ", argv[0]);
            exit(EXIT_FAILURE);
        }    
        if ((read = fopen(argv[2], "r")) == NULL)
        {
            printf("文件打开失败
    ");
            exit(EXIT_FAILURE);
        }
        while (fscanf(read, "%s", &word) != EOF) //测试是否读到文件末尾
        {
            ++length; //统计文本中单词的个数
        } 
        }
        if(argc==2){
        if (argc < 2 || argc > 2)
        {
            printf("找不到%s该文件
    ", argv[0]);
            exit(EXIT_FAILURE);
        }    
        char a[10]=".txt";
            argv[1]=strcat(argv[1],a);
        if ((read = fopen(argv[1], "r")) == NULL)
        {
            printf("文件打开失败
    ");
            exit(EXIT_FAILURE);
        }
        while (fscanf(read, "%s", &word) != EOF) 
        {
            ++length; 
        } 
        }
        rewind(read);                                 //清除错位信息
        Danci = (struct Word *)malloc (sizeof(struct Word) * length); 
        Fuzhi(Danci, read, length);                    
        
        return 0;
    }

    本周PSP:

    功能 预估花费时间 实际花费时间 花费时间差距 原因
    功能一 60min 184min 124min 文件操作的内容不熟练,经过上网查询才编写成功。
    功能二 30min 143min 113min 字符串的拼接编写格式出错了,临时补的课。
    功能三 60min 155min 95min 对C语言对一个文件批量操作之前没学到过,内容不会。
    功能四 60min 71min 11min 不知道该如何实现该功能,最终编写失败

     本周总结:本次作业对我来说难度非常大,我也花费了很长时间,但有的功能还是没能实现,虽然难度大,花费时间多,但见到了很多我之前没有接触过的东西,也学到了很多东西,自我认为这门课作用非常大。

      github中代码位置:https://github.com/wangyanhe/cipintongji

     
  • 相关阅读:
    菜鸟看懂算法以后之一:头痛的64次左移
    C语言通过指针数组和二维数组读取文件
    C++中构造函数调用构造函数
    bnuoj53075 外挂使用拒绝
    [CodeForces]String Reconstruction
    BNU-2017.7.4排位赛2总结
    BNU-2017.7.5排位赛3总结
    BNU-2017.7.3排位赛1总结
    微软大楼设计方案(困难)
    最长公共子序列针对小字符集的算法
  • 原文地址:https://www.cnblogs.com/wangyanhe/p/13716427.html
Copyright © 2011-2022 走看看