C# designed a simple calculator to realize the addition, subtraction, multiplication, division and idempotence calculation of two numbers. The running effect is shown in the following figure:

1. The subject requirements are as follows:

C# designed a simple calculator to realize the addition, subtraction, multiplication, division and idempotence calculation of two numbers. The running effect is shown in the following figure:

 

 2. What you need to use here is C# Windows Forms under VS2019

3. Come on, show:

using System;
using System.Windows.Forms;

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

        private void Form1_Load(object sender, EventArgs e)
        {
            MessageBox.Show("欢迎使用小关牌计算器!");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string s1 = textBox1.Text;
                string s2 = textBox2.Text;
                int i1, i2;
                i1 = (int)Convert.ToInt64(s1);
                i2 = (int)Convert.ToInt64(s2);
                string s3 = (i1 + i2).ToString();
                MessageBox.Show(s3);
            }
            catch
            {
                MessageBox.Show("错误,请检查!");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                string s1 = textBox1.Text;
                string s2 = textBox2.Text;
                double i1, i2;
                i1 = (int)Convert.ToInt64(s1);
                i2 = (int)Convert.ToInt64(s2);
                string s3 = (i1 / i2).ToString();
                MessageBox.Show(s3);
            }
            catch
            {
                MessageBox.Show("错误,请检查!");
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            
           
        }

        private void label2_Click(object sender, EventArgs e)
        {
            MessageBox.Show("目前不支持其他运算!");
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
           
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            
           
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            try
            {
                string s1 = textBox1.Text;
                string s2 = textBox2.Text;
                int i1, i2;
                i1 = (int)Convert.ToInt64(s1);
                i2 = (int)Convert.ToInt64(s2);
                string s3 = (i1 - i2).ToString();
                MessageBox.Show(s3);
            }
            catch
            {
                MessageBox.Show("错误,请检查!");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                string s1 = textBox1.Text;
                string s2 = textBox2.Text;
                int i1, i2;
                i1 = (int)Convert.ToInt64(s1);
                i2 = (int)Convert.ToInt64(s2);
                string s3 = (i1 * i2).ToString();
                MessageBox.Show(s3);
            }
            catch
            {
                MessageBox.Show("错误,请检查!");
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                string s1 = textBox1.Text;
                string s2 = textBox2.Text;
                int i1, i2;
                i1 = (int)Convert.ToInt64(s1);
                i2 = (int)Convert.ToInt64(s2);
                double s3 = Math.Pow(i1, i2);
                string s4 = Convert.ToString(s3);
                MessageBox.Show(s4);
            }
            catch
            {
                MessageBox.Show("错误,请检查!");
            }
        }
        private void button5_Click(object sender, EventArgs e)
        {
            //归零
            textBox1.Text = string.Empty;
            textBox2.Text = string.Empty;
        }
    }
}

This has more than 140 lines of code, which is a bit long and needs improvement

4. Take a look at the running result: ctrl+f5

 

 Here is free to play with the effect of pop-up prompts

 

 The result shown here is 9 to the 9th power

I hope I can help everyone, I ask you if you want a like, will you give it, thank you all.
Copyright statement: The copyright of this article belongs to the author (@攻城狮小关) and CSDN. Welcome to reprint, but you must keep this paragraph without the author’s consent Statement, and provide the original link in an obvious place on the article page, otherwise the right to pursue legal responsibility is reserved.
It is not easy for everyone to write articles, please respect the fruits of labor~ 
Communication plus Q: 1909561302
Blog Park Address https://www.cnblogs.com/guanguan-com/

Guess you like

Origin blog.csdn.net/Mumaren6/article/details/109282912