利用栈补全左括号
package luyunzhou.one;
import java.util.Scanner;
import java.util.Stack;
public class Answer139 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Stack<String> val = new Stack<String>();
Stack<String> ops = new Stack<>();
Scanner scanner = new Scanner(System.in);
String s;
System.out.println("输入#结束");
do{
s = scanner.nextLine();
if(s.equals("+")||s.equals("")||s.equals("*")||s.equals("/")){
ops.push(s);
}else if(s.equals(")")){
String v = val.pop();
String op = ops.pop();
if(op.equals("+")||op.equals("")||op.equals("*")||op.equals("/")){
v = "(" + val.pop() + op + v + ")";
val.push(v);
}
}else if(s.equals("#")){
break;
}else {
val.push(s);
}
}while(true);
System.out.println(val.pop());
}
}