//Evan Warner //This class evaluates postfix form and numerical evaluations of a complex-valued function of z import java.util.*; public class ComplexFunction { public static final String ops2 = "+-*/^"; //Two-argument operators public static final String ops1 = "scteonlmp"; //One-argument operators private static String myPostfixString; private static String myInfixString; public ComplexFunction(String s) //The variable for a ComplexFunction must be named 'z' { myInfixString = s.replaceAll(" ",""); myPostfixString = convert(myInfixString); Complex dummy = value(new Complex(0,0)); //To provide an extra check for syntax problems } //Returns the function in infix form public String toString() { return myInfixString; } //Returns the function in postfix form public String postfixString() { return myPostfixString; } //Returns whether the function has a syntax error or not public boolean isValid() { return myPostfixString!="Syntax Error"; } //Calls private "eval" method if this is a valid function, otherwise returns zero public Complex value(Complex a) { if(!myPostfixString.equals("Syntax Error")) return eval(myPostfixString, a); return new Complex(0,0); } //Converts infix form to postfix form private static String convert(String str) { try { if(str.length()>0&&str.substring(0,1).equals("-")) //Checks for leading minus sign str="0"+str; int index = 0; int l = str.length(); String postfix = ""; Stack stack = new Stack(); while(index16&&myInfixString.substring(myInfixString.length()-16,myInfixString.length()).equals("!!SYNTAX_ERROR!!"))) myInfixString+="!!SYNTAX_ERROR!!"; return "Syntax Error"; } } //Checks whether a character is part of a number or variable private static boolean isNumberOrVarPart(char c) { if(Character.isDigit(c)||c=='.'||c=='z'||c=='i') return true; return false; } //Compares the order of operations precedence of two characters private static boolean isLower(char c1, char c2) { int input1 = assignPref(c1); int input2 = assignPref(c2); if(input116&&myInfixString.substring(myInfixString.length()-16,myInfixString.length()).equals("!!SYNTAX_ERROR!!"))) myInfixString+="!!SYNTAX_ERROR!!"; return new Complex(0,0); } } } else { str=str.replace("i",""); try { if(str.equals("")) return new Complex(0,1); return new Complex(0,Double.parseDouble(str)); } catch(NumberFormatException e) { if(str.equals("z")) return new Complex(a.real(),a.imag()); else { myPostfixString="Syntax Error"; if(!(myInfixString.length()>16&&myInfixString.substring(myInfixString.length()-16,myInfixString.length()).equals("!!SYNTAX_ERROR!!"))) myInfixString+="!!SYNTAX_ERROR!!"; return new Complex(0,0); } } } } // private static Complex eval(String str, Complex a) { try { if(str.equals("z")) return a; int index = 0; int lengthOfString = str.length(); Stack stack = new Stack(); while(index16&&myInfixString.substring(myInfixString.length()-16,myInfixString.length()).equals("!!SYNTAX_ERROR!!"))) myInfixString+="!!SYNTAX_ERROR!!"; return new Complex(0,0); } } if(currentChar=='s') stack.push(input.sin().toString()); else if(currentChar=='c') stack.push(input.cos().toString()); else if(currentChar=='t') stack.push(input.tan().toString()); else if(currentChar=='e') stack.push(input.sec().toString()); else if(currentChar=='o') stack.push(input.csc().toString()); else if(currentChar=='n') stack.push(input.cot().toString()); else if(currentChar=='l') stack.push(input.ln().toString()); else if(currentChar=='m') stack.push(""+input.modulus()); else if(currentChar=='p') stack.push(input.exp().toString()); } } while(index16&&myInfixString.substring(myInfixString.length()-16,myInfixString.length()).equals("!!SYNTAX_ERROR!!"))) myInfixString+="!!SYNTAX_ERROR!!"; myPostfixString= "Syntax Error"; return new Complex(0,0); } } //Checks whether a given character is a two-argument operator private static boolean isTwoArgOperator(char c) { int l = ops2.length(); for(int i=0; i