C#简易计算器

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 计算器1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Left = Screen.PrimaryScreen.Bounds.Width / 2 - this.Width / 2;
            this.Top = Screen.PrimaryScreen.Bounds.Height / 2 - this.Height / 2;
            this.FormBorderStyle = FormBorderStyle.FixedToolWindow;
            textBox1.ReadOnly = true;
        }
        bool isCalc = false;
        bool isca = false;
        //1
        private void button1_Click(object sender, EventArgs e)
        {
            if (isCalc == true)
            {
                isCalc = false;
                textBox1.Text = "";
            }
            if (isca==true)
            {
                isca = false;
                textBox1.Text = "";
                textBox2.Text = "";
            }
            textBox1.Text += 1;
            textBox2.Text += 1;
        }
        //2
        private void button2_Click(object sender, EventArgs e)
        {
            if (isCalc == true)
            {
                isCalc = false;
                textBox1.Text = "";
            }
            if (isca == true)
            {
                isca = false;
                textBox1.Text = "";
                textBox2.Text = "";
            }
            textBox1.Text += 2;
            textBox2.Text += 2;
        }
        //3
        private void button3_Click(object sender, EventArgs e)
        {
            if (isCalc == true)
            {
                isCalc = false;
                textBox1.Text = "";
            }
            if (isca == true)
            {
                isca = false;
                textBox1.Text = "";
                textBox2.Text = "";
            }
            textBox1.Text += 3;
            textBox2.Text += 3;
        }
        //4
        private void button5_Click(object sender, EventArgs e)
        {
            if (isCalc == true)
            {
                isCalc = false;
                textBox1.Text = "";
            }
            if (isca == true)
            {
                isca = false;
                textBox1.Text = "";
                textBox2.Text = "";
            }
            textBox1.Text += 4;
            textBox2.Text += 4;
        }
        //5
        private void button6_Click(object sender, EventArgs e)
        {
            if (isCalc == true)
            {
                isCalc = false;
                textBox1.Text = "";
            }
            if (isca == true)
            {
                isca = false;
                textBox1.Text = "";
                textBox2.Text = "";
            }
            textBox1.Text += 5;
            textBox2.Text += 5;
        }
        //6
        private void button7_Click(object sender, EventArgs e)
        {
            if (isCalc == true)
            {
                isCalc = false;
                textBox1.Text = "";
            }
            if (isca == true)
            {
                isca = false;
                textBox1.Text = "";
                textBox2.Text = "";
            }
            textBox1.Text += 6;
            textBox2.Text += 6; ;
        }
        //7
        private void button9_Click(object sender, EventArgs e)
        {
            if (isCalc == true)
            {
                isCalc = false;
                textBox1.Text = "";
            }
            if (isca == true)
            {
                isca = false;
                textBox1.Text = "";
                textBox2.Text = "";
            }
            textBox1.Text += 7;
            textBox2.Text += 7; 
        }
        //8
        private void button10_Click(object sender, EventArgs e)
        {
            if (isCalc == true)
            {
                isCalc = false;
                textBox1.Text = "";
            }
            if (isca == true)
            {
                isca = false;
                textBox1.Text = "";
                textBox2.Text = "";
            }
            textBox1.Text += 8;
            textBox2.Text += 8;
        }
        //9
        private void button11_Click(object sender, EventArgs e)
        {
            if (isCalc == true)
            {
                isCalc = false;
                textBox1.Text = "";
            }
            if (isca == true)
            {
                isca = false;
                textBox1.Text = "";
                textBox2.Text = "";
            }
            textBox1.Text += 9;
            textBox2.Text += 9;
        }
        //0
        private void button14_Click(object sender, EventArgs e)
        {
            if (isCalc == true)
            {
                isCalc = false;
                textBox1.Text = "";
            }
            if (isca == true)
            {
                isca = false;
                textBox1.Text = "";
                textBox2.Text = "";
            }
            textBox1.Text += 0;
            textBox2.Text += 0;
        }
        bool isDel = false;
        //.
        private void button15_Click(object sender, EventArgs e)
        {
            if (isDel == false)
            {
                textBox1.Text += ".";
                textBox2.Text += ".";
                isDel = true;
            }
        }
        double num1;
        string type;
        //+
        private void button4_Click(object sender, EventArgs e)
        {
            type = "+";
            num1 = double.Parse(textBox1.Text);
            isCalc = true;
            isDel = false;
            textBox2.Text += type;
        }
        //-
        private void button8_Click(object sender, EventArgs e)
        {
            type = "-";
            num1 = double.Parse(textBox1.Text);
            isCalc = true;
            isDel = false;
            textBox2.Text += type;
        }
        //*
        private void button12_Click(object sender, EventArgs e)
        {
            type = "*";
            num1 = double.Parse(textBox1.Text);
            isCalc = true;
            isDel = false;
            textBox2.Text += type;
        }
        //÷
        private void button16_Click(object sender, EventArgs e)
        {
            type = "÷";
            num1 = double.Parse(textBox1.Text);
            isCalc = true;
            isDel = false;
            textBox2.Text += type;
        }
        //%
        private void button13_Click(object sender, EventArgs e)
        {
            type = "%";
            num1 = double.Parse(textBox1.Text);
            isCalc = true;
            isDel = false;
            textBox2.Text += type;
        }
        //=
        private void button18_Click(object sender, EventArgs e)
        {
            isCalc = true;
            double num2 = double.Parse(textBox1.Text);
            if (type == "+")
            {
                textBox1.Text = (num1 + num2).ToString();
            }
            if (type == "-")
            {
                textBox1.Text = (num1 - num2).ToString();
            }
            if (type == "*")
            {
                textBox1.Text = (num1 * num2).ToString();
            }
            if (type == "÷")
            {
                textBox1.Text = (num1 / num2).ToString();
            }
            if (type == "%")
            {
                textBox1.Text = (num1 % num2).ToString();
            }
            textBox2.Text += "=" + textBox1.Text;
        }
        //ce
        private void button17_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {

        }

        private void 关闭ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值