WordCount

一、Gitee项目地址

https://gitee.com/YSS_MYGit/WordCount

二、基本思路

  我看到项目后,首先想到的是使用C#语言编写,因为我对C#语言比较熟悉些。此项目有两个类,一个是Program类,该类是程序的入口,主要用于传参,另一个是WC类,该类中有四个方法,分别用于输入检测、统计信息、打印信息和显示信息。

三、代码设计

核心代码:

 public class WC
    {
        private string sFilename;    // 文件名
        private string[] sParameter; // 参数数组  
        private int iCharcount;      // 字符数
        private int iWordcount;      // 单词数
        private 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} is not found", 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++; // 统计行数
                    }
                }
                iCharcount = charcount;
                iWordcount = wordcount + 1;
                iLinecount = linecount + 1;
                sr.Close();
            }
            catch (IOException ex)
            {
                Console.WriteLine(ex.Message);
                return;
            }
        }
        // 打印信息
        public void SaveResult()
        {
            StreamWriter f = new StreamWriter(@"result.txt", false);
            foreach (string a in sParameter)
            {
                if (a == "-c")
                    f.WriteLine("{0}字符数:{1}", sFilename, iCharcount);
                if (a == "-w")
                    f.WriteLine("{0}单词数:{1}", sFilename, iWordcount);
                if (a == "-l")
                    f.WriteLine("{0}行数:{1}", sFilename, iLinecount);
            }
            f.Close();
            Console.ReadKey();
        }
        public void Display()
        {
            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);
                }
            }
            Console.WriteLine();
        }
    }

 

四、测试结果

五、总结

由于时间问题,所以这次作业写得简陋。但是通过本次作业我还是学到了很多。

 

posted @ 2018-09-30 23:00  Say_Hi  阅读(183)  评论(1编辑  收藏  举报