Advertisement
Guest User

Untitled

a guest
Sep 30th, 2014
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.40 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Stack;
  3. import java.lang.*;
  4. public class Infix {
  5.     public static double calculations(String here) {
  6.         Scanner input = new Scanner(System.in);
  7.         MyStack<Character> operatorstack =  new MyStack<Character>();
  8.         MyStack<Double> valuestack = new MyStack<Double>();
  9. String operator;
  10. char topOperator;
  11. char nextCharacter;
  12. double operandTwo;
  13. double operandOne;
  14. double result;
  15. double next;
  16.  
  17. while(input.hasNext())
  18. {
  19.     if(input.hasNextDouble() ){
  20.         next = input.nextDouble();
  21.         valuestack.push(next);
  22.     }
  23.     else {
  24.         operator = input.next();
  25.         nextCharacter = operator.charAt(0);
  26.        
  27.         switch(nextCharacter) {
  28.        
  29.         case '^':
  30.             operatorstack.push(nextCharacter);
  31.             break;
  32.         case '+':
  33.         case '-':
  34.         case '*':
  35.         case '/':
  36.             while(!operatorstack.isEmpty()&& getPriority(nextCharacter) <= getPriority(operatorstack.peek()))
  37.             {
  38.                 // Calculate operator at the top of the stack.
  39.                 topOperator = operatorstack.pop();
  40.                 operandTwo = valuestack.pop();
  41.                 operandOne = valuestack.pop();
  42.                
  43.         if (topOperator == '*')
  44.             result = operandOne * operandTwo;
  45.         else if (topOperator == '/')
  46.             result = operandOne / operandTwo;
  47.         else if (topOperator == '+')
  48.             result = operandOne + operandTwo;
  49.         else if (topOperator == '-')
  50.             result = operandOne - operandTwo;
  51.         else
  52.             throw new RuntimeException("You can't do that, illegal operator: " + topOperator);
  53.         valuestack.push(result);
  54.             topOperator = operatorstack.pop();
  55.             }
  56.             break;
  57.         default:
  58.             break;
  59.         }
  60.     }
  61.         while(!operatorstack.isEmpty()){
  62.             topOperator = operatorstack.pop();
  63.             operandTwo = valuestack.pop();
  64.             operandOne = valuestack.pop();
  65.        
  66.         if (topOperator == '*')
  67.             result = operandOne * operandTwo;
  68.         else if (topOperator == '/')
  69.             result = operandOne / operandTwo;
  70.         else if (topOperator == '+')
  71.             result = operandOne + operandTwo;
  72.         else if (topOperator == '-')
  73.             result = operandOne - operandTwo;
  74.         else
  75.             throw new RuntimeException("You can't do that, illegal operator: " + topOperator);
  76.             valuestack.push(result);
  77.    
  78.         }
  79.         double answer = valuestack.peek();
  80.         return answer;
  81.     }
  82.     }
  83.    
  84.     public static int getPriority(char nextCharacter) {
  85.         if (nextCharacter == '*' || nextCharacter == '/')
  86.                 return 1;
  87.         else if (nextCharacter == '+' || nextCharacter == '-')
  88.                 return 2;
  89.         else if (nextCharacter == '=')
  90.                 return 3;
  91.         else
  92.                 return Integer.MIN_VALUE;
  93.     }
  94.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement