Advertisement
SIRAKOV4444

Untitled

Jun 2nd, 2020
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1.  
  2. import java.util.ArrayDeque;
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import java.util.Scanner;
  6.  
  7. public class Labb {
  8. public static void main(String[] args) {
  9.  
  10. Scanner scan = new Scanner(System.in);
  11. String[] input = scan.nextLine().split(" ");
  12. ArrayDeque<String> operators = new ArrayDeque<>();
  13. ArrayDeque<String> expression = new ArrayDeque<>();
  14. Map<String,Integer> priorites = new HashMap<>();
  15. priorites.put("*", 3);
  16. priorites.put("/", 3);
  17. priorites.put("+", 2);
  18. priorites.put("-", 2);
  19. priorites.put("(", 1);
  20.  
  21. for (String anInput : input) {
  22. try {
  23. double num = Double.parseDouble(anInput);
  24. expression.addLast(anInput);
  25. } catch (Exception e) {
  26. switch (anInput) {
  27. case "x":
  28. expression.addLast(anInput);
  29. break;
  30. case "(":
  31. operators.push(anInput);
  32. break;
  33. case ")":
  34. String symbol = operators.pop();
  35. while (!symbol.equals("(")) {
  36. expression.addLast(symbol);
  37. symbol = operators.pop();
  38. }
  39. break;
  40. default:
  41. while (!operators.isEmpty() && priorites.get(operators.peek()) >= priorites.get(anInput)) {
  42. expression.addLast(operators.pop());
  43. }
  44. operators.push(anInput);
  45. break;
  46. }
  47. }
  48. }
  49. while (!operators.isEmpty()) {
  50. expression.addLast(operators.pop());
  51. }
  52.  
  53. while (expression.size() > 0) {
  54. System.out.print(expression.pop()+" ");
  55. }
  56. }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement