源代码统计程序

来源:互联网 发布: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
原创粉丝点击