0

***Can you help me to write a java program to evaluate a mathematical expression written in a a Prefix Order ??
***

4
Contributors
4
Replies
7
Views
4 Years
Discussion Span
Last Post by hala.qaddoumi
0

@hala.qaddoumi
Have you tried something on your own?
If yes pelase post the code and tell us the errors

0

Ah, building an expression (infix, prefix, postfix) tree! That takes me back! :-) Fortunately, I still have my copy of Knuth handy, including the last volume recently published! :-)

0

*i have postfix code.
on postfix we store the operator on the stack and the operand as output , but the prefix i don't get the idea of it .. :(
*

package Hallosh;
import java.util.Stack;
import java.util.StringTokenizer;
public class Driver {
  public static void main(String[] args) {
    // Taken from Daniel Liang – Intro to Java Prog.
    // the input is a correct postfix expression
String expression = "1 2 + 3 *";

try {
      System.out.println( evaluateExpression(expression) );
    }
catch (Exception ex) {
      System.out.println("Wrong expression");
    }
  }
/** Evaluate an expression **********************************************/
public static int evaluateExpression(String expression) {
    // Create operandStack to store operands
    Stack<Integer> operandStack = new Stack<Integer>();
    // Extract operands and operators
StringTokenizer tokens = new StringTokenizer(expression, " +-/*%", true);
// Phase 1: Scan tokens
while (tokens.hasMoreTokens()) {
      String token = tokens.nextToken().trim(); // Extract a token
      if (token.length() == 0) { // Blank space
        continue; // Back to the while loop to extract the next token
      }
else if (token.charAt(0) == '+' || token.charAt(0) == '-' ||
               token.charAt(0) == '*' || token.charAt(0) == '/') {
              processAnOperator(token.charAt(0), operandStack);
      }
else {  // An operand scanned
              // Push an operand to the stack
operandStack.push(new Integer(token));
      }
    }
// Return the result
return ((Integer)(operandStack.pop())).intValue();
}
/** Process one operator: Take an operator from operatorStack and
   *  apply it on the operands in the operandStack */
  public static void processAnOperator(char op, Stack operandStack) {

if (op == '+') {
      int op1 = ((Integer)(operandStack.pop())).intValue();
      int op2 = ((Integer)(operandStack.pop())).intValue();
      operandStack.push(new Integer(op2 + op1));
    }
else if (op == '-') {
      int op1 = ((Integer)(operandStack.pop())).intValue();
      int op2 = ((Integer)(operandStack.pop())).intValue();
      operandStack.push(new Integer(op2 - op1));
    }
else if ((op == '*')) {
      int op1 = ((Integer)(operandStack.pop())).intValue();
      int op2 = ((Integer)(operandStack.pop())).intValue();
      operandStack.push(new Integer(op2 * op1));
    }
else if (op == '/') {
      int op1 = ((Integer)(operandStack.pop())).intValue();
      int op2 = ((Integer)(operandStack.pop())).intValue();
      operandStack.push(new Integer(op2 / op1));
    }
  }
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.