码迷,mamicode.com
首页 > 其他好文 > 详细

WordCount

时间:2020-03-14 22:15:09      阅读:83      评论:0      收藏:0      [点我收藏+]

标签:etl   parameter   用户需求   一个   bsp   switch   函数   process   lines   

WordCount

1、GitHub项目地址

https://github.com/Anotn5/WordCount

2、项目需求

 

wc.exe 是一个常见的工具,它能统计文本文件的字符数、单词数和行数。这个项目要求写一个命令行程序,模仿已有wc.exe 的功能,并加以扩充,给出某程序设计语言源文件的字符数、单词数和行数。

 

实现一个统计程序,它能正确统计程序文件中的字符数、单词数、行数,以及还具备其他扩展功能,并能够快速地处理多个文件。
具体功能要求:
程序处理用户需求的模式为:

 

wc.exe [parameter] [file_name]

 

 

 

 

 

基本功能列表:

 

wc.exe -c file.c     //返回文件 file.c 的字符数

 

wc.exe -w file.c    //返回文件 file.c 的词的数目  

 

wc.exe -l file.c      //返回文件 file.c 的行数

 

 

 


扩展功能:
    -s   递归处理目录下符合条件的文件。(未实现)
    -a   返回更复杂的数据(代码行 / 空行 / 注释行)。

 

3、PSP

 

PSP2.1

Personal Software Process Stages

预估耗时(分钟)

实际耗时(分钟)

Planning

计划

 10

 

· Estimate

· 估计这个任务需要多少时间

 10

 

Development

开发

 200

 

· Analysis

· 需求分析 (包括学习新技术)

 70

 

· Design Spec

· 生成设计文档

 10

 

· Design Review

· 设计复审 (和同事审核设计文档)

 0

 

· Coding Standard

· 代码规范 (为目前的开发制定合适的规范)

 15

 

· Design

· 具体设计

 20

 

· Coding

· 具体编码

 40

 

· Code Review

· 代码复审

 20

 

· Test

· 测试(自我测试,修改代码,提交修改)

 15

 

Reporting

报告

 60

 

· Test Report

· 测试报告

 25

 

· Size Measurement

· 计算工作量

15 

 

· Postmortem & Process Improvement Plan

· 事后总结, 并提出过程改进计划

 10

 

合计

 

 520

 

 

4、解题思路

  设计语言

  本次开发选用C语言。

  结构分析

  分为-c,-l,-w,-a四种模式,通过函数调用实现功能。

  需要学习知识

  C语言中文件操作相关知识。

5、设计实现过程

  以用户输入的指令对各个函数进行调用,以用户输入的文件路径对相关文件进行操作。

6、代码说明

(1)主程序

void main() {

    FILE *fp;
    char filename[100];
    char choice, EndInput;
    int i = 0;

    for(; i<TestNum; i++){
        printf("\nwc.exe -  ");
        scanf("%c", &choice);
        scanf("%c", &EndInput);
        if((choice == c) || (choice == w) || (choice == l) || (choice == a)){
            printf("Please input the filename:");
            gets(filename);     //User input the filename
            if ((fp=fopen(filename,"r")) == NULL){        //Judge whether the file exists
                printf("error in opening %s\n",filename);
                exit(1);
            }
            else{
                Func(choice, filename);
            }
        }
        else{
            printf("Please input the correct operation!\n");
        }
    }
}

(2)功能函数

void Func(char Choice, char *DocPath) {   //Select function

    int i = 0;

    switch (Choice) {
        case c:
            printf("Character count: %d\n", GetCharacter(DocPath));
            break;
        case w:
            printf("Word count: %d\n", GetWord(DocPath));
            break;
        case l:
            printf("Line count: %d\n", GetLine(DocPath));
            break;
        case a:
            GetOtherData(DocPath);
            break;
        default:
            printf("Type Input Error!!!");
            break;
    }
}

(3)-c:字符数统计

int GetCharacter(char *DocPath) {    //Count characters

    FILE *file = fopen(DocPath, "r");
    char character;
    int counter = 0;

    while (!(feof(file))){
            character = fgetc(file);
            counter += ((character != ‘\n) && (character != ) && (character != \t));    //Judge is a character or not
    }
    fclose(file);

    return counter;
}

(4)-w:单词数统计

