SHARE
TWEET

Untitled

a guest Oct 21st, 2019 80 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package ru.compscicenter.java2019.calculator;
  2.  
  3. import javax.swing.*;
  4.  
  5. import static java.lang.Math.pow;
  6.  
  7. public class Calculator {
  8.  
  9.     public double count(String expression) {
  10.         expression = expression.toUpperCase();
  11.         StringBuilder noSpacesExpression = new StringBuilder();
  12.         for (int i = 0; i < expression.length(); ++i) {
  13.             if (expression.charAt(i) != ' ') {
  14.                 noSpacesExpression.append(expression.charAt(i));
  15.             }
  16.         }
  17.         expression = noSpacesExpression.toString();
  18.         return minusPlus(expression).answer;
  19.     }
  20.  
  21.     private Result minusPlus(String expression) {
  22.         Result current = mulDivDeg(expression);
  23.         while (current.lost.length() > 0) {
  24.             char sign = current.lost.charAt(0);
  25.             if (!(sign == '+' || sign == '-')) {
  26.                 break;
  27.             }
  28.             String nextExpression = current.lost.substring(1);
  29.             Result step = mulDivDeg(nextExpression);
  30.             if (sign == '+') {
  31.                 current.answer += step.answer;
  32.             } else {
  33.                 current.answer -= step.answer;
  34.             }
  35.             current.lost = step.lost;
  36.         }
  37.         return current;
  38.     }
  39.  
  40.     private Result mulDivDeg(String expression) {
  41.         Result current = brackets(expression);
  42.         while (current.lost.length() > 0) {
  43.             char sign = current.lost.charAt(0);
  44.             if (!(sign == '*' || sign == '/' || sign == '^')) {
  45.                 break;
  46.             }
  47.             String nextExpression = current.lost.substring(1);
  48.             Result step = brackets(nextExpression);
  49.             if (sign == '*') {
  50.                 current.answer *= step.answer;
  51.             } else if (sign == '/'){
  52.                 current.answer /= step.answer;
  53.             } else {
  54.                 current.answer = pow(current.answer, step.answer);
  55.             }
  56.             current.lost = step.lost;
  57.         }
  58.         return current;
  59.     }
  60.  
  61.     private Result brackets(String expression) {
  62.         if (expression.charAt(0) == '(') {
  63.             Result stepResult = minusPlus(expression.substring(1));
  64.             if (stepResult.lost.length() != 0 && stepResult.lost.charAt(0) == ')') {
  65.                 stepResult.lost = stepResult.lost.substring(1);
  66.             }
  67.             return stepResult;
  68.         }
  69.         return functions(expression);
  70.     }
  71.  
  72.     private Pair<Boolean, String> negativeExpression(String expression) {
  73.         boolean isNegative = false;
  74.         if (expression.charAt(0) == '-') {
  75.             isNegative = true;
  76.             expression = expression.substring(1);
  77.         } else if (expression.charAt(0) == '+') {
  78.             expression = expression.substring(1);
  79.         }
  80.         return new Pair<>(isNegative, expression);
  81.     }
  82.  
  83.     private Result functions(String expression) {
  84.         StringBuilder function = new StringBuilder();
  85.         int i = 0;
  86.         Pair<Boolean, String> negs = negativeExpression(expression);
  87.         boolean isNegative = negs.first;
  88.         expression = negs.second;
  89.         while (i < expression.length() && Character.isLetter(expression.charAt(i))) {
  90.             function.append(expression.charAt(i));
  91.             ++i;
  92.         }
  93.         if (function.length() > 0) {
  94.             if (i < expression.length() && expression.charAt(i) == '(') {
  95.                 Result result = brackets(expression.substring(i));
  96.                 result = makeFunction(function.toString(), result);
  97.                 if (isNegative) {
  98.                     result.answer *= -1;
  99.                 }
  100.                 return result;
  101.             }
  102.         }
  103.         if (isNegative) {
  104.             return getNumber("-" + expression);
  105.         } else {
  106.             return getNumber(expression);
  107.         }
  108.     }
  109.  
  110.     private Result getNumber(String expression) {
  111.         Pair<Boolean, String> negs = negativeExpression(expression);
  112.         boolean isNegative = negs.first;
  113.         expression = negs.second;
  114.         int i = 0;
  115.         boolean wasE = false;
  116.         while (i < expression.length() && (Character.isDigit(expression.charAt(i)) ||
  117.                 expression.charAt(i) == 'E' || expression.charAt(i) == '.' ||
  118.                 (wasE && expression.charAt(i) == '+'))) {
  119.             if (expression.charAt(i) == 'E') {
  120.                 wasE = true;
  121.             }
  122.             if (expression.charAt(i) == '+') {
  123.                 wasE = false;
  124.             }
  125.             ++i;
  126.         }
  127.         double doubleNumber = Double.valueOf(expression.substring(0, i));
  128.         if (isNegative) {
  129.             doubleNumber *= -1;
  130.         }
  131.         return new Result(doubleNumber, expression.substring(i));
  132.     }
  133.  
  134.     private Result makeFunction(String function, Result result) {
  135.         if (function.equals("SIN")) {
  136.             return new Result(Math.sin(Math.toRadians(result.answer)), result.lost);
  137.         } else if (function.equals("COS")) {
  138.             return new Result(Math.cos(Math.toRadians(result.answer)), result.lost);
  139.         } else {
  140.             return new Result(Math.abs(result.answer), result.lost);
  141.         }
  142.     }
  143.  
  144.     private class Result {
  145.  
  146.         double answer;
  147.         String lost;
  148.  
  149.         Result(double answer, String lost) {
  150.             this.answer = answer;
  151.             this.lost = lost;
  152.         }
  153.     }
  154.  
  155.     private class Pair<T, D> {
  156.         T first;
  157.         D second;
  158.  
  159.         Pair(T first, D second) {
  160.             this.first = first;
  161.             this.second = second;
  162.         }
  163.     }
  164.  
  165. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top