C#编程实现计算器

 

 

Form1.cs 代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace calc
{
    public partial class Form1 : Form
    {
        Double total;  //计算的总值
        Double before;  //输入运算符号之前的数字
        Double dengBefore;  //单击等号之前取得文本框中的值
        String str;  //运算符号的类型
        Boolean isInputOperator = false;  //在输入数字之前是否输入了运算符
        Boolean useJian = false;  //用于减号中
        Boolean useCheng = false;  //用于乘号中
        Boolean useChu = false;  //用于除号中
        Boolean useDeng = false; //用于等号中
        Boolean useContinueClic = false; //用于连续单击运算符号
        Boolean useOne = false;  //用于一次运算结束
        Boolean useOne2 = false;  //用于一次运算结束
        Boolean useSpace = false; //用于退格


        public Form1()
        {
            InitializeComponent();
            btnpercent.Visible = false;
            btnDao.Visible = false;
            btnContrary.Visible = false;
            btn_sqrt.Visible = false;
            this.Height = 300;
        }


        //数字输入 把其它数字键绑定到零数字键上
        private void button0_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
           
            if (btn != null)
            {
                if (textBox1.Text != "除数不能为零" && textBox1.Text != "函数输入无效")
                {
                    useOne2 = true;
                    if (useOne == true && useOne2 == true)
                    {
                        clear();
                    }

                    if (isInputOperator == false)  //没有输入运算符号时
                    {
                        if (textBox1.Text == "0")
                        {
                            textBox1.Text = "";
                            textBox1.Text = textBox1.Text + btn.Text;
                          
                        }
                        else
                        {
                            textBox1.Text = textBox1.Text + btn.Text;
                        }
                    }
                    else  //输入运算符号时
                    {
                        isInputOperator = false;
                        textBox1.Text = "";
                        textBox1.Text = textBox1.Text + btn.Text;
                    }
                    useContinueClic = false;
                    useSpace = false;
                }
            }
        }

        //定义方法用于清空
        public void clear()
        {
            total = 0;
            before = 0;
            dengBefore = 0;
            isInputOperator = false;
            useJian = false;
            useCheng = false;
            useChu = false;
            useDeng = false;
            useContinueClic = false;
            useOne = false;
            useOne2 = false;
            useSpace = false;
            str = "";
            textBox1.Text = "0";

        }

        //单击清空
        private void btn_clear_Click(object sender, EventArgs e)
        {
            clear();
        }

        //单击点号
        private void btn_dot_Click(object sender, EventArgs e)
        {
            String[] s = textBox1.Text.Split('.');
            if (textBox1.Text != "除数不能为零" && textBox1.Text != "函数输入无效")
            {
                useOne2 = true;
                if (useOne == true && useOne2 == true)
                {
                    clear();
                }
                if (isInputOperator == false)
                {
                    if (s.Length != 2)
                    {
                        textBox1.Text = textBox1.Text + btn_dot.Text;
                    }
                }
                else
                {
                    isInputOperator = false;
                    textBox1.Text = "0";
                    textBox1.Text = textBox1.Text + btn_dot.Text;
                }
            }
        }

        //单击加号
        private void btn_plus_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "除数不能为零" && textBox1.Text != "函数输入无效" && textBox1.Text != "")
            {

                isInputOperator = true;

                if (str == "-" && useContinueClic == false)
                {
                    total = total - Convert.ToDouble(textBox1.Text);
                    textBox1.Text = total.ToString();
                }
                else if (str == "*" && useContinueClic == false)
                {
                    total = total * Convert.ToDouble(textBox1.Text);
                    textBox1.Text = total.ToString();
                }
                else if (str == "/" && useContinueClic == false)
                {
                    if (total != 0 && Convert.ToDouble(textBox1.Text) != 0)
                    {
                        total = total / Convert.ToDouble(textBox1.Text);
                        textBox1.Text = total.ToString();
                    }
                }
                else if (useContinueClic != true)
                {
                    before = Convert.ToDouble(textBox1.Text);
                    total = total + before;
                    textBox1.Text = total.ToString();
                }
                str = btn_plus.Text;
                useDeng = false;
                useContinueClic = true;
                useOne = false;
                useSpace = true;
            }
        }


        //单击减号
        private void btn_minus_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "除数不能为零" && textBox1.Text != "函数输入无效" && textBox1.Text != "")
            {
                isInputOperator = true;

                if (str == "+" && useContinueClic == false)
                {
                    total = total + Convert.ToDouble(textBox1.Text);
                    textBox1.Text = total.ToString();
                }
                else if (str == "*" && useContinueClic == false)
                {
                    total = total * Convert.ToDouble(textBox1.Text);
                    textBox1.Text = total.ToString();
                }
                else if (str == "/" && useContinueClic == false)
                {
                    if (total != 0 && Convert.ToDouble(textBox1.Text) != 0)
                    {
                        total = total / Convert.ToDouble(textBox1.Text);
                        textBox1.Text = total.ToString();
                    }
                }
                else if (useContinueClic != true)
                {
                    if (useJian == false)
                    {
                        textBox1.Text = Convert.ToDouble(textBox1.Text).ToString();
                        total = Convert.ToDouble(textBox1.Text);
                        useJian = true;
                    }
                    else
                    {
                        before = Convert.ToDouble(textBox1.Text);
                        total = total - before;
                        textBox1.Text = total.ToString();
                    }

                }
                str = btn_minus.Text;
                useDeng = false;
                useContinueClic = true;
                useOne = false;
                useSpace = true;
            }
        }

        //单击乘号
        private void btn_mul_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "除数不能为零" && textBox1.Text != "函数输入无效" && textBox1.Text != "")
            {
                isInputOperator = true;

                if (str == "+" && useContinueClic == false)
                {
                    total = total + Convert.ToDouble(textBox1.Text);
                    textBox1.Text = total.ToString();
                }
                else if (str == "-" && useContinueClic == false)
                {
                    total = total - Convert.ToDouble(textBox1.Text);
                    textBox1.Text = total.ToString();
                }
                else if (str == "/" && useContinueClic == false)
                {
                    if (total != 0 && Convert.ToDouble(textBox1.Text) != 0)
                    {
                        total = total / Convert.ToDouble(textBox1.Text);
                        textBox1.Text = total.ToString();
                    }
                }
                else if (useContinueClic != true)
                {
                    if (useCheng == false)
                    {
                        textBox1.Text = Convert.ToDouble(textBox1.Text).ToString();
                        total = Convert.ToDouble(textBox1.Text);
                        useCheng = true;
                    }
                    else
                    {
                        before = Convert.ToDouble(textBox1.Text);
                        total = total * before;
                        textBox1.Text = total.ToString();
                    }
                }
                str = btn_mul.Text;
                useDeng = false;
                useContinueClic = true;
                useOne = false;
                useSpace = true;
            }
        }

        //单击除号
        private void btn_div_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "除数不能为零" && textBox1.Text != "函数输入无效" && textBox1.Text != "")
            {
                isInputOperator = true;

                if (str == "+" && useContinueClic == false)
                {
                    total = total + Convert.ToDouble(textBox1.Text);
                    textBox1.Text = total.ToString();
                }
                else if (str == "-" && useContinueClic == false)
                {
                    total = total - Convert.ToDouble(textBox1.Text);
                    textBox1.Text = total.ToString();
                }
                else if (str == "*" && useContinueClic == false)
                {
                    total = total * Convert.ToDouble(textBox1.Text);
                    textBox1.Text = total.ToString();
                }
                else if (useContinueClic != true)
                {
                    if (useChu == false)
                    {
                        textBox1.Text = Convert.ToDouble(textBox1.Text).ToString();
                        total = Convert.ToDouble(textBox1.Text);
                        useChu = true;
                    }
                    else
                    {
                        before = Convert.ToDouble(textBox1.Text);
                        if (before == 0)
                            textBox1.Text = "除数不能为零";
                        else
                        {
                            total = total / before;
                            textBox1.Text = total.ToString();
                        }
                    }
                }
                str = btn_div.Text;
                useDeng = false;
                useContinueClic = true;
                useOne = false;
                useSpace = true;
            }
        }

        //单击等号
        private void btn_equ_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "除数不能为零" && textBox1.Text != "函数输入无效" && textBox1.Text != "")
            {
                if (useDeng == false)
                    dengBefore = Convert.ToDouble(textBox1.Text);
                isInputOperator = true;
                switch (str)
                {
                    case "+":
                        total = total + dengBefore;
                        textBox1.Text = total.ToString();
                        break;
                    case "-":
                        total = total - dengBefore;
                        textBox1.Text = total.ToString();
                        break;
                    case "*":
                        total = total * dengBefore;
                        textBox1.Text = total.ToString();
                        break;
                    case "/":
                        if (Convert.ToDouble(textBox1.Text) == 0)
                            textBox1.Text = "除数不能为零";
                        else
                        {
                            total = total / dengBefore;
                            textBox1.Text = total.ToString();
                        }
                        break;
                }
                useDeng = true;
                useContinueClic = true;
                useOne = true;
                useOne2 = false;
                useSpace = true;
            }
        }


        private void btn_sqrt_Click(object sender, EventArgs e)
        {
        //单击平方根
        {
            if (textBox1.Text != "除数不能为零" && textBox1.Text != "函数输入无效" )
            {
                if (Convert.ToDouble(textBox1.Text)<0)
                    textBox1.Text = "函数输入无效";
                else
                textBox1.Text = (Math.Sqrt(Convert.ToDouble(textBox1.Text))).ToString();
            }
            useSpace = true;
        }

        }

        //单击百分号
        private void btnpercent_Click(object sender, EventArgs e)
        {
            isInputOperator = true;
            if (textBox1.Text != "除数不能为零" && textBox1.Text != "函数输入无效")
                textBox1.Text = ((Convert.ToDouble(textBox1.Text)) / 100).ToString();
            useSpace = true;
        }

        //单击倒数
        private void btnDao_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "除数不能为零" && textBox1.Text != "函数输入无效")
            {
                if (Convert.ToDouble(textBox1.Text) != 0)
                    textBox1.Text = (1 / (Convert.ToDouble(textBox1.Text))).ToString();
                else
                    textBox1.Text = "除数不能为零";
            }
            useSpace = true;
        }

        //单击正负号
        private void btnContrary_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "除数不能为零" && textBox1.Text != "函数输入无效")
            {
                if (textBox1.Text.Length > 1 && (Convert.ToDouble(textBox1.Text)) == 0)
                {
                    if (textBox1.Text.IndexOf("-") < 0)
                        textBox1.Text = "-" + textBox1.Text;
                    else
                        textBox1.Text = textBox1.Text.Substring(1);
                }
                else
                    textBox1.Text = (Convert.ToDouble(textBox1.Text) * (-1)).ToString();
            }
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked == true)
            {
                btnpercent.Visible = false;
                btnDao.Visible = false;
                btnContrary.Visible = false;
                btn_sqrt.Visible = false;

            }
            else
            {
                this.Height = 300;
                btnpercent.Visible = true;
                btnDao.Visible = true;
                btnContrary.Visible = true;
                btn_sqrt.Visible = true;
               
            }
        }

        //单击退格
        private void btnSpace_Click(object sender, EventArgs e)
        {
            String[] s = textBox1.Text.Split('E');
            int k = s.Length;
            if (textBox1.Text != "除数不能为零" && textBox1.Text != "函数输入无效" && k != 2 && useSpace == false)
            {
                int i = textBox1.TextLength;
                if (i > 1)
                {
                    if (i > 2)
                    {
                        String str = textBox1.Text.Remove(i - 1);
                        textBox1.Text = str;
                    }
                    else
                    {
                        if (Convert.ToDouble(textBox1.Text) <= 0)
                        {
                            textBox1.Text = "0";
                        }
                        else
                        {
                            String str = textBox1.Text.Remove(i - 1);
                            textBox1.Text = str;
                        }
                    }
                }
                else if (i == 1)
                {
                    if (textBox1.Text != "0")
                        textBox1.Text = "0";
                }

            }


           
        }


    }
}

