C#第一次作业,C#或者java代码总行数、代码行数、代码空格行数、注释行数的统计
来源:互联网 发布:吉林大学 网络教育 编辑:程序博客网 时间:2024/06/05 18:23
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”;
/// <summary> /// 多行注释替换字符 /// </summary> protected const String COMMMENT_REPLACE_STRING = "//WorldCount"; /// <summary> /// 统计内容 /// </summary> protected String fileContent; /// <summary> /// 代码行数 /// </summary> protected int codeLineCount; /// <summary> /// 总行数 /// </summary> protected int allLineCount; /// <summary> /// 空白行数 /// </summary> protected int blankLineCount; /// <summary> /// 注释行数 /// </summary> protected int commentLineCount; /// <summary> /// 设置需进行统计内容 /// </summary> /// <param name="fileContent"></param> public void SetFileContent(String fileContent) { this.fileContent = FilterContent(fileContent); } /// <summary> /// 过滤清除无效字符 /// </summary> /// <param name="fileContent"></param> /// <returns></returns> protected abstract String FilterContent(String fileContent); /// <summary> /// 替换内容,解除干扰 /// </summary> /// <param name="fileContent"></param> /// <param name="arrString"></param> /// <param name="replaceString"></param> /// <returns></returns> 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; } /// <summary> /// 代码行数 /// </summary> public abstract int GetCodeLineCount(); /// <summary> /// 总行数 /// </summary> public abstract int GetAllLineCount(); /// <summary> /// 空白行数 /// </summary> public abstract int GetBlankLineCount(); /// <summary> /// 注释行数 /// </summary> 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<String>(); } /// <summary> /// 获取文件内容 /// </summary> /// <param name="filePath"></param> /// <returns></returns> public static String GetFileContent(String filePath) { return GetFileContent(filePath, Encoding.UTF8); } /// <summary> /// 获取文件内容 /// </summary> /// <param name="filePath"></param> /// <param name="encoding"></param> /// <returns></returns> public static String GetFileContent(String filePath, Encoding encoding) { String fileContent = ""; using (StreamReader read = new StreamReader(filePath, encoding)) { fileContent = read.ReadToEnd(); read.Close(); } return fileContent; } /// <summary> /// 获取文件类型 /// </summary> /// <param name="filePath"></param> /// <returns></returns> public static String GetType(String filePath) { String[] fileArray = filePath.Split('.'); String type = fileArray[fileArray.Length - 1]; return type; } /// <summary> /// 文件保存 /// </summary> /// <param name="fileContent"></param> /// <param name="filePath"></param> /// <param name="fm"></param> 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(); } /// <summary> /// 文件保存 /// </summary> /// <param name="fileContent"></param> /// <param name="filePath"></param> public static void SaveFile(String fileContent, String filePath) { SaveFile(fileContent, filePath, FileMode.Create); } /// <summary> /// 遍历全部文件夹 /// </summary> /// <param name="parentFolder"></param> 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); } } /// <summary> /// 获取全部子文件夹下的文件 /// </summary> /// <param name="parentFolder"></param> 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;
/// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> 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()![这里写图片描述](http://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#第一次作业,C#或者java代码总行数、代码行数、代码空格行数、注释行数的统计
- C#第一次作业:输出文件总行数、空行数、注释行数、代码行数
- 《C#程序设计》第一次作业 给定一个源代码文件(.cs, ),输出该文件的代码行数、注释行数、空行数、代码总行数、。
- 统计源代码文件的总行数、空行数、注释行数、代码行数
- 给定一个源代码文件,输出该文件的总行数、空行数、注释行数、代码行数
- 输出该文件的总行数、空行数、注释行数、代码行数
- C#如何统计代码行数
- 作业一目标一给定一个源代码文件(.cs, .java),输出该文件的总行数、空行数、注释行数、代码行数
- 给定一个源代码文件(.cs, .java),输出该文件的总行数、空行数、注释行数、代码行数
- 给定一个源代码文件(.cs, .java),输出该文件的总行数、空行数、注释行数、代码行数
- 给定一个源代码文件(.cs, .java),输出该文件的总行数、空行数、注释行数、代码行数
- 给定一个源代码文件(.cs, .java),输出该文件的总行数、空行数、注释行数、代码行数
- 给定一个源代码文件(.cs, .java),输出该文件的总行数、空行数、注释行数、代码行数
- 给定一个源代码文件(.cs, .java),输出该文件的总行数、空行数、注释行数、代码行数
- 给定一个源代码文件(.cs, .java),输出该文件的总行数、空行数、注释行数、代码行数
- 给定一个源代码文件(.cs, .java),输出该文件的总行数、空行数、注释行数、代码行数
- 利用正则表达式统计某个某个文件夹下所有java文件的 空格行数、注释行数、和代码行数
- 一个统计代码行数的程序C#
- (R语言)有序因子和无序因子
- 喜羊羊系列之eclipse不能输入问题
- ‘RW_LOCK_UNLOCKED’未声明(不在函数内)
- EMV技术学习和研究-终端风险管理
- android开源系列:CircleImageView自定义圆形控件的使用
- C#第一次作业,C#或者java代码总行数、代码行数、代码空格行数、注释行数的统计
- BASH命令行基本操作
- (R语言)数组和矩阵
- Facebook Graph API 开发常见问题
- 千台Spark集群对千亿量级节点的相似度计算
- 系统pid=4占用80端口的解决方法
- 安卓获取图片缩略图的两种方式
- JAVA内存处理--堆和栈
- 第五周 课后实践:项目三——用多文件组织多个类的程序