Sergei_Langin

Sergei Smart Calculator Stage 7/8 main for dying_coder

Apr 9th, 2020
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.42 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4. import java.io.*;
  5.  
  6. public class calculator2 {
  7.     public static void main(String[] args) throws Exception {
  8.  
  9.         LinkedHashMap<String, Long> map = new LinkedHashMap<>();
  10.         LinkedList<String> list2 = new LinkedList<>();
  11.         for (; ; ) {
  12.             BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));
  13.             String data = sc.readLine().replaceAll("", "")
  14.                     .replaceAll(" ", " ")
  15.                     .replaceAll(" {2}", "")
  16.                     .replaceAll(" {3}", "")
  17.                     .replaceAll(" {4}", "")
  18.                     .replaceAll("[+]{3}", "+")
  19.                     .replaceAll("[+]{5}", "+")
  20.                     .replaceAll("[-]{2}", "+")
  21.                     .replaceAll("[-]{3}", "-");
  22.  
  23.  
  24.             String[] expression = data.split(" ");
  25.             String[] expression4 = data.split("");
  26.             int count = 0;
  27.             int count2 = 0;
  28.             for (int i=0; i <expression4.length; i++) {
  29.                 if (expression4[i].matches("\\(")) {
  30.                     count++;
  31.                 }
  32.                 if (expression4[i].matches("\\)")) {
  33.                     count++;
  34.                 }
  35.             }
  36.             for (int i=0; i <expression.length; i++) {
  37.                 if (expression[i].matches("\\*\\*") |
  38.                         expression[i].matches("\\*\\*\\*") |
  39.                         expression[i].matches("//") |
  40.                         expression[i].matches("///")) {
  41.                     count2++;
  42.                 }
  43.             }
  44.  
  45.             if (count%2 != 0 | count2>=1) {
  46.                 System.out.println("Invalid Expression");
  47.                 continue;
  48.             }
  49.  
  50.             //String data2 = data.replaceAll("", "");
  51.             String[] blocks = data.split("=");
  52.             String[] blocks2 = data.split("");
  53.             Pattern p = Pattern.compile("[*+-]");
  54.             Matcher m = p.matcher(data);
  55.  
  56.             if (blocks2[0].isEmpty()) {
  57.                 continue;
  58.             } else if (blocks[0].equals("/command")) {
  59.                 System.out.println("Unknown command");
  60.             } else if (blocks[0].equals("/exit")) {
  61.                 System.out.println("Bye!");
  62.                 System.exit(0);
  63.             } else if (blocks2[0].equals("-")) {
  64.                 System.out.println(blocks2[0] + blocks2[1] + blocks2[2]);
  65.             } else if (data.matches("(.*)\\w+=\\d+(.*)")) {
  66.                 if (blocks.length == 2) {
  67.                     try {
  68.                         map.put(blocks[0], Long.parseLong(blocks[1]));
  69.                         list2.add(blocks[0]);
  70.                     } catch (Exception e) {
  71.                         System.out.println("Invalid assignment");
  72.                     }
  73.                 }
  74.             } else if (m.find()) {
  75.                 String[] expression2 = data.split("");
  76.                 List<String> list3 = Arrays.asList(expression2);
  77.                 LinkedList<String> list = new LinkedList<>(list3);
  78.                 if (map.containsKey(expression2[0])) {
  79.                     for (String s : list2) {
  80.                         for (int i = 0; i < list.size(); i++) {
  81.                             if (list.get(i).equals(s)) {
  82.                                 list.set(i, map.get(list.get(i)).toString());
  83.                             }
  84.                         }
  85.                     }
  86.                     String arr ="";
  87.                     for (String s : list) {
  88.                         arr += s;
  89.                     }
  90.                     PostFixConverter pc = new PostFixConverter(arr);
  91.                     PostFixCalculator calc = new PostFixCalculator(pc.getPostfixAsList());
  92.                     System.out.println(calc.result());
  93.                 } else if (expression[0].matches("[0-9]")) {
  94.                     String[] expression3 = data.split(" ");
  95.                     String arr2 ="";
  96.                             for (String s : expression3) {
  97.                                 arr2 += s;
  98.                             }
  99.                     PostFixConverter pc = new PostFixConverter(arr2);
  100.                     PostFixCalculator calc = new PostFixCalculator(pc.getPostfixAsList());
  101.                     System.out.println(calc.result());
  102.                 }
  103.             } else if (blocks.length == 1) {
  104.                 System.out.println(blocks[0]);
  105.             }
  106.         }
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment