第2周个人作业:WordCount

本文介绍了WordCount程序的设计与实现过程,包括需求分析、设计思路、代码实现、测试及参考资料等内容。该程序能统计源文件中的字符数、单词数和行数。

1.GitHub地址

https://github.com/tsfdssm/WC

 

2.PSP表格

PSP2.1PSP 阶段预估耗时 (分钟)实际耗时 (分钟)
Planning计划3060
· Estimate· 估计这个任务需要多少时间600900
Development开发600300
· Analysis· 需求分析3030
· Design Spec· 生成设计文档2030
· Design Review· 设计复审1030
· Coding Standard· 代码规范300120
· Design· 具体设计2090
· Coding· 具体编码300720
· Code Review· 代码复审6060                  
· Test· 测试20180
Reporting报告3040
· Test Report· 测试报告2020
· Size Measurement· 计算工作量510
· Postmortem & Process Improvement Plan· 事后总结, 并提出过程改进计划1010
 合计18001680

3.WordCount需求说明

WordCount的需求可以概括为:对程序设计语言源文件统计字符数、单词数、行数,统计结果以指定格式输出到默认文件中,以及其他扩展功能,并能够快速地处理多个文件。

可执行程序命名为:wc.exe,该程序处理用户需求的模式为:

wc.exe [parameter] [input_file_name]

存储统计结果的文件默认为result.txt,放在与wc.exe相同的目录下。

4.解题思路

1.首先进行需求分析。

2.然后根据相应的需求进行初步设计。然而由于本人能力有有限,最初想要实现的几个比较难的功能一个都没有实现,我真的好菜。。。

3.由于我分析得比较简单,没有对需求做出更加深入的思考。所以代码编写得也比较乱。主要是每个功能由一个函数实现。

  大致如下:

 1     int _c(char*);
 2 
 3     int _w(char*);
 4 
 5     int _l(char*);
 6 
 7     bool _o(char*);
 8 
 9     bool _s(char*);
10 
11     bool _a(char *);
12 
13     bool _e(char*);
14 
15     void _x();

 

5.代码分析

(1)实现-c功能。

 1 int WordCount::_c(char * file)

(2)实现-w功能,这个是通过检测字符是否为空格,回车,逗号等以及其后的字符也是否为回车,空格,逗号来判断字符数量的。

int WordCount::_w(char *file)
{
    ////
    ifstream fin;
    int word = 1;

    fin.open(file);
    if (!fin.is_open())    //检测文件是否成功打开
    {
        cerr << "Could not open" << file << endl;
        fin.clear();
        fin.close();
        return - 1;
    }

    while(!fin.eof())
    {
        char cw = fin.get();
        char cp = fin.peek();
        if (!((cw == ' ')|| (cw == '\0')||(cw == '\t' )||( cw == '\n') || (cw == ',')))
        {
            if((cp == ' ') || (cp == '\0') || (cp == '\t') || (cp == '\n') || (cp == ','))
            {
                word++;
            }
        }

    }
    fin.clear();
    fin.close();

    return word;//返回计数值
}

(3)实现-l功能。

int WordCount::_l(char * file)

(4)实现-o功能。其实-o功能还需要其他代码才能实现。

 1 bool WordCount::_o(char *file)

(5)实现-s功能。

 1 bool WordCount::_s(char *file)

(6)实现-a功能。

1 bool WordCount::_a(char *file)

(7)实现-e功能:

1 bool WordCount::_e(char *stoplistfile)

(8)实现-x功能:

1 void WordCount::_x()
2 {
3     WinExec("WordCountUI.exe", SW_NORMAL);
4 }

6.测试

如图这只是在控制台上进行简单测试。这样效率较高.实际在文件中的输出与之类似,不在赘述。

由于时间较紧,关于禁用词表的功能还有部分未能实现,还望见谅。

(1)

(2)

(3)

(4)

(5)

(6)

 

(7)

(8)

(9)

(10‘)

7.参考的相关资料及网址:

https://wenku.baidu.com/view/7cb4e4096fdb6f1aff00bed5b9f3f90f76c64df0.html

https://www.cnblogs.com/collectionne/p/6815924.html

http://blog.youkuaiyun.com/u011543018/article/details/50724883

http://blog.youkuaiyun.com/cc514981717/article/details/52130621

http://blog.youkuaiyun.com/qq_15437667/article/details/52215917

https://www.cnblogs.com/collectionne/p/6815924.html

https://www.cnblogs.com/tinaluo/p/6824674.html

 

转载于:https://www.cnblogs.com/tsfdssm/p/8613421.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值