Advertisement
I_LIKE_COFFEE

RPN

Oct 31st, 2019
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.42 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.Scanner;
  3.  
  4. public class Main {
  5.  
  6.     public static class Calculator{
  7.  
  8.         public static String toRPN(String expr){//перевод в польскую нотацию
  9.             String current = "";
  10.             Stack<Character> stack = new Stack<>();
  11.             int priority;
  12.             for (int i = 0; i < expr.length(); i++){
  13.                 priority = getP(expr.charAt(i));
  14.                 if (priority == 0){
  15.                     current += expr.charAt(i);
  16.                 }
  17.                 if (priority == 1) stack.push(expr.charAt(i));
  18.                 if (priority > 1){
  19.                     current += ' ';
  20.  
  21.                     while (!stack.empty()){
  22.  
  23.                         if (getP(stack.peek()) >= priority) {
  24.                             current += stack.pop();
  25.                             current += ' ';
  26.  
  27.                         }
  28.                         else {
  29.                             break;
  30.                         }
  31.                     }
  32.  
  33.                     stack.push(expr.charAt(i));
  34.  
  35.                 }
  36.                 if (priority == -1){
  37.                     //current += ' ';
  38.                     while (getP(stack.peek()) != 1) {
  39.                         current += ' ';
  40.                         current += stack.pop();
  41.                     }
  42.                     stack.pop();
  43.                 }
  44.             }
  45.             current += ' ';
  46.             while (!stack.empty()) {
  47.                 current += stack.pop();
  48.                 current += ' ';
  49.             }
  50.  
  51.             return current;
  52.         }
  53.  
  54.  
  55.         public static int Answer(String rpn){//ответ
  56.             String operand = new String();
  57.             Stack<Integer> stack = new Stack<>();
  58.  
  59.             for (int i = 0; i < rpn.length(); i++){
  60.                 if (rpn.charAt(i) == ' ') continue;
  61.                 if (getP(rpn.charAt(i)) == 0){
  62.                     while (rpn.charAt(i) != ' ' && getP(rpn.charAt(i)) == 0){
  63.                         operand += rpn.charAt(i++);
  64.                         if (i == rpn.length()) break;
  65.                     }
  66.                     stack.push(Integer.parseInt(operand));
  67.                     operand = new String();
  68.                 }
  69.                 if (getP(rpn.charAt(i)) > 1){
  70.                     int a = stack.pop(), b = stack.pop();
  71.                     if (rpn.charAt(i) == '+') stack.push(b+a);
  72.                     if (rpn.charAt(i) == '-') stack.push(b-a);
  73.                     if (rpn.charAt(i) == '*') stack.push(b*a);
  74.                     if (rpn.charAt(i) == '/') stack.push(b/a);
  75.  
  76.                 }
  77.  
  78.             }
  79.  
  80.             return stack.pop();
  81.         }
  82.  
  83.  
  84.  
  85.  
  86.         private static int getP(char token){//приоритет операторов
  87.             if (token == '*' || token == '/') return 3;
  88.             else if (token == '+' || token == '-') return 2;
  89.             else if (token == '(') return 1;
  90.             else if (token == ')') return -1;
  91.             else return 0;
  92.         }
  93.  
  94.  
  95.  
  96.     }
  97.  
  98.  
  99.     public static void main(String[] args) {
  100.         Scanner sc = new Scanner(System.in);
  101.         String str = sc.nextLine();
  102.         System.out.println("Expression:");
  103.         System.out.println(str);
  104.         System.out.println("Reverse Polish Notation:");
  105.         System.out.println(Calculator.toRPN(str));
  106.         System.out.println("Result:");
  107.         System.out.println(Calculator.Answer(Calculator.toRPN(str)));
  108.     }
  109.  
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement