基本信息
源码名称:C# WindowsForms简单程序;数学计算器
源码大小:0.16M
文件格式:.rar
开发语言:C#
更新时间:2020-01-13
   源码介绍

数学计算器有很多种实现方式,这是其中比较笨的一种,仅供参考。

using System;
using System.Windows.Forms;

namespace WindowsFormsApp9
{
    public partial class Form1 : Form
    {
        public double x = 0;
        public double y = 0;
        public double jieguo = 0;
        public char fuhao = '0';
        public bool ky = false;
        public Form1()
        {
            InitializeComponent();
        }
        public void yunshuan()
        {
            switch (fuhao)
            {
                case ' ':
                    {
                        jieguo = x y;
                    }
                    break;
                case '-':
                    {
                        jieguo = x - y;
                    }
                    break;
                case '*':
                    {
                        jieguo = x * y;
                    }
                    break;
                case '/':
                    {
                        jieguo = x / y;
                    }
                    break;
                default:
                    break;
            }
        }
        private void buttonDEN_Click(object sender, EventArgs e)
        {
            ky = false;
            x = jieguo;
            this.textBox1.Text = Convert.ToString(jieguo);
        }
        private void buttonJIA_Click(object sender, EventArgs e)
        {
            if(ky==true)
            {
                ky = false;
                x = jieguo;
            }
            fuhao = ' ';
            ky = true;
            this.textBox1.Text = "0";
        }

        private void buttonJIAN_Click(object sender, EventArgs e)
        {
            if (ky == true)
            {
                ky = false;
                x = jieguo;
            }
            fuhao = '-';
            ky = true;
            this.textBox1.Text = "0";
        }

        private void buttonCHENG_Click(object sender, EventArgs e)
        {
            if (ky == true)
            {
                ky = false;
                x = jieguo;
            }
            fuhao = '*';
            ky = true;
            this.textBox1.Text = "0";
        }

        private void buttonCHU_Click(object sender, EventArgs e)
        {
            if (ky == true)
            {
                ky = false;
                x = jieguo;
            }
            fuhao = '/';
            ky = true;
            this.textBox1.Text = "0";
        }
        private void button0_Click(object sender, EventArgs e)
        {
                if (this.textBox1.Text == "0")
                {
                    this.textBox1.Text = "0";
                }
                else if (this.textBox1.Text.Length < 9)
                {
                    this.textBox1.Text = "0";
                }
                if (ky == false)
                {
                    x = Convert.ToDouble(this.textBox1.Text);
                }
                else if (ky == true)
                {
                    y = Convert.ToDouble(this.textBox1.Text);
                    yunshuan();
                }

        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (this.textBox1.Text == "0")
            {
                this.textBox1.Text = "1";
            }
            else if(this.textBox1.Text.Length < 9)
            {
                this.textBox1.Text = "1";
            }
            if (ky == false)
            {
                x = Convert.ToDouble(this.textBox1.Text);
            }
            else if (ky == true)
            {
                y = Convert.ToDouble(this.textBox1.Text);
                yunshuan();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (this.textBox1.Text == "0")
            {
                this.textBox1.Text = "2";
            }
            else if (this.textBox1.Text.Length < 9)
            {
                this.textBox1.Text = "2";
            }
            if (ky == false)
            {
                x = Convert.ToDouble(this.textBox1.Text);
            }
            else if (ky == true)
            {
                y = Convert.ToDouble(this.textBox1.Text);
                yunshuan();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (this.textBox1.Text == "0")
            {
                this.textBox1.Text = "3";
            }
            else if (this.textBox1.Text.Length < 9)
            {
                this.textBox1.Text = "3";
            }
            if (ky == false)
            {
                x = Convert.ToDouble(this.textBox1.Text);
            }
            else if (ky == true)
            {
                y = Convert.ToDouble(this.textBox1.Text);
                yunshuan();
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (this.textBox1.Text == "0")
            {
                this.textBox1.Text = "4";
            }
            else if (this.textBox1.Text.Length < 9)
            {
                this.textBox1.Text = "4";
            }
            if (ky == false)
            {
                x = Convert.ToDouble(this.textBox1.Text);
            }
            else if (ky == true)
            {
                y = Convert.ToDouble(this.textBox1.Text);
                yunshuan();
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (this.textBox1.Text == "0")
            {
                this.textBox1.Text = "5";
            }
            else if (this.textBox1.Text.Length < 9)
            {
                this.textBox1.Text = "5";
            }
            if (ky == false)
            {
                x = Convert.ToDouble(this.textBox1.Text);
            }
            else if (ky == true)
            {
                y = Convert.ToDouble(this.textBox1.Text);
                yunshuan();
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (this.textBox1.Text == "0")
            {
                this.textBox1.Text = "6";
            }
            else if (this.textBox1.Text.Length < 9)
            {
                this.textBox1.Text = "6";
            }
            if (ky == false)
            {
                x = Convert.ToDouble(this.textBox1.Text);
            }
            else if (ky == true)
            {
                y = Convert.ToDouble(this.textBox1.Text);
                yunshuan();
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            if (this.textBox1.Text == "0")
            {
                this.textBox1.Text = "7";
            }
            else if (this.textBox1.Text.Length < 9)
            {
                this.textBox1.Text = "7";
            }
            if (ky == false)
            {
                x = Convert.ToDouble(this.textBox1.Text);
            }
            else if (ky == true)
            {
                y = Convert.ToDouble(this.textBox1.Text);
                yunshuan();
            }
        }

        private void button8_Click(object sender, EventArgs e)
        {
            if (this.textBox1.Text == "0")
            {
                this.textBox1.Text = "8";
            }
            else if (this.textBox1.Text.Length < 9)
            {
                this.textBox1.Text = "8";
            }
            if (ky == false)
            {
                x = Convert.ToDouble(this.textBox1.Text);
            }
            else if (ky == true)
            {
                y = Convert.ToDouble(this.textBox1.Text);
                yunshuan();
            }
        }

        private void button9_Click(object sender, EventArgs e)
        {
            if (this.textBox1.Text == "0")
            {
                this.textBox1.Text = "9";
            }
            else if (this.textBox1.Text.Length < 9)
            {
                this.textBox1.Text = "9";
            }
            if (ky == false)
            {
                x = Convert.ToDouble(this.textBox1.Text);
            }
            else if (ky == true)
            {
                y = Convert.ToDouble(this.textBox1.Text);
                yunshuan();
            }
        }
        private void buttonDIAN_Click(object sender, EventArgs e)
        {
            string str = ".";
            if (this.textBox1.Text.Contains(str))
            {
                return;
            }
            else if (this.textBox1.Text.Length < 8)
            {
                this.textBox1.Text = ".";
            }
            if (ky == false)
            {
                x = Convert.ToDouble(this.textBox1.Text);
            }
            else if (ky == true)
            {
                y = Convert.ToDouble(this.textBox1.Text);
                yunshuan();
            }
        }

        private void buttonRESET_Click(object sender, EventArgs e)
        {
            x = 0;
            y = 0;
            jieguo = 0;
            fuhao = '0';
            ky = false;
            this.textBox1.Text = "0";
        }
    }
}