问题:键盘输入一个含有括号的四则运算表达式,可能含有多余的括号,编程整理该表达式,去掉所有的多余的括号,原表达式中所有变量和运算符相对位置保持不变,并保持与原表达式等价。
package main;
import java.util.Stack;
public class Main{
public static void main(String[] args){
String input = new String("a+(b*c+d)-e");
//String input = new String("((a))+b");
//String input = new String("((a))+b*c");
//String input = new String("(-a)+(-b)");
//String input = new String("(a+(b*c+d))*e");
Node n = new Main().proc(input);
new Main().print(n);
}
public Node proc(String input){
if (input.length() <= 0) {
return null;
}
if (input.length() == 1){
Node node = new Node();
node.c = input.charAt(0);
return node;
}
int i = 0;
Node op1 = null;
Node op2 = null;
Node first = null;
Node second = null;
Node third = null;
while (i < input.length()){
switch (input.charAt(i)){
case '(':{
i++;
Node n = proc(input.substring(i, i + rightBracket(input.substring(i))));
i += rightBracket(input.substring(i)) + 1;
if (first == null && op1!= null){//op1 一定是+-
n.parent = op1;
op1.left = n;
first = op1;
first.flag = true;
op1 = null;
}
if (first == null){
first = n;
} else if (second == null){
second = n;
} else {
third = n;
}
break;
}
case '+':
case '-':
case '*':
case '/':{
Node n = new Node();
n.c = input.charAt(i);
i++;
if(op1 == null){
op1 = n;
}else {
op2 = n;
}
break;
}
default:
if (ops.lastIndexOf(input.charAt(i)) == -1){
Node n = new Node();
n.c = input.charAt(i);
i++;
if (first == null && op1!= null){//op1 一定是+-
n.parent = op1;
op1.left = n;
first = op1;
first.flag = true;
op1 = null;
}
if (first == null){
first = n;
} else if (second == null){
second = n;
} else {
third = n;
}
}
}
if (op1 != null && op2 != null && first != null && second != null && third != null){
if ((op1.c == '*' || op1.c == '/')
|| (op2.c == '+' || op2.c == '-')){
first.parent = op1;
second.parent = op1;
op1.left = first;
op1.right = second;
first = op1;
op1 = op2;
second = third;
} else {
second.parent = op2;
third.parent = op2;
op2.left = second;
op2.right = third;
second = op2;
}
op2 = null;
third = null;
}
}
if (op1 != null && first != null && second != null){
first.parent = op1;
second.parent = op2;
op1.left = first;
op1.right = second;
return op1;
}
return first;
}
void print(Node root){
if (root == null){
return;
}
if (root.flag){
System.out.println(root.c);
root = root.left;
}
if (root.left != null){
if (root.c == '+' || root.c == '-'){
print(root.left);
} else if (root.c == '*' || root.c == '/'){
if (root.left.c == '*' || root.left.c == '/'){
print(root.left);
} else if (ops.lastIndexOf(root.left.c) == -1) {//只是一个数字
print(root.left);
} else {
System.out.println("(");
print(root.left);
System.out.println(")");
}
}
}
System.out.println(root.c);
if (root.right != null){
if (ops.lastIndexOf(root.right.c) == -1) {//只是一个数字
print(root.right);
} else if (root.c == '+') {
if (root.right.flag){
System.out.println("(");
print(root.right);
System.out.println(")");
} else {
print(root.right);
}
} else if (root.c == '-' || root.c == '*') {
if (root.right.c == '*' || root.right.c == '/'){
print(root.right);
} else {
System.out.println("(");
print(root.right);
System.out.println(")");
}
} else {// '/'
System.out.println("(");
print(root.right);
System.out.println(")");
}
}
}
private class Node{
public Character c;
public Node left = null;
public Node right = null;
public Node parent = null;
public boolean flag = false;//标记正负数
}
//查找右边匹配的括号,input为不包含第一个左括号的字符串
int rightBracket(String input){
Stack s = new Stack<>();
s.push(new Character('('));
for (int i=0; i
if (input.charAt(i) == '('){
s.push(new Character('('));
}
if (input.charAt(i) == ')'){
s.pop();
if (s.empty()){
return i;
}
}
}
return 0;
}
private String ops = "+-*/()";
}