Beispiel #1
0
        static void Main(string[] args)
        {
            try
            {
                Console.Write("请输入数字A:");
                string strNumberA = Console.ReadLine();

                Console.Write("请选择运算符号(+、-、*、/):");
                string    strOperate = Console.ReadLine();
                Operation oper;
                oper = OperationFactory.createOperate(strOperate);

                Console.Write("请输入数字B:");
                string strNumberB = Console.ReadLine();
                string strResult  = "";

                oper.NumberA = Convert.ToDouble(strNumberA);
                oper.NumberB = Convert.ToDouble(strNumberB);
                strResult    = oper.GetResult().ToString();

                Console.WriteLine("结果是:" + strResult);
                //Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine("您的输入有错:" + ex.Message);
            }
        }
Beispiel #2
0
        private void button_Operation_Click(object sender, EventArgs e)
        {
            string strOperator;

            try
            {
                strOperator = textBox_Operator.Text.Trim();

                //if (strOperator != "+" && strOperator != "-" && strOperator != "*" && strOperator != "/")
                //   throw new Exception("操作符输入错误");

                Operation Opr = OperationFactory.CreateOperation(strOperator);

                Opr.NumberA = Convert.ToDouble(textBox_NumberA.Text.Trim());
                Opr.NumberB = Convert.ToDouble(textBox_NumberB.Text.Trim());

                textBox_Result.Text = Opr.GetResult().ToString();
            }

            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
                textBox_Result.Text = "";
                throw;
            }
            //finally
            //{
            //    MessageBox.Show("欢迎!");
            //}
        }
Beispiel #3
0
        /// <summary>
        /// 根据运算符和两个数字计算结果
        /// 调用简单工厂模式生成的类
        /// </summary>
        private void Evaluate()
        {
            Operation oper;

            //根据不同的对象生成不同的类,多态!
            switch (currentOperator)
            {
            case EnumOperator.None:
                break;

            //加
            case EnumOperator.Add:
                oper          = OperationFactory.createOpeate(EnumOperator.Add);
                oper.NumberA  = numSum;
                oper.NumnberB = currentValue;
                numSum        = oper.GetResult();
                break;

            //减
            case EnumOperator.Minus:
                oper          = OperationFactory.createOpeate(EnumOperator.Minus);
                oper.NumberA  = numSum;
                oper.NumnberB = currentValue;
                numSum        = oper.GetResult();
                break;

            //乘
            case EnumOperator.Multiply:
                oper          = OperationFactory.createOpeate(EnumOperator.Multiply);
                oper.NumberA  = numSum;
                oper.NumnberB = currentValue;
                numSum        = oper.GetResult();
                break;

            //除
            case EnumOperator.Divide:
                //判断结果
                if (currentValue != 0)
                {
                    oper          = OperationFactory.createOpeate(EnumOperator.Divide);
                    oper.NumberA  = numSum;
                    oper.NumnberB = currentValue;
                    numSum        = oper.GetResult();
                }
                else
                {
                    MessageBox.Show("除数不能为0哦,亲~", "出错了~");
                }
                break;
            }
            currentValue    = 0;
            currentOperator = EnumOperator.None;
        }
Beispiel #4
0
        private void button1_Click(object sender, EventArgs e)
        {
            //this.label1.Text = this.textBox1.Text + comboBox1.SelectedItem + this.textBox2.Text;
            String    str  = comboBox1.GetItemText(comboBox1.SelectedItem);
            Operation oper = OperationFactory.createOperation(str);

            try
            {
                oper.num1 = Convert.ToDouble(this.textBox1.Text);
                oper.num2 = Convert.ToDouble(this.textBox2.Text);
            }catch (Exception)
            {
                throw;
            }
            double result = oper.getResult();

            this.label1.Text = Convert.ToString("result =" + result);
        }
Beispiel #5
0
        private void button_Operation_Click(object sender, EventArgs e)
        {
            string strOperator;

            //try
            //{
            strOperator = textBox_Operator.Text.Trim();
            Operation Opr = OperationFactory.CreateOperation(strOperator);

            Opr.NumberA         = Convert.ToDouble(textBox_NumberA.Text.Trim());
            Opr.NumberB         = Convert.ToDouble(textBox_NumberB.Text.Trim());
            textBox_Result.Text = Opr.GetResult().ToString();
            //}
            //catch (System.Exception ex)
            //{
            //    MessageBox.Show(ex.Message);
            //    textBox_Result.Text = "";
            //}
        }
Beispiel #6
0
        static void Main(string[] args)
        {
            List <string> listopersign = new List <string>();

            listopersign.Add("+");
            listopersign.Add("-");
            listopersign.Add("*");
            listopersign.Add("/");
            OperationFactory fac = new OperationFactory();

            foreach (var item in listopersign)
            {
                Operation oper = fac.createOperate(item);
                oper.NumberA = 88.8f;
                oper.NumberB = 11.1f;
                double result = oper.GetResult();
                Console.WriteLine("{0:#.###} {1} {2:#.###} = {3:#.###}", oper.NumberA, item, oper.NumberB, result);
            }
            Console.Read();
        }
