• 第二周博客作业20180918-1


    本次作业要求参见https://edu.cnblogs.com/campus/nenu/2018fall/homework/2126

    本次作业代码地址https://git.coding.net/fanhongda/test.git

    发表博客,介绍上述“项目”中每个功能的重点/难点,展示重要代码片断,给出执行效果截图,展示你感觉得意、突破、困难的地方。

    功能1

    (1)读取文件

    char* readFile(const char* filename){
        char* passage;
        FILE* fp;
        char buf[1024];
        fp = fopen(filename,"r");
        if(fp==NULL){
            exit(-1);
        }
        
        passage = (char*)malloc(sizeof(char)*MAXLENGTH);
        passage[0]='';
    
        while(!feof(fp)){
            fgets(buf,1024,fp);
            strcat(passage,buf);
        }
    
        return passage;
    }

    (2)把整篇文章切割成单词,为了避免同一个单词因为大小写的原因而识别为两个单词,因此把大写字母换成小写

    char** split(char* passage,int *size){
        int i;
        int len = strlen(passage);
        int c = 0,num=0;
        char **words,*word;
        words = (char**)malloc(sizeof(char*)*MAXWORDS);
        word = (char*)malloc(sizeof(char)*MAXWORD);
        for(i=0;i<len;i++){
            if(!isChar(passage[i])){
                passage[i]=' ';
            }else if(isUpper(passage[i])){
                passage[i]=(char)('a'+passage[i]-'A');
            }
        }
    
        len = strlen(passage);
        passage[len]='';
        len++;
    
        for(i=0;i<MAXWORDS;i++){
            words[i]=(char*)malloc(sizeof(char)*MAXWORD);
        }
    
        for(i=0;i<len;i++){
            if(passage[i]!=' '&&passage[i]!=''){
                word[c++]=passage[i];
            }else{
                if(c>0){
                    word[c]='';
                    c=0;
                    strcpy(words[num++],word);
                }
            }
        }
        //记录单词的数量
        *size = num;
        
        return words;
    }

    (3)按照单词出现频率进行排序

    void sort(Data* datas,int size){
        int i,j;
        Data t;
        for(i=0;i<size;i++){
            for(j=0;j<size-i-1;j++){
                if(datas[j].count<datas[j+1].count){
                    t = datas[j];
                    datas[j]=datas[j+1];
                    datas[j+1] = t;
                }
            }
        }
    }

    (4)存储单词和出现频率的结构体

    typedef struct Data{
        char* word;
        int count;
    }Data;

    (5)小文件输入,在控制台输入命令

    void function1(char* filename){
        char* passage;
        char** words;
        Data* datas;
        int total = 0;
        int i,j,size=0;
        int exist=0;
    
        datas = (Data*)malloc(sizeof(Data)*MAXWORDS);
    
        passage = readFile(filename);
        words = split(passage,&size);
        
        for(i=0;i<size;i++){
            exist=0;
            for(j=0;j<total;j++){
                if(strcmp(datas[j].word,words[i])==0){
                    exist = 1;
                    break;
                }
            }
            
            
            if(exist){
                exist=0;
                datas[j].count+=1;
            }else{
                datas[total].word = malloc(sizeof(char)*MAXWORD);
                strcpy(datas[total].word,words[i]);
                datas[total].count=1;
                total++;
            }
        }
    
        sort(datas,total);
        printf("total %d
    
    ",total);
        for(i=0;i<total;i++){
            printf("%s	%d
    ",datas[i].word,datas[i].count);
        }
    
    }

    运行截图

    功能2

    命令行输入作品文件名

    void function2(char* filename){
        char* passage;
        char** words;
        Data* datas;
        int total = 0;
        int i,j,size=0;
        int exist=0;
    
        datas = (Data*)malloc(sizeof(Data)*MAXWORDS);
    
        passage = readFile(filename);
        words = split(passage,&size);
        
        for(i=0;i<size;i++){
            exist=0;
            for(j=0;j<total;j++){
                if(strcmp(datas[j].word,words[i])==0){
                    exist = 1;
                    break;
                }
            }
            
    
            if(exist){
                exist=0;
                datas[j].count+=1;
            }else{
                datas[total].word = malloc(sizeof(char)*MAXWORD);
                strcpy(datas[total].word,words[i]);
                datas[total].count=1;
                total++;
            }
        }
    
        sort(datas,total);
        printf("total %d words
    
    ",total);
        for(i=0;i<total;i++){
            printf("%s	%d
    ",datas[i].word,datas[i].count);
        }
    }

    运行截图

    功能3

    命令行输入存有英文作品文件的目录名,虽然和功能2一样都是命令行输入,但功能2是文件功能3是文件夹

    void _function3(char* foldername,char* filename){
        char* passage;
        char** words;
        Data* datas;
        int total = 0;
        int i,j,size=0;
        int exist=0;
        char ffname[300];
    
        datas = (Data*)malloc(sizeof(Data)*MAXWORDS);
    
        strcpy(ffname,foldername);
        strcat(ffname,"//");
        strcat(ffname,filename);
    
        passage = readFile(ffname);
        words = split(passage,&size);
        
        for(i=0;i<size;i++){
            exist=0;
            for(j=0;j<total;j++){
                if(strcmp(datas[j].word,words[i])==0){
                    exist = 1;
                    break;
                }
            }
            
    
            if(exist){
                exist=0;
                datas[j].count+=1;
            }else{
                datas[total].word = malloc(sizeof(char)*MAXWORD);
                strcpy(datas[total].word,words[i]);
                datas[total].count=1;
                total++;
            }
        }
    
        sort(datas,total);
        printf("%s
    ",filename);
        printf("total %d words
    
    ",total);
        total = total>8?8:total;
        for(i=0;i<total;i++){
            printf("%s	%d
    ",datas[i].word,datas[i].count);
        }
        
    }

    运行截图

    功能4

    从控制台读入单篇英文作品,将输出结果保存到指定文件中

    void function4(char* words[],int size){
        char* passage;
    
        Data* datas;
        int total = 0;
        int i,j=0;
        int exist=0;
        char temp[10000];
    
        datas = (Data*)malloc(sizeof(Data)*MAXWORDS);
        passage = (char*)malloc(sizeof(char)*MAXLENGTH);
        passage[0]='';
        while(scanf("%s",temp)!=EOF){
            strcat(passage,temp);
            strcat(passage," ");
        }
        
        words = split(passage,&size);
    
        for(i=0;i<size;i++){
            exist=0;
            for(j=0;j<total;j++){
                if(strcmp(datas[j].word,words[i])==0){
                    exist = 1;
                    break;
                }
            }
            
    
            if(exist){
                exist=0;
                datas[j].count+=1;
            }else{
                datas[total].word = malloc(sizeof(char)*MAXWORD);
                strcpy(datas[total].word,words[i]);
                datas[total].count=1;
                total++;
            }
        }
    
        sort(datas,total);
        printf("total %d words
    
    ",total);
        for(i=0;i<total;i++){
            printf("%s	%d
    ",datas[i].word,datas[i].count);
        }
    }

    运行截图

    此项目PSP

    差异原因:没有实际编过程序,对所需时间并没有一个准确的认知,而且整个过程感觉速度缓慢,并且期间出现各种各样的小错误,修改它们花费了一些时间。如果再算上复习C语言的时间总时间将会更多,大约是1300分钟。

  • 相关阅读:
    1001. A+B Format
    1011. World Cup Betting
    1015. Reversible Primes
    1005. Spell It Right
    1020. Tree Traversals
    java初始
    lvds接口介绍
    优化对比度增强的实时图像视频去雾
    JavaScript通过attachEvent和detachEvent方法处理带参数的函数
    Chrome类似于Firefox Firebug的功能
  • 原文地址:https://www.cnblogs.com/fanhongda/p/9695812.html
Copyright © 2020-2023  润新知