当前位置:编程学习 > C#/ASP.NET >>

如何能够使计算器连续运算加减乘除?下面的代码只能生成一个只能运算一次加减乘除的计算器

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page 
{
      public static int a, b, c, d;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    


    protected void  Button9_Click(object sender, EventArgs e)
{
    c =c*10+1;
    TextBox1.Text =c.ToString();


}

    protected void Button10_Click(object sender, EventArgs e)
    {
        c = c*10+2;
        TextBox1.Text = c.ToString();

    }
    protected void Button11_Click(object sender, EventArgs e)
    {
        c = c * 10 + 3;
        TextBox1.Text = c.ToString();

    }
    protected void Button5_Click(object sender, EventArgs e)
    {
        c = c * 10 + 4;
        TextBox1.Text = c.ToString();

    }
    protected void Button6_Click(object sender, EventArgs e)
    {
        c = c * 10 + 5;
        TextBox1.Text = c.ToString();

    }
    protected void Button7_Click(object sender, EventArgs e)
    {
        c = c * 10 + 6;
        TextBox1.Text = c.ToString();

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        c = c * 10 + 7;
        TextBox1.Text = c.ToString();
        

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        c = c * 10 +8;
        TextBox1.Text = c.ToString();


    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        c = c * 10 +9;
        TextBox1.Text = c.ToString();


    }
    protected void Button0_Click(object sender, EventArgs e)
    {
        c = c * 10 + 0;
        TextBox1.Text = c.ToString();

    }
    protected void Plus_Click(object sender, EventArgs e)
    {
        string str = ((Button)sender).Text;
        Session["s"] = TextBox1.Text;
        Session["str"] = str;
        TextBox1.Text = "";
    }


    protected void Button4_Click(object sender, EventArgs e)
    {

        a = c;
        b = 1;
       TextBox1.Text = "";
       c = 0;
       
    }
    protected void Button8_Click(object sender, EventArgs e)
    {
        a = c;
        b = 2;
        TextBox1.Text = "";
        c = 0;
      
    }
    protected void Button12_Click(object sender, EventArgs e)
    {
        a = c;
        b = 3;
        TextBox1.Text = "";
        c = 0;
    }
    protected void Button13_Click(object sender, EventArgs e)
    {
        a = c;
        b = 4;
        TextBox1.Text = "";
        c = 0;
       

    }
    protected void Button16_Click(object sender, EventArgs e)
    {
        switch (b)
        {
            case 1:
                d = a + dzcc;
                TextBox1.Text = d.ToString();
                break;
            case 2:
                d = a - c;
                TextBox1.Text = d.ToString();
                break;
            case 3:
                d = a  * c;
                TextBox1.Text = d.ToString();
                break;
            case 4:
                d = a / c;
                TextBox1.Text = d.ToString();
                break;
            case 0:
                TextBox1.Text = "对不起";
                break;
        }       
    }
    protected void Button15_Click(object sender, EventArgs e)
    {
        TextBox1.Text = null;
        a = 0; b = 0; c = 0; d = 0;
    }
}
--------------------编程问答-------------------- wuyq11(人生如梦) 给出的代码

public class Operation
    {
        private double _numberA = 0;
        private double _numberB = 0;
        public double NumberA
        {
            get
            {
                return _numberA;
            }
            set
            {
                _numberA = value;
            }
        }
        public double NumberB
        {
            get
            {
                return _numberB;
            }
            set
            {
                _numberB = value;
            }
        }
        public virtual double GetResult()
        {
            double result = 0;
            return result;
        }
        public static string checkNumberInput(string currentNumber, string inputString)
        {
            string result = "";
            if (inputString == ".")
            {
                if (currentNumber.IndexOf(".") < 0)
                {
                    if (currentNumber.Length == 0)
                        result = "0" + inputString;
                    else
                        result = currentNumber + inputString;
                }
            }
            else if (currentNumber == "0")
            {
                result = inputString;
            }
            else
            {
                result = currentNumber + inputString;
            }

            return result;
        }


    }
    class OperationAdd : Operation
    {
        public override double GetResult()
        {
            double result = 0;
            result = NumberA + NumberB;
            return result;
        }
    }
    class OperationSub : Operation
    {
        public override double GetResult()
        {
            double result = 0;
            result = NumberA - NumberB;
            return result;
        }
    }
    class OperationMul : Operation
    {
        public override double GetResult()
        {
            double result = 0;
            result = NumberA * NumberB;
            return result;
        }
    }
    class OperationDiv : Operation
    {
        public override double GetResult()
        {
            double result = 0;
            if (NumberB == 0)
                throw new Exception("除数不能为0。");
            result = NumberA / NumberB;
            return result;
        }
    }
    class OperationSqr : Operation
    {
        public override double GetResult()
        {
            double result = 0;
            result = NumberB * NumberB;
            return result;
        }
    }
    class OperationSqrt : Operation
    {
        public override double GetResult()
        {
            double result = 0;
            if (NumberB < 0)
                throw new Exception("负数不能开平方根。");
            result = Math.Sqrt(NumberB);
            return result;
        }
    }
    class OperationReverse : Operation
    {
        public override double GetResult()
        {
            double result = 0;
            result = -NumberB;
            return result;
        }
    }
public class OperationFactory
{
    public static Operation createOperate(string operate)
    {
        Operation oper = null;
        switch (operate)
        {
            case "+":
                {
                    oper = new OperationAdd();
                    break;
                }
            case "-":
                {
                    oper = new OperationSub();
                    break;
                }
            case "*":
                {
                    oper = new OperationMul();
                    break;
                }
            case "/":
                {
                    oper = new OperationDiv();
                    break;
                }
            case "sqr":
                {
                    oper = new OperationSqr();
                    break;
                }
            case "sqrt":
                {
                    oper = new OperationSqrt();
                    break;
                }
            case "+/-":
                {
                    oper = new OperationReverse();
                    break;
                }
        }

        return oper;
    }


  private void buttonAdd_Click(object sender, EventArgs e)
        {
            if (txtShow.Text != "")
            {
                oper = OperationFactory.createOperate(((Button)sender).Text);
               
                oper.NumberA = Convert.ToDouble(txtShow.Text);
               
                bOperate = true;
            }
        }
private void buttonEqual_Click(object sender, EventArgs e)
        {
            if (txtShow.Text != "")
            {
                if (((Button)sender).Text != "=")
                {
                    oper = OperationFactory.createOperate(((Button)sender).Text);
                }
                oper.NumberB = Convert.ToDouble(txtShow.Text);
                txtShow.Text = oper.GetResult().ToString();
                bOperate = true;
            }
        }
--------------------编程问答-------------------- 我是个初学的,我看不明白你的代码,但我很感谢你回答
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,