C#源代码计算器
用C#可以算出.cs或者.java文件里的总行数,空白行数,注释行数,代码行数program.csusing System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace LineCount{ c
·
要求:用C#编写出一个可以算出.cs或者.java文件里的总行数,空白行数,注释行数,代码行数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication2
{
class Program
{
static int commentLineCount = 0, allLineCount = 0, blankLineCount = 0, codeLineCount = 0;
static void Main(string[] args)//主函数
{
Console.WriteLine("请输入文件路径:");
String path = Console.ReadLine();
count(path);
Console.WriteLine("总行数为{0}\n空白行数为{1}\n注释行数为{2}\n代码行数为{3}", allLineCount = blankLineCount + commentLineCount + codeLineCount, blankLineCount, commentLineCount, codeLineCount);
Console.WriteLine("请输入任意键继续");
Console.ReadKey();
}
static void count(string file)
{
try
{
//读取文件
FileStream aFile = new FileStream(file, FileMode.Open);
StreamReader sr = new StreamReader(aFile);
string line = sr.ReadLine();
//开始判断
while (line != null)
{
if (line.Trim() != "" && !(line.Trim().StartsWith(@"//")))
{
codeLineCount++;
}
else if (line.Trim().StartsWith(@"//") || line.Trim().StartsWith(@"///"))
{
commentLineCount++;
}
else
{
blankLineCount++;
}
line = sr.ReadLine();
}
sr.Close();
}
catch (IOException e)
{
Console.WriteLine("An IO exception has been thrown!");
Console.WriteLine(e.ToString());
return;
}
}
}
}
异常处理:
更多推荐
已为社区贡献1条内容
所有评论(0)