(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的各种工具有了更详尽的了解
原文:https://www.cnblogs.com/thirteend/p/9826537.html