个人作业

|▌冷眼眸甩不掉的悲伤 提交于 2020-03-16 02:44:47

1、github链接:https://github.com/asligia/hzxxiaoxiu/blob/master/wc.cpp

2、预计开发时间&实际开发时间

PSP2.1

Personal Software Process Stages

预估耗时(分钟)

实际耗时(分钟)

Planning

 计划

 10

 20

· Estimate

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

 20

 20

Development

开发

 120

 150

· Analysis

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

 20

 30

· Design Spec

· 生成设计文档

 10

 10

· Design Review

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

 10

 10

· Coding Standard

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

 10

 10

· Design

· 具体设计

 30

 30

· Coding

· 具体编码

 30

 25

· Code Review

· 代码复审

 10

 10

· Test

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

 30

 30

Reporting

报告

 20

 20

· Test Report

· 测试报告

 20

 20

· Size Measurement

· 计算工作量

 10

 10

· Postmortem & Process Improvement Plan

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

 10

 10

合计

 

 360

 

3、解题思路:

看到题目后,在百度搜索了一下读取文件相关的操作,然后对各个读取进行判断条件的确定。

4、实现过程:

主要分为主函数,功能函数CharCount、WordCount、LineCount,以及打印界面的函数Home组成,主函数用于打开关闭文件,和输入命令,功能函数用于实现三种功能。

5、代码说明:

主函数:

int main()
{
   FILE *file=NULL;
   char order=0;
   do
   {    
        if((file= fopen("1.txt","r"))==0)
    {
        printf("can't find the file\n");
        exit(0);
    }
        Home();
        scanf("%c",&order);
        switch(order-'a')
        {
            case 2:
            {
                CharCount(file);
                break;    
            }
            
            case 11:
            {
                LineCount(file);
                break;
            }
            
            case 22:
            {
                WordCount(file);
                break;
            }
            
            case 16:
            {
                printf("thinks to use!\n");
                printf("press to continue\n");
                getchar();
                getchar();
                break;
            }
            default:
            {
                printf("order error!");
                printf("press to continue\n");
                getchar();
                getchar();
                break;
            }
        } 
        fclose(file);
   }while(order!='q');
   return 0;
}

各功能函数:

void CharCount(FILE *f1)  //查询字符数 
{
    char ch;  
    int charCounts=0;
    while((ch=fgetc(f1))!=EOF)
    charCounts++;
    printf("CharCount is %d \n",charCounts);
    printf("press to continue\n");
    getchar();
    getchar();
} 
void LineCount(FILE *f2)  //查询行数 
{
    char ch;
    int lineCounts=1;    //防止最后一行没有回车 
    while((ch=fgetc(f2))!=EOF)
    {
        if(ch=='\n')  
        lineCounts++;   
    }
    printf("LineCount is %d \n",lineCounts);
    printf("press to continue\n");
    getchar();
    getchar();
}
void WordCount(FILE*f3)  //查询词数 
{       
    char ch;
    int wordCounts=0; 
    while((ch=fgetc(f3))!=EOF) 
    {
        if (ch >= 'a'&&ch <= 'z' || ch >= 'A'&&ch <= 'Z' || ch >= '0'&&ch <= '9')
        {
            while (ch >= 'a'&&ch <= 'z' || ch >= 'A'&&ch <= 'Z' || ch >= '0'&&ch <= '9' || ch == '_')
            {
                ch = fgetc(f3);
            }
            wordCounts++;
            ch = fgetc(f3);
        }
    }
    printf("WordCount is %d \n",wordCounts);
    printf("press to continue\n");
    getchar();
    getchar();
}

6、测试运行

测试文件:

 

 测试结果:

 

 

 

 

 

 

 

 

 

 7、小结:

整体来说,完成过程比想象中慢很多,主要原因是对代码不熟悉。

而且由于时间比较赶,并没有完成附加功能

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!