日期:2014-05-20  浏览次数:21016 次

有专家吗?给解决解决这个问题啊!!
设计一个统计代码行数的工具
这个统计代码工具主要能够同时统计不同的语言。
要求:
每一种语言的代码行数、注释行数、空白行过滤,还有可能不是代码的不要统计进来。
每一种语言有不同的特色,要考虑能够适用各种语言。

还要考虑各种注释情况。从OOP角度考虑问题

实际例子:
---源代码目录
  C#项目目录 
  VB项目目录
  .
  .
  .
  .

我只要选定源代码目录那么代码行数工具应该统计出如下表的数据(不包括测试目录和临时目录):

  指标
语言 总行数 代码行数 注释行数 空白行 设计行数(设计器生成)
C#
VB
……


那位专家能给画个类图啊,给小弟好好讲讲啊。。。先谢谢各位专家了


------解决方案--------------------
这里有个代码行统计器
http://d.download.csdn.net/down/162620/sriong

核心代码
C# code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace RecorderCount
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public static string strFilePath="";
        public  Int32 sumCount = 0;
        public static StreamReader mysr;
        public static string strFileType="";
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Dispose();
            Application.Exit();
        }

        private void btnOpenFile_Click(object sender, EventArgs e)
        {
            fbdFile.ShowNewFolderButton = true;
            fbdFile.Description = "请选择源代码所在的文件位置";
            fbdFile.ShowDialog();
            if (fbdFile.SelectedPath.Length > 1)
            {
                txtFilePath.Text = fbdFile.SelectedPath.ToString();
            }
            else
            {
                MessageBox.Show("请选择源代码所在的文件夹!", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
        }

        private void btnCount_Click(object sender, EventArgs e)
        {
            if (richTextBox1.TextLength > 0)
            {
                richTextBox1.Clear();
            }
            if (txtFileType.Text.Length == 0)
            {
                MessageBox.Show("请输入要源代码文件的类型","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Information );
                return;
            }
            sumCount = 0;
            DirectoryInfo di = new DirectoryInfo(@txtFilePath.Text.Trim());
            if (cbxSubPath.Checked == true)
            {

              
                if (di.Exists)
                {
                    int count=Info(di);
                    richTextBox1.Text = richTextBox1.Text + "总计:                         " + count.ToString() + "\n\r";
                    richTextBox1.Text = richTextBox1.Text + "OK\n\r";
                }
           

            }
            else
            {
                if (di.Exists)
                {
                    int count=currentInfo(di);
                    richTextBox1.Text = richTextBox1.Text + "总计:                         " + count.ToString() + "\n\r";
                    richTextBox1.Text = richTextBox1.Text + "OK\n\r";
                }

            }
        }
        private int  Info(DirectoryInfo di)
        {
            if (di.Exists)
            {
               DirectoryInfo[] di_list = di.GetDirectories();
               FileInfo[] fiA=di.GetFiles(txtFileType.Text.Trim().ToString());//获得了所有起始目录下的文件
               if (fiA.Length > 0)
               {
                   int totalCount = 0;
                   richTextBox1.Text=richTextBox1.Text+"统计:"+ di.FullName.ToString() + ":\n\r";
                   
                   for (int i = 0; i < fiA.Length; i++)
                   {

                       int count = 0;
                       StreamReader sr = new StreamReader(di.FullName.ToString() + "\\" + fiA[i].Name.ToString());
                       if (File.Exists(di.FullName.ToString() + "\\" + fiA[i].Name.ToString()))
                       {
                           string line = "";
                           while ((line = sr.ReadLine()) != null)
                           {
                               count++;

                           }

                           richTextBox1.Text = richTextBox1.Text + fiA[i].Name.ToString() + "  " + count.ToString() + "\n\r";
                           totalCount += count;
                       

                       }
                   }
                   sumCount += totalCount;
                   richTextBox1.Text=richTextBox1.Text+"小计:                         " + totalCount.ToString()+"\n\r";
                   richTextBox1.Text = richTextBox1.Text + "--------------------------------------------\n\r";
                   
               }
              
               
                foreach (DirectoryInfo di1 in di_list)
                {
                   DirectoryInfo[] di1_list = di1.GetDirectories();
                    Info(di1);
                } 
               
                } 
            return sumCount;
            }

        private void Form1_Load(object sender, EventArgs e)
        {
            txtFileType.Text = "*.cs";
            cbxSubPath.Checked = true;
        }
        private int currentInfo(DirectoryInfo di)
        {

            if (di.Exists)
            {
                DirectoryInfo[] di_list = di.GetDirectories();
                FileInfo[] fiA = di.GetFiles(txtFileType.Text.Trim().ToString());//获得了所有起始目录下的文件
                if (fiA.Length > 0)
                {
                    int totalCount=0;
                    richTextBox1.Text = richTextBox1.Text + di.FullName.ToString() + ":\n\r";
                    for (int i = 0; i < fiA.Length; i++)
                    {

                        int count = 0;
                        StreamReader sr = new StreamReader(di.FullName.ToString() + "\\" + fiA[i].Name.ToString());
                        if (File.Exists(di.FullName.ToString() + "\\" + fiA[i].Name.ToString()))
                        {
                            string line = "";
                            while ((line = sr.ReadLine()) != null)
                            {
                                count++;

                            }

                            richTextBox1.Text = richTextBox1.Text  + fiA[i].Name.ToString() + "  " + count.ToString() + "\n\r";
                            totalCount += count;
                            
                        }
                    }
                    sumCount += totalCount;
                    richTextBox1.Text = richTextBox1.Text + "小计:                         " + totalCount.ToString() + "\n\r";
                    richTextBox1.Text = richTextBox1.Text + "--------------------------------------------\n\r";
                   
                } 
            }
            return sumCount;
        }
      }
    }