calculator

19.54 viia 0 Comments

Public Class FormCalculator

Dim sign_Indicator As Integer = 0
Dim variable1 As Double
Dim variable2 As Double
Dim addBit As Integer = 0
Dim subBit As Integer = 0
Dim multBit As Integer = 0
Dim divBit As Integer = 0
Dim modBit As Integer = 0
Dim powerBit As Integer = 0
Dim permBit As Integer = 0
Dim combBit As Integer = 0
Dim andBit As Integer = 0
Dim orBit As Integer = 0
Dim xorBit As Integer = 0
Dim powerFunctionBit As Integer = 0
'Dim trigFunctionBit As Integer = 0
'Dim HypertrigFunctionBit As Integer = 0
'Dim InversetrigFunctionBit As Integer = 0
'Dim otherFuncsBit As Integer = 0
'Dim logicalFuncsBit As Integer = 0
' Dim memFuncsBit As Integer = 0
Dim fl As Integer = 0
Dim memoryVariable As Double = 0
'Dim scientificModeBit As Integer = 0

Private Sub btn0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn0.Click
If sign_Indicator = 0 Then
displayText.Text = displayText.Text & CStr(0)
ElseIf sign_Indicator = 1 Then
displayText.Text = 0
sign_Indicator = 0
End If
fl = 1
End Sub

Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
If sign_Indicator = 0 Then
displayText.Text = displayText.Text & CStr(1)
ElseIf sign_Indicator = 1 Then
displayText.Text = 1
sign_Indicator = 0
End If
fl = 1
End Sub

Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click
If sign_Indicator = 0 Then
displayText.Text = displayText.Text & CStr(2)
ElseIf sign_Indicator = 1 Then
displayText.Text = 2
sign_Indicator = 0
End If
fl = 1
End Sub

Private Sub btn3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn3.Click
If sign_Indicator = 0 Then
displayText.Text = displayText.Text & CStr(3)
ElseIf sign_Indicator = 1 Then
displayText.Text = 3
sign_Indicator = 0
End If
fl = 1
End Sub

Private Sub btn4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn4.Click
If sign_Indicator = 0 Then
displayText.Text = displayText.Text & CStr(4)
ElseIf sign_Indicator = 1 Then
displayText.Text = 4
sign_Indicator = 0
End If
fl = 1
End Sub

Private Sub btn5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn5.Click
If sign_Indicator = 0 Then
displayText.Text = displayText.Text & CStr(5)
ElseIf sign_Indicator = 1 Then
displayText.Text = 5
sign_Indicator = 0
End If
fl = 1
End Sub

Private Sub btn6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn6.Click
If sign_Indicator = 0 Then
displayText.Text = displayText.Text & CStr(6)
ElseIf sign_Indicator = 1 Then
displayText.Text = 6
sign_Indicator = 0
End If
fl = 1
End Sub

Private Sub btn7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn7.Click
If sign_Indicator = 0 Then
displayText.Text = displayText.Text & CStr(7)
ElseIf sign_Indicator = 1 Then
displayText.Text = 7
sign_Indicator = 0
End If
fl = 1
End Sub

Private Sub btn8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn8.Click
If sign_Indicator = 0 Then
displayText.Text = displayText.Text & CStr(8)
ElseIf sign_Indicator = 1 Then
displayText.Text = 8
sign_Indicator = 0
End If
fl = 1
End Sub

Private Sub btn9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn9.Click
If sign_Indicator = 0 Then
displayText.Text = displayText.Text & CStr(9)
ElseIf sign_Indicator = 1 Then
displayText.Text = 9
sign_Indicator = 0
End If
fl = 1
End Sub

Private Sub displayText_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles displayText.KeyPress
e.Handled = True
If e.KeyChar Like "[0-9]" Or e.KeyChar = Chr(&H8) Then
e.Handled = False
End If
End Sub

Private Sub btnSign_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSign.Click
If displayText.Text.Length = 0 Then
displayText.Text = displayText.Text + CStr("-")
ElseIf displayText.Text <> "." Then
displayText.Text = displayText.Text * -1
End If
End Sub

Private Sub btnClearAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClearAll.Click
displayText.Clear()
sign_Indicator = 0
variable1 = 0
variable2 = 0
memoryVariable = 0
reset_SignBits()
End Sub

Private Sub btnClearTextbox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClearTextbox.Click
displayText.Clear()
End Sub
Private Function reset_SignBits()
addBit = 0
subBit = 0
multBit = 0
divBit = 0
modBit = 0
powerBit = 0
permBit = 0
combBit = 0
andBit = 0
orBit = 0
xorBit = 0
fl = 0
Return 0
End Function

Private Function permutation(ByVal variable1, ByVal variable2)
Dim result As Double
result = Factorial(variable1) / Factorial(variable1 - variable2)
Return result
End Function

Private Function combination(ByVal variable1, ByVal variable2)
Dim result As Double
result = Factorial(variable1) / (Factorial(variable2) * Factorial(variable1 - variable2))
Return result
End Function
Private Function Calculate()
If displayText.Text <> "." Then

