C#第一次作业,C#或者java代码总行数、代码行数、代码空格行数、注释行数的统计

BlankCount.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WordCountForms
{
class BlankCount : Count
{
protected override string FilterContent(string fileContent)
{
return “”;
//txtPath.Text = “请输入正确的文件名”;
}

    public override int GetCodeLineCount()
    {
        return 0;
    }

    public override int GetAllLineCount()
    {
        return 0;
    }

    public override int GetBlankLineCount()
    {
        return 0;
    }

    public override int GetCommentLineCount()
    {
        return 0;
    }
}

}

Count.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WordCountForms
{
abstract class Count
{
///
/// 替换字符串
///
protected const String REPLACE_STRING = “WorldCount”;

    /// 
    /// 多行注释替换字符
    /// 
    protected const String COMMMENT_REPLACE_STRING = "//WorldCount";

    /// 
    /// 统计内容
    /// 
    protected String fileContent;

    /// 
    /// 代码行数
    /// 
    protected int codeLineCount;

    /// 
    /// 总行数
    /// 
    protected int allLineCount;

    /// 
    /// 空白行数
    /// 
    protected int blankLineCount;

    /// 
    /// 注释行数
    /// 
    protected int commentLineCount;


    /// 
    /// 设置需进行统计内容
    /// 
    /// 
    public void SetFileContent(String fileContent)
    {
        this.fileContent = FilterContent(fileContent);
    }

    /// 
    /// 过滤清除无效字符
    /// 
    /// 
    /// 
    protected abstract String FilterContent(String fileContent);

    /// 
    /// 替换内容,解除干扰
    /// 
    /// 
    /// 
    /// 
    /// 
    protected String Replace(String fileContent, String arrString, String replaceString)
    {
        String[] arr = arrString.Split('\n');
        foreach (string str in arr)
        {
            fileContent = fileContent.Replace(str, replaceString);
        }
        return fileContent;
    }

    /// 
    /// 代码行数
    /// 
    public abstract int GetCodeLineCount();

    /// 
    /// 总行数
    /// 
    public abstract int GetAllLineCount();

    /// 
    /// 空白行数
    /// 
    public abstract int GetBlankLineCount();

    /// 
    /// 注释行数
    /// 
    public abstract int GetCommentLineCount();
}

}

CShareCount.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;

namespace WordCountForms
{
class CShareCount : Count
{
protected override String FilterContent(string fileContent)
{
//匹配@””
MatchCollection matchs1 = Regex.Matches(fileContent, “@\”[\s\S]+?\”“);
foreach (Match match in matchs1)
{
String value = match.Value.ToString();
fileContent = Replace(fileContent, value, REPLACE_STRING);
}
//匹配/* */
MatchCollection matchs2 = Regex.Matches(fileContent, @”/*[\s\S]+?*/”);
foreach (Match match in matchs2)
{
String value = match.Value.ToString();
fileContent = Replace(fileContent, value, COMMMENT_REPLACE_STRING);
}
//匹配””
MatchCollection match3 = Regex.Matches(fileContent, “\”(.*?)\”“);
foreach (Match match in match3)
{
String value = match.Value.ToString();
fileContent = Replace(fileContent, value, REPLACE_STRING);
}
fileContent = fileContent + ‘\n’;
return fileContent;
}

    public override int GetCodeLineCount()
    {
        MatchCollection matchs = Regex.Matches(fileContent, @"(.*?)[\\S](.*?)\n");
        return matchs.Count;
    }

    public override int GetAllLineCount()
    {
        MatchCollection matchs = Regex.Matches(fileContent, @"\n");
        return matchs.Count;
    }

    public override int GetBlankLineCount()
    {
        return GetAllLineCount() - GetCodeLineCount() - GetCommentLineCount();
    }

    public override int GetCommentLineCount()
    {
        MatchCollection matchs = Regex.Matches(fileContent, @"(.*?)//(.*?)\n");
        return matchs.Count;
    }
}

}

