Advertisement
tombenko

Operator.java

Jun 15th, 2014
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.28 KB | None | 0 0
  1. import java.lang.Math;
  2.  
  3.  public class Operator{
  4.     /*
  5.      * This class just encapsulate the operators and their precedences.
  6.      * With the number of the operands is stored here too. There is also
  7.      * a method to check if the given string is an operator string. And
  8.      * last, we have to return the operators string for tokenizing.
  9.      */
  10.    
  11.     private final static String opSigns = new String("+-*/^");
  12.     private final static Byte[] opPrecedence = {2,2,4,4,8};
  13.     private final static Byte[] opOperands = {2,2,2,2,2};
  14.    
  15.     public static Byte getPrecedence(String sign){
  16.         /*
  17.          * Returns the precedence value of operator, represented by this
  18.          * string.
  19.          */
  20.         return opPrecedence[opSigns.indexOf(sign)];
  21.     }
  22.    
  23.     public static Byte getOperands(String sign){
  24.         /*
  25.          * Returns the number of operands of this operator, represented
  26.          * by the given string.
  27.          */
  28.         return opOperands[opSigns.indexOf(sign)];
  29.     }
  30.    
  31.     public static Boolean isOperator(String sign){
  32.         /*
  33.          * Returns true if the given string is an operator and false
  34.          * otherwise.
  35.          */
  36.         return (opSigns.indexOf(sign) == -1) ? new Boolean(false) : new Boolean(true);
  37.     }
  38.    
  39.     public static String getOperatorString(){
  40.         /*
  41.          * Just return the string containing the operators characters.
  42.          */
  43.         return opSigns;
  44.     }
  45.    
  46.     public static Double doOperation(String operator, Double[] operands){
  47.         /*
  48.          * Here we do the calculating with those operators. This is a
  49.          * good place to do this, because the operator signs listed here
  50.          * too, so maybe later won't be confused when the program gets
  51.          * widening.
  52.          * */
  53.         Double result;
  54.         switch( operator.charAt(0) ){
  55.             case '+':{
  56.                 result = new Double( operands[1].doubleValue() + operands[0].doubleValue() );
  57.                 break;
  58.             }
  59.             case '-':{
  60.                 result = new Double( operands[1].doubleValue() - operands[0].doubleValue() );
  61.                 break;
  62.             }
  63.             case '*':{
  64.                 result = new Double( operands[1].doubleValue() * operands[0].doubleValue() );
  65.                 break;
  66.             }
  67.             case '/':{
  68.                 result = new Double( operands[1].doubleValue() / operands[0].doubleValue() );
  69.                 break;
  70.             }
  71.             case '^':{
  72.                 result = new Double(Math.pow( operands[1].doubleValue(), operands[0].doubleValue()));
  73.                 break;
  74.             }
  75.             default:{
  76.                 result = new Double(0);
  77.                 break;
  78.             }
  79.         }
  80.         return result;
  81.     }
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement