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


        private void button1_Click(object sender, EventArgs e)
        {
            
            textBox2.Text += button1.Text;
        }
        private void button2_Click(object sender, EventArgs e)
        {
           
            textBox2.Text += button2.Text;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            textBox2.Text += button3.Text;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            textBox2.Text += button4.Text;
        }
        private void button5_Click(object sender, EventArgs e)
        {
            textBox2.Text += button5.Text;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            textBox2.Text += button6.Text;
        }

        private void button7_Click(object sender, EventArgs e)
        {
            textBox2.Text += button7.Text;
        }

        private void button8_Click(object sender, EventArgs e)
        {
            textBox2.Text += button8.Text;
        }

        private void button9_Click(object sender, EventArgs e)
        {
            textBox2.Text += button9.Text;
        }

        private void button10_Click(object sender, EventArgs e)
        {
            textBox2.Text += button10.Text;
        }
        //第一个数字i
        int i;
        //第二个数字j
        int j;
        //符号
        string h;
        //结果
        int k;
        private void button11_Click(object sender, EventArgs e)
        {
            jisuan();
            h = "+";
            //i = Convert.ToInt32(textBox1.Text);
            //textBox1.Text = button15.Text;
            //h = textBox1.Text;
            //textBox1.Text = "";
        }

        private void button12_Click(object sender, EventArgs e)
        {
            jisuan();
            h = "-";
            //i = Convert.ToInt32(textBox1.Text);
            //textBox1.Text = button15.Text;
            //h = textBox1.Text;
            //textBox1.Text = "";
        }

        private void button13_Click(object sender, EventArgs e)
        {
            jisuan();
            h = "×";
            //i = Convert.ToInt32(textBox1.Text);
            //textBox1.Text = button15.Text;
            //h = textBox1.Text;
            //textBox1.Text = "";
        }

        private void button14_Click(object sender, EventArgs e)
        {
            jisuan();
            h = "÷";
            //i = Convert.ToInt32(textBox1.Text);
            //textBox1.Text = button15.Text;
            //h = textBox1.Text;
            //textBox1.Text = "";
        }

        private void jisuan()
        {
            i = Convert.ToInt32(textBox2.Text);
            textBox2.Text ="";
           // h = textBox1.Text;
           // textBox1.Text = "";
        }

        private void button15_Click(object sender, EventArgs e)
        {
            jisuan();
            h = "%";
            //i = Convert.ToInt32(textBox1.Text);
            //textBox1.Text = button15.Text;
            //h = textBox1.Text;
            //textBox1.Text = "";
        }
        //清除
        private void button16_Click(object sender, EventArgs e)
        {
            textBox2.Text="";
        }
        //等于
        private void button17_Click(object sender, EventArgs e)
        {
            
            j = Convert.ToInt32(textBox2.Text);
            textBox2.Text = "";
            //k = i + j;
            //textBox1.Text =k.ToString();
            if (h == "+")
            {
                k = i + j;
            }
            if (h == "-")
            {
                k = i - j;
            }
            if (h == "×")
            {
                k = i * j;
            }
            if (h == "÷")
            {
                k = i / j;
            }
            if (h == "%")
            {
                k = i % j;
            }
            textBox2.Text = i.ToString() + h + j.ToString() + "=" + k.ToString();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            textBox2.ReadOnly = true;
        }
    }
}


 

猜你喜欢

转载自blog.csdn.net/weixin_43437202/article/details/85329831
今日推荐