注册 登录
编程论坛 C# 论坛

计算器怎么进行二次计算

即使你在 发布于 2015-12-22 12:54, 3850 次点击
        private string a=null;
        private string b=null;
        private double c=0;
        private string sign=null;
        private bool dian=false;
        

        public Form1()
        {
            InitializeComponent();
        }
        //跟按钮的单击事件关联的方法
        private void button3_Click(object sender, EventArgs e)
        {
            if (a != null && a != "0")
            {
                a = a + "0";
            }
            else if (a == null)
            {
                a = "0";
            }
            textBox1.Text = a;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (a != null && a != "0")
            {
                a = a + "1";
            }
            else if (a == null)
            {
                a = "1";
            }
            textBox1.Text = a;
        }

        private void button11_Click(object sender, EventArgs e)
        {
            if (dian == false)
            {
                dian = true;
                if (a != null)
                {
                    a = a + ".";
                }
                else
                {
                    a = "0.";
                }
                textBox1.Text = a;
            }
        }

        private void button13_Click(object sender, EventArgs e)
        {
            if (sign == null)
            {
                sign = "+";
                b = a;
                a = null;
                dian = false;
            }
        }

        private void button12_Click(object sender, EventArgs e)
        {
            if(sign =="+")
            {
                c = Convert.ToDouble(a) + Convert.ToDouble(b);
            }
            if(sign =="-")
            {
                c = Convert.ToDouble(b) - Convert.ToDouble(a);
            }
            if (sign == "*")
            {
                c = Convert.ToDouble(a) * Convert.ToDouble(b);
            }
            if (sign == "/")
            {
                c = Convert.ToDouble(b) / Convert.ToDouble(a);
            }
            textBox1.Text = Convert.ToString(c);
        }

        private void button9_Click(object sender, EventArgs e)
        {
            if (a != null && a != "0")
            {
                a = a + "2";
            }
            else if (a == null)
            {
                a = "2";
            }
            textBox1.Text = a;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (a != null && a != "0")
            {
                a = a + "3";
            }
            else if (a == null)
            {
                a = "3";
            }
            textBox1.Text = a;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (a != null && a != "0")
            {
                a = a + "4";
            }
            else if (a == null)
            {
                a = "4";
            }
            textBox1.Text = a;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (a != null && a != "0")
            {
                a = a + "5";
            }
            else if (a == null)
            {
                a = "5";
            }
            textBox1.Text = a;
        }

        private void button10_Click(object sender, EventArgs e)
        {
            if (a != null && a != "0")
            {
                a = a + "6";
            }
            else if (a == null)
            {
                a = "6";
            }
            textBox1.Text = a;
        }

        private void button7_Click(object sender, EventArgs e)
        {
            if (a != null && a != "0")
            {
                a = a + "7";
            }
            else if (a == null)
            {
                a = "7";
            }
            textBox1.Text = a;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (a != null && a != "0")
            {
                a = a + "8";
            }
            else if (a == null)
            {
                a = "8";
            }
            textBox1.Text = a;
        }

        private void button8_Click(object sender, EventArgs e)
        {
            if (a != null && a != "0")
            {
                a = a + "9";
            }
            else if (a == null)
            {
                a = "9";
            }
            textBox1.Text = a;
        }

        private void button14_Click(object sender, EventArgs e)
        {
            if (sign == null)
            {
                sign = "-";
                b = a;
                a = null;
                dian = false;
            }
        }

        private void button15_Click(object sender, EventArgs e)
        {
            if (sign == null)
            {
                sign = "*";
                b = a;
                a = null;
                dian = false;
            }
        }

        private void button16_Click(object sender, EventArgs e)
        {
            if (sign == null)
            {
                sign = "/";
                b = a;
                a = null;
                dian = false;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button17_Click(object sender, EventArgs e)
        {
            textBox1.Text = "0";
            c = 0;
            a = null;
            b = null;
            dian = false;
            sign = null;
        }
6 回复
#2
afdoa832015-12-22 13:22
假设只做加法
按钮13应该是加号,按钮12应该是等于把。
1.当第一次按等于计算后,没有初始化sign,在按加号时由于sign没有初始化,按钮13不执行。
2.当第一次按等于计算后,a的赋值不是和。不注意这里会结果错误。
#3
即使你在2015-12-22 14:00
回复 2楼 afdoa83
我还是搞不懂能不能帮我弄一下
只有本站会员才能查看附件,请 登录
#4
wp2319572015-12-22 14:22
回复 3楼 即使你在
你这个例子能进行二次计算啊

我不太明白你想做什么  或者你的二次计算是什么意思
#5
即使你在2015-12-22 14:37
回复 4楼 wp231957
就是覆盖前面的结果,进行下一次计算
#6
wp2319572015-12-22 14:38
不是你自己的代码吗    点 CE  按钮就清除了啊
#7
即使你在2015-12-22 14:41
回复 6楼 wp231957
这个我知道,我的意思是不要用CE这个键,像我们平常计算器可以覆盖,可以叠加
1