The instructions are as follows, along with what I have so far:
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:
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:
2. while not end of expression
3. switch next token
4. case complex number:
5. push the value onto the stack
6. case operator:
7. pop two operands from the stack
8. evaluate
9. push result onto the stack
10. pop the final result off the stack
The user should be allowed to enter and evaluate any number of expressions.
11. 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.
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.
1. A constructor.
2. A class method named fromString that accepts a StringTokenizer positioned at a complex number and returns that complex number.
3. A toString method that converts a complex number to a string.
4. A add method that adds two complex numbers and returns the result.
5. A subtract method that subtracts two complex numbers and returns the result.
6. A multiply method that multiplies two complex numbers and returns the result.
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.
publicclass Complex
{
publicstatic CompexfromString(StringTokenizer input)
{
//stub
return null;
}
// instancefields
privatedouble real;
privatedouble imaginary;
//constructors
publicComplex(double real, double imaginary)
{
this.real = real;
this.imaginary =imaginary;
}
publicString toString()
{
String output="";
if(real!=0)
{
output +=real;
}
if(imaginary!=0)
{
if(imaginary > 0)
{
output+="+";
}
output+=imaginary+"i";
}
return output;
}
// operations
publicstatic Complex add(Complex c1, Complexc2)
{
return newComplex(c1.real + c2.real,c1.imaginary + c2.imaginary);
}
publicstatic Complex subtract(Complex c1,Complex c2)
{
return newComplex(c1.real - c2.real,c1.imaginary - c2.imaginary);
}
publicstatic Complex multiply(Complex c1,Complex c2)
{
return newComplex(c1.real*c2.real - c1.imaginary*c2.imaginary, c1.real*c2.imaginary+c2.real*c1.imaginary);
}
}