wordcount代码实现及测试(代码片段)

marcopolo marcopolo     2023-01-09     416

关键词:

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 =   , 	, ,, ., ?, !, :, ;, , ", 
, , , (, ), + ,-,
 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 == 
)
 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.可以及时的发现一些致命的错误并及时改正

flink的安装部署及wordcount测试(代码片段)

一、本地模式在本地以多线程的方式模拟Flink中的多个角色。(开发环境不用)下载地址:https://flink.apache.org/downloads.html这里下载的是:flink-1.13.0-bin-scala_2.12.tgz上传到常用的位置,然后解压。启动:切换到flink的bin目... 查看详情

flink的安装部署及wordcount测试(代码片段)

一、本地模式在本地以多线程的方式模拟Flink中的多个角色。(开发环境不用)下载地址:https://flink.apache.org/downloads.html这里下载的是:flink-1.13.0-bin-scala_2.12.tgz上传到常用的位置,然后解压。启动:切换到flink的bin目... 查看详情

软件测试第二周个人作业wordcount程序实现(代码片段)

GitHub地址:https://github.com/Guchencc/WordCount一.PSP表格PSP2.1PSP阶段预估耗时(分钟)实际耗时(分钟)Planning计划  ·Estimate·估计这个任务需要多少时间 300440Development开发  ·Analysis·需求分析(包括学习新技术) 20&n... 查看详情

yarn命令使用及wordcount解析(代码片段)

...MapReduce与Yarn的架构设计及简单工作流程,本篇文章将以wordcount程序为例,简单介绍下Yarn的使用。1.wordcount示例运行[[email protected]~]#su-hadoop[[email protected]~]$jps9201SecondaryNameNode9425ResourceManager13875Jp 查看详情

软件开发及测试(代码片段)

项目地址github项目地址:https://github.com/81BlingBling18/WordCount本次项目中用到的PSP表格PSP2.1PSP阶段预估耗时(分钟)实际耗时(分钟)Planning计划2030·Estimate·估计这个任务需要多少时间1514Development开发280300·Analysis·需求分析(包括学... 查看详情

mapreduce编写wordcount程序代码实现(代码片段)

MapReduce经典案例代码(wordcount)以经典的wordcount为例,通过自定义的mapper和reducer来实现单词计数packagecom.fwmagic.mapreduce;importorg.apache.hadoop.conf.Configuration;importorg.apache.hadoop.fs.Path;importorg.apache.hadoop.io. 查看详情

个人项目(代码片段)

Github:https://github.com/Naren-Github66/WordCount项目相关要求1.题目描述WordCount实现一个简单而完整的软件工具(源程序特征统计程序)。进行单元测试、回归测试、效能测试,在实现上述程序的过程中使用相关的工具。进行个人软件过... 查看详情

wordcount优化——单词及词频统计(代码片段)

github地址https://github.com/lzwk/WCproPSP表格PSP2.1PSP阶段预估耗时(分钟)实际耗时(分钟)Planning计划1020·Estimate·估计这个任务需要多少时间1015Development开发500480·Analysis·需求分析(包括学习新技术)80100·DesignSpec·生成设计文档3030·Desi... 查看详情

wordcount开发与测试(代码片段)

(1)GitHub项目地址https://github.com/AnotherLZ/SoftwareTesting/tree/master(2)PSP表格PSP2.1PSP阶段预估耗时(分钟)实际耗时(分钟)Planing计划3030·Estimate·估计这个任务需要多少时间3030Development开发600850·Analysis·需求分析(包括学习新技术)4... 查看详情

wordcount编码与测试(代码片段)

github项目地址https://github.com/SSSGLQ/WordCountPSP表格分析PSP2.1PSP阶段预估耗时(分钟)实际耗时(分钟)Planning计划180200·Estimate·估计这个任务需要多上时间180200Development开发15601800·Analysis·需求分析(包括学习新技术)90100·DesignSpec·生成设计... 查看详情

wordcount(代码片段)

...java.io.File;importjava.io.FileReader;importjava.io.IOException;publicclassWordCount2publicstaticvoidmain(String[]args)throwsIOExcepti 查看详情

spark编程环境搭建及wordcount实例(代码片段)

  基于IntellijIDEA搭建Spark开发环境搭建 基于IntellijIDEA搭建Spark开发环境搭——参考文档  ●参考文档http://spark.apache.org/docs/latest/programming-guide.html  ●操作步骤·a)创建maven项目·b)引入依赖(Spark依赖、... 查看详情

hadoop学习笔记-2wordcount完整实例(代码片段)

...义函数定义 函数代码说明创建Main.java 完整代码​运行wordcount生成jar包启动hadoop准备测试文件 运行开发工具IDEA创建Maven项目NewProject->Maven, 查看详情

wordcount-软件测试初体验(代码片段)

github:https://github.com/skz12345/WordCountPSP2.1PSP阶段预估耗时(分钟)实际耗时(分钟)Planning计划4060·Estimate·估计这个任务需要多少时间4060Development开发305510·Analysis·需求分析(包括学习新技术)60120·DesignSpec·生成设计文档2030·DesignRevi... 查看详情

wordcount(代码片段)

1.Github项目地址https://github.com/ZJHqs/WC2.实现程序前,模块开发预计时间PSP2.1PersonalSoftwareProcessStages预估耗时(分钟)实际耗时(分钟)Planning计划60    30Estimate估计这个任务需要多少时间20 10Development开发400 300Analysi... 查看详情

wordcount(小组)(代码片段)

合作者:201631062314,201631062214码云地址:https://gitee.com/dsjyun/Word-Count-three一、代码互审:   第一次都是实现了基本功能,没有完成扩展功能,这次还有个高级功能,于是讨论了后续功能如何实现。我们的意见基本一致... 查看详情

count.exe个人项目(代码片段)

 Github项目地址:https://github.com/bravedreamer/wordCount/tree/master/wc一、题目描述:WordCount1.实现一个简单而完整的软件工具(源程序特征统计程序)。2.进行单元测试、回归测试、效能测试,在实现上述程序的过程中使用相关的工具... 查看详情

软件测试第四周作业wordcount优化(代码片段)

Github地址https://github.com/husterC/WordCountGroupworkPSP表格PSP2.1PSP阶段预估耗时(分钟)实际耗时(分钟)Planning计划  ·Estimate·估计这个任务需要多少时间540 780Development开发  ·Analysis·需求分析(包括学习新技术) 60&... 查看详情