このコードはまだ作りかけのものです。※ 見ると目から汗がでてくるのは気のせいではありません。
Namespace Torikobito.WankumaFestival.No1
Public Class Calculator
Public Sub New()
Me.Initialize()
End Sub
Public Sub Initialize()
Me.DisplayText = "0"
Me._memo = Decimal.Zero
Me._operation = ArithmeticOperation.None
Me._nextInputIsNew = True
End Sub
Private _displayText As String
Public Property DisplayText() As String
Get
Return _displayText
End Get
Private Set(ByVal value As String)
_displayText = value
End Set
End Property
Private Shared ReadOnly _validInputNumbers As Decimal() = {1D, 2D, 3D, 4D, 5D, 6D, 7D, 8D, 9D, 0D}
Public Shared ReadOnly Property ValidInputNumbers() As System.Collections.ObjectModel.ReadOnlyCollection(Of Decimal)
Get
Return New System.Collections.ObjectModel.ReadOnlyCollection(Of Decimal)(Calculator._validInputNumbers)
End Get
End Property
Public Sub InputNumber(ByVal number As Decimal)
If Not Calculator.ValidInputNumbers.Contains(number) Then
Exit Sub
End If
Dim newText As String
If Me._nextInputIsNew Then
newText = "0"
Me._nextInputIsNew = False
Else
newText = Me.DisplayText
End If
If newText = "0" Then
newText = number.ToString
Else
newText &= number.ToString
End If
Dim parseValue As Decimal
If Decimal.TryParse(newText, parseValue) Then
Me.DisplayText = newText
End If
End Sub
Public Enum ArithmeticOperation
None
Addition
Subtraction
Multiplication
Division
End Enum
Private _memo As Decimal
Private _operation As Calculator.ArithmeticOperation
Private _nextInputIsNew As Boolean
Public Sub SetArithmeticOperation(ByVal op As Calculator.ArithmeticOperation)
Me.Calculate()
Me._memo = Decimal.Parse(Me.DisplayText)
Me._operation = op
Me._nextInputIsNew = True
End Sub
Public Sub EndCalculation()
Me.Calculate()
Me._memo = Decimal.Zero
Me._operation = ArithmeticOperation.None
Me._nextInputIsNew = True
End Sub
Private Sub Calculate()
Dim current As Decimal = Decimal.Parse(Me.DisplayText)
Dim calcResult As Decimal
Select Case Me._operation
Case ArithmeticOperation.Addition
calcResult = Decimal.Add(Me._memo, current)
Case ArithmeticOperation.Subtraction
calcResult = Decimal.Subtract(Me._memo, current)
Case ArithmeticOperation.Multiplication
calcResult = Decimal.Multiply(Me._memo, current)
Case ArithmeticOperation.Division
calcResult = Decimal.Divide(Me._memo, current)
Case Else
Exit Sub
End Select
Me.DisplayText = calcResult.ToString
End Sub
Public Sub SetDecimalPoint()
If Me.DisplayText.Contains(".") Then
Exit Sub
End If
Me.DisplayText &= "."
End Sub
Public Sub Negate()
Dim newText As String = Me.DisplayText
If newText = "0" Then
Exit Sub
End If
If newText.Contains("-") Then
newText = newText.Replace("-", String.Empty)
Else
newText = "-" & newText
End If
Dim parseValue As Decimal
If Decimal.TryParse(newText, parseValue) Then
Me.DisplayText = newText
End If
End Sub
Public Sub Clear()
Me.DisplayText = "0"
Me._memo = Decimal.Zero
Me._operation = ArithmeticOperation.None
Me._nextInputIsNew = True
End Sub
End Class
End Namespace
どんだけぇ~!