Advertisement
Manh_LK

Ký pháp Ba Lan ngược

Feb 19th, 2020
1,040
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.80 KB | None | 0 0
  1. import java.io.FileInputStream;
  2. import java.io.FileNotFoundException;
  3. import java.util.Scanner;
  4. public class ONP {
  5.  
  6.     static class Stack{
  7.         //properties
  8.         final int size=100;
  9.         char[]s;
  10.         int top;
  11.         //Constructor
  12.         public Stack()
  13.         {
  14.             s=new char[size];
  15.             top=-1;
  16.         }
  17.         //method
  18.         public boolean isEmpty()
  19.         {
  20.             if(top==-1) return true;
  21.             else return false;
  22.         }
  23.         public void push(char c)
  24.         {
  25.             top+=1;
  26.             s[top]=c;
  27.         }
  28.         public char pop()
  29.         {
  30.             if(!this.isEmpty())
  31.             {
  32.                 return s[top--];
  33.             }else {
  34.                 System.out.println("Stack underflow");
  35.                 return '0';
  36.             }
  37.         }
  38.         public char peek() {
  39.             if(!this.isEmpty()) return s[top];
  40.             else {
  41.                 System.out.println("Stack underflow");
  42.                 return '1';
  43.             }
  44.         }
  45.     }
  46.     public static int priority(char c)
  47.     {
  48.         if(c=='('||c==')') return 0;
  49.         else if(c=='+'||c=='-') return 1;
  50.         else if(c=='*'||c=='/') return 2;
  51.         else return 3;
  52.     }
  53.     public static void main(String[] args) throws FileNotFoundException {
  54.         System.setIn(new FileInputStream("D:\\SVMC\\kpbl.txt"));
  55.         Scanner sc=new Scanner(System.in);
  56.         byte testCase=sc.nextByte();
  57.         for(int tc=1;tc<=testCase;tc++)
  58.         {
  59.             String strInput=sc.next();
  60.             Stack S=new Stack();
  61.             for(int i=0;i<strInput.length();i++) {
  62.                 char tmpt=strInput.charAt(i);
  63.                 if(tmpt>=97&&tmpt<=122)
  64.                 {
  65.                     System.out.print(tmpt);
  66.                 }else {
  67.                     if(tmpt=='(') S.push(tmpt);
  68.                     else
  69.                     {
  70.                         if(tmpt==')')
  71.                         {
  72.                             while(S.peek()!='(')
  73.                             {
  74.                                 System.out.print(S.pop());
  75.                             }
  76.                             S.pop();//remove '(' in stack
  77.                         }
  78.                         else
  79.                         {
  80.                             if(priority(S.peek())<=priority(tmpt)) S.push(tmpt);
  81.                             else S.pop();
  82.                         }
  83.                     }
  84.                 }
  85.             }
  86.             while(!S.isEmpty())
  87.             {
  88.                 System.out.print(S.pop());
  89.             }
  90.             System.out.println();
  91.         }
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement