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

WordCount代码实现及测试

时间:2018-10-21 21:48:40      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:版本   出现   项目   sha   ast   设计   没有   基础   img   

1.项目地址

开发者:201631062515 201631062415

码云地址:https://gitee.com/heshuxiang/WordCount/tree/master

2.项目需求

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

(1)基本功能:wc.exe -c file.c     //返回文件 file.c 的字符数

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

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

                     wc.exe -o outputFile.txt     //将结果输出到指定文件outputFile.txt

 

(2)代码互审情况

大部分的代码都没有问题,在主函数传递和计算单词量时出现了一些函数传递不一致和变量问题,已修改。

 

3.基本思路

    拿到该项目时,我首先想到的是要先确定字符,单词,行数的判断条件。我想只要这个确定了,那么剩下的工作就很简单了,我只需要按照这个判断条件去进行编码实现。由于现阶段我们只需要完成这些基础功能,因而在编程语言方面我选择了最早接触的C语言。

当然在确定判断条件时我们还是要动点脑子的,可以联系实际去思考。例如在统计字符时,我们可以通过循环来判断当前字符是否为空格,或其他非字符元素,如果是,字符数就加一。又例如在判断单词数时,我们可以先确定单词之间可以是逗号,可以是空格,通过这些条件来判断哪些字符组成了单词。当然如果实在不行,我们还可以借助网络去查询,毕竟这是一个网络信息时代。

4.源代码

 

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.IO;
  6 using System.Threading.Tasks;
  7 
  8 
  9 namespace WordCount
 10 {
 11     class Program
 12     {
 13         public class WC
 14         {
 15             public string sFilename;    // 文件名
 16             public string[] sParameter; // 参数数组  
 17             public int iCharcount;      // 字符数
 18             public int iWordcount;      // 单词数
 19             public int iLinecount;      // 总行数
 20 
 21             // 参数控制信息
 22             public void Operator(string[] sParameter, string sFilename)
 23             {
 24                 this.sParameter = sParameter;
 25                 this.sFilename = sFilename;
 26                 foreach (string s in sParameter)
 27                 {
 28                     //  基本功能
 29                     if (s == "-c" || s == "-w" || s == "-l")
 30                     {
 31                         break;
 32                     }
 33                     else
 34                     {
 35                         Console.WriteLine("参数 {0} 不存在", s);
 36                         break;
 37                     }
 38                 }
 39             }
 40             // 统计基本信息:字符数 单词数 行数
 41             public void BaseCount()
 42             {
 43                 try
 44                 {
 45                     // 打开文件
 46                     FileStream file = new FileStream(sFilename, FileMode.Open, FileAccess.Read, FileShare.Read);
 47                     StreamReader sr = new StreamReader(file);
 48                     int nChar;
 49                     int charcount = 0;
 50                     int wordcount = 0;
 51                     int linecount = 0;
 52                     //定义一个字符数组
 53                     char[] symbol = {  , \t, ,, ., ?, !, :, ;, \‘, \", \n, {, }, (, ), + ,-,
 54               *, =};
 55                     while ((nChar = sr.Read()) != -1)
 56                     {
 57                         charcount++;     // 统计字符数
 58 
 59                         foreach (char c in symbol)
 60                         {
 61                             if (nChar == (int)c)
 62                             {
 63                                 wordcount++; // 统计单词数
 64                             }
 65                         }
 66                         if (nChar == \n)
 67                         {
 68                             linecount++; // 统计行数
 69                         }
 70                     }
 71                     iCharcount = charcount;
 72                     iWordcount = wordcount + 1;
 73                     iLinecount = linecount + 1;
 74                     sr.Close();
 75                 }
 76                 catch (IOException ex)
 77                 {
 78                     Console.WriteLine(ex.Message);
 79                     return;
 80                 }
 81             }
 82             // 打印信息
 83             public void Display()
 84             {
 85                 foreach (string s in sParameter)
 86                 {
 87                     if (s == "-c")
 88                     {
 89                         Console.WriteLine("字 符 数:{0}", iCharcount);
 90                     }
 91                     else if (s == "-w")
 92                     {
 93                         Console.WriteLine("单 词 数:{0}", iWordcount);
 94                     }
 95                     else if (s == "-l")
 96                     {
 97                         Console.WriteLine("总 行 数:{0}", iLinecount);
 98                     }
 99                 }
100                 Console.WriteLine();
101             }
102         }
103 
104         [STAThread]
105         static void Main(string[] args)
106         {
107             string message = ""; // 存储用户命令
108             while (message != "exit")
109             {
110                 Console.Write("wc.exe ");
111                 message = Console.ReadLine();               // 得到输入命令
112                 string[] arrMessSplit = message.Split( ); // 分割命令
113                 int iMessLength = arrMessSplit.Length;
114                 string[] sParameter = new string[iMessLength - 1];
115                 // 获取命令参数数组
116                 for (int i = 0; i < iMessLength - 1; i++)
117                 {
118                     sParameter[i] = arrMessSplit[i];
119                 }
120                 // 获取文件名
121                 string sFilename = arrMessSplit[iMessLength - 1];
122                 Console.WriteLine();
123                 WC wc = new WC();
124                 wc.Operator(sParameter, sFilename);//执行
125                 wc.BaseCount();//统计信息
126                 wc.Display();//打印信息
127                 
128               }
129         }
130 
131     }
132 }

 

 

5.测试文本

技术分享图片

 

6.测试结果

 技术分享图片

 

7.总结

 通过这回的结对编程项目,我发现了一些结对编程的优点和更广阔的提升技术的平台码云能帮助我保存代码版本,博客园能提供我软件开发的知识和视野,有许多软件开发者在博客园这个平台上发表自己的学习体会和开发经验

1.结对编程可是通过队友的讨论使编码更有思路
2.可以及时的发现一些致命的错误并及时改正

WordCount代码实现及测试

标签:版本   出现   项目   sha   ast   设计   没有   基础   img   

原文地址:https://www.cnblogs.com/marcopolo/p/9826756.html

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