C#代码计算器

这篇博客介绍了如何使用C#编程语言创建一个简单的计算器应用。从开始界面到功能实现,详细阐述了代码实现过程,涵盖了输入处理、运算逻辑及结果显示等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

        static int codeLine = 0;
        static int commentLine = 0;
        static int blankLine = 0;
        static int all = 0;

        private void btnstart_Click(object sender, EventArgs e)
        {
            lblshow.Text = "";
            btnstart.Visible = false;
            btncontinue.Visible = true;

            txtBox.Visible = true;


        }

        private void btncontinue_Click(object sender, EventArgs e)
        {
            lblshow.Text = "正在统计...";

            string dir = txtBox.Text;
            
            directoryCountLine(dir);

            btnresult.Visible = true;
        }

        static void directoryCountLine(string directory)
        {
            foreach (string file in Directory.GetFiles(directory, "*.cs"))
            {
                countLine(file);
            }
        }

        static void countLine(string file)
        {
            FileStream stream = new FileStream(file, FileMode.Open);
            StreamReader reader = new StreamReader(stream);
            string line = reader.ReadLine();
            while (line != null)
            {
                if (line.Trim() != "" && !(line.Trim().StartsWith(@"//")))
                {
                    codeLine++;
                }
                else if (line.Trim().StartsWith(@"//"))
                {
                    blankLine++;
                }
                else
                {
                    commentLine++;
                }
                all = codeLine + commentLine + blankLine;

                line = reader.ReadLine();
            }
            reader.Close();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            lblshow.Text = "功能介绍!\n" +
                "1.此程序可以统计出所在文件夹C#程序代码总行数\n" +
                "2.代码行数\n" +
                "3.注释行数\n" +
                "4.空白行数\n";
        }

        private void btnresult_Click(object sender, EventArgs e)
        {
            btncontinue.Visible = false;
            txtBox.Visible = false;

            lblshow.Text = "C#代码行数为:\n"+codeLine+
                "\n注释行:\n" + commentLine +
                "\n空白行:\n" + blankLine +
                "\n总的代码行数为:\n" + all;  
        }
    }
}

开始界面:





评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值