1. 程式人生 > >第二次作業WordCount

第二次作業WordCount

writer check sta hnu null 打開 adr readline cas

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=

第二次作業WordCount