hai,

i need some help in developing a simple calculator!

i've finished the front-end part using jigloo software

but the problem is when i click the first number,that number will appear in the textfield after that when i click the '+' button the textfield is clearing but i'm not able able to read the entered value.

Also tell me where should i implement the logic to add these 2 numbers

## All 6 Replies

Get the first number
user clicks +... register that
Get the second number

doesn't seem like a logical solution to me, but then I'm used to scientific calculators with built-in equation editors ;)

Create the entire equation as a single string and parse that using (for example) a recursive descent parser.
There's a nice book that does pretty much just that in one chapter called "the art of Java", then in the next chapter turns that calculator into a simplistic BASIC interpreter.

try this:
1.user enter number
2a.user click operator
2b.set code to store number to a variable
2c.set code to store the status of the (last) operator click
3. user enters next numbers
4a. user clicks another operator button
4b. update stored variable (in step 2b, calculating the new value, depending on the operator used in step 2c)...
5...

there is still alot of gap to fill but i think its a good starting point... booleans and counters may be a good helps when writing the real code

--
fd

doesn't seem like a logical solution to me, but then I'm used to scientific calculators with built-in equation editors ;)

True, but I don't think they wanted a full fledged multi operator calculator that follows PEMDAS.

It would be nice to write an equation parse, because there could be some stumbling blocks somewhere in there. Most people would write a parenthesis balancer first, but do you really need it?

1 + (2-5)

should be the same as:

1 + (2-5

without the parenthesis

A simple equation like:

5*2+5*4/45

might not be that bad, but I think the parenthesis would get me.

>might not be that bad, but I think the parenthesis would get me.

I was wondering dat so I decided to do some research into it and build
one on the fly.

I think it can be done using syntax trees. Essentially, you have
to write your own grammer and parsing engine however, the idea would
be as follows:

For example if your expression is:-

3*(7+1)/4+(17-5)

Then you would have your engine parse it into a syntax tree as follows:

``````(+)
/  \
/    \
/      \
(*)      (-)
/   \     /  \
3    (/)  17   5
/   \
(+)    4
/ \
7   1``````

Evaluting the expression recursively from the bottom upwards yields the
correct solution.

Most of what I learnt from the binary search tree was obtained from narue'
handy tutorial's www.eternallyconfuzzled.com

My binary tree syntax is a bit rusty in java so I did it in C, however, if I feel like it,
I might write one for java wen I get home from school. Tee he he.

Simple as pie kiddo.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.