如何制作 VB.NET MDAS 计算器?

How can I make a VB.NET MDAS Calculator?

我已经有了代码,但我不知道如何添加MDAS功能。这是我的代码:

Public Class Form1
'variables-----------------------------------------------------------------------------
Dim inp As Double = 0
Dim oprtn As String
Dim expr As Boolean = False
'numbers and decimal input-----------------------------------------------------------------------------
Private Sub Button_Click(sender As Object, e As EventArgs) Handles Number1.Click, Number9.Click, Number8.Click, Number7.Click, Number6.Click, Number5.Click, Number4.Click, Number3.Click, Number2.Click, Number0.Click, Period.Click
    Dim num As Button = sender
    If ((TextBox1.Text = "0") Or (expr)) Then
        TextBox1.Clear()
        TextBox1.Text = num.Text
        expr = False
    ElseIf (num.Text = ".") Then
        If (Not TextBox1.Text.Contains(".")) Then
            TextBox1.Text = TextBox1.Text + num.Text
        End If
    Else
        TextBox1.Text = TextBox1.Text + num.Text
    End If
End Sub
'operators button-----------------------------------------------------------------------------
Private Sub oprtrs(sender As Object, e As EventArgs) Handles PlusSign.Click, MinusSign.Click, MultiplySign.Click, DivideSign.Click
    Dim operations As Button = sender
    If (inp <> 0) Then
        Equals.PerformClick()
        expr = True
        oprtn = operations.Text
        Label1.Text = inp & "   " & oprtn
    Else
        oprtn = operations.Text
        inp = Double.Parse(TextBox1.Text)
        expr = True
        Label1.Text = inp & "   " & oprtn
    End If
End Sub
'equals button-----------------------------------------------------------------------------
Private Sub Equal_Click(sender As Object, e As EventArgs) Handles Equals.Click
    Label1.Text = ""
    Select Case oprtn
        Case "+"
            TextBox1.Text = (inp + Double.Parse(TextBox1.Text)).ToString()
        Case "-"
            TextBox1.Text = (inp - Double.Parse(TextBox1.Text)).ToString()
        Case "*"
            TextBox1.Text = (inp * Double.Parse(TextBox1.Text)).ToString()
        Case "/"
            TextBox1.Text = (inp / Double.Parse(TextBox1.Text)).ToString()
    End Select

    inp = Double.Parse(TextBox1.Text)
    oprtn = ""
End Sub
'other buttons-----------------------------------------------------------------------------
Private Sub Button13_Click(sender As Object, e As EventArgs) Handles Backspace.Click
    If TextBox1.Text < " " Then
        TextBox1.Text = Mid(TextBox1.Text, 1, Len(TextBox1.Text) - 1 + 1)
    Else
        TextBox1.Text = Mid(TextBox1.Text, 1, Len(TextBox1.Text) - 1)
    End If
End Sub

Private Sub Button14_Click(sender As Object, e As EventArgs) Handles Clear.Click
    TextBox1.Text = "0"
End Sub

Private Sub Button15_Click(sender As Object, e As EventArgs) Handles ClearEverything.Click
    TextBox1.Text = "0"
    Label1.Text = ""
End Sub

Private Sub Button20_Click(sender As Object, e As EventArgs) Handles Prcent.Click
    Dim percent As Double
    percent = Convert.ToDouble(TextBox1.Text) / Convert.ToDouble(100)
    TextBox1.Text = System.Convert.ToString(percent)
End Sub

Private Sub Button21_Click(sender As Object, e As EventArgs) Handles SqrRoot.Click
    Dim squared As Double
    squared = Math.Sqrt(TextBox1.Text)
    TextBox1.Text = System.Convert.ToString(squared)
    Label1.Text = "Sqr(" & System.Convert.ToString(squared) & ")"
End Sub

Private Sub Button22_Click(sender As Object, e As EventArgs) Handles RaiseTwo.Click
    Dim expo2 As Double
    expo2 = Convert.ToDouble(TextBox1.Text) * Convert.ToDouble(TextBox1.Text)
    TextBox1.Text = System.Convert.ToString(expo2)
End Sub

Private Sub Button23_Click(sender As Object, e As EventArgs) Handles RaiseThree.Click
    Dim expo3 As Double
    expo3 = Convert.ToDouble(TextBox1.Text) * Convert.ToDouble(TextBox1.Text) * Convert.ToDouble(TextBox1.Text)
    TextBox1.Text = System.Convert.ToString(expo3)
End Sub

Private Sub Button24_Click(sender As Object, e As EventArgs) Handles OneOver.Click
    Dim ov1 As Double
    ov1 = Convert.ToDouble(1.0 / Convert.ToDouble(TextBox1.Text))
    TextBox1.Text = System.Convert.ToString(ov1)
End Sub

结束Class

这就是我所做的几乎所有工作,它按预期工作,但它不遵循 MDAS,MDAS 优先考虑乘法,然后是除法,然后是加法,然后是减法。我的设计是这样的:

I started with the interface first then code, I haven't really designed it yet. This is just to give you an idea what is already coded or included in the code. Every button functions as should be.

这就是大卫所说的。

需要的代码很少。

Public Class ExpressionCalculator
    Private Sub Buttons_Click(sender As Object, e As EventArgs) Handles ButtonPlus.Click, ButtonMultiply.Click, ButtonMinus.Click, ButtonDot.Click, ButtonDivide.Click, Button9.Click, Button8.Click, Button7.Click, Button6.Click, Button5.Click, Button4.Click, Button3.Click, Button2.Click, Button10.Click, Button1.Click
        Dim tb = DirectCast(sender, Button)
        TextBox1.Text &= tb.Text
    End Sub

    Private Sub ButtonCalculate_Click(sender As Object, e As EventArgs) Handles ButtonCalculate.Click
        Dim table = New DataTable()
        Dim result = table.Compute(TextBox1.Text, Nothing)
        TextBox1.Text = result.ToString
    End Sub
End Class