WordCount

wordCount的实现

    201631062630  王菲

一 、本文代码

  Github代码下载链接:https://gitee.com/wang1007/WordCount.git

二、功能概述及psp:

2.1.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.1.2 扩展功能(未完成)

wc.exe -s            //递归处理目录下符合条件的文件

wc.exe -a file.c     //返回更复杂的数据(代码行 / 空行 / 注释行)

wc.exe -e stopList.txt  // 停用词表,统计文件单词总数时,不统计该表中的单词

[file_name]: 文件或目录名,可以处理一般通配符

三、代码实现:

WC类代码:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.IO;
  4 using System.Linq;
  5 using System.Text;
  6 using System.Threading.Tasks;
  7 
  8 namespace WordCount
  9 {
 10     class WC
 11     {
 12         string testFile;    // 文件名
 13         List<string> lstOrder; // 输入指令的集合  
 14         int Charcount;      // 字符数
 15         int Wordcount;      // 单词数
 16         int Linecount;      // 总行数
 17         
 18         public bool isChar = false;  //是否查询字符数
 19         public bool isWord = false;  //是否查询单词数
 20         public bool isLine = false;  //是否查询行数
 21         public bool isOut = false;  //是否输出数据
 22 
 23         //---------------------检查输入的指令----------------------
 24         public void Check(List<string> lstOrder, string testFile)//检测输入
 25         {
 26             this.lstOrder = lstOrder;
 27             this.testFile = testFile;
 28 
 29             foreach (string sign in lstOrder)
 30             {
 31                 if (sign == "-c")
 32                 {
 33                     isChar = true;
 34                 }
 35                 else if (sign == "-w")
 36                 {
 37                     isWord = true;
 38                 }
 39                 else if (sign == "-l")
 40                 {
 41                     isLine = true;
 42                 }
 43                 else if (sign == "-o")
 44                 {
 45                     isOut = true;
 46                 }
 47                 else
 48                 {
 49                     Console.ForegroundColor = ConsoleColor.Red;
 50                     Console.WriteLine("操作指令 {0} 不存在", sign);
 51                     Console.ForegroundColor = ConsoleColor.White;
 52                 }
 53             }
 54         }
 55 
 56         //-----------------------统计数据-------------------------
 57         public void CountData(string testFile)
 58         {
 59             try
 60             {
 61                  Charcount = 0;
 62                  Wordcount = 0;
 63                  Linecount = 0;
 64                  int nChar;
 65                 //字符数组
 66                 char[] symbol = { ' ', ',', '.', '?', '!', ':', ';', '\'', '\"', '\t', '{', '}', '(', ')', '+', '-', '*', '=' };
 67                 //文件打开操作
 68                 FileStream file = new FileStream(testFile, FileMode.Open, FileAccess.Read, FileShare.Read);
 69                 StreamReader fr = new StreamReader(file);
 70                 while ((nChar = fr.Read()) != -1)
 71                 {
 72                     Charcount++; // 统计字符数
 73 
 74                     foreach (char c in symbol)
 75                     {
 76                         if (nChar == (int)c)
 77                         {
 78                             Wordcount++; // 统计单词数
 79                         }
 80                     }
 81                     if (nChar == '\n')
 82                     {
 83                         Linecount++; // 统计行数
 84                     }
 85                 }
 86                 Wordcount += Linecount;
 87                 fr.Close();
 88                 file.Close();
 89             }
 90             catch (IOException)
 91             {
 92                 Console.WriteLine("文件名错误");
 93                 return;
 94             }
 95         }
 96 
 97         //--------------------显示数据------------------------
 98         public void ShowData()
 99         {
100             if (isChar)
101             {
102                 Console.WriteLine("{0},字 符 数:{1}", testFile, Charcount);
103             }
104             if (isWord)
105             {
106                 Console.WriteLine("{0},单 词 数:{1}", testFile, Wordcount);
107             }
108             if (isLine)
109             {
110                 Console.WriteLine("{0},总行数:{1}", testFile, Linecount);
111             }
112             Console.WriteLine();
113         }
114 
115         //-------------------将数据写到文件-------------------
116         public void Write(string resultFile)
117         {
118             //是否有输出指令
119             if(isOut)
120             {
121                 FileStream file = new FileStream(resultFile, FileMode.Create);
122                 StreamWriter fw = new StreamWriter(file);
123                 if (isChar)
124                 {
125                     fw.Write("{0},字 符 数:{1}\r\n", testFile, Charcount);
126                 }
127                 if (isWord)
128                 {
129                     fw.Write("{0},单 词 数:{1}\r\n", testFile, Wordcount);
130                 }
131                 if (isLine)
132                 {
133                     fw.Write("{0},总行数:{1}\r\n", testFile, Linecount);
134                 }
135                 Console.ForegroundColor = ConsoleColor.Green;
136                 Console.WriteLine("已写入文件:{0}", resultFile);
137                 Console.ForegroundColor = ConsoleColor.White;
138                 Console.WriteLine("");
139                 //清空缓冲区
140                 fw.Flush();
141                 //关闭流
142                 fw.Close();
143                 file.Close();
144             }
145         }
146     }
147 }

 

main函数代码:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace WordCount
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             WC file = new WC();
14             while(true)
15             {
16                 Console.ForegroundColor = ConsoleColor.Yellow;
17                 Console.WriteLine("命令格式为 -c_-w_-l_-o   { _(空格) c(字符) w(单词) l(行) o(输出) }");
18                 Console.ForegroundColor = ConsoleColor.White;
19                 Console.Write("WordCount.exe ");
20                 //要查询的文件名
21                 string fileName = "test.txt";
22                 //定义指令
23                 List<string> order = new List<string>();
24                 //输入
25                 string input = Console.ReadLine();
26                 //空格分离,获取指令
27                 string[] ord = input.Split(' ');
28                 for (int i = 0; i < ord.Length; i++)
29                 {
30                     order.Add(ord[i]);
31                 }
32                 //检查指令
33                 file.Check(order, fileName);
34                 //根据指令统计数据
35                 file.CountData(fileName);
36                 //根据指令输出数据
37                 file.ShowData();
38                 file.Write("result.txt");
39                 //重置指令
40                 file.isChar = false;
41                 file.isWord = false;
42                 file.isLine = false;
43                 file.isOut = false;
44             }
45         }
46     }
47 }

 

四、测试结果

 1.完整命令输入

 

2.部分命令输入

 

3.错误命令输入和无序输入(result.txt 内容为上次的输出数据)

posted on 2018-10-20 22:07  划痕  阅读(163)  评论(0编辑  收藏  举报

导航