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

wordCount

时间:2018-10-20 17:26:01      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:alt   counter   efault   cti   task   encoder   def   line   symbol   

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


namespace WordCount
{
    class Program
    {
        static void Main(string[] args)
        {
            Type type = new Type();
            Count count = new Count();

            int ctr;
            int Operand;
            string theword = "";
            int ope1, ope2, ope3;
            ope1 = ope2 = ope3 = 0;

            theword += string.Format(args[1]);

            if(args.Length>2)
            {
                Console.WriteLine("读取出错");
            }
            else
            {
                type.ope = args[0];

                for(ctr = 0; ctr < args.Length; ctr++)
                {
                    theword += string.Format("参数 {0} 为:{1}\n", ctr + 1, args[ctr]);
                }             
            }

            Operand = type.ChooseType();

            if(Operand==1)
            {
                ope1 = count.countchar(args[1]);
                theword += string.Format("总字符数为:{0}", ope1);
            }
            else if(Operand==2)
            {
                ope2 = count.countword(args[1]);
                theword += string.Format("总单词数为:{0}", ope2);
            }
            else if(Operand==3)
            {
                ope3 = count.countLine(args[1]);
                theword += string.Format("总行数为:{0}", ope3);
            }
            else
            {
                Console.WriteLine("读取出错");
            }

            CreatFile creatFile = new CreatFile();
            creatFile.Write(theword);
        }
    }     
}

type类

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

namespace WordCount
{
    class Type
    {
        public string ope;
        public int ChooseType()
        {
            if (ope == "-c")
            {
                return 1;
            }
            else if (ope == "-w")
            {
                return 2;
            }
            else if (ope == "-l")
            {
                return 3;
            }
            else
                return 0;
        }
    }
}

count类

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

namespace WordCount
{
    class Count
    {
        Open open = new Open();//将open类实例化
        public int countchar(string filename)
        {
            open.LoadFile(filename);
            int charcounter = 0;
            int nChar;

            while ((nChar = open.sr.Read()) != -1)
            {
                charcounter++;     // 统计字符数
            }
            Console.WriteLine("字符数为:" + charcounter);//显示字符总数

            open.sr.Close();
            open.fs.Close();
            return charcounter;
        }

        public int countword(string filename)
        {
            open.LoadFile(filename);
            int nChar;
            int wordcounter = 0;
            char[] symbol = {  , \t, ,, ., ?, !, :, ;, \‘, =,
                              \", \n, {, }, (, ), +, -, * };
            //间隔符
            while ((nChar = open.sr.Read()) != -1)
            {
                foreach (char c in symbol)
                {
                    if (nChar == (int)c)
                    {
                        wordcounter++; // 统计单词数
                    }
                }
            }
            Console.WriteLine("单词数为:" + wordcounter);//显示单词总数

            open.sr.Close();
            open.fs.Close();
            return wordcounter;
        }

        public int countLine(string filename)
        {
            open.LoadFile(filename);
            string line;
            int Linecounter = 0;//记录行数

            //当当前行不为空,即当前行存在,即 +1 
            while ((line = open.sr.ReadLine()) != null)
            {
                Linecounter++;
            }
            Console.WriteLine("总行数为:" + Linecounter);//显示行数

            //关闭文件
            open.sr.Close();
            open.fs.Close();

            return Linecounter;
        }

    }
}

create类

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

namespace WordCount
{
    class CreatFile
    {
        public void Write(string theword)
        {
            FileStream fs = new FileStream("outputFile.txt", FileMode.Create);
            //获得字符串
            byte[] data = System.Text.Encoding.Default.GetBytes(theword);
            //开始写入
            fs.Write(data, 0, data.Length);
            //清空缓冲区、关闭流
            fs.Flush();
            fs.Close();
        }
    }
}

open类

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

namespace WordCount
{
    class Open
    {
        public StreamReader sr;
        public FileStream fs;

        public void LoadFile(string fileName)
        {
            Encoding encoder = Encoding.GetEncoding("GB2312");
            fs = new FileStream(fileName, FileMode.Open);
            sr = new StreamReader(fs, encoder);

        }
    }
}



输入错误的指令就会报错
技术分享图片

技术分享图片

 

 

 

wordCount

标签:alt   counter   efault   cti   task   encoder   def   line   symbol   

原文地址:https://www.cnblogs.com/blogchw/p/9822274.html

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