It is not well tested and it is not pretty! Not a classroom example of
how to parse strings and evaluate the expressions. The code is below.
The class does not extend TextField as I think it is nicer to implement
the class as ether a ActionListener or a FocusListener and then add it
to the TextField as needed.
There should be room for inprovements.
Jan Agermose
perltools: http://www.quateams.com/oro/downloads/
CODE::
import java.util.Vector;
import java.awt.event.FocusListener;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.FocusEvent;
import java.awt.TextComponent;
import java.io.*;
import com.oroinc.text.perl.*;
public final class Math implements ActionListener, FocusListener {
public void actionPerformed(ActionEvent e) {
try {
TextComponent c = (TextComponent)e.getSource();
double d = evaluate(c.getText());
c.setText(Double.toString(d));
} catch (Exception exception) {}
}
public void focusGained(FocusEvent e) {
}
public void focusLost(FocusEvent e) {
try {
TextComponent c = (TextComponent)e.getSource();
double d = evaluate(c.getText());
c.setText(Double.toString(d));
} catch (Exception exception) {}
}
public double evaluate(String str) {
Perl5Util perl = new Perl5Util();
Vector v = new Vector();
String process = str;
do {
perl.match("/([\\(\\)\\-\\+\\*\\%\\/]|[\\d\\.\\,]+)/", process);
for(int i=1;i<perl.groups();i++) {
v.addElement(perl.group(i));
}
process = perl.postMatch();
} while (perl.groups()>0 &&
(!process.trim().equals("")));
// return op(op(0,v),v);
return op(true, 0,v);
}
private String pop(Vector v) {
String s = (String)v.elementAt(0);
v.removeElementAt(0);
return s;
}
private double op(boolean ekstra,double d, Vector v) {
double val = d;
try {
String op = pop(v);
if (op.equals("+")) {
val = d+op(true, 0,v);
} else if (op.equals("-")) {
val = d-op(true, 0,v);
} else if (op.equals("*")) {
double d2 = op(false, 0,v);
val = op(true, d*d2, v);
} else if (op.equals("/")) {
double d2 = op(false, 0,v);
val = op(true, d/d2, v);
} else if (op.equals("(")) {
val = op(true, 0, v);
ekstra = true;
} else if (op.equals(")")) {
val = d;
ekstra = false;
} else {
val = Double.parseDouble(op);
}
if (ekstra)
val = op(false,val,v);
} catch (Exception e) {
}
return val;
}
public static void main(String[] argv) {
Math m = new Math();
if (argv.length == 1) {
if ((argv[0].equals("TEST"))) {
java.awt.Frame f = new java.awt.Frame("Test");
java.awt.TextField tf = new java.awt.TextField(20);
tf.addActionListener(m);
tf.addFocusListener(m);
tf.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
((TextComponent)e.getSource()).selectAll();
}
});
f.add(tf);
f.pack();
f.show();
} else {
System.err.println(m.evaluate(argv[0]));
}
}
else {
try {
BufferedReader keyb = new BufferedReader(new
InputStreamReader(System.in));
String str;
while (!(str = keyb.readLine()).equals("")) {
System.out.println("val = "+m.evaluate(str));
}
} catch (Exception e) {
System.err.println(e);
}
}
}
}
Joost Helberg wrote:
>
> Hi,
>
> I'm developing a system for small IT businesses and I need to enter
> numbers into fields.
>
> Currently I use a JTextField to enter the number into, converting it
> to Integer or Double when needed.
>
> I want to implement a speciliazed version of JTextField which will
> evaluate integer expressions.
>
> So entering
> (3 * 14 + 2) / (5+6)
>
> will display (upon leaving the field)
> 4
>
> Anyone familiar with Open Source Code to (partly) implement this?
>
> Joost Helberg
>
> PS: I don't understand why not all number-entering fields in all
> applications support this. It is so obvious! Applications with a
> pop-up calculator are so improductive!
>
> THANKS in advance
>
> --
> Joost Helberg Unix consultants v v
> [EMAIL PROTECTED] OO developers \ /
> ### ## ## # >---X---<
> http://snow.nl # # # # # # # # / \
> Snow B.V. ## # # # # # # # ^ ^
> Tel. 0418-653333 # # # # # # # #
> Fax. 0418-653666 ### # # ## ## ##
> PGP PblKey fprnt=4D BD 6A 45 6A 86 81 59 0D BA 7D D4 B2 F8 63 34
>
> ----------------------------------------------------------------------
> To UNSUBSCRIBE, email to [EMAIL PROTECTED]
> with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]
--
Mvh Jan Agermose
Tranekærvej 58, v92
8240 Risskov, Denmark
[EMAIL PROTECTED]
----------------------------------------------------------------------
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]