using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 源码计算器
{
class Program
{
static bool isAnnotationed = false; // 判断是否启动多行注释的行数计数
static int lines = 0; // 代码行
static int useless = 0; // 总行数
static StreamReader content = null;
static void Main(string[] args)
{
Console.WriteLine("请输入要检查的源代码文件:");
String path = Console.ReadLine();
try
{
FileStream openFile = new FileStream(@"path", FileMode.Open, FileAccess.Read); // 打开文件并设置读取权限
content = new StreamReader(openFile, System.Text.Encoding.ASCII); // 一种特定的方法从字节流中读取字符
}
catch (System.Exception ex)
{
Console.WriteLine("未知文件");
Console.ReadLine();
{
if (isEmptyLine1(str) || isAnnotationed) // 判断是否为空行,或者处于多行注释计数中
{
// isAnnotationMulti(str); // 判断是否终止多行注释计数
isStopMultiCount(str); // 终止多行注释计数
str = content.ReadLine();
useless++;
continue;
{
isStopMultiCount(str);// 终止多行注释计数
str = content.ReadLine();
useless++;
continue;
{
str = content.ReadLine();
useless++;
continue;
str = content.ReadLine();
useless++;
continue;
{
Console.WriteLine("注释没有闭合");
isAnnotationed = false;
Console.ReadLine();
Console.ReadLine();
/// 多行注释判断 ,有四种情况 , 多行注释计数启动器 isAnnotationed(bool值)判断是否为多行注释
/// </summary>
/// <param name="tmp"></param>
{
string str = tmp;
int n = str.IndexOf("/*");
int m = str.IndexOf("*/");
if (n < 0 && m < 0)
{ // 如果 语句中既没有/*也没有*/, 则不是注释行
return false;
}
if (n > 0 && m > 0) // 语句中既有/*也有*/
{
{
// Console.WriteLine("可能存在代码书写不规范情况"); // 若*/在/*前面,也是书写不规范的表现
return false;
{
return true; // 是注释行
}
}
isStopMultiCount(str);// 终止多行注释计数
if (m < 0 || n > 0) // 若只有/* 没有*/ ,则开启多行注释计数
{
isAnnotationed = true;
return true;
}
return true;
{
string str = tmp;
int n = str.IndexOf("//");
if (n < 0)
{
return false;
}
string subStr = str.Substring(0, n);
if (isAllSpace(subStr))
{
return true; // 是注释行
}
return false; // 不是注释行
{
string str = tmp;
if (str == "")
{
return true;
}
if (isAllSpace(str))
{
return true;
}
return false;
} // 判断是否为空行
public static bool isAllSpace(string tmp)
{
string str = tmp;
for (int i = 0; i < str.Length; i++)
{
if ((int)str[i] != 32)
{
return false;
}
}
return true;
{
string str = tmp;
int n = str.IndexOf("/*");
int m = str.IndexOf("*/");
if (m >= 0 && n < 0)
{ // 若只有*/并且之前存在/* , 则结束多行注释计数。
if (isAnnotationed)
{
isAnnotationed = false;
return true;
}
else
{
Console.WriteLine("可能存在代码书写不规范情况"); // 若*/在/*前面,也是书写不规范的表现
return false;
}
}
return false;
} // 终止多行注释计数
}
}
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 源码计算器
{
class Program
{
static bool isAnnotationed = false; // 判断是否启动多行注释的行数计数
static int lines = 0; // 代码行
static int useless = 0; // 总行数
static StreamReader content = null;
static void Main(string[] args)
{
Console.WriteLine("请输入要检查的源代码文件:");
String path = Console.ReadLine();
try
{
FileStream openFile = new FileStream(@"path", FileMode.Open, FileAccess.Read); // 打开文件并设置读取权限
content = new StreamReader(openFile, System.Text.Encoding.ASCII); // 一种特定的方法从字节流中读取字符
}
catch (System.Exception ex)
{
Console.WriteLine("未知文件");
Console.ReadLine();
}
useless++;
{
if (isEmptyLine1(str) || isAnnotationed) // 判断是否为空行,或者处于多行注释计数中
{
// isAnnotationMulti(str); // 判断是否终止多行注释计数
isStopMultiCount(str); // 终止多行注释计数
str = content.ReadLine();
useless++;
continue;
}
{
isStopMultiCount(str);// 终止多行注释计数
str = content.ReadLine();
useless++;
continue;
}
{
str = content.ReadLine();
useless++;
continue;
}
str = content.ReadLine();
useless++;
continue;
}
{
Console.WriteLine("注释没有闭合");
isAnnotationed = false;
Console.ReadLine();
}
Console.ReadLine();
}
/// 多行注释判断 ,有四种情况 , 多行注释计数启动器 isAnnotationed(bool值)判断是否为多行注释
/// </summary>
/// <param name="tmp"></param>
/// <returns></returns>
{
string str = tmp;
int n = str.IndexOf("/*");
int m = str.IndexOf("*/");
if (n < 0 && m < 0)
{ // 如果 语句中既没有/*也没有*/, 则不是注释行
return false;
}
if (n > 0 && m > 0) // 语句中既有/*也有*/
{
string subStr = str.Substring(0, n);
{
// Console.WriteLine("可能存在代码书写不规范情况"); // 若*/在/*前面,也是书写不规范的表现
return false;
}
{
return true; // 是注释行
}
}
isStopMultiCount(str);// 终止多行注释计数
if (m < 0 || n > 0) // 若只有/* 没有*/ ,则开启多行注释计数
{
isAnnotationed = true;
return true;
}
return true;
}
// 判断是否为多行注释
public static bool isAnnotationSimple(string tmp){
string str = tmp;
int n = str.IndexOf("//");
if (n < 0)
{
return false;
}
string subStr = str.Substring(0, n);
if (isAllSpace(subStr))
{
return true; // 是注释行
}
return false; // 不是注释行
}
// 判断是否为//单行注释行
public static bool isEmptyLine1(string tmp){
string str = tmp;
if (str == "")
{
return true;
}
if (isAllSpace(str))
{
return true;
}
return false;
} // 判断是否为空行
public static bool isAllSpace(string tmp)
{
string str = tmp;
for (int i = 0; i < str.Length; i++)
{
if ((int)str[i] != 32)
{
return false;
}
}
return true;
}
// 判断行的内容是否为空格
public static bool isStopMultiCount(string tmp){
string str = tmp;
int n = str.IndexOf("/*");
int m = str.IndexOf("*/");
if (m >= 0 && n < 0)
{ // 若只有*/并且之前存在/* , 则结束多行注释计数。
if (isAnnotationed)
{
isAnnotationed = false;
return true;
}
else
{
Console.WriteLine("可能存在代码书写不规范情况"); // 若*/在/*前面,也是书写不规范的表现
return false;
}
}
return false;
} // 终止多行注释计数
}
}