吴哲瀚

导航

WC

  1. 通过学习和查找  库函数的fread,fpoen,fclose等,并进行运用完成基本功能。
  2. 先逐个完成功能函数,然后完成cmd交互功能,再在main中集中调用
  3. https://github.com/Asura-GM/Word-count.git
  4. charCount 返回文件字符数 函数
  5.  

     

  6. int charCount(char* filePath){//返回文件字符数 
    	FILE* fp;
    	fp = fopen(filePath,"r");	
    	int n = 0;
    	char ch;
    	if(fp == NULL)
    	{
    		printf("cannot find file!\n");
    		return -1;
    	}else{
    		do{
    			ch = fgetc(fp);
    			if(ch>=0 && ch<=127){	/*由于ch!=NULL出现不知道理由的读取txt文档 n 会多1,读取cpp文档多2 
    							,就像终止标识符被读取了一样,改用ASCII码时,读取txt文档正常,读取cpp文档多1	*/
    				n++;	
    			}
    		}while(ch != EOF);
    		fclose(fp);
    	}
    	return n;
    }
    
  7. 返回文件单词数函数
  8. int wordCount(char* filePath) {//返回文件的词的数目 
        FILE* file;
        file =  fopen(filePath,"r");
        int n = 1 ;
        printf("%s",file);
        char ch;
        if(file == NULL)
        {
            printf("cannot find file!\n");
            return 0 ;
        }else if((ch=fgetc(file))==EOF){//排除空文件
            n=0;
        }else{
            do{
                ch = fgetc(file);
                if(ch=='\n' || ch==' '){
                    n++;    
                }
            }while(ch != EOF);
            fclose(file);
        }
        return n;
    }

     

  9. 返回文件行数
  10. int lineCount(char* filePath) {//返回文件的行数 
        FILE* file ;
        file = fopen(filePath,"r");
        int n = 1;
        char ch;
        if(file == NULL)
        {
            printf("cannot find file!\n");
            return -1;
        }else if( (ch=fgetc(file)) == EOF){//排除空文件
            n=0;
        }else{
            do{
                ch = fgetc(file);
                if(ch=='\n'){
                    n++;    
                }
            }while(ch != EOF);
            fclose(file);
        }
        return n;
    }

     

  11. 互动
  12. int operateFILE(char* filePath, char cmd) {
        int count = 0;
        switch (cmd) {
        case 'c':
            count = charCount(filePath);
            break;
        case 'l':
            count = lineCount(filePath);
            break;
        case 'w':
            count = wordCount(filePath);
            break;
        default:
            break;
        }
        return count;
    }
    
    bool isCharCmd(char c) {
        return (c == 'c' || c == 'l' || c == 'w');
    }

     

  13. 主函数
  • int main(int argc, char* argv[]) {
        char* path;
        //检查输入格式正确与否
        if (argc == 3 && argv[1][0]== '-' && isCharCmd(argv[1][1])) {
            int count = operateFILE(argv[2], argv[1][1]);
            switch (argv[1][1]) {
            case 'c':
                printf("charCount=%d",count);
                break;
            case 'w':
                printf("wordCount=%d",count);
                break;
            case 'l':
                printf("lineCount=%d",count);
                break;
            default:
                break;
            }
        }
        else {
            printf("wrong");
           return 0;
        }
    }

     

  • 回归测试
    • 空文件  1.txt
    •  

       

    •  

       

    • 一字符文件   2.txt
    • 一词文件  3.txt
    •  

      一行文件 4.txt

    •  

       

    •  

       

      代码文件(就用本代码 的wc.cpp)

       

      PSP

      PSP2.1 预估时间(min) 实际时间(min)
      Planning 40 40
      Estimate 1500 1140
      Development 1455 1075
      Analysis 240 180
      Design Spec 20 20
      Design Review 10 10
      Coding Standard 15 20
      Design 60 60
      Coding 600 300
      Code Review 20 30
      Test 900 1200
      Reporting 60 120
      Test Report 60 120
      Size Measurement 15 20

      Postmortem & Process Improvemet Plan

      30 45
      合计 1750 1360

       

      项目及小结:要多去查阅甚至去记住有哪些库函数及其功能和应用,才能更好的知道在满足某些功能时,需要调用什么函数来实现。

      还有cmd,git,github等编程相关软件的学习与应用,才能方便日后使用。

       



       

       

       

posted on 2020-03-23 23:12  GM66  阅读(345)  评论(0编辑  收藏  举报