Оптимизация калькулятора - Visual Basic .NET
Формулировка задачи:
Здравствуйте! Прошу помощи!
Совсем недавно начал программировать. И решил создать калькулятор.
Вот код:
Как его можно оптимизировать? Заранее спасибо!
Листинг программы
- Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
- If TextBox1.Text <> "" Then
- TX = Val(TextBox1.Text)
- If TextBox2.Text <> "" Then
- XT = Val(TextBox2.Text)
- TextBox3.Text = TX / XT
- Else
- MsgBox("Введите второе число", MsgBoxStyle.Critical)
- End If
- Else
- MsgBox("Введите первое число!", MsgBoxStyle.Critical)
- End If
- End Sub
- Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
- If TextBox1.Text <> "" Then
- TX = Val(TextBox1.Text)
- If TextBox2.Text <> "" Then
- XT = Val(TextBox2.Text)
- TextBox3.Text = TX * XT
- Else
- MsgBox("Введите второе число", MsgBoxStyle.Critical)
- End If
- Else
- MsgBox("Введите первое число!", MsgBoxStyle.Critical)
- End If
- End Sub
- Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
- If TextBox1.Text <> "" Then
- TX = Val(TextBox1.Text)
- If TextBox2.Text <> "" Then
- XT = Val(TextBox2.Text)
- TextBox3.Text = TX - XT
- Else
- MsgBox("Введите второе число", MsgBoxStyle.Critical)
- End If
- Else
- MsgBox("Введите первое число!", MsgBoxStyle.Critical)
- End If
- End Sub
- Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
- If TextBox1.Text <> "" Then
- TX = Val(TextBox1.Text)
- If TextBox2.Text <> "" Then
- XT = Val(TextBox2.Text)
- TextBox3.Text = TX + XT
- Else
- MsgBox("Введите второе число", MsgBoxStyle.Critical)
- End If
- Else
- MsgBox("Введите первое число!", MsgBoxStyle.Critical)
- End If
- End Sub
Решение задачи: «Оптимизация калькулятора»
textual
Листинг программы
- Public Enum Operations
- Plus
- Minus
- End Enum
- Private actions As New Dictionary(Of Operations, Func(Of Integer, Integer, Integer))() From { _
- {Operations.Plus, Function(x, y) x + y}, _
- {Operations.Minus, Function(x, y) x - y} _
- }
- Public Function Math(x As Integer, y As Integer, operations As Operations) As Integer
- Return actions(operations)(x, y)
- End Function
- Public Function Math(x As String, y As String, operations As Operations) As Integer
- If x = [String].Empty OrElse y = [String].Empty Then
- Throw New Exception("X or Y is Empry!")
- End If
- Return actions(operations)(Convert.ToInt32(x), Convert.ToInt32(y))
- End Function
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д