WordCount实现

一、开发者:201631062410

二、代码地址:https://gitee.com/goddragon/WordCount/tree/master

三、https://edu.cnblogs.com/campus/xnsy/Test/homework/2203

四、功能概述

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

  注意:

  空格,水平制表符,换行符,均算字符。

  由空格或逗号分割开的都视为单词,且不做单词的有效性校验,例如:thi#,that视为用逗号隔开的2个单词。

  -c, -w, -l参数可以共用同一个输入文件,形如:wc.exe –w –c file.c

  -o 必须与文件名同时使用,且输出文件必须紧跟在-o参数后面,不允许单独使用-o参数。 

1.2 扩展功能

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

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

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

  其中,

  代码行:本行包括多于一个字符的代码。

  空   行:本行全部是空格或格式控制字符,如果包括代码,则只有不超过一个可显示的字符,例如“{”

  注释行:本行不是代码行,并且本行包括注释。一个有趣的例子是有些程序员会在单字符后面加注释:}//注释

  在这种情况下,这一行属于注释行。

  -e 必须与停用词文件名同时使用,且停用词文件必须紧跟在-e参数后面,不允许单独使用-e参数。

  stopList.txt中停用词可以多于1个,单词之间以空格分割,不区分大小写,形如:while if switch

  则whileifswitch作为3个停用词,在单词统计的时候不予考虑。停用词表仅对单词统计产生影响,不影响字符和行数的统计。

2.设计

 分析输入符

public void ExecutiveCommand(string strCommand)
获取字符数

public int GetCharNum(string fileName)

获取单词数
public int GetWordNum(string fileName)

获取总行数
public int GetRowNum(string fileName)

循环执行所有文件
public void ExeAllFile()

获取代码行数
public int GetCodeRowNum(string fileName)

获取空行数
public int GetBlankRowNum(string fileName)

获取注释行数
public int GetNoteRowNum(string fileName)

设置禁用词
public void GetStopList(string fileName)

将结果写入文件
public void OutputResult(string fileName)

五、项目实现

Program.cs 

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

namespace WordCount
{
   class Program
     {
        static void Main(string[] args)
        {
             Console.Write("wc.exe -c file.c\t返回文件 file.c 的字符数\n" +
                 "wc.exe -w file.c\t返回文件 file.c 的单词总数\n" +
                 "wc.exe -l file.c\t返回文件 file.c 的总行数\n" +
                 "wc.exe -a file.c\t返回更复杂的数据(代码行/空行/注释行)\n" + 
                 "wc.exe -o outputFile.txt\t将结果输出到指定文件outputFile.txt\n"+
                 "wc.exe -e stopList.txt\t停用词表,统计文件单词总数时,不统计该表中的单词\n"+
                 "wc.exe -s\t循环执行所有.c文件\n");
             WC wc = new WC();
             while(true)
             {
                 Console.WriteLine("--------------------------");
                 Console.WriteLine("输入命令:");
                 string str = Console.ReadLine();
                 wc.ExecutiveCommand(str);
             }
         }
     }
}

  WC.cs

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

namespace WordCount
{
    class WC
    {  
          int charNum = 0;
          int wordNum = 0;
          int rowNum = 0;
          string strResult = "";
          string[] stopWord;
          public void ExecutiveCommand(string strCommand)
          {
              string[] strOperate = strCommand.Split(' ');
              string fileName = "file.c";
             for (int i = 0; i < strOperate.Length; ++i)
             {
                 if (strOperate[i] != "-c" && strOperate[i] != "-w" &&
                     strOperate[i] != "-l" && strOperate[i] != "-a" &&
                     strOperate[i] != "-o" && strOperate[i] != "-e" &&
                     strOperate[i] != "-s" && System.IO.Path.GetExtension(strOperate[i]).Equals(".c"))
                 {
                     fileName = strOperate[i];
                 }
             }
             for (int i = 0; i < strOperate.Length; ++i)
             {
                 if (strOperate[i].Equals("-c"))
                 {
                     charNum = GetCharNum(fileName);
                    SaveResult(fileName + ",字符数:" + charNum);
                 }
                 else if (strOperate[i].Equals("-w"))
                {
                     wordNum = GetWordNum(fileName);
                     SaveResult(fileName + ",单词数:" + wordNum);
               }
                 else if (strOperate[i].Equals("-l"))
                 {
                     rowNum = GetRowNum(fileName);
                     SaveResult(fileName + ",行数:" + rowNum);
                }
                 else if (strOperate[i].Equals("-a"))
                {
                     SaveResult(fileName + ",代码行数:" + GetCodeRowNum(fileName));
                    SaveResult(fileName + ",空白行数:" + GetBlankRowNum(fileName));
                     SaveResult(fileName + ",注释行数:" + GetNoteRowNum(fileName));
                 }
                else if (strOperate[i].Equals("-o"))
                 {
                     OutputResult(strOperate[i + 1]);
                 }
                else if (strOperate[i].Equals("-e"))
                 {
                     GetStopList(strOperate[i + 1]);
                 }
                  else if (strOperate[i].Equals("-s"))
                  {
                      ExeAllFile();
                 }
             }
          }
         
