码迷,mamicode.com
首页 > 其他好文 > 详细

WordCount

时间:2018-10-01 00:08:09      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:not found   输入   save   parameter   git   stream   入口   message   参数   

一、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();
        }
    }

 

四、测试结果

技术分享图片

五、总结

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

 

WordCount

标签:not found   输入   save   parameter   git   stream   入口   message   参数   

原文地址:https://www.cnblogs.com/yangss-myblog/p/9733669.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!