//CS5-HMC-Sp.,00-Lab 12 //A. Khakpour, May, 00 import java.awt.*; import java.applet.*; import java.awt.event.*; import java.util.*; //================================================================== // This applet accepts a postfix expression from the user and evaluates // it. Note that little error checking is performed. The applet // may terminate if the expression is not in valid postfix form. //================================================================== public class Postfix4 extends Applet { private PostfixGUI gui; //------------------------------------------------------ // Sets up the applet. //------------------------------------------------------ public void init () { gui = new PostfixGUI (this); } } //================================================================== // The GUI presents a field into which the user types a postfix // expression, and an evaluate button to process it. //================================================================== class PostfixGUI { private static Label instructions; private static Label expressionTitle; private static Label resultTitle; private static Button evaluateButton; private static Button clearButton; private static Label result; private static TextField expression; //------------------------------------------------------ // Sets up the GUI. //------------------------------------------------------ public PostfixGUI (Applet app) { instructions = new Label ("Enter a postfix expression," + " then press enter or click the Evaluate button.", Label.CENTER); expressionTitle = new Label ("Expression:"); resultTitle = new Label ("Result:"); evaluateButton = new Button ("Evaluate"); evaluateButton.addActionListener (new EvaluateActionListener()); clearButton = new Button ("Clear"); clearButton.addActionListener (new ClearActionListener()); result = new Label ("__________"); expression = new TextField (40); expression.addActionListener (new EvaluateActionListener()); app.add (instructions); app.add (expressionTitle); app.add (expression); app.add (resultTitle); app.add (result); app.add (evaluateButton); app.add (clearButton); app.setSize (425, 150); } //------------------------------------------------------ // Clears the text field and result label. //------------------------------------------------------ public static void clearFields () { expression.setText (""); result.setText ("__________"); } //------------------------------------------------------ // Returns the expression typed by the user. //------------------------------------------------------ public static String getExpression () { return expression.getText(); } //------------------------------------------------------ // Sets the result label. //------------------------------------------------------ public static void showResult (String answer) { result.setText (answer); } } //================================================================== // The action listener for the clear button. //================================================================== class ClearActionListener implements ActionListener { //------------------------------------------------------ // Clears the text field and result label. //------------------------------------------------------ public void actionPerformed (ActionEvent event) { PostfixGUI.clearFields(); } } //================================================================== // The action listener for the evaluate button and text field. //================================================================== class EvaluateActionListener implements ActionListener { //------------------------------------------------------ // Performs the evaluation and presents the results. //------------------------------------------------------ public void actionPerformed (ActionEvent event) { String expr = PostfixGUI.getExpression(); if (!expr.equals("")) { PostfixEvaluator evaluator = new PostfixEvaluator(expr); double answer = evaluator.evaluate(); PostfixGUI.showResult (String.valueOf(answer)); } } } //================================================================== // This class evaluates a postfix expression. //================================================================== class PostfixEvaluator { private StringTokenizer tokens; private Stack stack; public PostfixEvaluator (String expr) { tokens = new StringTokenizer (expr, "+-*/ ", true); stack = new Stack(); } public boolean isOperator (char c) { if (c == '+' || c == '-' || c == '*'|| c == '/') return true; else return false; } public double evaluate() { String d = ""; while (tokens.hasMoreTokens()) { double a=0, b=0, e=0; d = tokens.nextToken(); char s = d.charAt(0); if(isOperator(s)) { a = ((Double)stack.pop()).doubleValue(); b = ((Double)stack.pop()).doubleValue(); } switch(s) { case '+': e = b + a; stack.push(new Double(e)); break; case '-': e = b - a; stack.push(new Double(e)); break; case '*': e = b * a; stack.push(new Double(e)); break; case '/': e = b / a; stack.push(new Double(e)); break; case ' ': break; default : stack.push(new Double(d)); } } double r = ((Double)stack.pop()).doubleValue(); return r; } }