练习:WinForm 计算器

界面设计:

代码:

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
    {
        //存储上次点击的按钮(0:没点击,1:数字,2:运算符)
        private int prev = 0;

        //存储计算的中间结果
        private decimal zhong = 0;

        //记录上次点击的运算符
        private string prevYSF = "+";

        public Form1()
        {
            InitializeComponent();
        }

        //数字键的操作
        private void button27_Click(object sender, EventArgs e)
        {
            //将事件源转换成按钮
            Button btn = sender as Button;

            //替换(如果下面文本框为0或者上次点击了运算符)
            if (txtBottom.Text == "0" || prev == 2)
            {
                txtBottom.Text = btn.Text;
            }

            //追加
            else
            {
                txtBottom.Text += btn.Text;
            }

            //点击了数字
            prev = 1;
        }

        //运算符的操作
        private void button26_Click(object sender, EventArgs e)
        {
            //将事件源转换为按钮
            Button btn = sender as Button;

            //上次按了数字
            if (prev == 1)
            {
                txtTop.Text += txtBottom.Text + btn.Text;
                switch (prevYSF)
                {
                    case "+":
                        zhong = zhong + Convert.ToDecimal(txtBottom.Text);
                        txtBottom.Text = zhong.ToString();
                        break;
                    case "-":
                        zhong = zhong - Convert.ToDecimal(txtBottom.Text);
                        txtBottom.Text = zhong.ToString();
                        break;
                    case "*":
                        zhong = zhong * Convert.ToDecimal(txtBottom.Text);
                        txtBottom.Text = zhong.ToString();
                        break;
                    case "/":
                        if (txtBottom.Text == "0")
                        {
                            txtBottom.Text = "除数不能为0";
                        }
                        else
                        {
                            zhong = zhong / Convert.ToDecimal(txtBottom.Text);
                            txtBottom.Text = zhong.ToString();
                        }
                        break;
                } 
            }


            //上次按了运算符
            else
            {
                if (txtTop.Text == "")
                {
                    txtTop.Text = txtBottom.Text + btn.Text;
                }
                else
                {
                    txtTop.Text = txtTop.Text.Substring(0, txtTop.Text.Length - 1) + btn.Text;
                }
            }

            //点击运算符
            prev = 2;

            //记录运算符
            prevYSF = btn.Text;
        }

        //清空键的操作
        private void button8_Click(object sender, EventArgs e)
        {
            txtTop.Text = "";
            txtBottom.Text = "0";
            prev = 0;
            zhong = 0;
            prevYSF = "+";
        }

        //等号操作
        private void button28_Click(object sender, EventArgs e)
        {
            //将事件源转换为按钮
            Button btn = sender as Button;

            //上次按了数字
            if (prev == 1)
            {
                txtTop.Text += txtBottom.Text + btn.Text;
                switch (prevYSF)
                {
                    case "+":
                        zhong = zhong + Convert.ToDecimal(txtBottom.Text);
                        txtBottom.Text = zhong.ToString();
                        break;
                    case "-":
                        zhong = zhong - Convert.ToDecimal(txtBottom.Text);
                        txtBottom.Text = zhong.ToString();
                        break;
                    case "*":
                        zhong = zhong * Convert.ToDecimal(txtBottom.Text);
                        txtBottom.Text = zhong.ToString();
                        break;
                    case "/":
                         if (txtBottom.Text == "0")
                        {
                            txtBottom.Text = "除数不能为0";
                        }
                        else
                        {
                            zhong = zhong / Convert.ToDecimal(txtBottom.Text);
                            txtBottom.Text = zhong.ToString();
                        }
                        break;
                } 
            }
            //上次按了运算符
            else
            {
                txtTop.Text = txtTop.Text.Substring(0, txtTop.Text.Length - 1) + btn.Text;
            }

            //点击运算符
            prev = 2;

            //记录运算符
            prevYSF = btn.Text;

            txtTop.Text = "";
        }

    }
}

原文地址:https://www.cnblogs.com/xiao55/p/5805423.html