•

Check out our Community Blogs

Register and join over 40,000 other developers!

### Recent Blog Entries

• phi

I love this community !

• JackJames

hi i am jack i am seo expert jack james would love you to read new post

# calculator problem

calculator

4 replies to this topic

### #1 farhanyun91

farhanyun91

CC Regular

• Member
• 46 posts

Posted 20 February 2012 - 12:13 AM

i want to create a calculator with 10 buttons which is for 1-10 numbers.let say when user click button "1"(for number 1) the "1" will appear in textbox.and when the user press button "2"(for number 2) number "1" will be replaced by number "2" in the textbox.but my problem now is, how to code for an event where user wants to press 12?click "1" ,fine it will appears in textbox,press "2", "1" gone.so how can i solve this logical error?and another one is, i'm remained clueless on how to get them calculated. let say, user will need to click number "2",number "1" and "plus"(button plus)to be added together to become "3", how can i make a firstvariable that consists of number from 1-10 to be added with another variable,let say secondvariable, which also consists from 1-10.anybody here can help me out?
• 0

### #2 gregwarner

gregwarner

Obi Wan of Programming

• Expert Member
• 1586 posts
• Location:Arkansas
• Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 20 February 2012 - 07:17 AM

but my problem now is, how to code for an event where user wants to press 12?click "1" ,fine it will appears in textbox,press "2", "1" gone.so how can i solve this logical error?

There are two ways to solve this:

1. Use string concatenation to append each button press to the end of the current text. This of course would not be done when the current text reads "0", or when you've just pressed an operator key.

2. Take the current number in the text box, multiply it by 10, and then add the value of the most recently pressed button.

and another one is, i'm remained clueless on how to get them calculated. let say, user will need to click number "2",number "1" and "plus"(button plus)to be added together to become "3", how can i make a firstvariable that consists of number from 1-10 to be added with another variable,let say secondvariable, which also consists from 1-10.anybody here can help me out?

When you say "click number 2, [then] number 1, [then] plus," do you mean to say you are trying to build an RPN calculator? In which case you need to implement a stack and also an "enter" key to press between the number 2 and number 1 button presses. If that's not the case, and what you actually meant was "click number 2, then 'plus', then number 1", then it might be easier to just code a couple of variables to use as registers for the operations, and use a state variable to keep track of what state the calculator is in ('plus' pressed state, 'subtract' pressed state, 'multiply' pressed state, etc.)

Let me know if those explanations don't make sense to you. It's pre-coffee morning for me here.
• 0

Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid

### #3 farhanyun91

farhanyun91

CC Regular

• Member
• 46 posts

Posted 20 February 2012 - 08:13 AM

There are two ways to solve this:

1. Use string concatenation to append each button press to the end of the current text. This of course would not be done when the current text reads "0", or when you've just pressed an operator key.

2. Take the current number in the text box, multiply it by 10, and then add the value of the most recently pressed button.

When you say "click number 2, [then] number 1, [then] plus," do you mean to say you are trying to build an RPN calculator? In which case you need to implement a stack and also an "enter" key to press between the number 2 and number 1 button presses. If that's not the case, and what you actually meant was "click number 2, then 'plus', then number 1", then it might be easier to just code a couple of variables to use as registers for the operations, and use a state variable to keep track of what state the calculator is in ('plus' pressed state, 'subtract' pressed state, 'multiply' pressed state, etc.)

Let me know if those explanations don't make sense to you. It's pre-coffee morning for me here.

i don't really understand your first solution.aha.and for the second solution, what is the purpose of multiplying every pressed numbers to 10 before adding them with the latest pressed numbers?

what i mean was, how to calculate the pressed numbers?user will press any number for the first variable and second variable.meaning, user press number 1(which is for the first variable) and user press number 2(which is for second variable).how can i total them up?i am a bit at sea here.aha.hope you can come out with something that can help me.Thanks anyway for your concern.
• 0

### #4 gregwarner

gregwarner

Obi Wan of Programming

• Expert Member
• 1586 posts
• Location:Arkansas
• Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 20 February 2012 - 08:27 AM

i don't really understand your first solution.aha.and for the second solution, what is the purpose of multiplying every pressed numbers to 10 before adding them with the latest pressed numbers?

In order to treat multiple button presses as a single number, you need some way of transforming the data as the number is being entered. For instance, to enter the number "12" (twelve), the user would first press '1' followed by '2'. In the 1st solution, you treat these numbers as strings and perform string concatenation (literally, appending one string to the other.) So the string "1" gets put into the text box on the 1st button press, and then the string "2" gets appended onto the existing string on the 2nd button press, resulting in the string "12", which may be parsed as a number when your user presses an operator button.

