Advertisement
Guest User

Expression Evaluation

a guest
Feb 21st, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.10 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package Question2;
  7.  
  8. import Question1.MyStack;
  9. import java.util.Scanner;
  10.  
  11. /**
  12.  *
  13.  * @author CSC2017
  14.  */
  15. public class EvalExpression {
  16.     private static MyStack<Integer> numStack = new MyStack<>();
  17.     private static MyStack<Character> opStack = new MyStack<>();
  18.    
  19.     public static void main(String[] args) {
  20.         Scanner s = new Scanner(System.in);
  21.         System.out.println("Enter Expression: ");
  22.         String exprsion = s.nextLine();
  23.         int value = eval(exprsion);
  24.         System.out.println(exprsion+" = "+value);
  25.     }
  26.     public static int eval(String exprsion){
  27.         int result = 0;
  28.         for(char c:exprsion.toCharArray()){
  29.             if((int)c <= 57 && (int)c >=48){
  30.                 //char c is a number
  31.                 numStack.push((int)c - 48);
  32.             }else{
  33.                 //if not a number then an operator
  34.                 if(opStack.empty()){
  35.                     //no operands pushed yet
  36.                     opStack.push(c);
  37.                 }else{
  38.                     //operands inside
  39.                     char topOp = opStack.peek();
  40.                     if(isHigherOperand(topOp,c)){
  41.                         do{
  42.                             topOp = opStack.pop();
  43.                             int num1 = numStack.pop();
  44.                             int num2 = numStack.pop();
  45.                             int value = evalOperation(num2,topOp,num1);
  46.                             numStack.push(value);
  47.                             topOp = opStack.peek();
  48.                         }while(isHigherOperand(topOp,c));
  49.                         opStack.push(c);
  50.                     }else{
  51.                         opStack.push(c);                    }
  52.                 }
  53.             }
  54.         }
  55.         if(!opStack.empty()){
  56.             while(!opStack.empty()){
  57.                 char topOp = opStack.pop();
  58.                 int num1 = numStack.pop();
  59.                 int num2 = numStack.pop();
  60.                 int value = evalOperation(num2,topOp,num1);
  61.                 numStack.push(value);
  62.             }
  63.         }
  64.         result = numStack.pop();
  65.         return result;
  66.     }
  67.     public static boolean isHigherOperand(char firstOp,char secOp){
  68.         //returns true if first operand greater than second
  69.         if(firstOp == '*' || firstOp == '/') return true; //assuming the operaots are + - * /
  70.         else return false;
  71.     }
  72.     public static int evalOperation(int num1,char topOp,int num2){
  73.         int value = 0;
  74.         switch(topOp){
  75.             case '+':
  76.                 value = num1+num2;
  77.                 break;
  78.             case '-':
  79.                 value = num1-num2;
  80.                 break;
  81.             case '*':
  82.                 value = num1*num2;
  83.                 break;
  84.             case '/':
  85.                 value = num1/num2;
  86.                 break;
  87.             default:
  88.                 value = 0;
  89.                 break;
  90.         }
  91.         return value;
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement