2016-04-26 96 views
0

我已經把這段代碼停止插入兩個操作符,正在+,*,/但 不工作的「 - 」,當試圖把(-3-3)你必須按等於按鈕來允許其他運營商被插入計算器微軟視覺工作室2010

Public Class Form1 

    'Global variable to check if equals has been pressed 
    Dim is_equals_pressed As Boolean = False 

    Dim operator_count As Integer = 0 
    Dim allowed_input As Boolean = False 


Private Sub btnMinus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMinus.Click 
     operator_count += 1 

     allowed_input = Check_Values() 
     If allowed_input <> False Then 

      If txtAnswer.Text = "" Then 

       If Check_Values() = False Then 
        txtAnswer.Focus() 
        Exit Sub 
       End If 
      End If 

      If is_equals_pressed = True Then 
       txtTyped.Text = txtAnswer.Text & "-" 
       txtAnswer.Text = "" 'clear the text box 
       is_equals_pressed = False 

      Else 

       txtTyped.Text += txtAnswer.Text & "-" 
       txtAnswer.Text = "" 'clear the text box 
       txtAnswer.Focus() 
      End If 
     End If 
    End Sub 


    Private Sub btnMultiply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMultiply.Click 
     operator_count += 1 

     allowed_input = Check_Values() 
     If allowed_input <> False Then 

      If txtAnswer.Text = "" Then 

       If Check_Values() = False Then 
        txtAnswer.Focus() 
        Exit Sub 
       End If 
      End If 

      If is_equals_pressed = True Then 
       txtTyped.Text = txtAnswer.Text & "*" 
       txtAnswer.Text = "" 'clear the text box 
       is_equals_pressed = False 

      Else 

       txtTyped.Text += txtAnswer.Text & "*" 
       txtAnswer.Text = "" 'clear the text box 
       txtAnswer.Focus() 
      End If 
     End If 
    End Sub 

Private Sub btnEquals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEquals.Click 
     Dim equation As String 


     operator_count = 0 

     'Cos, Sin, Tan 
     Dim num1 As String 
     'End 

     Dim i As Integer = 1 

     txtTyped.Text += txtAnswer.Text 
     'Capture value for Cos, Sin, Tan calculation 
     num1 = txtAnswer.Text 
     'End 

     txtAnswer.Text = ("") 'Clear the text box 

     equation = txtTyped.Text 

     'Check if equals has been pressed 
     is_equals_pressed = True 

     'Check for +, -, =,/simbol in string 
     For i = 1 To equation.Length - 1 
      If equation(i) = "+" Then 
       addNumbers(equation) 
      ElseIf equation(i) = "-" Then 
       subtrtactNumbers(equation) 
      ElseIf equation(i) = "*" Then 
       multiplyNumbers(equation) 
      ElseIf equation(i) = "/" Then 
       divideNumbers(equation) 
      ElseIf equation(i) = "^" Then 
       exponentNumber(equation) 
      ElseIf equation(i) = "%" Then 
       modulusNumbers(equation) 
      ElseIf equation.Contains("1/") Then 
       inverseNumbers(equation) 

       'We do this calculation in btnCos_Click 
      ElseIf equation.Contains("Cos") Then 
      ElseIf equation.Contains("Sin") Then 
      ElseIf equation.Contains("Tan") Then 

      End If 
     Next 

     txtAnswer.Select(txtAnswer.Text.Length, 0) 

    End Sub 

Private Function Check_Values() As Boolean 



     If operator_count > 1 Then 
      allowed_input = False 
     Else 
      allowed_input = True 
     End If 

     Check_Values = allowed_input 

    End Function 

回答

1

(-3-3)在這裏,你已經有兩個「 - 」運營商,因爲第一負也作爲一個經營者,所以Check_Values處理()返回假。要解決這個問題,在btnMinus_Click你可以檢查減號是不是第一個字符,只有在這種情況下確認operator_count。

+0

你可以輸入代碼,請更有意義感謝您的回覆 – Rakan

+0

我不擅長VB,但我會嘗試 –

+0

我認爲這應該足以添加'如果txtTyped.Text ==「 「然後返回'在'btnMinus_Click'開頭 –