Исходный файл CUPCalc.java (все
изменения выделены):
P.S. Это довольно-таки жульническое решение
этой задачи. Те, кто поняли, что я имею в виду
молодцы, а кто не понял, то вам лучше и не
понимать.
/** * Calculator of ariphmetic expressions * Uses CUP (Java version of YACC) technology */ import java.awt.*; import java.awt.event.*; import java.applet.Applet;
import java_cup.runtime.*; // CUP runtime environment import parser; // Parser generated by CUP from the file "CUPCalc.cup"
public class CUPCalc extends Applet { // //---------------------------------------------------------------------- Button derivativeButton; //---------------------------------------------------------------------- // TextField expression; TextField result; // //----------------------------------------------------------------------------- TextField point; //----------------------------------------------------------------------------- // // Layout constants static final int margin = 5; static final int headlineHeight = 30; static final int vertSkip = 5; static final int horSkip = 5; static final int labelWidth = 80; static final int textWidth = 300; static final int buttonWidth = 80; static final int buttonHeight = 30;
public static void main(String[] args) { Frame f = new Frame("CUP Calculator");
WindowAdapter wa = new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }; f.addWindowListener(wa);
f.setFont(new Font("Helvetica", Font.PLAIN, 14));
// Add menu bar final MenuBar mb = new MenuBar(); f.setMenuBar(mb); final Menu fileMenu = new Menu("File"); final MenuItem quitItem = new MenuItem("Quit"); fileMenu.add(quitItem); mb.add(fileMenu);
ActionListener al = new ActionListener() { public void actionPerformed(ActionEvent e) { // System.out.println("Action " + e); if (e.getActionCommand().equals("Quit")) { System.exit(0); } } }; fileMenu.addActionListener(al);
CUPCalc cc = new CUPCalc(); f.add("Center", cc);
// f.pack(); f.setSize( 2*margin + labelWidth + horSkip + textWidth + 10+100, 2*margin + headlineHeight + 3*buttonHeight + 3*vertSkip + 50+100 ); cc.init(); cc.start();
f.setVisible(true); }
public void init() { setLayout(null);
setFont(new Font("Helvetica", Font.PLAIN, 14)); setBackground(Color.lightGray);
// //----------------------------------------------------------------- Label headline = new Label("Calculates the Derivative in Point"); //----------------------------------------------------------------- // headline.setFont(new Font("TimesRoman", Font.BOLD, 18)); add(headline);
int x = margin; int y = margin; headline.setBounds(x, y, 300, 30);
y += headlineHeight + vertSkip;
Label l = new Label("Expression:"); add(l); l.setBounds(x, y, labelWidth, buttonHeight); x += labelWidth + horSkip;
expression = new TextField(); add(expression); expression.setBounds(x, y, textWidth, buttonHeight); y += buttonHeight + vertSkip; x = margin;
// //----------------------------------------------------------------------------- Label p = new Label("Point"); add(p); p.setBounds(x, y, labelWidth, buttonHeight); x += labelWidth + horSkip;
point = new TextField(); add(point); point.setBounds(x, y, textWidth, buttonHeight); y += buttonHeight + vertSkip;
derivativeButton = new Button("Derivative"); add(derivativeButton); x += (textWidth - buttonWidth); derivativeButton.setBounds(x, y, buttonWidth, buttonHeight); y += buttonHeight + vertSkip; x = margin; //---------------------------------------------------------------- //
l = new Label("Result:"); add(l); l.setBounds(x, y, labelWidth, buttonHeight); x += labelWidth + horSkip;
result = new TextField(); result.setEditable(false); add(result); result.setBounds(x, y, textWidth, buttonHeight); result.setForeground(Color.blue);
// Add action listener for "Expression" text field // (for processing of the "Enter" button) and "Derivative" button ActionListener al = new ActionListener() { public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if (source == derivativeButton){ onDerivative(); }
} };
expression.addActionListener(al); derivativeButton.addActionListener(al); }
// //----------------------------------------------------------------------------- void onDerivative() {
double c = 0.000001; double point2 = 0; double x2 = (double)onEvaluate(point.getText()); point2 = Double.valueOf(point.getText()).doubleValue()+c; double x1 = (double)onEvaluate(""+point2); result.setText(""+((x1-x2)/c) ); }
// Evaluate an expression
double onEvaluate(String po) { parser parser_obj = new parser(); int k = 0; try { double x = Double.valueOf(po).doubleValue(); } catch (NumberFormatException e) { return 0.; }
String str = expression.getText(); String str2 = ""; String result = ""; while (k<str.length()) { if (str.charAt(k) != 'x') { str2 += str.charAt(k); } else { str2 += po; } k++; }
parser_obj.setParsingLine(str2);
try {
// Parse a line Symbol s = parser_obj.parse(); if (s != null && s.value != null) { result=""+s.value; return Double.valueOf(result).doubleValue(); } else { expression.setText("Syntax error"); return 0.; }
} catch (Exception e) { //... System.err.println("" + e); expression.setText("Syntax error"); return 0.; } } //--------------------------------------------------------------- // }