WordCount第三次作業
阿新 • • 發佈:2018-10-21
dir 修改 pat subst reat 代碼 mbo https mod
(1)合作者:201631062117,201631062616
(2)gitee地址:https://gitee.com/ThirteenD/WC
(3)本次作業地址:[https://edu.cnblogs.com/campus/xnsy/2018softwaretest2398/homework/2187]
互審代碼情況
經過審查,在原本作業的導出文件,結果顯示以及輸入判定幾個模塊有較多贅余部分,將其優化修改,部分錯誤進行修正,因都對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; }
主函數
static void Main(string[] args) { string file=" "; bool outputisfalse =true; string oprations = " "; string filetype = " "; while (outputisfalse) { try { Console.WriteLine("請輸入文件名:"); //讀取文件名 file = Console.ReadLine(); FileStream fs = new FileStream(file, FileMode.Open); fs.Close(); filetype = Filetype(file); // Output(file); outputisfalse = false; } catch { Console.WriteLine("未檢測到這個文件請重新輸入:"); } } //讀取操作 List<string> lstopration = new List<string>(); bool issave = false; string savename=" "; l: lstopration.Clear(); Console.WriteLine("請選擇操作:"); Console.WriteLine("查詢所有本類型的文件:-s。\r\n字符數:-c。\r\n單詞數:-w。\r\n行數:-l。\r\n保存:-o+文件名。\r\n代碼行/空行/註釋行:-a。\r\n不查詢此文件:-e+文件名。"); oprations = Console.ReadLine(); bool havee = false; string efile = " "; for (int i = 0; i < oprations.Length; i++) { if ( i < oprations.Length - 1&&oprations.Substring(i, 2) == "-o" ) { issave = true; while (oprations.Substring(i + 2) == " ") i++; savename = oprations.Substring(i + 2); if (savename == "") { Console.WriteLine("未輸入保存的文件名"); goto l; } break; } if (oprations.Substring(i, 1) == "-" && i < oprations.Length - 1 && (oprations.Substring(i + 1, 1) == "w" || oprations.Substring(i + 1, 1) == "c" || oprations.Substring(i + 1, 1) == "l" || oprations.Substring(i + 1, 1) == "a" || oprations.Substring(i + 1, 1) == "s" || oprations.Substring(i + 1, 1) == "e")) { string opration = oprations.Substring(i, 2); if(oprations.Substring(i, 2)=="-e") { i += 2; int l = i; for (; i < oprations.Length - 1; i++) { if (oprations.Substring(i, 1) == "-") { havee = true; efile = oprations.Substring(l, i -l); i--; break; } if (i == oprations.Length - 2) { efile = oprations.Substring(l); } } } lstopration.Add(opration); } } List<string> lstname = new List<string>(); if (lstopration.Count > 0 && lstopration[0] == "-s") { lstname = Lstname(filetype); if (havee) { if (efile == " ") { Console.WriteLine("未輸入不查詢的文件名"); goto l; } for (int i1 = 0; i1 < lstname.Count; i1++) { if (lstname[i1] == efile) { lstname.RemoveAt(i1); break; } } } } else lstname.Add(file); for (int i = 0; i < lstopration.Count; i++) { for (int j = 0; j < lstname.Count;j++ ) Output(lstname[j], lstopration[i]); } if (issave) { for (int i = 0; i < lstopration.Count; i++) for (int j = 0; j < lstname.Count; j++) ExportFile(lstname[j], lstopration[i], savename); } oprations = Console.ReadLine(); }
字符數
public static int ChNum(string file)
{
int x = 0;
StreamReader sr = StrReadr(file);
while ((nchar = sr.Read()) != -1)
{
x++;
}
sr.Close();
return x;
}
返回更復雜的數據(代碼行 / 空行 / 註釋行)
public static string All(string file) { StreamReader sr = StrReadr(file); int x=0; int y=0; int z=0; string strline; while ((strline = sr.ReadLine()) != null) { int count=0; bool isnote=false; for (int i = 0; i < strline.Length; i++) { if (strline.Substring(i, 1) != " " && strline.Substring(i, 1) != "{" && strline.Substring(i, 1) != "}") { count++; } if (i < strline.Length - 1 && strline.Substring(i, 2) == "//") { isnote = true; } } if (isnote) z++; else if (count > 0) x++; else y++; } sr.Close(); return x+"/"+y+"/"+z;
單詞數
public static int WordNum(string file)
{
StreamReader sr = StrReadr(file);
nchar = sr.Read();
int x = 0;
//判斷是否為單詞
char[] symbol = { ‘ ‘, ‘,‘, ‘\n‘ };
while ((nchar = sr.Read()) != -1)
{
foreach (char c in symbol)
{
if (nchar == (int)c)
x++;
}
}
sr.Close();
return x;
}
遍歷指定類型文件
public static string Filetype(string file)
{
string filetype = " ";
for (int i = 0; i < file.Length; i++)
{
if (file.Substring(i, 1) == ".")
{
filetype = file.Substring(i);
}
}
return filetype;
}
統計文件單詞總數時,不統計該表中的單詞
public static List<string> Lstname(string type)
{
List<string> lstname = new List<string>();
String[] files = Directory.GetFiles(Environment.CurrentDirectory, "*"+type, SearchOption.AllDirectories);
for (int i = 0; i < files.Length; i++)
{
lstname.Add(Path.GetFileName(files[i]));
}
return lstname;
}
字符行數
public static int LineNum( 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;
}
結果顯示
public static void Output(string file, string input)
{
//int x = 0;
if (input == "-c")
Console.WriteLine(file + ":的字符數為:" + ChNum(file) + "\r\n");
if (input == "-w")
Console.WriteLine(file + ":的單詞數為:" + WordNum(file) + "\r\n");
if (input == "-l")
Console.WriteLine(file + ":的行數為:" + LineNum(file) + "\r\n");
if (input == "-a")
Console.WriteLine(file + "代碼行/空行/註釋行:" + All(file) + "\r\n");
}
導出文件
public static void ExportFile(string file,string Opt,string savename)
{
if (!File.Exists(savename))
{
FileStream fsl = new FileStream(savename, FileMode.Create, FileAccess.Write);
fsl.Close();
}
FileStream fs = new FileStream(savename, FileMode.Append, FileAccess.Write);
StreamWriter sr = new StreamWriter(fs);
switch (Opt)
{
case "-c":
sr.WriteLine(file + ":的字符數為:" + ChNum(file));
break;
case "-w":
sr.WriteLine(file + ":的d單詞數為:" + WordNum(file));
break;
case "-l":
sr.WriteLine(file + ":的行數為:" + LineNum(file));
break;
case "-a":
sr.WriteLine(file + "代碼行/空行/註釋行:" + All(file));
break;
default:
break;
}
sr.Close();
fs.Close();
}
靜態代碼審查
使用了Visual Studio 2010所自帶的靜態代碼審查工具
效果:
總結
體會到了結對編程的優勢,以及對於VS的各種工具有了更詳盡的了解
WordCount第三次作業