C#制作简单计算器

一、界面设置。

1、设置两个Label标签,并将其AutoSize属性设置为False,此时可以放大Label标签。

2、具体界面如下所示。

二、代码编写。

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;

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

        public double num1 = 0,num2 = 0,sum = 0;
        public char ch = '\0';

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.label2.Text == "")
            {
                this.label1.Text += "1";
            }
            else {
                this.label1.Text = this.label2.Text;
                this.label2.Text = "";
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (this.label2.Text == "")
            {
                this.label1.Text += "2";
            }
            else
            {
                this.label1.Text = this.label2.Text;
                this.label2.Text = "";
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (this.label2.Text == "")
            {
                this.label1.Text += "3";
            }
            else
            {
                this.label1.Text = this.label2.Text;
                this.label2.Text = "";
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (this.label2.Text == "")
            {
                this.label1.Text += "4";
            }
            else
            {
                this.label1.Text = this.label2.Text;
                this.label2.Text = "";
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (this.label2.Text == "")
            {
                this.label1.Text += "5";
            }
            else
            {
                this.label1.Text = this.label2.Text;
                this.label2.Text = "";
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (this.label2.Text == "")
            {
                this.label1.Text += "6";
            }
            else
            {
                this.label1.Text = this.label2.Text;
                this.label2.Text = "";
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            if (this.label2.Text == "")
            {
                this.label1.Text += "7";
            }
            else
            {
                this.label1.Text = this.label2.Text;
                this.label2.Text = "";
            }
        }

        private void button8_Click(object sender, EventArgs e)
        {
            if (this.label2.Text == "")
            {
                this.label1.Text += "8";
            }
            else
            {
                this.label1.Text = this.label2.Text;
                this.label2.Text = "";
            }
        }

        private void button9_Click(object sender, EventArgs e)
        {
            if (this.label2.Text == "")
            {
                this.label1.Text += "9";
            }
            else
            {
                this.label1.Text = this.label2.Text;
                this.label2.Text = "";
            }
        }

        private void button10_Click(object sender, EventArgs e)
        {
            if (this.label2.Text == "")
            {
                this.label1.Text += "0";
            }
            else
            {
                this.label1.Text = this.label2.Text;
                this.label2.Text = "";
            }
        }

        private void button11_Click(object sender, EventArgs e)
        {
            if (this.label2.Text == "")
            {
                this.label1.Text += ".";
            }
            else
            {
                this.label1.Text = this.label2.Text;
                this.label2.Text = "";
            }
        }

        private void button12_Click(object sender, EventArgs e)
        {
            this.label1.Text = "";
            this.label2.Text = "";
        }

        private void button16_Click(object sender, EventArgs e)
        {
            if (this.label2.Text == "")
            {
                this.label1.Text += "/";
            }
            else
            {
                this.label1.Text = this.label2.Text+"/";
                this.label2.Text = "";
            }
            ch = '/';
        }

        private void button15_Click(object sender, EventArgs e)
        {
            if (this.label2.Text == "")
            {
                this.label1.Text += "*";
            }
            else
            {
                this.label1.Text = this.label2.Text+"*";
                this.label2.Text = "";
            }
            ch = '*';
        }

        private void button14_Click(object sender, EventArgs e)
        {
            if (this.label2.Text == "")
            {
                this.label1.Text += "-";
            }
            else
            {
                this.label1.Text = this.label2.Text+"-";
                this.label2.Text = "";
            }
            ch = '-';
        }

        private void button13_Click(object sender, EventArgs e)
        {
            if (this.label2.Text == "")
            {
                this.label1.Text += "+";
            }
            else
            {
                this.label1.Text = this.label2.Text+"+";
                this.label2.Text = "";
            }
            ch = '+';
        }

        private void button17_Click(object sender, EventArgs e)
        {
            switch (ch) {
                case ('+'):
                    num1 = Convert.ToDouble(this.label1.Text.Split('+').GetValue(0));
                    num2 = Convert.ToDouble(this.label1.Text.Split('+').GetValue(1));
                    sum = num1 + num2;
                    break;
                case ('-'):
                    num1 = Convert.ToDouble(this.label1.Text.Split('-').GetValue(0));
                    num2 = Convert.ToDouble(this.label1.Text.Split('-').GetValue(1));
                    sum = num1 - num2;
                    break;
                case ('*'):
                    num1 = Convert.ToDouble(this.label1.Text.Split('*').GetValue(0));
                    num2 = Convert.ToDouble(this.label1.Text.Split('*').GetValue(1));
                    sum = num1 * num2;
                    break;
                case ('/'):
                    num1 = Convert.ToDouble(this.label1.Text.Split('/').GetValue(0));
                    num2 = Convert.ToDouble(this.label1.Text.Split('/').GetValue(1));
                    if (num2 == 0) {
                        MessageBox.Show("除数不能为0!");
                        throw new Exception("除数不能为0!");
                    }
                    sum = num1 / num2;
                    break;
            }
            this.label1.Text += "=";
            this.label2.Text = Convert.ToString(sum);
        }
    }
    
}

三、结果展示。

存在问题:不支持多个运算符一起操作。

-----END-----

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值