Results 1 to 6 of 6

Thread: Adding plusminus to calulator as a variable

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2008
    Posts
    11

    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!

  2. #2
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    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.

  3. #3
    Addicted Member
    Join Date
    Sep 2006
    Posts
    160

    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).

  4. #4

    Thread Starter
    New Member
    Join Date
    Feb 2008
    Posts
    11

    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
    Last edited by akumaxkun; Feb 13th, 2008 at 12:08 PM.

  5. #5
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    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.
    Doctor Ed

  6. #6

    Thread Starter
    New Member
    Join Date
    Feb 2008
    Posts
    11

    Re: Adding plusminus to calulator as a variable

    I think my teacher wants something more simple

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width