FileHelper.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WordCountForms
{
public class FileHelper
{
///
/// 子文件List
///
public List fileList { get; set; }

    public FileHelper()
    {
        fileList = new List();
    }

    /// 
    /// 获取文件内容
    /// 
    /// 
    /// 
    public static String GetFileContent(String filePath)
    {
        return GetFileContent(filePath, Encoding.UTF8);
    }

    /// 
    /// 获取文件内容
    /// 
    /// 
    /// 
    /// 
    public static String GetFileContent(String filePath, Encoding encoding)
    {
        String fileContent = "";
        using (StreamReader read = new StreamReader(filePath, encoding))
        {
            fileContent = read.ReadToEnd();
            read.Close();
        }
        return fileContent;
    }

    /// 
    /// 获取文件类型
    /// 
    /// 
    /// 
    public static String GetType(String filePath)
    {
        String[] fileArray = filePath.Split('.');
        String type = fileArray[fileArray.Length - 1];
        return type;
    }

    /// 
    /// 文件保存
    /// 
    /// 
    /// 
    /// 
    public static void SaveFile(String fileContent, String filePath, FileMode fm)
    {
        //如果文件txt存在就打开,不存在就新建 .append 是追加写
        FileStream fst = new FileStream(filePath, fm);
        //写数据到txt
        StreamWriter swt = new StreamWriter(fst, System.Text.Encoding.GetEncoding("utf-8"));
        //写入
        swt.WriteLine(fileContent);
        swt.Close();
        fst.Close();
    }

    /// 
    /// 文件保存
    /// 
    /// 
    /// 
    public static void SaveFile(String fileContent, String filePath)
    {
        SaveFile(fileContent, filePath, FileMode.Create);
    }

    /// 
    /// 遍历全部文件夹
    /// 
    /// 
    public static void GetChilds(String parentFolder)
    {
        Console.WriteLine(parentFolder);
        DirectoryInfo folder = new DirectoryInfo(parentFolder);
        DirectoryInfo[] folderChild = folder.GetDirectories();
        foreach (DirectoryInfo childFolder in folderChild)
        {
            GetChilds(childFolder.FullName);
        }
    }

    /// 
    /// 获取全部子文件夹下的文件
    /// 
    /// 
    public void GetChildsFile(String parentFolder)
    {
        DirectoryInfo folder = new DirectoryInfo(parentFolder);
        DirectoryInfo[] folderChild = folder.GetDirectories();
        FileInfo[] files = folder.GetFiles();
        foreach (FileInfo file in files)
        {
            fileList.Add(file.FullName);
        }
        foreach (DirectoryInfo childFolder in folderChild)
        {
            GetChildsFile(childFolder.FullName);
        }
    }
}

}

Form1.Designer.cs
namespace WordCountForms
{
partial class Form1
{
///
/// Required designer variable.
///
private System.ComponentModel.IContainer components = null;

    /// 
    /// Clean up any resources being used.
    /// 
    /// true if managed resources should be disposed; otherwise, false.
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// 
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// 
    private void InitializeComponent()
    {
        this.txtPath = new System.Windows.Forms.TextBox();
        this.btnCountWord = new System.Windows.Forms.Button();
        this.lblPathInfo = new System.Windows.Forms.Label();
        this.lblAllLine = new System.Windows.Forms.Label();
        this.lblCodeLine = new System.Windows.Forms.Label();
        this.lblBlankLine = new System.Windows.Forms.Label();
        this.lblCommentLine = new System.Windows.Forms.Label();
        this.txtAllLine = new System.Windows.Forms.TextBox();
        this.txtCodeLine = new System.Windows.Forms.TextBox();
        this.txtBlanckLine = new System.Windows.Forms.TextBox();
        this.txtCommentLine = new System.Windows.Forms.TextBox();
        this.SuspendLayout();
        // 
        // txtPath
        // 
        this.txtPath.Location = new System.Drawing.Point(27, 50);
        this.txtPath.Name = "txtPath";
        this.txtPath.Size = new System.Drawing.Size(223, 21);
        this.txtPath.TabIndex = 0;
        // 
        // btnCountWord
        // 
        this.btnCountWord.Location = new System.Drawing.Point(292, 48);
        this.btnCountWord.Name = "btnCountWord";
        this.btnCountWord.Size = new System.Drawing.Size(77, 23);
        this.btnCountWord.TabIndex = 1;
        this.btnCountWord.Text = "代码统计";
        this.btnCountWord.UseVisualStyleBackColor = true;
        this.btnCountWord.Click += new System.EventHandler(this.btnCountWord_Click);
        // 
        // lblPathInfo
        // 
        this.lblPathInfo.AutoSize = true;
        this.lblPathInfo.Location = new System.Drawing.Point(30, 24);
        this.lblPathInfo.Name = "lblPathInfo";
        this.lblPathInfo.Size = new System.Drawing.Size(329, 12);
        this.lblPathInfo.TabIndex = 2;
        this.lblPathInfo.Text = "请输入要统计的java程序或者c#程序路径,如D://MyApp.java";
        // 
        // lblAllLine
        // 
        this.lblAllLine.AutoSize = true;
        this.lblAllLine.Location = new System.Drawing.Point(31, 105);
        this.lblAllLine.Name = "lblAllLine";
        this.lblAllLine.Size = new System.Drawing.Size(77, 12);
        this.lblAllLine.TabIndex = 3;
        this.lblAllLine.Text = "代码总行数:";
        // 
        // lblCodeLine
        // 
        this.lblCodeLine.AutoSize = true;
        this.lblCodeLine.Location = new System.Drawing.Point(31, 143);
        this.lblCodeLine.Name = "lblCodeLine";
        this.lblCodeLine.Size = new System.Drawing.Size(65, 12);
        this.lblCodeLine.TabIndex = 4;
        this.lblCodeLine.Text = "代码行数:";
        // 
        // lblBlankLine
        // 
        this.lblBlankLine.AutoSize = true;
        this.lblBlankLine.Location = new System.Drawing.Point(31, 180);
        this.lblBlankLine.Name = "lblBlankLine";
        this.lblBlankLine.Size = new System.Drawing.Size(89, 12);
        this.lblBlankLine.TabIndex = 5;
        this.lblBlankLine.Text = "代码空白行数:";
        // 
        // lblCommentLine
        // 
        this.lblCommentLine.AutoSize = true;
        this.lblCommentLine.Location = new System.Drawing.Point(31, 217);
        this.lblCommentLine.Name = "lblCommentLine";
        this.lblCommentLine.Size = new System.Drawing.Size(89, 12);
        this.lblCommentLine.TabIndex = 6;
        this.lblCommentLine.Text = "代码注释行数:";
        // 
        // txtAllLine
        // 
        this.txtAllLine.Location = new System.Drawing.Point(151, 102);
        this.txtAllLine.Name = "txtAllLine";
        this.txtAllLine.Size = new System.Drawing.Size(100, 21);
        this.txtAllLine.TabIndex = 7;
        // 
        // txtCodeLine
        // 
        this.txtCodeLine.Location = new System.Drawing.Point(151, 140);
        this.txtCodeLine.Name = "txtCodeLine";
        this.txtCodeLine.Size = new System.Drawing.Size(100, 21);
        this.txtCodeLine.TabIndex = 8;
        // 
        // txtBlanckLine
        // 
        this.txtBlanckLine.Location = new System.Drawing.Point(151, 177);
        this.txtBlanckLine.Name = "txtBlanckLine";
        this.txtBlanckLine.Size = new System.Drawing.Size(100, 21);
        this.txtBlanckLine.TabIndex = 9;
        // 
        // txtCommentLine
        // 
        this.txtCommentLine.Location = new System.Drawing.Point(150, 214);
        this.txtCommentLine.Name = "txtCommentLine";
        this.txtCommentLine.Size = new System.Drawing.Size(100, 21);
        this.txtCommentLine.TabIndex = 10;
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(397, 297);
        this.Controls.Add(this.txtCommentLine);
        this.Controls.Add(this.txtBlanckLine);
        this.Controls.Add(this.txtCodeLine);
        this.Controls.Add(this.txtAllLine);
        this.Controls.Add(this.lblCommentLine);
        this.Controls.Add(this.lblBlankLine);
        this.Controls.Add(this.lblCodeLine);
        this.Controls.Add(this.lblAllLine);
        this.Controls.Add(this.lblPathInfo);
        this.Controls.Add(this.btnCountWord);
        this.Controls.Add(this.txtPath);
        this.Name = "Form1";
        this.Text = "Form1";
        this.ResumeLayout(false);
        this.PerformLayout();

    }

    #endregion

    private System.Windows.Forms.TextBox txtPath;
    private System.Windows.Forms.Button btnCountWord;
    private System.Windows.Forms.Label lblPathInfo;
    private System.Windows.Forms.Label lblAllLine;
    private System.Windows.Forms.Label lblCodeLine;
    private System.Windows.Forms.Label lblBlankLine;
    private System.Windows.Forms.Label lblCommentLine;
    private System.Windows.Forms.TextBox txtAllLine;
    private System.Windows.Forms.TextBox txtCodeLine;
    private System.Windows.Forms.TextBox txtBlanckLine;
    private System.Windows.Forms.TextBox txtCommentLine;
}

}

JavaCount.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace WordCountForms
{
class JavaCount : Count
{
protected override string FilterContent(string fileContent)
{
//匹配/* */
MatchCollection matchs2 = Regex.Matches(fileContent, @”/*[\s\S]+?*/”);
foreach (Match match in matchs2)
{
String value = match.Value.ToString();
fileContent = Replace(fileContent, value, COMMMENT_REPLACE_STRING);
}
//匹配””
MatchCollection match3 = Regex.Matches(fileContent, “\”(.*?)\”“);
foreach (Match match in match3)
{
String value = match.Value.ToString();
fileContent = Replace(fileContent, value, REPLACE_STRING);
}
fileContent = fileContent + ‘\n’;
return fileContent;
}

    public override int GetCodeLineCount()![这里写图片描述](https://img-blog.csdn.net/20150408224416770)
    {
        MatchCollection matchs = Regex.Matches(fileContent, @"(.*?)[\\S](.*?)\n");
        return matchs.Count;
    }

    public override int GetAllLineCount()
    {
        MatchCollection matchs = Regex.Matches(fileContent, @"\n");
        return matchs.Count;
    }

    public override int GetBlankLineCount()
    {
        return GetAllLineCount() - GetCodeLineCount() - GetCommentLineCount();
    }

    public override int GetCommentLineCount()
    {
        MatchCollection matchs = Regex.Matches(fileContent, @"(.*?)//(.*?)\n");
        return matchs.Count;
    }
}

}

Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WordCountForms
{
static class Program
{
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}

WorldCountFactory.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WordCountForms
{
class WorldCountFactory
{
public static Count CreateCount(String type)
{
if (type == “java”)
{
return new JavaCount();
}
if (type == “cs”)
{
return new CShareCount();
}
return new BlankCount();
}
}
}

你可能感兴趣的:(c#课程作业,代码)