im试图创建一个递归方法来分离作为infix表达式的String,im使用2个队列和1个堆栈来使用Shunting-yard algorithm,问题是im混淆了将infix表达式与括号分开,例如例如:
3+(12+(8/2))-> 3+ |(12+ |(8/2)|)|
我已经有代码将表达式重写为后缀,然后对表达式求值,但是我想做一个递归方法,该方法首先重写括号表达式以表示优先级,而无需使用括号:
(8/2)---> 8/2 ---> 82 /
这是我的能力,但是我对递归方法非常困惑,非常感谢你们给我的任何帮助或建议:
public class infijoToPosfijo {
private static int preference(String op) {
int prf = 0;
if (op.equals("*") || op.equals("/") || op.equals("%")) {
prf = 2;
}
if (op.equals("+") || op.equals("-")) {
prf = 1;
}
return prf;
}
public String Recursive(String Recursive) {
IterableQueueListas<String> input= new IterableQueueListas<>();
IterableQueueListas<String> output = new IterableQueueListas<>();
IterableStacksListas<String> stack = new IterableStacksListas<>();
String infix="";
String separate[] = infix.split("(?=[-+*/%()])|(?<=[^-+*/%][-+*/])|(?<=[()])");
for (int i = 0; i < separate.length; i++) {
if (separate[i] == "(") {
int a = 0;
do {
if (separate[i].equals("(")) {
a++;
}
if (separate[i].equals(")")) {
a--;
}
input.enqueue(separate[i]);
} while (a != 0);
for (String item : input) {
Recursive = input.dequeue();
Recursive(Recursive);
}
input.enqueue(separate[i]);
}
for (String item : input) {
String s = input.dequeue();
if (!s.equals("+")) {
if (!s.equals("-")) {
if (!s.equals("*")) {
if (!s.equals("/")) {
if (!s.equals("%"))
output.enqueue(s);
}
}
}
}
if (s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/") || s.equals("%")) {
if (stack.isEmpty()) {
stack.push(s);
} else if (preference(s) > preference(stack.ver())) {
stack.push(s);
} else if (preference(s) <= preference(stack.ver())) {
String auxDos = stack.pop();
output.enqueue(auxDos);
stack.push(s);
}
}
}
for (String item : stack) {
output.enqueue(stack.pop());
}
input = output;
for (String item : input)
System.out.print(item + " ");
System.out.println();
// Evaluate infix expression
for (String item : output) {
String s = input.dequeue();
if (!s.equals("+")) {
if (!s.equals("-")) {
if (!s.equals("*")) {
if (!s.equals("/")) {
if (!s.equals("%")) {
stack.push(s);
System.out.println("Sending the " + s + " to the stack");
}
}
}
}
}
if (s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/") || s.equals("%")) {
String y = stack.pop();
String x = stack.pop();
System.out.println("Operation " + x + " y " + y);
if (s.equals("+")) {
System.out.println("Add the numbers");
int r = Integer.parseInt(x) + Integer.parseInt(y);
stack.push(String.valueOf(r));
}
if (s.equals("-")) {
System.out.println("Subtract the numbers");
int r = Integer.parseInt(x) - Integer.parseInt(y);
stack.push(String.valueOf(r));
}
if (s.equals("*")) {
System.out.println("Multiply the numbers");
int r = Integer.parseInt(x) * Integer.parseInt(y);
stack.push(String.valueOf(r));
}
if (s.equals("/")) {
System.out.println("se dividen los numeros");
int r = Integer.parseInt(x) / Integer.parseInt(y);
stack.push(String.valueOf(r));
}
if (s.equals("%")) {
System.out.println("Modulus between the numbers");
int r = Integer.parseInt(x) % Integer.parseInt(y);
stack.push(String.valueOf(r));
}
}
}
return stack.pop();}
return infix;
}