源代码统计程序
来源:互联网 发布:ubuntu如何打开软件 编辑:程序博客网 时间:2024/06/05 05:53
</pre><pre name="code" class="csharp">using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.IO;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace 源代码计算{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } public String FileName { get; set; } private void btnOpen_Click(object sender, EventArgs e) { if (openFileDialog1.ShowDialog() == DialogResult.OK) { FileName = openFileDialog1.FileName; CoutLine.countLine(FileName); textBox1.Text = CoutLine.allLine.ToString(); textBox2.Text = CoutLine.codeLine.ToString(); textBox3.Text = CoutLine.commentaryLine.ToString(); } } }}
CoutLine 类
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.IO;namespace 源代码计算{ class CoutLine { public static int codeLine // 代码行数 { get; set; } public static int allLine // 总行数 { get; set; } public static int commentaryLine // 注释函数 { get; set; } public static bool isMultiCommentary; // 用于判断是否为多行注释 /// <summary> /// 计算C#源代码的文件代码函数 /// </summary> /// <param name="filePath">传入的参数:文件的路径</param> /// <returns></returns> public static void countLine(string filePath) { FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read); // 创建文件流对象。打开文件 StreamReader reader = new StreamReader(file, System.Text.Encoding.ASCII); // 读取文件内容 string str = reader.ReadLine(); // 读取一行 while (str != null) // 当文件读取结果为空时,停止计数 { str = str.Trim(); // 去掉行首,行尾的空格 if (isMultiCommentary != true) // 判断是否正在进行多行注释 { countSimpleLine(str); // 计算文档里面的单行注释 即 //类型的注释 } countMultiLine(str); // 计算代码中多行注释的函数 即 /* */ 注释 allLine++; str = reader.ReadLine(); } } /// <summary> /// 计数当行注释 /// </summary> /// <param name="str"></param> /// <returns></returns> public static bool countSimpleLine(string str) { // 判断是否在代码行中出现 “//” 的情况; 代码行+1 // 例如 if (str.StartsWith("//")) 该语句含有//但它不是注释行 if (str.Contains(@"""//""")) { codeLine++; return false; } else { // 判断是否以 // 开头 ; 若是, 则为注释行 注释行+1 // 例如: // 判断是否在代码行中出现 if (str.StartsWith("//")) { commentaryLine++; return true; } // 若//在语句中不是,位于最前;则该行既有注释又有代码,两者都+1 // 例如: string str = reader.ReadLine(); // 读取一行 if (str.IndexOf("//") > 0) { codeLine++; commentaryLine++; return false; } } return false; } /// <summary> /// 计算代码中多行注释的函数 即 /* */ 注释 /// </summary> /// <param name="str"></param> public static void countMultiLine(string tmp) { string str = tmp; int n = str.IndexOf("/*"); int m = str.IndexOf("*/"); if (n < 0 && m < 0 && !str.Contains("//")) { codeLine++;// 如果 语句中既没有/*也没有*/, 没有//, 则不是注释行 return; } if (n > 0 && m > 0) // 语句中既有/*也有*/ { if (m > n) // 注释大小为一行 { commentaryLine++; return; // 是注释行 } } if (isMultiCommentary) { isStopMultiCount(str);// 终止多行注释计数 return; } if (m < 0 && n > 0) // 若只有/* 没有*/ ,则开启多行注释计数 { isMultiCommentary = true; commentaryLine++; return; } return; } public static void isStopMultiCount(string tmp) { string str = tmp; commentaryLine++; int n = str.IndexOf("/*"); int m = str.IndexOf("*/"); if (m >= 0 && n < 0) { // 若只有*/ , 则结束多行注释计数。 isMultiCommentary = false; return; } } }}
0 0
- C#源代码统计程序
- C#源代码统计程序
- C#源代码统计程序
- C#源代码统计程序
- 源代码统计程序
- 页面统计程序源代码
- 用Shell扩展实现源代码统计程序
- (转)用Shell扩展实现源代码统计程序
- 某公司面试题:使用多线程,统计项目程序源代码行数
- C语言代码行数分类统计小程序(源代码)
- 统计源代码行数
- lucene 3.02源代码统计
- java源代码统计工具
- xcode统计源代码行数
- 统计源代码行数
- xcode统计源代码行数
- xcode统计源代码行数
- xcode统计源代码行数
- Android一些属性常识
- android 百度地图3.0
- 求二叉树的深度和宽度
- Git使用心得
- POJ 2955 - Brackets(区间DP)
- 源代码统计程序
- Search for a Range 寻找一个区间 二分查找左边展开
- Oracle Database(1)-归档模式设置
- Number of 1 Bits
- python操作符的优先级 一图见分晓
- MySQL中找出数据库中以同一字符串开头的表名的所有表
- Linux C 异常处理:绝对异常终止 exit()/abort()、assert()
- UITableViewCell水平滑动时,有时候不出来delete按钮的原因
- C++指针详解