Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* package whatever; // don't place package name! */
- import java.util.*;
- import java.lang.*;
- import java.io.*;
- /* Name of the class has to be "Main" only if the class is public. */
- class Ideone
- {
- public static void main (String[] args) throws java.lang.Exception
- {
- Scanner sc = new Scanner(System.in);
- String s = sc.nextLine();
- Stack<Character> st = new Stack();
- String ans = "";
- for(int i = 0 ; i < s.length() ; ++i){
- char cur = s.charAt(i);
- if(!isDelim(cur)){
- if(cur == '('){
- st.push(cur);
- }else if(isOperator(cur)){
- while(!st.empty() && getPriority(st.peek()) <= getPriority(cur)){
- ans += st.pop();
- }
- st.push(cur);
- }else if(cur == ')'){
- while(!st.empty() && st.peek() != '('){
- ans += st.pop();
- }
- if(!st.empty())st.pop();
- }else{
- ans += cur;
- }
- }
- }
- while(!st.empty())ans += st.pop();
- System.out.println(ans);
- }
- static boolean isDelim(char ch){
- return ch == ' ';
- }
- static boolean isOperator(char ch){
- return (ch == '*' || ch == '+' || ch == '.');
- }
- static int getPriority(char ch){
- switch(ch){
- case '*': return 1;
- case '+': return 2;
- case '.': return 2;
- case '(': return 3;
- default: return -1;
- }
- }
- }
- /*
- a.(a+b+c*)*.c*
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement