wordCount
Posted blogchw
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了wordCount相关的知识,希望对你有一定的参考价值。
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} ", 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 = { ‘ ‘, ‘ ‘, ‘,‘, ‘.‘, ‘?‘, ‘!‘, ‘:‘, ‘;‘, ‘‘‘, ‘=‘, ‘"‘, ‘ ‘, ‘{‘, ‘}‘, ‘(‘, ‘)‘, ‘+‘, ‘-‘, ‘*‘ }; //间隔符 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); } } }
输入错误的指令就会报错