Example #1
0
        public double Calculate(ArrayList bArray)//86+9*4-8 ,{86,9,4,*,+,8}{-}
        {
            double no1, no2, ret;
            Stack  myStack = new Stack();

            myStack.Clear();
            string ch;

            for (int i = 0; i < bArray.Count; i++)
            {
                if (bArray[i] == null || Convert.ToString(bArray[i]) == "")
                {
                    bArray.RemoveAt(i);
                }
            }
            string[] aStr = new string[bArray.Count];
            for (int i = 0; i < bArray.Count; i++)
            {
                aStr[i] = Convert.ToString(bArray[i]);
                aStr[i] = aStr[i].Replace(" ", "");
                aStr[i] = aStr[i].ToUpper();
            }
            myStack.Clear();

            for (int i = 0; i < aStr.Length; i++)    //86+9*4-8 ,{86,9,4,*,+,8}{-}
            {
                ch = aStr[i];
                if (IsOperandStr(ch))    //如果是操作数,直接压入栈 122,8
                {
                    myStack.Push(ch);
                }
                else     //如果是操作符,就弹出两个数字来进行运算
                {
                    string str1 = Convert.ToString(myStack.Pop());
                    no1 = Convert.ToDouble(str1);
                    string str2 = Convert.ToString(myStack.Pop());
                    no2 = Convert.ToDouble(str2);

                    IOpperationFactory fac  = new MyFactory();
                    IMathOpperation    oper = fac.CreationFactory(no2, no1, ch);
                    ret = oper.GetResult();
                    myStack.Push(ret);    //将结果压入栈
                }
            }

            return(Convert.ToDouble(myStack.Pop()));//弹出最后的运算结果
        }
Example #2
0
        public IMathOpperation CreationFactory(double numA, double numB, string operation)
        {
            IMathOpperation Coco = null;

            switch (operation)
            {
            case "+":
                Coco = new AddOpperation();
                break;

            case "-":
                Coco = new SubtractionOpperation();
                break;

            case "*":
                Coco = new MultiplyOpperation();
                break;

            case "/":
                Coco = new DivideOpperation();
                break;
                //case "±":
                //       Coco = new AddandSubOpperation();
                //       break;
                //case "^x":
                //      Coco = new PowerOpperation();
                //      break;
                //case "sqrt":
                //      Coco = new SqrtOpperation();
                //      break;
                //case "log":
                //      Coco = new LogOpperation();
                //      break;
                //case "ln":
                //      Coco = new LnOpperation();
                //      break;
            }
            Coco.NumA = numA;
            Coco.NumB = numB;
            return(Coco);
        }