Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package Bai7;
- import java.util.*;
- /**
- *
- * @author Vu Minh Duc
- */
- public class vidu {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- String s = in.nextLine();
- StackOfChars st = new StackOfChars(1000);
- try {
- for (int i = 0; i < s.length(); i++) {
- if (st.isEmpty() || s.charAt(i) != ')') {
- st.push(s.charAt(i));
- } else {
- while (!st.isEmpty() && st.peak() != '(') {
- int tmp1 = st.pop() - '0';
- char dau = st.pop();
- int tmp2 = st.pop() - '0';
- int res = 0;
- switch (dau) {
- case '+':
- res = tmp1 + tmp2;
- break;
- case '-':
- res = tmp2 - tmp1;
- break;
- case '*':
- res = tmp1 * tmp2;
- break;
- case '/':
- res = tmp2 / tmp1;
- break;
- }
- st.push((char) res);
- }
- }
- }
- } catch (ArrayIndexOutOfBoundsException e) {
- e.printStackTrace();
- }
- System.out.println(st.pop());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement