C# implements a simple calculator

C# implements a simple calculator

Environment: VS2010 and above

  1. Create a Window Form application
    insert image description here
  2. Drag out two TextBoxes in the toolbox, put the first one on top and the second one on the bottom. The main name here is textBox1 above and textBox2 below. This involves writing the code behind
    insert image description here
  3. Drag the Button in the toolbar and place it. Use the alignment tools above to aid in your design.
    insert image description here
  4. Change the Text of each Button in the properties, as follows
    insert image description here
    Note that the 1~9, decimal point, and ±*/ Text should only have one character, do not enter more.
  5. Select any Button, right click, select view code, go to Form1.cs
    insert image description here
  6. start writing code
    AddNum modifies the Text of the TextBox, and applies it to the Click event of 1~9 and the decimal point
    Reset resets temp, myoperator, and the Text of the two TextBoxes
    Delete deletes the last character of Text in textBox2
    Calculate converts the Text of textBox2 to double to temp, and modifies myoperator
    Equal specific calculation
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 WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
//----上面是自动生成的代码,下面得我们手写----
        private double temp = 0;  //存储临时数据
        private char myoperator = ' ';  //判断之前按的是+-*/中的哪个

        private void AddNum(object sender, EventArgs e)
        {   // 1~9与小数点的Click事件
            //sender是引发该事件的控件,这里我们拆箱为Button
            Button button = (Button)sender;
            textBox2.Text += button.Text;
        }

        private void Reset(object sender, EventArgs e)
        {   // CE的Click事件
            temp = 0;
            myoperator = ' ';
            textBox1.Text = textBox2.Text = "";
        }

        private void Delete(object sender, EventArgs e)
        {   // ←的Click事件
            //移除最后一个字符
            if (textBox2.TextLength > 0)
                textBox2.Text = textBox2.Text.Remove(textBox2.TextLength - 1);
        }

        private void Calculate(object sender, EventArgs e)
        {   // +-*/的Click事件
            Button button = (Button)sender;
            if (double.TryParse(textBox2.Text, out temp))  //尝试把textBox2的Text转为double并赋值给temp
            {
                myoperator = button.Text[0]; //Text是string,取第一个字符
                textBox1.Text = temp.ToString() + ' ' + myoperator;
                textBox2.Text = "";
            }
            else
            {   //转换失败,重置所有
                Reset(sender, e);
            }
        }

        private void Equal(object sender, EventArgs e)
        {   // = 的Click事件,计算并显示
            double temp2;
            //尝试转换,失败则重置并返回
            if (!double.TryParse(textBox2.Text, out temp2)) { Reset(sender, e); return; }
            switch (myoperator)
            {
                case '+':
                    temp += temp2;
                    break;

                case '-':
                    temp -= temp2;
                    break;

                case '*':
                    temp *= temp2;
                    break;

                case '/':
                    temp /= temp2;
                    break;

                default:
                    break;
            }
            textBox1.Text = "";
            textBox2.Text = temp.ToString();
        }
    }
}
  1. Set the Click event of each Button
  • AddNum: Click event for 1~9 and decimal point
  • Reset: Click event of CE
  • Delete: the Click event of ←
  • Calculate : ±*/Click event
  • Equal := Click event
    insert image description here
  1. Start (F5)

Guess you like

Origin blog.csdn.net/weixin_46068322/article/details/114920031