1. 程式人生 > >第三次作業WordCount拓展

第三次作業WordCount拓展

(1)合作者:201631062323徐建敏        201631062223 林布林

(2)程式碼地址:https://gitee.com/xjm861710023/wordcount_development

(3)本次作業連結地址:https://edu.cnblogs.com/campus/xnsy/2018softwaretest2398/homework/2187

一、拓展程式碼

(1)主函式

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace wc { class Program { static void Main(string[] args) { string msg = ""; //輸入字串 WC wordcount = new WC(); //引用WC類 Char[] c = {' '};//分隔符 while (msg != "exit") { msg = Console.ReadLine(); //輸入操作字串
string[] msgSplit = msg.Split(c, StringSplitOptions.RemoveEmptyEntries);//將字串按空格分割 wordcount.Operator(msgSplit); } } } }

(2)WC類

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace
wc { public class WC { public string filename; // 檔名 public string[] filenames; //多個檔名 public string stopwordfile; //停用詞檔名 public string[] msgSplit; //所有引數陣列 public string outfile; //輸出檔名 public string str; //輸出字串 public int iccount; // 字元數 public int iwcount; // 單詞數 public int ilcount; // 總行數 public int inulllinecount; //空行數 public int icodelinecount; //程式碼行數 public int inotelinecount; //註釋行數 //引入輸入字串,並對字串進行處理 public void Operator(string[] msgSplit) { this.msgSplit = msgSplit; //引入運算元組 int Length; Length = msgSplit.Length; foreach (string s in msgSplit) { if (s == "-e") { Length = Length - 2; } else if (s == "-o") { Length = Length - 2; } } this.filename = msgSplit[Length - 1];//獲取檔名 if (msgSplit[0] == "wc.exe") { foreach (string s in msgSplit) { if (s == "-s") { //獲取所有後綴名符合的檔案 string fileDir = Environment.CurrentDirectory; filenames = Directory.GetFiles(fileDir, filename); } else if (s == "-e") { //獲取停用詞表檔案 for (int i = 0; i < msgSplit.Length; i++) { if (msgSplit[i] == "-e") { stopwordfile = msgSplit[i + 1]; } } } } if (filenames == null) { SuperCount(filename); BaseCount(filename, stopwordfile); Display(filename); } else { for (int i = 0; i < filenames.Length; i++) { SuperCount(filenames[i]); BaseCount(filenames[i], stopwordfile); Display(filenames[i]); } } } else { Console.WriteLine("輸入指令不存在!請重新輸入。。。"); } } // 統計基本資訊:字元數 單詞數 行數 public void BaseCount(string filename,string stopwordfile) { 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','\r', ',', '.', '?', '!', ':', ';', '\'', '\"', '\n', '{', '}', '(', ')', '<','>','+' ,'-','*', '='}; while ((nChar = sr.Read()) != -1) { charcount++; // 統計字元數 if (nChar == '\n') { linecount++; // 統計行數 } } sr.Close(); file = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read); sr = new StreamReader(file); string words = sr.ReadToEnd(); string[] word = words.Split(symbol, StringSplitOptions.RemoveEmptyEntries);//將檔案內所有單詞分割 if (stopwordfile != null) { //有停用詞表 FileStream stopfile = new FileStream(stopwordfile, FileMode.Open, FileAccess.Read, FileShare.Read); StreamReader stopsr = new StreamReader(stopfile); string stopwords = stopsr.ReadToEnd(); string[] stopword = stopwords.Split(symbol, StringSplitOptions.RemoveEmptyEntries);//將停用詞表內所有單詞分割 foreach (string s in word) { foreach (string stop in stopword) { if (s != stop) { wordcount++;//統計單詞數 } } } stopsr.Close(); } else { //沒有停用詞表 wordcount = word.Length; } sr.Close(); iccount = charcount; iwcount = wordcount; ilcount = linecount + 1; } catch (IOException ex) { Console.WriteLine(ex.Message); return; } } // 統計程式碼行 空行 註釋行 public void SuperCount(string filename) { try { // 開啟檔案 FileStream file = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read); StreamReader sr = new StreamReader(file); String line; int nulllinecount = 0; int codelinecount = 0; int notelinecount = 0; while ((line = sr.ReadLine()) != null) { // 除去每行開頭多餘空格和格式控制字元 line = line.Trim(' '); line = line.Trim('\t'); // 空行 if (line == "" || line.Length <= 1) { nulllinecount++; } // 註釋行 else if (line.Substring(0, 2) == "//" || line.Substring(1, 2) == "//") { notelinecount++; } // 程式碼行 else { codelinecount++; } } inulllinecount = nulllinecount; icodelinecount = codelinecount; inotelinecount = notelinecount; sr.Close(); } catch (IOException ex) { Console.WriteLine(ex.Message); return; } } // 列印資訊 public void Display(string filename) { //將操作命令以-c,-w,-l的順序輸出 foreach (string s in msgSplit) { if (s == "-c") //遍歷第一次找-c命令,有的話輸出字元數 { Console.WriteLine(filename + ",字元數:{0}", iccount); str = filename + ",字 符 數:" + iccount + "\r\n"; File.AppendAllText("result.txt", str, Encoding.Default); } } foreach (string s in msgSplit) { if (s == "-w") //遍歷第二次找-w命令,有的話輸出單詞數 { Console.WriteLine(filename + ",單詞數:{0}", iwcount); str = filename + ",單 詞 數:" + iwcount + "\r\n"; File.AppendAllText("result.txt", str, Encoding.Default); } } foreach (string s in msgSplit) { if (s == "-l") //遍歷第三次找-l命令,有的話輸出行數 { Console.WriteLine(filename + ",行 數:{0}", ilcount); str = filename + ",總 行 數:" + ilcount + "\r\n"; File.AppendAllText("result.txt", str, Encoding.Default); } } foreach (string s in msgSplit) { if (s == "-a") //遍歷第四次找-a命令,有的話輸出程式碼行/空行/註釋行 { Console.WriteLine(filename + ",程式碼行/空行/註釋行:{0}/{1}/{2}", icodelinecount, inulllinecount, inotelinecount); str = filename + ",程式碼行/空行/註釋行:" + icodelinecount + '/' + inulllinecount + '/' + inotelinecount + "\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 = filename + ",字 符 數:" + iccount + "\r\n"; File.AppendAllText("" + outfile + "", str, Encoding.Default); } } foreach (string st in msgSplit) { if (st == "-w") //遍歷第二次找-w命令,有的話將單詞數存入輸出文件 { str = filename + ",單 詞 數:" + iwcount + "\r\n"; File.AppendAllText("" + outfile + "", str, Encoding.Default); } } foreach (string st in msgSplit) { if (st == "-l") //遍歷第三次找-l命令,有的話將行數存入輸出文件 { str = filename + ",總 行 數:" + ilcount + "\r\n"; File.AppendAllText("" + outfile + "", str, Encoding.Default); } } foreach (string st in msgSplit) { if (st == "-a") //遍歷第四次找-a命令,有的話將程式碼行/空行/註釋行存入輸出文件 { str = filename + ",程式碼行/空行/註釋行:" + icodelinecount + '/' + inulllinecount + '/' + inotelinecount + "\r\n"; File.AppendAllText("" + outfile + "", str, Encoding.Default); } } } } Console.WriteLine(); } } }

(3)單元測試程式碼

using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;

namespace TestWC
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestBaseCount()
        {
            //測試統計字元數,單詞書,行數
            wc.WC wordcount = new wc.WC();
            string filename = "F://徐建敏//wc//wc//bin//Debug//file1.c";
            string stopwordfile = "F://徐建敏//wc//wc//bin//Debug//stop.txt";
            wordcount.BaseCount(filename, stopwordfile);
            int ccount = wordcount.iccount;
            int wcount = wordcount.iwcount;
            int lcount = wordcount.ilcount;
            Assert.AreEqual(87, ccount);
            Assert.AreEqual(10, wcount);
            Assert.AreEqual(7, lcount);
        }
        [TestMethod]
        public void TestOperator()
        {
            //測試對輸入字串進行引入並處理
            wc.WC wordcount = new wc.WC();
            string[] msgSplit = { "wc.exe", "-c", "-w", "-l", "F://徐建敏//wc//wc//bin//Debug//file1.c"};
            wordcount.Operator(msgSplit);
            string filename = wordcount.filename;
            string stopword = wordcount.stopwordfile;
            string[] filenames = wordcount.filenames;
            Assert.AreEqual("F://徐建敏//wc//wc//bin//Debug//file1.c", filename);
            Assert.AreEqual(null, stopword);
            Assert.AreEqual(null, filenames);
        }
        [TestMethod]
        public void TestSuperCount()
        {
            //測試統計程式碼行 空行 註釋行
            wc.WC wordcount = new wc.WC();
            string filename = "F://徐建敏//wc//wc//bin//Debug//file1.c";
            wordcount.SuperCount(filename);
            int codecount = wordcount.icodelinecount;
            int nullcount = wordcount.inulllinecount;
            int notecount = wordcount.inotelinecount;
            Assert.AreEqual(4, codecount);
            Assert.AreEqual(2, nullcount);
            Assert.AreEqual(1, notecount);
        }
        [TestMethod]
        public void TestDisplay()
        {
            //測試輸出
            wc.WC wordcount = new wc.WC();
            string[] msg={"wc.exe","-c","F://徐建敏//wc//wc//bin//Debug//file1.c"};
            wordcount.msgSplit = msg;
            string filename = "F://徐建敏//wc//wc//bin//Debug//file1.c";
            wordcount.Display(filename);
            string result = wordcount.str;
            Assert.AreEqual("F://徐建敏//wc//wc//bin//Debug//file1.c,字 符 數:0\r\n", result);
        }
    }
}

二、拓展功能展示

 

 

三、靜態程式碼檢查

四、單元測試

五、個人總結

通過這次作業,學會了使用VS自帶的單元測試工具。同時,結隊程式設計互相討論,互相提供靈感,對程式設計的幫助很大。但是實在沒有找到靜態程式碼測試工具,只能看VS自帶的錯誤提示,沒有學習到靜態程式碼工具的使用,有一些遺憾。