根据WordCount的需求描述,先编程实现,再编写单元测试,最后撰写博客。至少完成需求中的基本功能。
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
注意:
空格,水平制表符,换行符,均算字符。
由空格或逗号分割开的都视为单词,且不做单词的有效性校验,例如:thi#,that视为用逗号隔开的2个单词。
-c, -w, -l参数可以共用同一个输入文件,形如:wc.exe –w –c file.c 。
-o 必须与文件名同时使用,且输出文件必须紧跟在-o参数后面,不允许单独使用-o参数。
我打算用C#来解决这个问题。首先,我打算分为两个类,第一个类从键盘输入中读取操作字符串并对其进行分割处理。第二个类传入第一个类中处理后的数据进行操作。这个类里建立三个方法。第一个用来引入操作数据并判断输入是否规范。第二个方法用来统计字符数,单词数和行数。第三个方法用来输出并存入文件。
··· using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace wc { class Program { static void Main(string[] args) { string msg = ""; //输入字符串 string filename; //文件名 int length=0; //数组长度 WC wordcount = new WC(); //引用WC类 while (msg != "exit") { msg = Console.ReadLine(); //输入操作字符串 string[] msgSplit = msg.Split(‘ ‘); //将字符串按空格分割 for (int i = 0; i < msgSplit.Length; i++) //判断操作命令中是否带输入文件 { if (msgSplit[i] != "-o") { length++; } else { break; } } if (length == msgSplit.Length) //不带输出文件,文件名为最后一个字符串 { filename = msgSplit[msgSplit.Length - 1]; } else //带输出文件,文件名为倒数第三个字符串 { filename = msgSplit[msgSplit.Length - 3]; }
length = 0; wordcount.Operator(msgSplit, filename); wordcount.BaseCount(filename); wordcount.Display(); } } } } ···
··· public void Operator( string[] msgSplit, string filename) { this.filename = filename; //引入文件名 this.msgSplit = msgSplit; //引入操作数组 foreach (string s in msgSplit) { // 基本功能 if (s=="wc.exe"||s == "-c" || s == "-w" || s == "-l" || s == "-o") { break; } else { Console.WriteLine("参数 {0} 不存在", s); //输入不规范,提示参数不存在 break; } } } ···
··· public void BaseCount(string filename) { try { // 打开文件 FileStream file = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read); StreamReader sr = new StreamReader(file); int nChar; int charcount = 0; int wordcount = 0; int linecount = 0; //定义一个字符数组 char[] symbol = { ‘ ‘, ‘\t‘, ‘,‘, ‘.‘, ‘?‘, ‘!‘, ‘:‘, ‘;‘, ‘\‘‘, ‘\"‘, ‘\n‘, ‘{‘, ‘}‘, ‘(‘, ‘)‘, ‘+‘ ,‘-‘, ‘*‘, ‘=‘}; while ((nChar = sr.Read()) != -1) { charcount++; // 统计字符数 foreach (char c in symbol) { if (nChar == (int)c) { wordcount++; // 统计单词数 } } if (nChar == ‘\n‘) { linecount++; // 统计行数 } } iccount = charcount; iwcount = wordcount + 1; ilcount = linecount + 1; sr.Close(); } catch (IOException ex) { Console.WriteLine(ex.Message); return; } } ···
··· public void Display() { //将操作命令以-c,-w,-l的顺序输出 foreach (string s in msgSplit) { if (s == "-c") //遍历第一次找-c命令,有的话执行 { Console.WriteLine("字 符 数:{0}", iccount); str = "file.c,字 符 数:" + iccount + "\r\n"; File.AppendAllText("result.txt", str, Encoding.Default); } } foreach (string s in msgSplit) { if (s == "-w") //遍历第二次找-w命令,有的话执行 { Console.WriteLine("单 词 数:{0}", iwcount); str = "file.c,单 词 数:" + iwcount + "\r\n"; File.AppendAllText("result.txt", str, Encoding.Default); } } foreach (string s in msgSplit) { if (s == "-l") //遍历第三次找-l命令,有的话执行 { Console.WriteLine("总 行 数:{0}", ilcount); str = "file.c,总 行 数:" + ilcount + "\r\n"; File.AppendAllText("result.txt", str, Encoding.Default); } } foreach (string s in msgSplit) { if (s == "-o") //遍历第四次找-o命令,有的话执行 { this.outfile = msgSplit[msgSplit.Length - 1]; //引入输出文件名 foreach (string st in msgSplit) { if (st == "-c") //遍历第一次找-c命令,有的话存入输出文档 { str = "file.c,字 符 数:" + iccount + "\r\n"; File.AppendAllText("" + outfile + "", str, Encoding.Default); } } foreach (string st in msgSplit) { if (st == "-w") //遍历第二次找-w命令,有的话存入输出文档 { str = "file.c,单 词 数:" + iwcount + "\r\n"; File.AppendAllText("" + outfile + "", str, Encoding.Default); } } foreach (string st in msgSplit) { if (st == "-l") //遍历第三次找-l命令,有的话存入输出文档 { str = "file.c,总 行 数:" + ilcount + "\r\n"; File.AppendAllText("" + outfile + "", str, Encoding.Default); } } } } Console.WriteLine(); } ···
重新完成这次作业,收获很多,学到了更多知识。
原文:https://www.cnblogs.com/xjm201631062323/p/9732641.html