int GetWord(char *DocPath) {    //Count words

    FILE *file = fopen(DocPath, "r");
    char word;
    int tag = 0;    //The identifier determine whether the character is in the word
    int counter = 0;
    int i;

    while (!(feof(file))) {   //There are characters in the file
        word = fgetc(file);
        if ((word >= a && word <= z) || (word >= A && word <= Z)) {    //Judge the character is letter or not
            i = ((tag == 0) ? 1 : 0);
            counter += i;
            tag = 1;
        }
        else
            tag = 0;
    }
    fclose(file);

    return counter;
}

(5)-l:行数统计

int GetLine(char *DocPath) {    //Count lines

    FILE *file = fopen(DocPath, "r");
    char *s = (char*)malloc(1000 * sizeof(char));
    int counter;

    for (counter = 0; fgets(s, 1000, file) != NULL; counter++);
    free(s);
    fclose(file);

    return counter;
}

(6)-a:空行、注释行、代码行统计

void GetOtherData(char *DocPath){

    int CodeLine = 0, EmptyLine = 0, NoteLine = 0, Kind = 0, NoteTag = 0;
    //Kind: 0->EmptyLine  1->NoteLine  2->CodeLine
    char Identifiter;
    FILE *file = fopen(DocPath, "r");

    while (!feof(file)){
        Identifiter = fgetc(file);
        if (Kind == 0){         //Judge the EmptyLine
            if (Identifiter ==  )
                continue;
            else if (Identifiter == \n || ((Identifiter == { || Identifiter == }) && Kind == 0)){
                EmptyLine++;
                Kind = 0;       //Reset Kind
                continue;
            }
            else{
                if (Identifiter == /)
                    Kind = 1;
                else
                    Kind = 2;
            }
        }
        if (Kind == 1){         //Judge the NoteLine
            while (!feof(file)){
                Identifiter = fgetc(file);
                //Note by ‘//
                if (Identifiter == / && NoteTag == 0){
                    while (!feof(file) && Identifiter != \n)
                        Identifiter = fgetc(file);
                    NoteLine++;
                    Kind = 0;
                    NoteTag = 0;   //Reset NoteTag
                    break;
                }
                //Note by ‘/* */‘
                if(Identifiter == * && NoteTag == 0){
                    NoteTag = 1;
                    while(!feof(file) && Identifiter != \n)
                        Identifiter = fgetc(file);
                    NoteLine++;
                }
                if(Identifiter == * && NoteTag == 1){ //Judge the ‘*‘ is inside of the note
                    Identifiter = fgetc(file);
                    if(Identifiter == /){
                        while (!feof(file) && Identifiter != \n)
                            Identifiter = fgetc(file);
                        NoteLine++;
                        Kind = 0;
                        NoteTag = 0;
                        break;
                    }
                    else
                        NoteTag = 1;
                }

            }
        }
        if (Kind == 2){         //Judge the CodeLine
            while (!feof(file) && Identifiter != \n)
                Identifiter = fgetc(file);
            Kind = 0;
            CodeLine++;
            continue;
        }
    }
    printf("EmptyLine count: %d  CodeLine count: %d  NoteLine count: %d\n", EmptyLine, CodeLine - 1, NoteLine);  //CodeLine-1 cuz of feof
}

6、运行测试

(1)测试文件

技术图片

(2)测试结果

技术图片

7、实际花费时间 

 

PSP2.1

Personal Software Process Stages

预估耗时(分钟)

实际耗时(分钟)

Planning

计划

 

 10

· Estimate

· 估计这个任务需要多少时间

 

 10

Development

开发

 

 220

· Analysis

· 需求分析 (包括学习新技术)

 

 90

· Design Spec

· 生成设计文档

 

 10

· Design Review

· 设计复审 (和同事审核设计文档)

 

 0

· Coding Standard

· 代码规范 (为目前的开发制定合适的规范)

 

 15

· Design

· 具体设计

 

 15

· Coding

· 具体编码

 

 45

· Code Review

· 代码复审

 

 20

· Test

· 测试(自我测试,修改代码,提交修改)

 

 30

Reporting

报告

 

 60

· Test Report

· 测试报告

 

 30

· Size Measurement

· 计算工作量

 

 10

· Postmortem & Process Improvement Plan

· 事后总结, 并提出过程改进计划

 

 10

合计

 

 

 575

8、项目小结

(1)第一次做项目,经验不足,无论是GitHub还是博客操作都不熟,有待提高。

(2)对C语言文件操作部分不够了解,仍需学习。

(3)由于个人技术原因,只做了基本需求,任务完成的不完整,仍需努力

 

 

 

 

  

  

WordCount

标签:etl   parameter   用户需求   一个   bsp   switch   函数   process   lines   

原文地址:https://www.cnblogs.com/Anton5/p/12494449.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!