Exemplo n.º 1
0
        // 进行运算
        private double Calculate(string operators, string numbers) {
            // 将数字和操作符字符串存到数组当中
            string[] numberarray = Regex.Split(numbers, " ");
            string[] operatorarray = Regex.Split(operators, " ");
            Stack numberStack = new Stack();// 数字栈
            Stack operatorStack = new Stack();// 操作符栈

            // 先将前两个数字和一个操作符push到栈中
            numberStack.Push(Convert.ToDouble(numberarray[0]));
            numberStack.Push(Convert.ToDouble(numberarray[1]));
            operatorStack.Push(operatorarray[0]);
            
            // 当遍历完操作符数组且栈中操作符为空时跳出循环
            for (int i = 1; i < operatorarray.Length || !operatorStack.IsEmpty();) {
                // 当操作符栈为空时,push一个数字和一个操作符
                if (operatorStack.IsEmpty()) {
                    numberStack.Push(Convert.ToDouble(numberarray[i + 1]));
                    operatorStack.Push(operatorarray[i]);
                    i++;continue;
                }
                // 当栈顶操作符为+或者-时
                if (operatorStack.getTop().Equals("+") || operatorStack.getTop().Equals("-")) {
                    if (i < operatorarray.Length) { // 当下一个操作符不是最后一个操作符时
                        // 如果下一个操作符为*或者/,则push一个数字和操作符
                        if (operatorarray[i].Equals("*") || operatorarray[i].Equals("/")) {
                            numberStack.Push(Convert.ToDouble(numberarray[i + 1]));
                            operatorStack.Push(operatorarray[i]);
                            i++;
                        } else {// 否则,栈内进行加减运算,将结果push到栈顶,下同
                            double after = Convert.ToDouble(numberStack.Pop());
                            double pre = Convert.ToDouble(numberStack.Pop());
                            string op = operatorStack.Pop().ToString();
                            switch (op) {
                                case "+": { numberStack.Push(pre + after); continue; }
                                case "-": { numberStack.Push(pre - after); continue; }
                                default: continue;
                            }
                        }
                    } else {// 如果没有下一个操作符,则栈内进行加减运算
                        double after = Convert.ToDouble(numberStack.Pop());
                        double pre = Convert.ToDouble(numberStack.Pop());
                        string op = operatorStack.Pop().ToString();
                        switch (op) {
                            case "+": { numberStack.Push(pre + after); continue; }
                            case "-": { numberStack.Push(pre - after); continue; }
                            default: continue;
                        }
                    }
                } else {// 如果栈顶操作符为*或者/,则栈内直接进行乘除运算
                    double after = Convert.ToDouble(numberStack.Pop());
                    double pre = Convert.ToDouble(numberStack.Pop());
                    string op = operatorStack.Pop().ToString();
                    switch (op) {
                        case "*": { numberStack.Push(pre * after); continue; }
                        case "/": { numberStack.Push(pre / after); continue; }
                        default: continue;
                    }
                }
            }
            // 取出栈顶元素作为结果返回
            object result = numberStack.Pop();
            return Convert.ToDouble(result);
        }