Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.util.Scanner;
- public class ONP {
- static class Stack{
- //properties
- final int size=100;
- char[]s;
- int top;
- //Constructor
- public Stack()
- {
- s=new char[size];
- top=-1;
- }
- //method
- public boolean isEmpty()
- {
- if(top==-1) return true;
- else return false;
- }
- public void push(char c)
- {
- top+=1;
- s[top]=c;
- }
- public char pop()
- {
- if(!this.isEmpty())
- {
- return s[top--];
- }else {
- System.out.println("Stack underflow");
- return '0';
- }
- }
- public char peek() {
- if(!this.isEmpty()) return s[top];
- else {
- System.out.println("Stack underflow");
- return '1';
- }
- }
- }
- public static int priority(char c)
- {
- if(c=='('||c==')') return 0;
- else if(c=='+'||c=='-') return 1;
- else if(c=='*'||c=='/') return 2;
- else return 3;
- }
- public static void main(String[] args) throws FileNotFoundException {
- System.setIn(new FileInputStream("D:\\SVMC\\kpbl.txt"));
- Scanner sc=new Scanner(System.in);
- byte testCase=sc.nextByte();
- for(int tc=1;tc<=testCase;tc++)
- {
- String strInput=sc.next();
- Stack S=new Stack();
- for(int i=0;i<strInput.length();i++) {
- char tmpt=strInput.charAt(i);
- if(tmpt>=97&&tmpt<=122)
- {
- System.out.print(tmpt);
- }else {
- if(tmpt=='(') S.push(tmpt);
- else
- {
- if(tmpt==')')
- {
- while(S.peek()!='(')
- {
- System.out.print(S.pop());
- }
- S.pop();//remove '(' in stack
- }
- else
- {
- if(priority(S.peek())<=priority(tmpt)) S.push(tmpt);
- else S.pop();
- }
- }
- }
- }
- while(!S.isEmpty())
- {
- System.out.print(S.pop());
- }
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement