java莫名奇妙多了括号_用JAVA解决ACM剔除多余的括号

问题:键盘输入一个含有括号的四则运算表达式,可能含有多余的括号,编程整理该表达式,去掉所有的多余的括号,原表达式中所有变量和运算符相对位置保持不变,并保持与原表达式等价。

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 = "+-*/()";

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值