Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
4.3 kB
2
Indexable
Never
import java.io.*;
import java.util.*;

public class Main {

    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String exp = br.readLine();
        Solution ob = new Solution();
        ob.evaluate(exp);
    }
}

class Solution {

    static int opVal(Character ch) {
        if (ch == '+' || ch == '-') {
            return 1;
        } else if (ch == '*' || ch == '/') {
            return 2;
        } else {
            return -1;
        }
    }

    static int value(int n1, int n2, Character ch) {
        if (ch == '+') {
            return n1 + n2;
        } else if (ch == '-') {
            return n1 - n2;
        } else if (ch == '*') {
            return n1 * n2;
        } else {
            return n1 / n2;
        }
    }

    static String post(String exp) {
        Stack<String> num = new Stack<>();
        Stack<Character> op = new Stack<>();
        for (int i = 0; i < exp.length(); i++) {
            char ch = exp.charAt(i);
            if (ch == '(') {
                op.push(ch);
            } else if (Character.isDigit(ch)) {
                num.push(ch + "");
            } else if (ch == ')') {
                while (op.size() != 0 && op.peek() != '(') {
                    String b = num.pop();
                    String a = num.pop();
                    String ans = a + b + op.pop();
                    num.push(ans);
                }
                op.pop();
            } else {
                while (op.size() != 0 && opVal(ch) <= opVal(op.peek())) {
                    String b = num.pop();
                    String a = num.pop();
                    String ans = a + b + op.pop();
                    num.push(ans);
                }
                op.push(ch);
            }
        }
        while (op.size() != 0) {
            String b = num.pop();
            String a = num.pop();
            String ans = a + b + op.pop();
            num.push(ans);
        }
        String ans = num.pop();
        return ans;
    }

    static String pre(String exp) {
        Stack<String> num = new Stack<>();
        Stack<Character> op = new Stack<>();
        for (int i = 0; i < exp.length(); i++) {
            char ch = exp.charAt(i);
            if (ch == '(') {
                op.push(ch);
            } else if (Character.isDigit(ch)) {
                num.push(ch + "");
            } else if (ch == ')') {
                while (op.size() != 0 && op.peek() != '(') {
                    String b = num.pop();
                    String a = num.pop();
                    String ans = op.pop() + a + b;
                    num.push(ans);
                }
                op.pop();
            } else {
                while (op.size() != 0 && opVal(ch) <= opVal(op.peek())) {
                    String b = num.pop();
                    String a = num.pop();
                    String ans = op.pop() + a + b;
                    num.push(ans);
                }
                op.push(ch);
            }
        }
        while (op.size() != 0) {
            String b = num.pop();
            String a = num.pop();
            String ans = op.pop() + a + b;
            num.push(ans);
        }
        String ans = num.pop();
        return ans;
    }

public void evaluate(String exp){

Stack<Integer> num = new Stack<>();
Stack<Character> op = new Stack<>();

for(int i = 0 ; i < exp.length() ; i++){
char ch = exp.charAt(i);
if(ch == '('){
op.push(ch);
}
if(Character.isDigit(ch)){
int num1 = ch - '0';
num.push(num1);
}
if(ch == '+' || ch == '-' || ch == '*' || ch == '/'){
while(op.size() != 0 && op.peek() != '(' && opVal(ch) <= opVal(op.peek())){
int n2 = num.pop();
int n1 = num.pop();
char ch1 = op.pop();
int val = value(n1 , n2 , ch1);
num.push(val);
}
op.push(ch);
}
if(ch == ')'){
while(op.size() != 0 && op.peek() != '('){
int n2 = num.pop();
int n1 = num.pop();
char ch1 = op.pop();
int val = value(n1 , n2 , ch1);
num.push(val);
}
op.pop();
}

}
while(op.size() != 0){
int n2 = num.pop();
int n1 = num.pop();
char ch1 = op.pop();
int val = value(n1 , n2 , ch1);
num.push(val);
}
System.out.println(num.pop());
System.out.println(post(exp));
System.out.println(pre(exp));

return;
}
}