1. 程式人生 > >WordCount的基本功能及擴充套件功能(兩人組隊完成)

WordCount的基本功能及擴充套件功能(兩人組隊完成)

合作者:201631062118,201631062217
專案gitee連結:https://gitee.com/suiran90/wc/ (注:由於gitee操作不熟練,此次我們兩人程式碼上傳時出現問題,商議為由我上傳,相關操作正在學習)
本次作業連結地址:https://edu.cnblogs.com/campus/xnsy/2018softwaretest2398/homework/2187

程式碼互審情況

在上一個專案中,我們各自完成了 wordcount 的基礎功能,但由於各自單獨編寫,出現了許多小問題,在這裡舉出:
1.在比較兩個字元是否相同時,使用 args[i].equals(“-o”) ,若引數 args[i] 缺失,會使程式出現Bug,應該使用 "-o".equals(args[i]) ;
2.命名問題,出現某些無意義變數,類似 int a 之類;
3.變數,函式命名字母大小寫問題;
4.某些註釋不清楚問題

程式碼互審情況 :

靜態程式碼檢查情況:

出現了下列程式碼檢查情況

單元測試情況:

對測試程式碼實現語句覆蓋
-a:

-s:

-e:

相關函式

當用戶選擇讀取 程式碼行、空行、註釋行

Encoding encode = Encoding.GetEncoding("GB2312");  
FileStream fs = new FileStream(filename, FileMode.Open);
StreamReader sr = new StreamReader(fs, encode);

來對檔案操作,其中使用 "GB2312" 可以對中文字元正常讀取,filename 引數為檔案路徑。

程式碼說明

Rows.cs(讀取行數)

 //空行數
        public int BlankLines(string filename)//傳入檔案位置字串
        {
            //開啟檔案
            Encoding encode = Encoding.GetEncoding("GB2312");//中文字元讀取
            //fs只讀方式開啟檔案,錯誤,需要共享鎖,要選擇flieShare方式為ReadWrite,否則會錯誤
            FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            StreamReader sr = new StreamReader(fs, encode);

            string line;
            int NumberOfBlankLines = 0;//記錄行數

            //噹噹前行不為空,即當前行存在,即 +1 
            while ((line = sr.ReadLine()) != null)
            {
                while (line == "")
                {
                    NumberOfBlankLines++;
                    break;//統計一次,退出,否則會死迴圈
                }
            }            
 //           Console.WriteLine("Black line:" + NumberOfBlankLines);//顯示行數

            //關閉檔案
            sr.Close();
            fs.Close();

            return NumberOfBlankLines;//注意函式是有返回值的
        }

        //註釋行        
        public int CommentLines(string filename)//傳入檔案位置字串
        {
            //開啟檔案
            Encoding encode = Encoding.GetEncoding("GB2312");//中文字元讀取
            //fs只讀方式開啟檔案,錯誤,需要共享鎖,要選擇flieShare方式為ReadWrite,否則會錯誤
            FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            StreamReader sr = new StreamReader(fs, encode);

            string line;
            int NumberOfCommentLines = 0;//記錄行數

            //噹噹前行不為空,即當前行存在,即 +1 
            while ((line = sr.ReadLine()) != null)
            {
                char[] ArrayChar = line.ToCharArray();
                if (ArrayChar.Length >= 2)
                {
                    for (int i = 0; i < ArrayChar.Length - 1; i++)//防止越界
                    {
                        if ((ArrayChar[i] == '/' && ArrayChar[i + 1] == '/'))
                        {
                            NumberOfCommentLines++;
                            break;
                        }
                    }
                }                
            }
  //          Console.WriteLine("Black line:" + NumberOfCommentLines);//顯示行數

            //關閉檔案
            sr.Close();
            fs.Close();

            return NumberOfCommentLines;//注意函式是有返回值的
        }

        //程式碼行
        public int CodeLines(string filename)//傳入檔案位置字串
        {
            //開啟檔案
            Encoding encode = Encoding.GetEncoding("GB2312");//中文字元讀取

            //fs只讀方式開啟檔案,錯誤,需要共享鎖,要選擇flieShare方式為ReadWrite,否則會錯誤
            FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            StreamReader sr = new StreamReader(fs, encode);

            string line;

            int NumberOfBlankLines = 0;//記錄空白行數
            NumberOfBlankLines = BlankLines(filename);
            //噹噹前行不為空,即當前行存在,即 +1 
            int CountLine = 0;//記錄總行數
            int NumberOfCommentLines = 0;
            NumberOfCommentLines = CommentLines(filename);
            //噹噹前行不為空,即當前行存在,即 +1 
            while ((line = sr.ReadLine()) != null)
            {
                CountLine++;
            }
            Console.WriteLine("Code line:" + (CountLine - NumberOfBlankLines - NumberOfCommentLines)
                + "\nBlack Line:" + NumberOfBlankLines + "\nComment Line:" + NumberOfCommentLines + "\n");//顯示行數

            //關閉檔案
            sr.Close();
            fs.Close();

            return NumberOfBlankLines;//注意函式是有返回值的
        }

當讀取StopList.txt時

class StopList
    {
        //總行數
        public string StopOfList()//傳入檔案位置字串
        {
            string filename = System.IO.Directory.GetCurrentDirectory() + "\\StopList.txt";
            //開啟檔案
            Encoding encode = Encoding.GetEncoding("GB2312");//中文字元讀取
            FileStream fs = new FileStream(filename, FileMode.Open);
            StreamReader sr = new StreamReader(fs, encode);

            string deactivateTable = "";

            deactivateTable = sr.ReadToEnd();
            Console.WriteLine("sop list:" + deactivateTable);
            
            //應該關閉檔案
            sr.Close();
            fs.Close();

            return deactivateTable;
        }
    }

總結

1.處理讀取當前目錄檔案,檔案佔用等問題,學習了前輩的文章;
2.對一個功能處理時,需要一步步思考;
3.程式碼編寫許多相關工具是很有意思的,可以多多發掘其功能;
4.編寫樁模組、驅動程式對開發很有利;
5.完成專案過程中,要有自己的計劃,遵循計劃,避免拖沓

參考文獻連結

感謝以下文章作者的文獻,讓我受益良多
C#讀取目錄下所有指定型別檔案的方法:https://blog.csdn.net/autumn20080101/article/details/52999456
c# 讀寫檔案時檔案正由另一程序使用,因此該程序無法訪問該檔案:https://blog.csdn.net/superhoy/article/details/7931234?utm_source=blogxgwz0