第二次作业WordCount

gitee地址:https://gitee.com/ThirteenD

作业思路

最初拿到作业想用最近学习的WinForm完成,但因对WinForm的不熟悉和开始写作业时间太晚,导致到截止时间时仍有大量问题为解决,因此第二次作业时选择更加熟悉的C#控制台程序完成。

程序设计实现过程

程序只有一个类,八个函数,包括打开文件,三个基本功能,检查输入,输出,导出文件和主函数。

代码说明

打开文件

public static StreamReader StrReadr(string file)
{
Filename = file;
FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);
return sr;
}

基本功能

统计字符数


public static int ChNum(int charcount, string file)
{
StreamReader sr = StrReadr(file);
while ((nchar = sr.Read()) != -1)
{
charcount++;
}
sr.Close();
return charcount;
}

统计单词数


public static int WordNum(int wordcount, string file)
{
StreamReader sr = StrReadr(file);
nchar = sr.Read();

//判断是否为单词
char[] symbol = { ' ', ',', '\n' };
while ((nchar = sr.Read()) != -1)
{
foreach (char c in symbol)
{
if (nchar == (int)c)
wordcount++;
}
}
sr.Close();
return wordcount;
}

统计行数


public static int LineNum(int linecount, string file)
{
int i = 0;
StreamReader sr = StrReadr(file);
sr.BaseStream.Seek(0, SeekOrigin.Begin);
string strline = sr.ReadLine();
while (strline != null)
{
i++;
strline = sr.ReadLine();
}
sr.Close();
return i;
}

主函数


static void Main(string[] args)
{
string file;
string opration = " ";
Console.WriteLine("请输入文件名:");

//读取文件名
file = Console.ReadLine();

//读取操作
Console.WriteLine("请选择操作:");
Console.Write("字符数:-c。单词数:-w。行数:-l。");
opration = Console.ReadLine();
input = InputcCheck(opration);
Output(file);

//是否保存
Console.WriteLine("是否需要导出文件Y/N:");
string opt = Console.ReadLine();
if (opt == "Y")
{
ExportFile(input);
Console.WriteLine("文件已导出");
}
else if (opt == "N")
while (opt != "Y" && opt != "N")
{
Console.WriteLine("输入错误");

//读取Y,N
opt = Console.ReadLine();
if (opt == "Y")
{
ExportFile(input);
break;
}
else
continue;
}
}

导出文件


public static void ExportFile(string Opt)
{
int i = 0;
Opt = input;
string filePath = "result.txt";
if (!File.Exists(filePath))
{
FileStream fsl = new FileStream(filePath, FileMode.Create,FileAccess.Write);
StreamWriter sw = new StreamWriter(fsl);
switch (Opt)
{
case "-c":
sw.Write(Filename + ":的字符数为:" + ChNum(i, Filename));
break;
case "-w":
sw.Write(Filename + ":的字符数为:" + WordNum(i, Filename));
break;
case "-l":
sw.Write(Filename + ":的字符数为:" + LineNum(i, Filename));
break;
default:
break;
}

sw.Close();
fsl.Close();
}
else
{
FileStream fs = new FileStream(filePath, FileMode.Append, FileAccess.Write);
StreamWriter sr = new StreamWriter(fs);
switch (Opt)
{
case "-c":
sr.WriteLine(Filename + ":的字符数为:" + ChNum(i, Filename));
break;
case "-w":
sr.WriteLine(Filename + ":的字符数为:" + WordNum(i, Filename));
break;
case "-l":
sr.WriteLine(Filename + ":的字符数为:" + LineNum(i, Filename));
break;
default:
break;
}
sr.Close();
fs.Close();

}

最终效果


总结

本次作业完成大量依靠网络查询以及询问同学,暴露自己很多不足,特别是对于窗体应用程序的编写方面,但同时也加强了自学能力,加深了对于编程的兴趣,同时在后续的作业中我会尽量将WinForme本版本做出来

参考文献

https://jingyan.baidu.com/album/647f0115ef07b57f2048a810.html?picindex=2

http://jingyan.baidu.com/album/c1465413e4fc8b0bfdfc4c6f.html?stepindex=0&wap_detail_test=T1&st=5&os=1&bd_page_type=1&net_type=&ssid=&from=

https://blog.csdn.net/qq_22889875/article/details/77924840

https://zhidao.baidu.com/question/1303793488153179699

posted @ 2018-09-30 18:16  ThirteenD  阅读(107)  评论(0编辑  收藏  举报