Calculator Logic

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2004
Posts: 1
Reputation: Proton is an unknown quantity at this point 
Solved Threads: 0
Proton Proton is offline Offline
Newbie Poster

Calculator Logic

 
0
  #1
Oct 29th, 2004
Hi, first off I'd like to say that this is my first post here. I just finished typing up a long explanation of my problem. And when I submitted it, I wasn't logged in anymore, so I logged in, but then subsequently lost my entire post. All I can say is that is VERY frustrating. Other than that, glad to be here.

I am a student trying to build a working calculator like the windows calculator. Let me say that I have definitely put alot of time into this already and I can't figure it out. I spent around a whole day on this one piece of logic. I have no problem doing 1+1 = 2 but when it comes to doing 1*1*1 = 3 I am encountering some logic trouble.

My code takes the first value entered by the user when the user clicks the plus button and adds it to my variable Operand2. From there, I set Operand1 = operand1 + operand2. With this logic I can add numerous time before hitting the equals button.

That logic works great for the plus button, but when it comes to the subtract, multiply, and divide button, I am running into trouble because 0+1= 1 so that works great. But 0*1=0 and I need it to equal 1, because operand 1 should be equal to the product of operand 1 and 2 so that I can multiply numerous times before hitting the equal button. I hope I've been able to explain my dillema well enough.

Here is my Code, any insights you have would be GREATLY appreciated.

In my btnAdd event handler
Calc.operand2 = convert.ToDouble(me.txtInput.text)
Calc.operator = "add"
me.txtInput.clear
calc.result

In my Calc Class

Public Function Result() As Double
Select Case sOperator
Case is = "add"
Result = dOperand1 + dOperand2
dOperand1 = Result
End Select
End Function

In my btnEquals event handler
Calc.Operand2 = Convert.ToDouble(Me.txtInput.text)
Me.txtInput.Clear()
Me.txtInput.text = Calc.Result.ToString
Calc.Operand1 = Nothing


I hope this post wasn't too long and confusing. :o Thanks for reading
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 1
Reputation: jhayrbrizuela is an unknown quantity at this point 
Solved Threads: 0
jhayrbrizuela jhayrbrizuela is offline Offline
Newbie Poster

Re: Calculator Logic

 
0
  #2
Jul 12th, 2007
Please... Post About d code of VB calculator by keypress in keyboard please send to my email jhayrbrizuela2@yahoo.com and the syntax of keypress.. thank you..!!!
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 65
Reputation: SolTec is an unknown quantity at this point 
Solved Threads: 4
SolTec SolTec is offline Offline
Junior Poster in Training

Re: Calculator Logic

 
0
  #3
Feb 21st, 2008
Originally Posted by Proton View Post
Hi, first off I'd like to say that this is my first post here. I just finished typing up a long explanation of my problem. And when I submitted it, I wasn't logged in anymore, so I logged in, but then subsequently lost my entire post. All I can say is that is VERY frustrating. Other than that, glad to be here.

I am a student trying to build a working calculator like the windows calculator. Let me say that I have definitely put alot of time into this already and I can't figure it out. I spent around a whole day on this one piece of logic. I have no problem doing 1+1 = 2 but when it comes to doing 1*1*1 = 3 I am encountering some logic trouble.

My code takes the first value entered by the user when the user clicks the plus button and adds it to my variable Operand2. From there, I set Operand1 = operand1 + operand2. With this logic I can add numerous time before hitting the equals button.

That logic works great for the plus button, but when it comes to the subtract, multiply, and divide button, I am running into trouble because 0+1= 1 so that works great. But 0*1=0 and I need it to equal 1, because operand 1 should be equal to the product of operand 1 and 2 so that I can multiply numerous times before hitting the equal button. I hope I've been able to explain my dillema well enough.

Here is my Code, any insights you have would be GREATLY appreciated.

In my btnAdd event handler
Calc.operand2 = convert.ToDouble(me.txtInput.text)
Calc.operator = "add"
me.txtInput.clear
calc.result

In my Calc Class

Public Function Result() As Double
Select Case sOperator
Case is = "add"
Result = dOperand1 + dOperand2
dOperand1 = Result
End Select
End Function

In my btnEquals event handler
Calc.Operand2 = Convert.ToDouble(Me.txtInput.text)
Me.txtInput.Clear()
Me.txtInput.text = Calc.Result.ToString
Calc.Operand1 = Nothing


I hope this post wasn't too long and confusing. :o Thanks for reading
your code is a bit confusing, since the gui for a calulator is just capturing input of the numbers. forcing the user to enter only numbers (IsNumeric) function will keep that.

Dim i as Integer
i = txtTextboxtName.text
Logic if the + addition or - subtraction or *or multiplication or division / buttons are pressed to add, subtract, multiply or divide your integer. Each time you press the +,-,* or / the txtTextboxName.text is string.empty or "", so you don't have 1 =1+ 11, instead of 1 +1 pressing equal would give you 2

You can also format your number by using CDbl for double or CInt for Integer or CStr for String

i += i in addition
i -= i in subtraction
i *= i in multiplication
i /= i in division

if you hit = then a total of i is made
if you hit +, -, * or / the i value is continued

txtTextboxName. text = string.empty
i = 0
to start over
Last edited by SolTec; Feb 21st, 2008 at 6:59 pm.
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC