Question: 1. Construct a new method named 'evaluate' in the program to evaluate the result of the postfix string expression that is generated after converting from
1. Construct a new method named 'evaluate' in the program to evaluate the result of the postfix string expression that is generated after converting from its infix form.
2. Add the code to call the method in main method, and also output the result to the output file 'out2a.txt'.
See the sample output below to get an idea. Postfix of the string is what, needed to be evaluated
I am providing the code that I have below, just need to add a method to evaluate the postfix expression.
Step3.java:
import java.io.BufferedWriter; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.io.Writer;
public class Step3 { // Expression string static String expression = "9.5 - 4 * 3 ^ 2 / 6 + 8 * 4"; public static void main(String[] args) { // Create SymbolTable object SymbolTable st = new SymbolTable(expression); // Printing the required information in Console // Display my Name System.out.println("
// Printing the required information in Output File try (Writer writer = new BufferedWriter(new OutputStreamWriter( new FileOutputStream("C:/Users/patel/Desktop/JavaFiles/out2a.txt")))) { // Display my Name writer.write("
// Display the operands used writer.write("Variable String: " + st.strVariable); ((BufferedWriter) writer).newLine(); // Display the infix of expression writer.write("Infix of the expression: " + st.buildInfix()); ((BufferedWriter) writer).newLine(); // Display the postfix of expression writer.write("Postfix of the expression: " + obj.infix2Postfix(expression)); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private boolean isOperator(char c){ if(c == '+' || c == '-' || c == '*' || c =='/' || c == '^') return true; return false; } /** * Checks if c2 has same or higher precedence than c1 * @param c1 first operator * @param c2 second operator * @return true if c2 has same or higher precedence */ private boolean checkPrecedence(char c1, char c2) { if((c2 == '+' || c2 == '-') && (c1 == '+' || c1 == '-')) return true; else if((c2 == '*' || c2 == '/') && (c1 == '+' || c1 == '-' || c1 == '*' || c1 == '/')) return true; else if((c2 == '^') && (c1 == '+' || c1 == '-' || c1 == '*' || c1 == '/')) return true; else return false; } /** * infix2Postfixs infix expression to postfix * @param infix infix expression to be infix2Postfixed * @return postfix expression */ public String infix2Postfix(String infix) { String postfix = ""; //equivalent postfix is empty initially Stack
Sample Output:
Valid Expression: 9.5 - 4 * 3 ^ 2 / 6 + 8 * 4 Operator String: -*^/+*
Symbol Table: A 9.5 B 4.0 C 3.0 D 2.0 E 6.0 F 8.0 G 4.0
Variable String: ABCDEFG Infix of the expression: A-B*C^D/E+F*G Postfix of the expression: 9.5 4 3 2 ^* 6 /- 8 4*+
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
