java 逆波兰,Java的RPN(逆波兰式)中缀到后缀

I am pretty sure, that stacks are used for building PRN and '(' are ignored, but it does not seem to be the case. For example:

input 1: 52+(1+2)*4-3

input 2: 52+((1+2)*4)-3

input 3: (52+1+2)*4-3

Input 1 and input 2 output should be the same, and input 1 and input 3 should differ.

output 1: 52 1 2 + 4 3 - * +

output 2: 52 1 2 + 4 * 3 - +

output 3: 52 1 2 + 4 3 - * +

public static String Infix2(String input) {

char[] in = input.toCharArray();

Stack stack = new Stack();

StringBuilder out = new StringBuilder();

for (int i = 0; i < in.length; i++)

switch (in[i]) {

case '+':

case '*':

case '-':

out.append(' ');

stack.push(in[i]);

break;

case ' ':

case '(':

break;

case ')':

out.append(' ');

out.append(stack.pop());

break;

default:

out.append(in[i]);

break;

}

while (!stack.isEmpty()) {

out.append(' ');

out.append(stack.pop());

}

return out.toString();

}

Assuming that i want input 1 and 3 also to work, what approach should i use?

edit:

After the changes '+','-','*' and '/' worked for given inputs.

public static String Infix2(String input) {

if (input == null)

return "";

char[] in = input.toCharArray();

Stack stack = new Stack();

StringBuilder out = new StringBuilder();

for (int i = 0; i < in.length; i++)

switch (in[i]) {

case '+':

case '-':

while (!stack.empty()

&& (stack.peek() == '*' || stack.peek() == '/'))

out.append(' ').append(stack.pop());

case '*':

case '/':

out.append(' ');

case '(':

stack.push(in[i]);

case ' ':

break;

case ')':

while (!stack.empty() && stack.peek() != '(')

out.append(' ').append(stack.pop());

if (!stack.empty())

stack.pop();

break;

default:

out.append(in[i]);

break;

}

while (!stack.isEmpty())

out.append(' ').append(stack.pop());

return out.toString();

}

解决方案

The algorithm is pretty simple (and here is a good explanation). Every operation has a binding weight, with + and - being the lowest. There are two rules:

print out numbers immediately

never put a lighter item on a heavier item

left parentheses go on the stack

right parentheses pop off the stack until you hit a left parentheses, and then remove the left parentheses

Given your first example, 52+(1+2)*4-3, here is the stack:

52+ => +

52+( => + (

52+(1+ => + ( +

52+(1+2) => + //right parentheses popped +

52+(1+2)*4 => + *

52+(1+2)*4-3 => + - //can't put - on top of *, so pop off *

... and then pop the stack until it's empty.

Replacing your switch loop with the following (closest analog to what you had) will give correct answers for your three examples. In a real parser you would give each operator a weight and generalize the pop mechanism.

for (int i = 0; i < in.length; i++)

switch (in[i]) {

case '+':

case '-':

while (!stack.empty() && (stack.peek() == '*' || stack.peek() == '/')) {

out.append(' ');

out.append(stack.pop());

}

out.append(' ');

stack.push(in[i]);

break;

case '*':

case '/':

out.append(' ');

stack.push(in[i]);

break;

case '(':

stack.push(in[i]);

break;

case ')':

while (!stack.empty() && stack.peek() != '(') {

out.append(' ');

out.append(stack.pop());

}

stack.pop();

break;

default:

out.append(in[i]);

break;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值