本文为C#课程设计第四周作业,作业布置在丁老师空间
本次作业三个目标:
目标1:给定一个源代码文件(.cs, .java),输出该文件的总行数、空行数、注释行数、代码行数。
目标2:从命令行输入一个参数(指定目录或文件),输出该目录下指定类型文件(.cs, .java)的个数、每个文件的总行数、空行数、注释行数、代码行数。
目标3:从命令行输入两个参数(第一个参数:指定目录或文件;第二个参数:指定类型文件),输出该目录下指定类型文件的个数、每个文件的总行数、空行数、注释行数、代码行数。
实际测试三个目标都可以实现
以下为代码和部分运行截图
代码:
using System;
using System.IO;
//using System.IO.File
namespace aa
{
class MainClass
{
static int[] lines = new int[5];
static int count = 0;
public static void BlankLine(string filename)
{
//File f=new File(filename);
StreamReader ss = File.OpenText(filename);
string j = "";
while ((j = ss.ReadLine()) != null)
{
//count1++;
if (j.Trim().Length == 0)
count++;
}
}
//return count;
public static void LinesOfCode(string filename)
{
StreamReader sr = File.OpenText(filename);
string s = sr.ReadToEnd();
sr.Close();
bool isLine = false; //一行中拥有有效字符时为true,该行可记入代码行数
//bool isCommitLf = false; //注释/*...*/中出现至少一个折行时为true
//int[] lines =new int[5]; //代码行数统计
for (int i = 0; i < s.Length; i++)
{
//无效字符
if (s[i] == ' ' || s[i] == '\r' || s[i] == '\t')
{
continue;
}
//搜索到换行,若该行有有效字符
if (s[i] == '\n')
{
if (isLine)
{
lines[3]++;
}
if (!isLine)
{
lines[4]++;
}
lines[0]++;
isLine = false;
//lines[3]++;
continue;
}
//字符串,占多少行按多少行算
if (s[i] == '\"')
{
//isLine = true;
while (true)
{
i++;
//如果文件遍历完毕则强行中止
if (i >= s.Length)
{
break;
}
//再次遇到字符'"'且前方没有或有偶数个'//'时,中止循环并退出
if (s[i] == '\"')
{
int sign = 0, counter = 0;
while (true)
{
sign++;
if (i - sign < 0)
{
break;
}
else if (s[i - sign] == '\\')
{
counter++;
}
else
{
break;
}
}
if (counter % 2 == 0)
{
break;
}
}
//字符串中的换行,直接算作一行代码
if (s[i] == '\n')
{
lines[3]++;
lines[0]++;
isLine = true;
}
}
isLine = true;
continue;
}
//遇到形如 /*...*/ 的注释
if (s[i] == '/' && i < s.Length - 1)
{
if (s[i + 1] == '*')
{
i++;
//lines [0]++;
while (true)
{
i++;
//如果文件遍历完毕则强行中止
if (i >= s.Length)
{
break;
}
if (s[i] == '\n')
{
//lines [3]++;
lines[0]++;
//isLine = false;
lines[2]++;
//isCommitLf = true;
}
if (s[i] == '*' && i < s.Length - 1)
{
if (s[i + 1] == '/')
{
i++;
lines[2]++;
break;
}
}
}
//isLine = true;
//isCommitLf = false;
continue;
}
}
//遇到形如 // 的注释
if (s[i] == '/' && i < s.Length - 1 && s[i + 1] == '/')
{
lines[0]++;
if (isLine)
{
lines[3]++;
//isLine = false;
}
/*
if (isLine == true)
{
//line++;
lines[0]++;
isLine = false;
}*/
while (true)
{
i++;
//lines[0]++;
if (i >= s.Length || s[i] == '\n')
{
//lines[0]++;
lines[1]++;
break;
}
}
continue;
}
//该行有了有效字符,算作一行
isLine = true;
}
//最后一行可能没有字符'\n'结尾
if (isLine)
{
lines[3]++;
//line++;
lines[0]++;
}
//return lines[3];
//return count;
}
/*
public static int LinesOfFolder(string foldername)
{
//行数统计
int lines = 0;
//文件夹信息
DirectoryInfo dif = new DirectoryInfo(foldername);
//遍历文件夹中的各子文件夹
foreach (DirectoryInfo di in dif.GetDirectories())
{
lines += LinesOfFolder(di.FullName);
}
//统计本文件夹中C语言文件代码
foreach (System.IO.FileInfo f in dif.GetFiles())
{
if (f.Extension == ".cs" || f.Extension == ".cpp" ||
f.Extension == ".c" || f.Extension == ".h")
{
lines += LinesOfCode(f.FullName);
}
}
return lines;
}*/
public static void Main(string[] args)
{
Console.Write("请输入要统计的文件路径:");
try
{
string order = Console.ReadLine();
LinesOfCode(order.Trim());
BlankLine(order.Trim());
Console.WriteLine("总行数 " + lines[0] + " 行");
Console.WriteLine("注释行数 " + (lines[1] + lines[2]) + " 行");
Console.WriteLine("空行数 " + count + " 行");
Console.WriteLine("有效行数:" + lines[3] + " 行");
//Console.WriteLine(count1);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}
}
}
截图: