WordCount C#

Gitee代码链接:https://gitee.com/Lin-Ling/codes/obsp28igd6kfajln7vut323#读取文件

1.解题思路

刚开始看到这个作业,觉得C#直接就是.exe文件,就选择用C#做,但是太久没做C#,还是很生疏。

主要实现的功能就是

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.程序设计实现过程

需要四个类,主类是读取文件。其他三个类是读取开始的信息的,计算行数、字符数或者单词数。统计行数,字符数和单词数。输出结果的。

第一步做的就是把读取文件的部分写出来,也就是我的主函数。然后再把需要实现的功能的函数列出来。剩下的就是依次填充各函数的内容。

然后就是运行测试,有错误则改正。

3.代码说明

读取文件

string message =""; // 存储用户命令
            while (message != "exit")
            {
                Console.Write("wc.exe ");
                message = Console.ReadLine();               // 得到输入命令
                string[] arrMessSplit = message.Split(' '); // 分割命令
                int iMessLength = arrMessSplit.Length;
                string[] sParameter = new string[iMessLength - 1];
                // 获取命令参数数组
                for (int i = 0; i < iMessLength - 1; i++)
                {
                    sParameter[i] = arrMessSplit[i];
                }
                // 获取文件名
                string sFilename = arrMessSplit[iMessLength - 1];
            }

在写函数之前还有一段

public string sFilename;    // 文件名
            public string[] sParameter; // 参数数组  
            public int iCharcount;      // 字符数
            public int iWordcount;      // 单词数
            public int iLinecount;      // 总行数

 

控制信息,控制返回的是行数字符数还是单词数

public void Operator(string[] sParameter, string sFilename)
            {
                this.sParameter = sParameter;
                this.sFilename = sFilename;

                foreach (string s in sParameter)
                {
                    //  基本功能
                    if (s == "-c" || s == "-w" || s == "-l")
                    {
                        break;
                    }
                    else
                    {
                        Console.WriteLine("参数 {0} 不存在", s);
                        break;
                    }
                }
            }

 

统计行数,字符数,单词数

private 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++; // 统计行数
                        }
                    }
                    iCharcount = charcount;
                    iWordcount = wordcount + 1;
                    iLinecount = linecount + 1;
                    sr.Close();
                }
                catch (IOException ex)
                {
                    Console.WriteLine(ex.Message);
                    return;
                }
            }

 

转成txt格式输出

private void Display()
            {
                StreamWriter sw = new StreamWriter(@"D:\专业\VS\Test\Test\Output.txt");
                Console.SetOut(sw);
                foreach (string s in sParameter)
                {

                    if (s == "-c")
                    {
                        Console.WriteLine("字 符 数:{0}", iCharcount);
                    }
                    else if (s == "-w")
                    {
                        Console.WriteLine("单 词 数:{0}", iWordcount);
                    }
                    else if (s == "-l")
                    {
                        Console.WriteLine("总 行 数:{0}", iLinecount);
                    }
                }
                sw.Flush();
                sw.Close();
            }

 

4.测试

测试还是可以的。

5.心得体会

太久没写这样一整个的代码,实在是很生疏。以后一定要多多练习写代码的能力了。不过这一次我认真了解、学习了分割命令,算是不小的收获。以后若用起来也会更加的得心应手。

6.参考文献

分割命令  https://www.cnblogs.com/yugen/archive/2010/08/18/1802781.html

 

posted @ 2018-09-24 19:03  林橙多  阅读(474)  评论(0编辑  收藏  举报