//Title: Expression Test //Version: //Copyright: Copyright (c) 1998 //Author: Dumb Crawford //Company: Programming Languages //Description: package CSC350; import java.awt.*; import java.awt.event.*; import java.applet.*; import borland.jbcl.layout.*; import borland.jbcl.control.*; //import com.sun.java.swing.UIManager; public class csc350 extends Applet { XYLayout xYLayout1 = new XYLayout(); boolean isStandalone = false; TextControl ResulttextControl = new TextControl(); ButtonControl buttonControl1 = new ButtonControl(); TextFieldControl ExpressiontextFieldControl = new TextFieldControl(); //Get a parameter value public String getParameter(String key, String def) { return isStandalone ? System.getProperty(key, def) : (getParameter(key) != null ? getParameter(key) : def); } //Construct the applet public csc350() { } //Initialize the applet public void init() { try { jbInit(); } catch (Exception e) { e.printStackTrace(); } } //static { // try { // //UIManager.setLookAndFeel(new com.sun.java.swing.plaf.metal.MetalLookAndFeel()); // //UIManager.setLookAndFeel(new com.sun.java.swing.plaf.motif.MotifLookAndFeel()); // UIManager.setLookAndFeel(new com.sun.java.swing.plaf.windows.WindowsLookAndFeel()); // } // catch (Exception e) {} //} //Component initialization private void jbInit() throws Exception { xYLayout1.setWidth(600); xYLayout1.setHeight(200); ResulttextControl.setText("Expression result"); buttonControl1.setLabel("buttonControl1"); buttonControl1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { buttonControl1_actionPerformed(e); } }); ExpressiontextFieldControl.setText("346"); this.setLayout(xYLayout1); this.add(ResulttextControl, new XYConstraints(15, 63, 352, -1)); this.add(buttonControl1, new XYConstraints(40, 167, -1, -1)); this.add(ExpressiontextFieldControl, new XYConstraints(20, 18, 340, -1)); } //Start the applet public void start() { } //Stop the applet public void stop() { } //Destroy the applet public void destroy() { } //Get Applet information public String getAppletInfo() { return "Applet Information"; } //Get parameter info public String[][] getParameterInfo() { return null; } //Main method public static void main(String[] args) { csc350 applet = new csc350(); applet.isStandalone = true; DecoratedFrame frame = new DecoratedFrame(); frame.setTitle("Applet Frame"); frame.add(applet, BorderLayout.CENTER); applet.init(); applet.start(); frame.setSize(600,220); Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2); frame.setVisible(true); } static String ParseString; static char CurrentChar='#'; static int Index=0; static String ErrorMessage[] = {"Expected end of string", "Expecting )", "Expecting digit or ("}; static void InitCurrent(String X) { ParseString = X; CurrentChar='#'; Index=-1; } static void GetChar() { boolean NotDone = true; Index++; do if (Index < ParseString.length()) if (ParseString.charAt(Index) == ' ') Index++; else NotDone = false; else NotDone = false; while (NotDone); if (Index >= ParseString.length()) CurrentChar = '~'; else CurrentChar = ParseString.charAt(Index); } static void ParseNumber(int Value[]) { int Z=0; while (CurrentChar >= '0' & CurrentChar <= '9') { Z=10*Z+(CurrentChar - '0'); GetChar(); }//end while Value[0] = Z; // return Z } static void Term(int Value[]) // Term -> Number | ( Expression ) { switch (CurrentChar) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': ParseNumber(Value); break; case '(': GetChar(); Expression(Value); if (CurrentChar==')') GetChar(); else if (Value[1] < 0) { Value[1] = 1; //GetChar(); } break; default: { if (Value[1] < 0) {Value[1] = 2; GetChar();} } } } static void Expression(int Value[]) { boolean isMinus = false; if (CurrentChar == '+') GetChar(); else if (CurrentChar == '-') { isMinus = true; GetChar(); } Term(Value); if (isMinus) Value[0] = -Value[0]; int temp[]={0, -1}; while (CurrentChar=='+'|CurrentChar=='-') { isMinus = (CurrentChar=='-'); GetChar(); Term(temp); if (Value[1] < 0) Value[1] = temp[1]; if (isMinus) Value[0] -= temp[0]; else Value[0] += temp[0]; } } void buttonControl1_actionPerformed(ActionEvent e) { // do this String X; int J; X=ExpressiontextFieldControl.getText(); InitCurrent(X); int Y[]={0,-1}; GetChar(); Expression(Y); if (CurrentChar != '~') if (Y[1] < 0) Y[1] = 0; if (Y[1] < 0) ResulttextControl.setText("The value of the expression is " +X.valueOf(Y[0])); else ResulttextControl.setText(ErrorMessage[Y[1]]); } }