          public int GetCharNum(string fileName)
          { 
              FileStream fileStream = new FileStream(@"1.c", FileMode.Open, FileAccess.Read, FileShare.Read);
              byte[] bytes = new byte[fileStream.Length];
              fileStream.Read(bytes, 0, bytes.Length);
              fileStream.Close();
              string strfile = Encoding.UTF8.GetString(bytes);
  
              return strfile.Length - GetRowNum(fileName) + 1;
          }
         
          public int GetWordNum(string fileName)
         { 
              FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
              byte[] bytes = new byte[fileStream.Length];
              fileStream.Read(bytes, 0, bytes.Length);
              fileStream.Close();
             string strfile = Encoding.UTF8.GetString(bytes);
              int wNum = 0;
              string strword = "";
              strfile += '\n';
              for (int i = 1; i < strfile.Length; ++i)
              {
                  if (strfile[i].Equals(' ') || strfile[i].Equals(',') || strfile[i].Equals('\n'))
                  {
                     if (strfile[i - 1].Equals(' ') == false && strfile[i - 1].Equals(',') == false && strfile[i - 1].Equals('\n') == false)
                     {
                         ++wNum;
                         
                         if (stopWord != null)
                         {
                            strword = strword.ToLower();
                             for (int j = 0; j < stopWord.Length; ++j)
                             {
                                if (strword == stopWord[j])
                                 {
                                     --wNum;
                                 }
                             }
                        }
                         strword = "";
                     }
                 }
                 else
                 {
                     strword += strfile[i];
                 }
             }
             return wNum;
         }
         
         public int GetRowNum(string fileName)
         {
             StreamReader sr = new StreamReader(fileName, Encoding.Default);
             int rNum = 0;
             while (sr.ReadLine() != null)
             {
                 ++rNum;
             }
             return rNum;
         }
        
         public int GetCodeRowNum(string fileName)
         {
             StreamReader sr = new StreamReader(fileName, Encoding.Default);
             string str;
             int codeNum = 0;
             int noteNum = 0;
            int blankNum = 0;
            while ((str = sr.ReadLine()) != null)
             {
                 noteNum = 0;
                 blankNum = 0;
                 for (int i = 0; i < str.Length; ++i)
                 {
                     if (i < str.Length - 1 && str[i].Equals('/') && str[i + 1].Equals('/'))
                     {
                         noteNum = i;
                         break;
                     }
                     if (str[i].Equals(' ') || str[i].Equals('\t') || str[i].Equals('{') || str[i].Equals('}'))
                     {
                         ++blankNum;
                     }
                 }
                 if (noteNum != 0 && noteNum > blankNum)
                 {
                     ++codeNum;
                 }
                 else if (noteNum == 0 && blankNum < str.Length)
                 {
                     ++codeNum;
                 }
             }
             return codeNum;
         }
         public int GetBlankRowNum(string fileName)
         {
             StreamReader sr = new StreamReader(fileName, Encoding.Default);
             string str;
             int num = 0;
            int noteNum = 0;
             int blankNum = 0;
             while ((str = sr.ReadLine()) != null)
             {
                 noteNum = 0;
                 blankNum = 0;
                 for (int i = 0; i < str.Length; ++i)
                 {
                     if (i < str.Length - 1 && str[i].Equals('/') && str[i + 1].Equals('/'))
                     {
                         noteNum = i;
                         break;
                     }
                     if (str[i].Equals(' ') || str[i].Equals('\t') || str[i].Equals('{') || str[i].Equals('}'))
                     {
                        ++blankNum;
                    }
                }
                 if (noteNum != 0 && noteNum == blankNum)
                 {
                     ++num;
                 }
                else if (str.Length == 0 || noteNum == 0 && blankNum == str.Length)
                 {
                     ++num;
                 }
             }
 
             return num;
         }
         public int GetNoteRowNum(string fileName)
         {
             StreamReader sr = new StreamReader(fileName, Encoding.Default);
            string str;
             int noteNum = 0;
             while ((str = sr.ReadLine()) != null)
             {
                 for (int i = 0; i < str.Length; ++i)
                 {
                     if (i < str.Length - 1 && str[i].Equals('/') && str[i + 1].Equals('/'))
                     {
                         ++noteNum;
                         break;
                     }
                }
             }
             return noteNum;
         }
        
         public void OutputResult(string fileName)
         {
             FileStream fileStream = new FileStream(fileName, FileMode.Create);
             StreamWriter sw = new StreamWriter(fileStream);
             sw.WriteLine(strResult);
             sw.Flush();
             sw.Close();
             fileStream.Close();
         }
         
         public void SaveResult(string str)
        {
             Console.WriteLine(str);
             strResult += str + "\r\n";
         }
         
         public void GetStopList(string fileName)
         {
             StreamReader sr = new StreamReader(fileName, Encoding.Default);
             string strfile = sr.ReadToEnd();
             strfile = strfile.ToLower();
             stopWord = strfile.Split(' ');
             for (int i = 0; i < stopWord.Length; ++i)
             {
                 Console.Write(stopWord[i] + "\t");
             }
            Console.WriteLine();
         }
        
         public void ExeAllFile()
         {
             string rootPath = Directory.GetCurrentDirectory();
             DirectoryInfo sfolder = new DirectoryInfo(rootPath);
             foreach (FileInfo file in sfolder.GetFiles("*.c"))
             {
                 SaveResult("\n");
                 SaveResult(file.Name + ",字符数:" + GetCharNum(file.Name));
                 SaveResult(file.Name + ",单词数:" + GetWordNum(file.Name));
                 SaveResult(file.Name + ",总行数:" + GetRowNum(file.Name));
                 SaveResult(file.Name + ",代码行数:" + GetCodeRowNum(file.Name));
                 SaveResult(file.Name + ",空白行数:" + GetBlankRowNum(file.Name));
                 SaveResult(file.Name + ",注释行数:" + GetNoteRowNum(file.Name));
             }
         }
     }
    }

  


六、测试用例

等价类分析

输入

有效等价类

无效等价类

执行文件

wc.exe

其他文件名

操作符

-c

其他任意字符串

-w

-l

-a

-o

-e

-s

被操作文件

*.c  *.txt

其他文件名

 

测试用例

测试用例

预期结果

wc.exe -c file.c

file.c,字符数:[数字]

wc.exe -w file.c

file.c,单词数:[数字]

wc.exe -l file.c

file.c,总行数:[数字]

wc.exe -c -w -l file.c

file.c,字符数:[数字]

file.c,单词数:[数字]

file.c,总行数:[数字]

 wc.exe -a file.c

file.c,代码行数:[数字]

file.c,空行数:[数字]

file.c,注释行数:[数字]

 wc.exe -o outputFile.txt

 已存入执行结果

 wc.exe -s

 对所有.c文件执行所有操作

wc.exe -e stopList.txt

存入所有禁用单词

wccff.exe  sff dfsffgg...c

无操作

outputFile.txt -c -w-s-l-w

无操作

 

 七、测试结果

 

 

 

 

 

 

猜你喜欢

转载自www.cnblogs.com/YuanXiaolong/p/9827425.html