The second project involves writing a program to evaluate postfix expressions co
ID: 3620345 • Letter: T
Question
The second project involves writing a program to evaluate postfix expressions containing complex numbers using a stack. This program should contain two classes. The first class Project2 should contain the two methods described below:<br /><br />1. The main method, which performs the expression evaluation. The algorithm for evaluating a postfix expression requires a stack of Complex numbers. The pseudocode for evaluating postfix expressions is given below:<br /><br />while not end of expression<br />switch next token<br />case complex number:<br />push the value onto the stack<br />case operator:<br />pop two operands from the stack<br />evaluate<br />push result onto the stack<br />pop the final result off the stack<br /><br />The user should be allowed to enter and evaluate any number of expressions.<br />2. A class method called evaluate, which accepts two complex numbers and a operator and returns the result of performing the operation on those two numbers.<br /><br />The second class should be a class named Complex. Each complex number should contain a real and imaginary part. Each should be of type double. This class should contain the following methods.<br /><br />1. A constructor.<br />2. A class method named fromString that accepts a StringTokenizer positioned at a complex number and returns that complex number.<br />3. A toString method that converts a complex number to a string.<br />4. A add method that adds two complex numbers and returns the result.<br />5. A subtract method that subtracts two complex numbers and returns the result.<br />6. A multiply method that multiplies two complex numbers and returns the result.<br /><br />Each complex number will all be enclosed in a pair of parentheses. The operators permitted include +, - and *. You may assume that all expressions are syntactically correct.Explanation / Answer
Dear.. public class PostfixEval { public static void main(String[] args) { TextIO.putln("This program can evaluate postfix expressions such as "); TextIO.putln(" 2 2 +"); TextIO.putln("or"); TextIO.putln(" 7.3 89.2 + 9 1.83 * 2 + / "); TextIO.putln("The operators +, -, *, /, and ^ can be used. "); while (true) { TextIO.putln(" Enter a postfix expression or press return to end: "); TextIO.put("? "); skipSpaces(); if (TextIO.peek() == ' ') { break; } readAndEvaluate(); TextIO.getln(); } TextIO.putln(" Exiting program."); } static void skipSpaces() { while (TextIO.peek() == ' ' || TextIO.peek() == ' ') { TextIO.getAnyChar(); } } static void readAndEvaluate( ) NumberStack stack; stack = new NumberStack(); TextIO.putln(); while (TextIO.peek() != ' ') { if ( Character.isDigit(TextIO.peek()) ) { double num = TextIO.getDouble(); stack.push(num); TextIO.putln(" Pushed constant " + num); } else { char op; double x,y; double answer; op = TextIO.getChar(); if (op != '+' && op != '-' && op != '*' && op != '/' && op != '^') { TextIO.putln(" Illegal operator found in input: " + op); return; } if (stack.isEmpty()) { TextIO.putln(" Stack is empty while trying to evaluate " + op); TextIO.putln(" Not enough numbers in expression!"); return; } y = stack.pop(); if (stack.isEmpty()) { TextIO.putln(" Stack is empty while trying to evaluate " + op); TextIO.putln(" Not enough numbers in expression!"); return; } x = stack.pop(); switch (op) { case '+': answer = x + y; break; case '-': answer = x - y; break; case '*': answer = x * y; break; case '/': answer = x / y; break; default: answer = Math.pow(x,y); } stack.push(answer); TextIO.putln(" Evaluated " + op + " and pushed " + answer); } skipSpaces(); } if (stack.isEmpty()) { TextIO.putln("No expression provided."); return; } double value = stack.pop(); TextIO.putln(" Popped " + value + " at end of expression."); if (stack.isEmpty() == false) { TextIO.putln(" Stack is not empty."); TextIO.putln(" Not enough operators for all the numbers!"); return; } TextIO.putln(" Value = " + value); } }