Beispiel #7
0
        private void btnResult_Click(object sender, EventArgs e)
        {
            try
            {
                string strNumberA = TextNum1.Text;

                string    strOperate = OperatorBox.Text;
                Operation oper;
                oper = OperationFactory.createOperate(strOperate);

                string strNumberB = TextNum2.Text;
                string strResult  = "";

                oper.NumberA = Convert.ToDouble(strNumberA);
                oper.NumberB = Convert.ToDouble(strNumberB);
                strResult    = oper.GetResult().ToString();

                TextResult.Text = strResult;
            }
            catch (Exception ex)
            {
                TextResult.Text = ex.Message;
            }
        }
Beispiel #8
0
        private void btnOperation_Click(object sender, EventArgs e)
        {
            Button    btn = sender as Button;
            Operation oper;

            switch (btn.Name)
            {
            case "btnAdd":
                equalStatus = false;
                multiDeal();
                calShow       += "+";
                txtResual.Text = calShow;
                setBtnEnableMethon(false);
                break;

            case "btnSub":
                equalStatus = false;
                multiDeal();
                calShow       += "-";
                txtResual.Text = calShow;
                setBtnEnableMethon(false);
                break;

            case "btnMul":
                equalStatus = false;
                multiDeal();
                calShow       += "*";
                txtResual.Text = calShow;
                setBtnEnableMethon(false);
                break;

            case "btnDiv":
                equalStatus = false;
                multiDeal();
                calShow       += "/";
                txtResual.Text = calShow;
                setBtnEnableMethon(false);
                break;

            case "btnEqual":
                equalStatus = true;
                multiDeal();
                break;

            case "btnPoint":
                if (equalStatus)    //等于号按过之后点数字则直接清除原来的结果
                {
                    equalStatus    = false;
                    calShow        = "";
                    resultDouble   = 0.0;
                    txtResual.Text = "";
                }
                oper           = OperationFactory.createOperation(".");
                calShow        = oper.addPoint(calShow);
                txtResual.Text = calShow;
                setBtnEnableMethon(true);
                break;

            case "btnDel":
                if (calShow.Length != 0)
                {
                    calShow = calShow.Substring(0, calShow.Length - 1);    //删除最后一个字符
                }
                txtResual.Text = calShow;
                setBtnEnableMethon(true);
                break;

            case "btnClear":
                calShow        = "";
                resultDouble   = 0;
                txtResual.Text = "";
                break;

            case "btnSquareRoot":
                multiDeal();    //先得出前面表达式的值
                oper           = OperationFactory.createOperation("SquareRoot");
                resultDouble   = oper.getResult();
                calShow        = resultDouble.ToString();
                txtResual.Text = calShow;
                break;

            case "btnPosAndNeg":
                multiDeal();    //先计算结果,再来加正负号
                oper           = OperationFactory.createOperation("+/-");
                calShow        = oper.posAndNeg(calShow);
                txtResual.Text = calShow;
                break;

            default:
                break;
            }
        }
Beispiel #9
0
        private void multiDeal()
        {
            try
            {
                //将前面的表达式的值计算出来
                Regex reg = new Regex(@"(\+|\-)?(\d+)(\.\d+)?(\+|\-|\*|\/)(\+|\-)?(\d+)(\.\d+)?");
                var   res = reg.Match(calShow).Groups;
                //res[0]    整个匹配的表达式
                //res[1]    第一个操作数的符号
                //res[2]    第一个操作数的整数部分
                //res[3]    第一个操作数的小数部分
                //res[4]    操作符
                //res[5]    第二个操作数的符号
                //res[6]    第二个操作数的整数部分
                //res[7]    第而个操作数的小数部分
                Operation oper;
                switch (res[4].ToString())
                {
                case "+":
                    oper         = OperationFactory.createOperation("+");
                    oper.NumberA = Convert.ToDouble(res[1].ToString() + res[2].ToString() + res[3].ToString());
                    oper.NumberB = Convert.ToDouble(res[5].ToString() + res[6].ToString() + res[7].ToString());
                    resultDouble = oper.getResult();
                    calShow      = resultDouble.ToString();
                    break;

                case "-":
                    oper         = OperationFactory.createOperation("-");
                    oper.NumberA = Convert.ToDouble(res[1].ToString() + res[2].ToString() + res[3].ToString());
                    oper.NumberB = Convert.ToDouble(res[5].ToString() + res[6].ToString() + res[7].ToString());
                    resultDouble = oper.getResult();
                    calShow      = resultDouble.ToString();
                    break;

                case "*":
                    oper         = OperationFactory.createOperation("*");
                    oper.NumberA = Convert.ToDouble(res[1].ToString() + res[2].ToString() + res[3].ToString());
                    oper.NumberB = Convert.ToDouble(res[5].ToString() + res[6].ToString() + res[7].ToString());
                    resultDouble = oper.getResult();
                    calShow      = resultDouble.ToString();
                    break;

                case "/":
                    oper         = OperationFactory.createOperation("/");
                    oper.NumberA = Convert.ToDouble(res[1].ToString() + res[2].ToString() + res[3].ToString());
                    oper.NumberB = Convert.ToDouble(res[5].ToString() + res[6].ToString() + res[7].ToString());
                    if (oper.NumberB == 0)
                    {
                        throw new Exception("除数不能为0!");
                    }
                    resultDouble = oper.getResult();
                    calShow      = resultDouble.ToString();
                    break;

                default:
                    break;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "警告");
            }
            txtResual.Text = calShow;
        }
Beispiel #10
0
 public Operation SetOperation(string type)
 {
     return(OperationFactory.CreateOperation(type));
 }