calculator program

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2006
Posts: 9
Reputation: Sheki RDH is an unknown quantity at this point 
Solved Threads: 0
Sheki RDH Sheki RDH is offline Offline
Newbie Poster

calculator program

 
0
  #1
Mar 24th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: calculator program

 
0
  #2
Mar 24th, 2006
Get the first number
user clicks +... register that
Get the second number
Display addition.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: calculator program

 
0
  #3
Mar 25th, 2006
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.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 28
Reputation: fdrage is an unknown quantity at this point 
Solved Threads: 0
fdrage's Avatar
fdrage fdrage is offline Offline
Light Poster

Re: calculator program

 
0
  #4
Mar 27th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: calculator program

 
0
  #5
Mar 27th, 2006
http://www.java2s.com/ExampleCode/Sw...Calculator.htm

Looks a good example, haven't tried it though.

Assuming you know that basics about making a GUI and responding to User Input I don't see many problems.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: calculator program

 
0
  #6
Mar 27th, 2006
Originally Posted by jwenting
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: calculator program

 
0
  #7
Mar 28th, 2006
>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:

  1. (+)
  2. / \
  3. / \
  4. / \
  5. (*) (-)
  6. / \ / \
  7. 3 (/) 17 5
  8. / \
  9. (+) 4
  10. / \
  11. 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.
Attached Files
File Type: zip thwee.zip (182.8 KB, 37 views)
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 5714 | Replies: 6
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC