Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.Stack;
- public class type {
- public static void main(String args[]) throws NumberFormatException,
- IOException {
- String s;
- Stack<Character> st;
- // use scanner or use bufferedinputstream
- /* Scanner scanner = new Scanner(System.in); */
- BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
- int a = Integer.parseInt(in.readLine());
- while ((a--) > 0) {
- s = in.readLine();
- st = new Stack<Character>();
- for (int i = 0; i < s.length(); i++) {
- // use stack to solve problem
- if (s.charAt(i) == '(' || s.charAt(i) == ')')
- ;
- else if (s.charAt(i) == '+' || s.charAt(i) == '-'
- || s.charAt(i) == '*' || s.charAt(i) == '/'
- || s.charAt(i) == '^') {
- // pop stack and print to console
- if (!st.isEmpty())
- System.out.print(st.pop());
- st.push(s.charAt(i));
- } else if (s.charAt(i) >= 'a' && s.charAt(i) <= 'z') {
- // push s.charAt(i) to stack
- System.out.print(s.charAt(i));
- // System.out.print(st.pop());
- }
- }
- while (!st.isEmpty()) {
- System.out.print(st.pop());
- }
- // System.out.println(s);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment