Advertisement
tombenko

Calculator.java

Jun 15th, 2014
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.95 KB | None | 0 0
  1. import java.util.Stack;
  2. import java.lang.reflect.Array;
  3.  
  4. public class Calculator{
  5.     /*
  6.      * This is the main processing class. Gets an input stream  as a
  7.      * String, then creates a Parser object, and then calls convertToRPN
  8.      * method to get a manageable form for it. Then we compute it. There
  9.      * we do some checking to make the work safe and sane.
  10.      */
  11.      
  12.      public static Double getResult(String opLine) throws BraNotEnough, OperandsNotEnough {
  13.          /*
  14.           * THis is the only method in this class which is visible. This
  15.           * is very ugly, I think, but I don't know, how t get it better
  16.           * yet. Maybe later...
  17.           * */
  18.          
  19.          if(checkBras(opLine) > 0){
  20.              throw new BraNotEnough("More left bras than right!");
  21.          } else if(checkBras(opLine) < 0){
  22.              throw new BraNotEnough("Less left bras than right!");
  23.          }
  24.          
  25.          Parser workLine = new Parser(opLine);
  26.          Stack<Double> opStack = new Stack<Double>();
  27.          String[] operation = workLine.convertToRPN();
  28.          int index;
  29.          
  30.          if( !(checkOps(operation) ) ){
  31.              throw new OperandsNotEnough("There aren't enough operands!");
  32.          }
  33.          
  34.          for(index = 0; index < Array.getLength(operation); index++){
  35.              String temporary = operation[index];
  36.              if(operation[index] == null){continue;}
  37.              if( Operator.isOperator(temporary).booleanValue() ){
  38.                  int jndex;
  39.                  Double result;
  40.                  Double[] operands = new Double[Operator.getOperands(temporary)];
  41.                  for(jndex = 0; jndex < Operator.getOperands(temporary); jndex++){
  42.                      operands[jndex] = opStack.pop();
  43.                  }
  44.                  opStack.push( Operator.doOperation(temporary, operands) );
  45.              } else{
  46.                  opStack.push( Double.valueOf(operation[index]) );
  47.              }
  48.          }
  49.          
  50.          return opStack.pop();
  51.          
  52.      }
  53.      
  54.      private static int checkBras(String check){
  55.          /*
  56.           * There must be the same number of opening and closing bras.
  57.           * So we return their difference to throw the correct message.
  58.           * */
  59.          int left = 0, right = 0;
  60.          int index;
  61.          for(index = 0; index < check.length(); index++){
  62.              if(check.charAt(index) == '('){
  63.                  left++;
  64.              } else if(check.charAt(index) == ')'){
  65.                  right++;
  66.              }
  67.          }
  68.          return left-right;
  69.      }
  70.      
  71.      private static boolean checkOps(String[] checkThis){
  72.          /*
  73.           * The numbers are always equal the number of operators plus
  74.           * one. So we can easily check them.
  75.           * */
  76.          int index, operators = 0, numbers = 0;
  77.          for(index = 0; index < Array.getLength(checkThis); index++){
  78.              try{
  79.                  if(Operator.isOperator(checkThis[index])){
  80.                     operators++;
  81.                 } else if( !( Double.isNaN( Double.valueOf( checkThis[index]) ) ) ){
  82.                     numbers++;
  83.                 }
  84.             } catch(NullPointerException npe){
  85.                 /*
  86.                  * When there were bras, the converted array of strings
  87.                  * inhalts some null object at the end. This isn't error,
  88.                  * therefore we have to handle this case.
  89.                  * */
  90.                 return ( (numbers - operators) == 1 ) ? true : false;
  91.             }
  92.          }
  93.          return ( (numbers - operators) == 1 ) ? true : false;
  94.      }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement