C#作业第一期,简单的源代码计算器

首先看一下运行结果:


这是界面


选择Choose键可以打开文件如:



选择test2.txt,输出结果:



我们来看下test.txt文件里面有什么东西:



完美输出~

好吧为什么是个txt呢?其实要其他的话只需要修改一下代码就可以了。



好吧,代码出来了基本上都能看懂了,只需要修改上述代码中的 openFileDialog1.Filter = "*.txt|*.txt|所有文件(*.*)|*.*"; 即可,如:  openFileDialog1.Filter = "*.cs|*.cs|所有文件(*.*)|*.*"; ,这样就可以打开cs文件了。


这是所调用的OpenFileDialog类,用于提示用户打开文件。


很明显,上面代码是控制button1也就是那个叫做Choose的按钮。


这是的整型数组,很明显用来存放统计结果的。那么是怎么将结果统计后存进去的呢?



这就是那个计算的函数,code是代码行数,space是空格数,explain1是注释行数


这里是读取文件的代码



由于本人是个渣渣,所以写了个很糟糕的算法,望各位不要狂喷。





这样就可以计算出的空行数,代码行数,注释行数。然后存入一个数组并返回该数组:



接下来在button1_Click函数那里调用该函数就可以了。。。


好吧,本人一直做的是c++所以提供的测试代码也只是c++的,c#我根本就不会!好吧,这东西是乱写出来的,不要在意细节~

什么,你问我窗口怎么设计的?这个就算了吧,超难看。。。

就是用winform画画画,总之这不重要,大家无视吧。(个人还是喜欢QT多一点~)

下面是源代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            {   //打开文件对话框
                OpenFileDialog openFileDialog1 = new OpenFileDialog();
                openFileDialog1.Filter = "*.txt|*.txt|所有文件(*.*)|*.*";
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        string Name = openFileDialog1.FileName;
                        int[] n =  count(Name);
                        textBox3.Text = n[1].ToString();       //存放空行数
                        textBox4.Text = n[2].ToString();       //存放代码行
                        textBox2.Text = n[0].ToString();       //存放注释行
                        

                        //  richTextBox1.LoadFile(Name,RichTextBoxStreamType.PlainText);
                        //  statusBarPanel1.Text=Name;

                    }
                    catch (Exception) { }
                }


            }
        }


        public static int[] count(string str)
        {

              int code = 0;
             int space = 0;
             int explain1 = 0;

             try
             {
                 FileStream fileread = new FileStream(str, FileMode.Open, FileAccess.Read, FileShare.Read);
             }
             catch (Exception)
             {
                 Console.ReadLine();
                 throw;
             }


            StreamReader sr = new StreamReader(str);
            
                    string line1=null;
                    //int count;
                    //string a1,a2;
                    while ((line1 = sr.ReadLine()) != null)
                    {
                        // line1 = sr.ReadLine();
                        line1 = line1.Trim();
                        // a1 =a1.Trim();
                        //计算注释行
                        if (line1.StartsWith("//") || line1.StartsWith("///"))
                        {
                            explain1++;
                        }
                        else if (line1.StartsWith("/*") || line1.EndsWith("*/"))
                        {
                            explain1++;
                        }
                        //计算空行数
                        else if (line1 == "")
                        {
                            space++;
                        }
                        //计算代码行
                        else
                        {
                            code++;
                        }
                    }

                    int[] i = { code,space, explain1 };
                   
        

                    return i;
                }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {

        }

        private void label1_Click_1(object sender, EventArgs e)
        {

        }

    }
}
     



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值