Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.72 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package Bai7;
  7.  
  8. import java.util.*;
  9.  
  10. /**
  11.  *
  12.  * @author Vu Minh Duc
  13.  */
  14. public class vidu {
  15.  
  16.     public static void main(String[] args) {
  17.         Scanner in = new Scanner(System.in);
  18.         String s = in.nextLine();
  19.         StackOfChars st = new StackOfChars(1000);
  20.         try {
  21.             for (int i = 0; i < s.length(); i++) {
  22.                 if (st.isEmpty() || s.charAt(i) != ')') {
  23.                     st.push(s.charAt(i));
  24.                 } else {
  25.                     while (!st.isEmpty() && st.peak() != '(') {
  26.                         int tmp1 = st.pop() - '0';
  27.                         char dau = st.pop();
  28.                         int tmp2 = st.pop() - '0';
  29.                         int res = 0;
  30.                         switch (dau) {
  31.                             case '+':
  32.                                 res = tmp1 + tmp2;
  33.                                 break;
  34.                             case '-':
  35.                                 res = tmp2 - tmp1;
  36.                                 break;
  37.                             case '*':
  38.                                 res = tmp1 * tmp2;
  39.                                 break;
  40.                             case '/':
  41.                                 res = tmp2 / tmp1;
  42.                                 break;
  43.                         }
  44.                         st.push((char) res);
  45.                     }
  46.                 }
  47.             }
  48.         } catch (ArrayIndexOutOfBoundsException e) {
  49.             e.printStackTrace();
  50.         }
  51.         System.out.println(st.pop());
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement