123
-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 0|601|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=-
Socoder -> Concept/Design -> Coding a caclulator?

Wed, 31 Oct 2007, 05:56
spinal
I am going to have a stab a coding a simple calculator, but having never tried before I'm not quite sure where to start.
Code-wize I assume I would just add all the button presses to a string then pars it when the second function (+=-*/) is pressed, perform the first calculation then repeat using the result at the start of my string.

is this a good plan?

-=-=-
Check out my excellent homepage!
Wed, 31 Oct 2007, 06:31
Jayenkai
No need to parse, really, just have Number 1, Function and Number 2, then do a switch(case) for function and return the result.

Purely out of interest, why would you store it as a string as opposed to an Integer?

-=-=-
''Load, Next List!''
Wed, 31 Oct 2007, 06:50
mike_g
You could have 2 variables, preferably doubles, that store the left and right hand side of the expression. The user starts off adding digits to the left hand side. When each digits entered you multiply the existing number by 10. When they press +, -, *, or / then you log the action taken and start adding future data to the right hand side variable. When they press = you just add(or whatever) the righthand side to the left, then reset the right side to zero. Thats how I would do it anyway.
Wed, 31 Oct 2007, 07:02
spinal
jay - no idea, just the first thing that popped into my head.


-=-=-
Check out my excellent homepage!
Wed, 31 Oct 2007, 07:48
spinal
so far i have this (just quick test)

blitz


it doesnt seem to be switching numbers properly, i thik its my input method, but i have to go to work, so ill look at it tomorrow.

-=-=-
Check out my excellent homepage!
Wed, 31 Oct 2007, 08:10
Phoenix
What kind of calculator are you after? One that parses a string with an expression, or something that resembles the standard Windows one? I made a simple calculator last year, perhaps I still have the source. I'll have a look...
Wed, 31 Oct 2007, 11:12
mindstorm8191
If it'll help you any, I have an expressions parsing module, which takes strings of a math equation and converts it to a single result. Works with parenthesis too. You can get it from the modules section on my site - here

I think mike_g's approach to a calculator would be the simplest and most effective.

-=-=-
Vesuvius web game
Wed, 31 Oct 2007, 12:00
JL235
Mike_G's sounds familiar to calculator designs I've heard in the past. I'd go with that.

But it all depends on the interface. For example if you want the user to be able to type in long mathematical equations then you will need some sort of lexical analyser and parser. If it's just a GUI with buttons to press, then I'd go with Mike_G's idea.
Wed, 31 Oct 2007, 18:14
spinal
I'm going for something similar to windows calculator, quite simple, no scientific stuff.

-=-=-
Check out my excellent homepage!
Thu, 01 Nov 2007, 13:21
Scherererer
windows calculator has a scientific mode though! :o

-=-=-
YouTube Twitter
Computer Science Series: Logic (pt1) (part 2) (part 3) 2's Complement Mathematics: Basic Differential Calculus