双栈实现 表达式求值

/*
 * 双栈算术表达式(未省略括号)求值算法
 * 用两个栈(一个用于保存运算符,一个用于保存操作数)
 * 1.将操作数压入操作数栈;
 * 2.将运算符压入运算符栈;
 * 3.忽略左括号;
 * 4.在遇到右括号时,弹出一个运算符,弹出所需数量的操作数,并将运算符和操作数的运算结果压入操作数栈;
*/

public class Evaluate {
	public static void main(String[] args) throws IOException {
		//String expression="((8*(3+2))/(2+8))";   
		String expression = "(1+((2+3)*(4*5)))";
		
		evaluate(expression);
	
	}

    public static void evaluate(String expression) {
    	Stack<Character> ops = new Stack<>();
        Stack<Double> vals = new Stack<>();
        for(int i = 0; i < expression.length(); i++) {
            char c = expression.charAt(i);
            if	   (c == '(')            ;
            else if(c == '+') 	ops.push(c);
            else if(c == '-') 	ops.push(c);
            else if(c == '*') 	ops.push(c);
            else if(c == '/') 	ops.push(c);
            else if(c == ')') {
            	/*如果字符是“(”,弹出运算符和操作数,计算结果并压入操作数栈*/
                char op = ops.pop();
                Double v = vals.pop();
                if(op == '+')	 v = vals.pop() + v;
                if(op == '-') 	 v = vals.pop() - v;
                if(op == '*')	 v = vals.pop() * v;
                if(op == '/')	 v = vals.pop() / v;
                vals.push(v);
            }
            else {
                /*如果字符既非运算符也非括号,将它作为double值压入操作数栈*/
                vals.push(Double.parseDouble(String.valueOf(c)));
            }
        }
        System.out.println(vals.pop());
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 C++ 语言实现双栈表达式求值代码: ```cpp #include <iostream> #include <stack> #include <string> using namespace std; int calculate(string expression) { stack<char> operator_stack; stack<int> operand_stack; unordered_map<char, int> precedence = {{'+', 1}, {'-', 1}, {'*', 2}, {'/', 2}}; auto evaluate = [&]() { char op = operator_stack.top(); operator_stack.pop(); int operand2 = operand_stack.top(); operand_stack.pop(); int operand1 = operand_stack.top(); operand_stack.pop(); int result; switch (op) { case '+': result = operand1 + operand2; break; case '-': result = operand1 - operand2; break; case '*': result = operand1 * operand2; break; case '/': result = operand1 / operand2; break; } operand_stack.push(result); }; for (char token : expression) { if (isdigit(token)) { operand_stack.push(token - '0'); } else if (token == '+' || token == '-' || token == '*' || token == '/') { while (!operator_stack.empty() && operator_stack.top() != '(' && precedence[token] <= precedence[operator_stack.top()]) { evaluate(); } operator_stack.push(token); } else if (token == '(') { operator_stack.push(token); } else if (token == ')') { while (operator_stack.top() != '(') { evaluate(); } operator_stack.pop(); } } while (!operator_stack.empty()) { evaluate(); } return operand_stack.top(); } int main() { string expression = "3*(4+5)-2/(7-3)"; int result = calculate(expression); cout << "The result of " << expression << " is " << result << endl; return 0; } ``` 这个程序使用了 STL 中的 stack 容器来实现操作符和操作数。我们还使用了一个无序映射 unordered_map 来存储不同操作符的优先级。在程序的主函数中,我们调用 calculate 函数来计算表达式的值。在 calculate 函数内部,我们定义了一个 lambda 表达式 evaluate,用于弹出操作符顶的操作符并弹出操作数顶的两个操作数,计算结果并将结果压入操作数。然后我们循环扫描表达式的每个字符,如果是数字,就将其压入操作数;如果是操作符,就比较其与操作符顶的运算符优先级,如果该运算符优先级低于顶运算符,则将顶运算符弹出并弹出操作数顶的两个操作数,计算结果并将结果压入操作数,直到该运算符优先级大于顶运算符或者为空时,将该运算符压入中;如果是左括号,就将其压入操作符;如果是右括号,就依次弹出操作符顶的操作符并弹出操作数顶的两个操作数,计算结果并将结果压入操作数,直到遇到左括号。最后,我们处理剩余的操作符,直到操作符为空,最终操作数中仅有一个数,即为表达式的值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值