variable2 = displayText.Text
If fl = False Then
variable1 = variable2
ElseIf addBit = 1 Then
variable1 = variable1 + variable2
ElseIf subBit = 1 Then
variable1 = variable1 - variable2
ElseIf multBit = 1 Then
variable1 = variable1 * variable2
ElseIf divBit = 1 Then
variable1 = variable1 / variable2
ElseIf modBit = 1 Then
variable1 = variable1 Mod variable2
ElseIf powerBit = 1 Then
variable1 = Math.Pow(variable1, variable2)
ElseIf permBit = 1 Then
variable1 = permutation(variable1, variable2)
ElseIf combBit = 1 Then
variable1 = combination(variable1, variable2)
ElseIf andBit = 1 Then
variable1 = variable1 And variable2
ElseIf orBit = 1 Then
variable1 = variable1 Or variable2
ElseIf xorBit = 1 Then
variable1 = variable1 Xor variable2
Else
variable1 = variable2
End If
displayText.Text = CStr(variable1)

End If
Return 0
End Function

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
If displayText.Text.Length <> 0 Then
Calculate()
reset_SignBits()
addBit = 1
sign_Indicator = 1
End If
End Sub

Private Sub btnSub_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSub.Click

If displayText.Text.Length <> 0 Then
variable2 = displayText.Text
Calculate()
reset_SignBits()
subBit = 1
sign_Indicator = 1
End If
End Sub

Private Sub btnMult_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMult.Click
If displayText.Text.Length <> 0 Then
Calculate()
reset_SignBits()
multBit = 1
sign_Indicator = 1
End If
End Sub

Private Sub btnDiv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDiv.Click
If displayText.Text.Length <> 0 Then
Calculate()
reset_SignBits()
divBit = 1
sign_Indicator = 1
End If
End Sub

Private Sub btnModulus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModulus.Click
If displayText.Text.Length <> 0 Then
Calculate()
reset_SignBits()
modBit = 1
sign_Indicator = 1
End If
End Sub

Private Sub btnPower_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPower.Click
If displayText.Text.Length <> 0 Then
Calculate()
reset_SignBits()
powerBit = 1
sign_Indicator = 1
End If
End Sub
Private Sub btnEqual_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEqual.Click

If displayText.Text.Length <> 0 Then
Calculate()
reset_SignBits()
End If
sign_Indicator = 1
End Sub

Private Sub btnDecimal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDecimal.Click
Dim i As Integer
Dim chr As Char
Dim decimal_Indicator As Integer = 0

If sign_Indicator <> 1 Then
For i = 0 To displayText.Text.Count - 1
chr = displayText.Text(i)
If chr = "." Then
decimal_Indicator = 1
End If
Next

If decimal_Indicator <> 1 Then
displayText.Text = displayText.Text & CStr(".")
End If
End If
End Sub

Private Sub btnBackSpace_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBackSpace.Click
If displayText.Text.Length <> 0 Then
displayText.Text = displayText.Text.Remove(displayText.Text.Length - 1, 1)
End If
End Sub














Private Sub btnsqrt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsqrt.Click
If displayText.Text.Length <> 0 Then
If displayText.Text <> "." Then
displayText.Text = Math.Sqrt(displayText.Text)
End If
sign_Indicator = 1
End If
End Sub

Public Function Factorial(ByVal temp)
Dim result As Double = temp
If temp = 0 Then
Return 1
Else
For i = temp - 1 To 1 Step -1
result = result * i
Next
End If
Return result
End Function

Private Sub btnSquare_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSquare.Click
If displayText.Text.Length <> 0 Then
displayText.Text = displayText.Text * displayText.Text
End If
sign_Indicator = 1
End Sub

Private Sub btnCube_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCube.Click
If displayText.Text.Length <> 0 Then
displayText.Text = displayText.Text * displayText.Text * displayText.Text
End If
sign_Indicator = 1
End Sub

Private Sub btnxPower4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnxPower4.Click
If displayText.Text.Length <> 0 Then
displayText.Text = displayText.Text * displayText.Text * displayText.Text * displayText.Text
End If
sign_Indicator = 1
End Sub

Private Sub btnM_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnM.Click
If displayText.Text.Length <> 0 Then
If memoryVariable = 0 Then
memoryVariable = CDbl(displayText.Text)
End If
End If
sign_Indicator = 1
displayText.Text = CStr(memoryVariable)
End Sub

Private Sub btnMplus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMplus.Click
If memoryVariable <> 0 Then
memoryVariable = memoryVariable + memoryVariable
End If
sign_Indicator = 1
displayText.Text = CStr(memoryVariable)
End Sub

Private Sub btnMminus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMminus.Click
If memoryVariable <> 0 Then
memoryVariable = memoryVariable - memoryVariable
End If
sign_Indicator = 1
displayText.Text = CStr(memoryVariable)
End Sub
End Class

0 komentar: