Adding plusminus to calulator as a variable
Hey guys,
Im making a calculator right now and the only thing i have left to put in is the +/- function.
My teacher does not want us to use:
lblOutput.caption = lblOutput.caption - (lblOutput.caption * 2)
Rather, she wants us to use variables to be able to make a number +/-.
I've run out of ideas on how to do that.
Please help!
Thanks!
Re: Adding plusminus to calulator as a variable
Wouldn't you just Multiply by -1? (a positive value would become negative and a negative value become positive) Straightforward mathematics.
Re: Adding plusminus to calulator as a variable
If I understand what you say the teacher wants you to do... you shouldn't be changing the caption directly, and instead declare some variable as Integer, Long, Double or Single (depending the precission you need) and do the operations with it :S.
Quote:
Originally Posted by Doogle
Wouldn't you just Multiply by -1? (a positive value would become negative and a negative value become positive) Straightforward mathematics.
Yep, and that's the same as doing: var = -(var).
Re: Adding plusminus to calulator as a variable
I'm new to VB so how would i put it in as a code?
t = -(t) ? I tried this and it did not work
this is what i have so far:
Code:
Public x As Double
Public y As Double
Public z As Double
Public w As Integer
Public t As Double
Private Sub cmd1_Click()
lblOutput.Caption = lblOutput.Caption + "1"
End Sub
Private Sub cmd2_Click()
lblOutput.Caption = lblOutput.Caption + "2"
End Sub
Private Sub cmd3_Click()
lblOutput.Caption = lblOutput.Caption + "3"
End Sub
Private Sub cmd4_Click()
lblOutput.Caption = lblOutput.Caption + "4"
End Sub
Private Sub cmd5_Click()
lblOutput.Caption = lblOutput.Caption + "5"
End Sub
Private Sub cmd6_Click()
lblOutput.Caption = lblOutput.Caption + "6"
End Sub
Private Sub cmd7_Click()
lblOutput.Caption = lblOutput.Caption + "7"
End Sub
Private Sub cmd8_Click()
lblOutput.Caption = lblOutput.Caption + "8"
End Sub
Private Sub cmd9_Click()
lblOutput.Caption = lblOutput.Caption + "9"
End Sub
Private Sub cmd0_Click()
lblOutput.Caption = lblOutput.Caption + "0"
End Sub
Private Sub cmdDecimal_Click()
lblOutput.Caption = lblOutput.Caption + "."
End Sub
Private Sub cmdClear_Click()
lblOutput.Caption = ""
End Sub
Private Sub cmdPlusMinus_Click()
t =-(t)
End Sub
Private Sub cmdEqual_Click()
Dim strlength As Integer
Dim temp As Integer
Dim temp_op As String
If w = 1 Then
temp_op = "+"
ElseIf w = 2 Then
temp_op = "-"
ElseIf w = 3 Then
temp_op = "*"
Else
temp_op = "/"
End If
temp = InStr(lblOutput.Caption, temp_op)
strlength = Len(lblOutput.Caption)
y = CInt(Right(lblOutput.Caption, strlength - temp))
If w = 1 Then
z = x + y
ElseIf w = 2 Then
z = x - y
ElseIf w = 3 Then
z = x * y
Else
z = x / y
End If
lblOutput.Caption = lblOutput.Caption + "=" & z
End Sub
Private Sub cmdMultiply_Click()
x = CDbl(lblOutput.Caption)
lblOutput.Caption = lblOutput.Caption + "*"
w = 3
End Sub
Private Sub cmdPlus_Click()
x = CDbl(lblOutput.Caption)
lblOutput.Caption = lblOutput.Caption + "+"
w = 1
End Sub
Private Sub cmdSubtract_Click()
x = CDbl(lblOutput.Caption)
lblOutput.Caption = lblOutput.Caption + "-"
w = 2
End Sub
Private Sub cmdDivide_Click()
x = CDbl(lblOutput.Caption)
lblOutput.Caption = lblOutput.Caption + "/"
w = 4
End Sub
Re: Adding plusminus to calulator as a variable
Here's how I do it. At the point the +/- key is pressed, you change the display and perform no calculations. The calculations eventually work on whatever is in the display:
Code:
' +/- pressed'
If Readout <> "0." Then
If Left$(Readout, 1) = "-" Then Readout = Mid$(Readout, 2) Else Readout = "-" & Readout
End If
Readout is whatever string goes into the text box. Note that the Windows calculator does not display a -0. in the display and leaves it alone if 0. is there. However, this is somewhat optional, because my Sharp does add the minus sign if the 0 key was pressed prior to the +/- key.
Re: Adding plusminus to calulator as a variable
I think my teacher wants something more simple