In the 2nd solution, if you treat the number as a number rather than a string, then you need some way of "pushing" the existing digits down while adding the new digits in order to build your number. Numbers are entered into a calculator greatest significant digit first, so for our example above, if the user wants to enter "12", he presses "1" first (the 10's digit), and then "2" (the 1's digit.) When he presses 1, your algorithm takes the value that's already there (0, because nothing has been entered yet), multiplies it by 10 to "push" it down one digit, (still 0, because 0 times anything is still zero), and finally adds the value of the most recent button press, (0 + 1 = 1). Now repeat the process for the 2nd button press. Multiply the existing value by 10 to "push" it down, (1 * 10 = 10), then add 2. (10 + 2 = 12) Make sense? It keeps working for more digits as well. Suppose now you want to enter 123. Just press 3, and it multiplies the existing value by 10 (12 * 10 = 120) and adds 3 (120 + 3 = 123). Try experimenting with that algorithm with pencil and paper with a few more examples and you'll see how it works.

what i mean was, how to calculate the pressed numbers?user will press any number for the first variable and second variable.meaning, user press number 1(which is for the first variable) and user press number 2(which is for second variable).how can i total them up?i am a bit at sea here.aha.hope you can come out with something that can help me.Thanks anyway for your concern.

You need some way of telling the program you're finished entering the 1st value and ready to enter the 2nd value, otherwise subsequent button presses will just continue appending digits onto the existing value, as in the 1st part of your problem. This signal that you're ready to enter the 2nd value is done either with an 'Enter' key in an RPN calculator (which, by the order of events you described, sounds like the kind of calculator you're trying to implement), or, by pressing the operator key in an infix notation calculator (which is the more common type outside of the engineering community).

Either way, you're going to need some mechanism for temporarily storing the values while your user is typing in the 2nd value. With an RPN calculator, this mechanism is a stack. (Google "stack data structure"). In an infix notation calculator, you can fudge a little by just implementing a couple of variables to use as temporary registers, but you also need to store which operator is currently being carried out, as this needs to be "remembered" by your software while the user is typing the 2nd number, and performed when he presses the 'equals' button.

Encoding the operator state can be done simply by arbitrarily mapping the different operators to different numbers and storing them in a variable. You could say 0 = no state (no operator pressed yet, or 'clear' state), 1 = addition, 2 = subtraction, 3 = multiplication, etc. That way, when the equals key is pressed, you simply check the value of your state variable and perform the appropriate calculation on the values in your register variables and the current number in the display.
• 0

Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid

### #5 farhanyun91

farhanyun91

CC Regular

• Member
• 46 posts

Posted 20 February 2012 - 09:22 AM

In order to treat multiple button presses as a single number, you need some way of transforming the data as the number is being entered. For instance, to enter the number "12" (twelve), the user would first press '1' followed by '2'. In the 1st solution, you treat these numbers as strings and perform string concatenation (literally, appending one string to the other.) So the string "1" gets put into the text box on the 1st button press, and then the string "2" gets appended onto the existing string on the 2nd button press, resulting in the string "12", which may be parsed as a number when your user presses an operator button.

In the 2nd solution, if you treat the number as a number rather than a string, then you need some way of "pushing" the existing digits down while adding the new digits in order to build your number. Numbers are entered into a calculator greatest significant digit first, so for our example above, if the user wants to enter "12", he presses "1" first (the 10's digit), and then "2" (the 1's digit.) When he presses 1, your algorithm takes the value that's already there (0, because nothing has been entered yet), multiplies it by 10 to "push" it down one digit, (still 0, because 0 times anything is still zero), and finally adds the value of the most recent button press, (0 + 1 = 1). Now repeat the process for the 2nd button press. Multiply the existing value by 10 to "push" it down, (1 * 10 = 10), then add 2. (10 + 2 = 12) Make sense? It keeps working for more digits as well. Suppose now you want to enter 123. Just press 3, and it multiplies the existing value by 10 (12 * 10 = 120) and adds 3 (120 + 3 = 123). Try experimenting with that algorithm with pencil and paper with a few more examples and you'll see how it works.

okay i think im getting to understand more.is it possible for me to use Cstr to transform integer to string?

You need some way of telling the program you're finished entering the 1st value and ready to enter the 2nd value, otherwise subsequent button presses will just continue appending digits onto the existing value, as in the 1st part of your problem. This signal that you're ready to enter the 2nd value is done either with an 'Enter' key in an RPN calculator (which, by the order of events you described, sounds like the kind of calculator you're trying to implement), or, by pressing the operator key in an infix notation calculator (which is the more common type outside of the engineering community).

Either way, you're going to need some mechanism for temporarily storing the values while your user is typing in the 2nd value. With an RPN calculator, this mechanism is a stack. (Google "stack data structure"). In an infix notation calculator, you can fudge a little by just implementing a couple of variables to use as temporary registers, but you also need to store which operator is currently being carried out, as this needs to be "remembered" by your software while the user is typing the 2nd number, and performed when he presses the 'equals' button.

Encoding the operator state can be done simply by arbitrarily mapping the different operators to different numbers and storing them in a variable. You could say 0 = no state (no operator pressed yet, or 'clear' state), 1 = addition, 2 = subtraction, 3 = multiplication, etc. That way, when the equals key is pressed, you simply check the value of your state variable and perform the appropriate calculation on the values in your register variables and the current number in the display.

this is seems complicated but i'll try to figure it out through your suggestion
• 0

### Also tagged with one or more of these keywords: calculator

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download