2.数字输入 把其它数字键绑定到零数字键上 实现方法,

Form1.Designer.cs 部分代码:

            // button0
            //
            this.button0.Location = new System.Drawing.Point(33, 188);
            this.button0.Name = "button0";
            this.button0.Size = new System.Drawing.Size(35, 25);
            this.button0.TabIndex = 1;
            this.button0.Text = "0";
            this.button0.UseVisualStyleBackColor = true;
            this.button0.Click += new System.EventHandler(this.button0_Click);

            // button4
            //
            this.button4.Location = new System.Drawing.Point(33, 157);
            this.button4.Name = "button4";
            this.button4.Size = new System.Drawing.Size(35, 25);
            this.button4.TabIndex = 5;
            this.button4.Text = "1";
            this.button4.UseVisualStyleBackColor = true;
            this.button4.Click += new System.EventHandler(this.button0_Click);
            //
            // button5
            //
            this.button5.Location = new System.Drawing.Point(91, 157);
            this.button5.Name = "button5";
            this.button5.Size = new System.Drawing.Size(35, 25);
            this.button5.TabIndex = 6;
            this.button5.Text = "2";
            this.button5.UseVisualStyleBackColor = true;
            this.button5.Click += new System.EventHandler(this.button0_Click);
            //
            // button6
            //
            this.button6.Location = new System.Drawing.Point(153, 157);
            this.button6.Name = "button6";
            this.button6.Size = new System.Drawing.Size(35, 25);
            this.button6.TabIndex = 7;
            this.button6.Text = "3";
            this.button6.UseVisualStyleBackColor = true;
            this.button6.Click += new System.EventHandler(this.button0_Click);
            //
            // btn_minus
            //
            this.btn_minus.Location = new System.Drawing.Point(205, 157);
            this.btn_minus.Name = "btn_minus";
            this.btn_minus.Size = new System.Drawing.Size(35, 25);
            this.btn_minus.TabIndex = 8;
            this.btn_minus.Text = "-";
            this.btn_minus.UseVisualStyleBackColor = true;
            this.btn_minus.Click += new System.EventHandler(this.btn_minus_Click);
            //
            // button8
            //
            this.button8.Location = new System.Drawing.Point(33, 126);
            this.button8.Name = "button8";
            this.button8.Size = new System.Drawing.Size(35, 25);
            this.button8.TabIndex = 9;
            this.button8.Text = "4";
            this.button8.UseVisualStyleBackColor = true;
            this.button8.Click += new System.EventHandler(this.button0_Click);
            //
            // button9
            //
            this.button9.Location = new System.Drawing.Point(91, 126);
            this.button9.Name = "button9";
            this.button9.Size = new System.Drawing.Size(35, 25);
            this.button9.TabIndex = 10;
            this.button9.Text = "5";
            this.button9.UseVisualStyleBackColor = true;
            this.button9.Click += new System.EventHandler(this.button0_Click);
            //
            // button10
            //
            this.button10.Location = new System.Drawing.Point(153, 126);
            this.button10.Name = "button10";
            this.button10.Size = new System.Drawing.Size(35, 25);
            this.button10.TabIndex = 11;
            this.button10.Text = "6";
            this.button10.UseVisualStyleBackColor = true;
            this.button10.Click += new System.EventHandler(this.button0_Click);

 

 

 


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值