Guest User

TR Code

a guest
Aug 2nd, 2023
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 18.42 KB | Source Code | 0 0
  1. Imports System.Globalization
  2. Imports System.Text
  3. Imports System.Text.RegularExpressions
  4. Public Class Form1
  5.     Public Property zahl As String = ""
  6.     Public Property calc As String = ""
  7.  
  8.     Dim input As String = "CultureInfo.InvariantCulture"
  9.  
  10.  
  11.     Private Function GetDezimalTrennzeichen() As String
  12.         Return CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator
  13.     End Function
  14.  
  15.     Private WithEvents b1Button As Button ' WithEvents-Variable für den Button b1
  16.     Private WithEvents b2Button As Button ' WithEvents-Variable für den Button b2
  17.  
  18.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  19.         ' Event Handler für die Steuerelemente zuweisen
  20.         b1Button = b1 ' Hier b1 durch den tatsächlichen Namen des Buttons im Designer ersetzen
  21.         b2Button = b2 ' Hier b2 durch den tatsächlichen Namen des Buttons im Designer ersetzen
  22.         ' Weitere Event Handler für die restlichen Steuerelemente zuweisen
  23.     End Sub
  24.  
  25.     Private Sub b1_Click(sender As Object, e As EventArgs) Handles b1.Click
  26.         tb.Text += "1"
  27.         Debug.WriteLine("Current expression: " & tb.Text)
  28.     End Sub
  29.  
  30.  
  31.     Private Sub b2_Click(sender As Object, e As EventArgs) Handles b2.Click
  32.         tb.Text += "2"
  33.         Debug.WriteLine("Current expression: " & tb.Text)
  34.     End Sub
  35.  
  36.  
  37.     Private Sub b3_Click(sender As Object, e As EventArgs) Handles b3.Click
  38.         tb.Text += "3"
  39.         Debug.WriteLine("Current expression: " & tb.Text)
  40.     End Sub
  41.  
  42.  
  43.     Private Sub b4_Click(sender As Object, e As EventArgs) Handles b4.Click
  44.         tb.Text += "4"
  45.         Debug.WriteLine("Current expression: " & tb.Text)
  46.     End Sub
  47.  
  48.  
  49.     Private Sub b5_Click(sender As Object, e As EventArgs) Handles b5.Click
  50.         tb.Text += "5"
  51.         Debug.WriteLine("Current expression: " & tb.Text)
  52.     End Sub
  53.  
  54.  
  55.     Private Sub b6_Click(sender As Object, e As EventArgs) Handles b6.Click
  56.         tb.Text += "6"
  57.         Debug.WriteLine("Current expression: " & tb.Text)
  58.     End Sub
  59.  
  60.  
  61.     Private Sub b7_Click(sender As Object, e As EventArgs) Handles b7.Click
  62.         tb.Text += "7"
  63.         Debug.WriteLine("Current expression: " & tb.Text)
  64.     End Sub
  65.  
  66.  
  67.     Private Sub b8_Click(sender As Object, e As EventArgs) Handles b8.Click
  68.         tb.Text += "8"
  69.         Debug.WriteLine("Current expression: " & tb.Text)
  70.     End Sub
  71.  
  72.  
  73.     Private Sub b9_Click(sender As Object, e As EventArgs) Handles b9.Click
  74.         tb.Text += "9"
  75.         Debug.WriteLine("Current expression: " & tb.Text)
  76.     End Sub
  77.  
  78.  
  79.     Private Sub b0_Click(sender As Object, e As EventArgs) Handles b0.Click
  80.         tb.Text += "0"
  81.         Debug.WriteLine("Current expression: " & tb.Text)
  82.     End Sub
  83.  
  84.  
  85.     Private Sub bComma_Click(sender As Object, e As EventArgs) Handles bcomma.Click
  86.         tb.Text += ","
  87.         Debug.WriteLine("Current expression: " & tb.Text)
  88.     End Sub
  89.  
  90.  
  91.  
  92.  
  93.     Private Sub bdel_Click(sender As Object, e As EventArgs) Handles bdel.Click
  94.         If tb.Text.Length > 0 Then
  95.             tb.Text = tb.Text.Substring(0, tb.Text.Length - 1)
  96.         End If
  97.     End Sub
  98.  
  99.     Private Sub bac_Click(sender As Object, e As EventArgs) Handles bac.Click
  100.         zahl = ""
  101.         calc = ""
  102.         tb.Text = ""
  103.     End Sub
  104.  
  105.  
  106.     Private Sub bsolve_Click(sender As Object, e As EventArgs) Handles bsolve.Click
  107.         Dim input As String = tb.Text
  108.  
  109.  
  110.         Dim decimalSeparator As String = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator
  111.         input = input.Replace(",", ".")
  112.  
  113.  
  114.  
  115.         tb.Text = input
  116.  
  117.         ' Quadratwurzel berechnen
  118.         Dim sqrtRegex As New Regex("sqrt\(([^()]+)\)")
  119.         While sqrtRegex.IsMatch(input)
  120.             input = sqrtRegex.Replace(input, Function(match)
  121.                                                  Dim innerExpression As String = match.Groups(1).Value
  122.                                                  Dim result As Double = EvaluateExpression(innerExpression)
  123.                                                  If result >= 0 Then
  124.                                                      Return Math.Sqrt(result).ToString()
  125.                                                  Else
  126.                                                      Throw New ArithmeticException("Negatives Ergebnis")
  127.                                                  End If
  128.                                              End Function)
  129.         End While
  130.  
  131.         ' Kehrwert berechnen
  132.         Dim inverseRegex As New Regex("1\s*/\s*([^()]+)")
  133.         While inverseRegex.IsMatch(input)
  134.             input = inverseRegex.Replace(input, Function(match)
  135.                                                     Dim innerExpression As String = match.Groups(1).Value
  136.                                                     Dim result As Double = EvaluateExpression(innerExpression)
  137.                                                     If result <> 0 Then
  138.                                                         Return (1.0 / result).ToString()
  139.                                                     Else
  140.                                                         Throw New DivideByZeroException()
  141.                                                     End If
  142.                                                 End Function)
  143.         End While
  144.  
  145.         ' Auswertung der Klammerausdrücke zuerst
  146.         Dim parenRegex As New Regex("\(([^\(\)]+)\)")
  147.  
  148.         While parenRegex.IsMatch(input)
  149.             input = parenRegex.Replace(input, Function(match)
  150.                                                   Dim innerExpression As String = match.Groups(1).Value
  151.                                                   Dim result As Double = EvaluateExpression(innerExpression)
  152.                                                   Return result.ToString()
  153.                                               End Function)
  154.         End While
  155.  
  156.         ' Multiplikation und Division nach der Klammerauswertung
  157.         Dim mulDivRegex As New Regex("\d+(\.\d+)?\s*[*/]\s*\d+(\.\d+)?")
  158.  
  159.         While mulDivRegex.IsMatch(input)
  160.             input = mulDivRegex.Replace(input, Function(match)
  161.                                                    Dim parts() As String = match.Value.Split({"*", "/"}, StringSplitOptions.TrimEntries)
  162.                                                    Dim leftVal As Double = Double.Parse(parts(0))
  163.                                                    Dim rightVal As Double = Double.Parse(parts(1))
  164.                                                    Dim result As Double
  165.  
  166.                                                    If match.Value.Contains("*") Then
  167.                                                        result = leftVal * rightVal
  168.                                                    Else
  169.                                                        result = leftVal / rightVal
  170.                                                    End If
  171.  
  172.                                                    Return result.ToString()
  173.                                                End Function)
  174.         End While
  175.  
  176.         ' Addition und Subtraktion nach der Klammerauswertung
  177.         Dim addSubRegex As New Regex("\d+(\.\d+)?\s*[+\-]\s*\d+(\.\d+)?")
  178.  
  179.         While addSubRegex.IsMatch(input)
  180.             input = addSubRegex.Replace(input, Function(match)
  181.                                                    Dim parts() As String = match.Value.Split({"+", "-"}, StringSplitOptions.TrimEntries)
  182.                                                    Dim leftVal As Double = Double.Parse(parts(0))
  183.                                                    Dim rightVal As Double = Double.Parse(parts(1))
  184.                                                    Dim result As Double
  185.  
  186.                                                    If match.Value.Contains("+") Then
  187.                                                        result = leftVal + rightVal
  188.                                                    Else
  189.                                                        result = leftVal - rightVal
  190.                                                    End If
  191.  
  192.                                                    Return result.ToString()
  193.                                                End Function)
  194.         End While
  195.  
  196.         ' Set the final result in the TextBox
  197.         tb.Text = input
  198.  
  199.         Dim containsInvalidCharacters As Boolean = False
  200.         For i As Integer = tb.Text.Length - 1 To 0 Step -1
  201.             If Not IsNumeric(tb.Text.Substring(i, 1)) And Not "+-*/".Contains(tb.Text.Substring(i, 1)) Then
  202.                 containsInvalidCharacters = True
  203.                 Exit For
  204.             End If
  205.         Next
  206.  
  207.         ' Beibehaltene For-Schleife
  208.         For index As Integer = 1 To 2
  209.             Debug.Write(index.ToString & "")
  210.         Next
  211.         Debug.WriteLine("")
  212.         'Output: 12
  213.  
  214.         For index As Integer = 3 To 1 Step -1
  215.             Debug.Write(index.ToString & "")
  216.         Next
  217.         Debug.WriteLine("")
  218.         'Output: 321
  219.  
  220.         For index As Integer = 4 To 1 Step -1
  221.             Debug.Write(index.ToString & "")
  222.         Next
  223.         Debug.WriteLine("")
  224.         'Output: 4321
  225.  
  226.         For index As Integer = 5 To 1 Step -1
  227.             Debug.Write(index.ToString & "")
  228.         Next
  229.         Debug.WriteLine("")
  230.         'Output: 54321
  231.  
  232.         For index As Integer = 6 To 1 Step -1
  233.             Debug.Write(index.ToString & "")
  234.         Next
  235.         Debug.WriteLine("")
  236.         'Output: 654321
  237.  
  238.         For index As Integer = 7 To 1 Step -1
  239.             Debug.Write(index.ToString & "")
  240.         Next
  241.         Debug.WriteLine("")
  242.         'Output: 7654321
  243.  
  244.         For index As Integer = 8 To 1 Step -1
  245.             Debug.Write(index.ToString & "")
  246.         Next
  247.         Debug.WriteLine("")
  248.         'Output: 87654321
  249.  
  250.         For index As Integer = 9 To 1 Step -1
  251.             Debug.Write(index.ToString & "")
  252.         Next
  253.         Debug.WriteLine("")
  254.         'Output: 987654321
  255.  
  256.         For index As Integer = 0 To 0
  257.             Debug.Write(index.ToString & "")
  258.         Next
  259.         Debug.WriteLine("")
  260.         'Output: 0
  261.         Dim evaluatedExpression As Double = EvaluateExpression(tb.Text.Replace(",", "."))
  262.         tb.Text = evaluatedExpression.ToString()
  263.  
  264.  
  265.     End Sub
  266.  
  267.  
  268.  
  269.     Private Sub bmult_Click(sender As Object, e As EventArgs) Handles bmult.Click
  270.         zahl += tb.Text
  271.         calc = "*"
  272.         tb.Text += "*"
  273.     End Sub
  274.  
  275.     Private Sub bdiv_Click(sender As Object, e As EventArgs) Handles bdiv.Click
  276.         zahl += tb.Text
  277.         calc = "/"
  278.         tb.Text += "/"
  279.     End Sub
  280.  
  281.     Private Sub bPlus_Click(sender As Object, e As EventArgs) Handles bplus.Click
  282.         tb.Text += "+"
  283.         Debug.WriteLine("Current expression: " & tb.Text)
  284.     End Sub
  285.  
  286.  
  287.     Private Sub bminus_Click(sender As Object, e As EventArgs) Handles bmin.Click
  288.         zahl += tb.Text
  289.         calc = "-"
  290.         tb.Text += "-"
  291.     End Sub
  292.  
  293.     Private Sub bRound_Click(sender As Object, e As EventArgs) Handles bRound.Click
  294.         tb.Text += Math.Round(Val(tb.Text))
  295.     End Sub
  296.  
  297.     Private Sub bkehrwert_Click(sender As Object, e As EventArgs) Handles bkehrwert.Click
  298.         tb.Text = 1.0 / Val(tb.Text)
  299.     End Sub
  300.  
  301.     Private Sub bPI_Click(sender As Object, e As EventArgs) Handles bPI.Click
  302.         tb.Text = Math.PI
  303.     End Sub
  304.  
  305.     Private Sub be_Click(sender As Object, e As EventArgs) Handles be.Click
  306.         tb.Text = Math.E
  307.     End Sub
  308.  
  309.     Private Sub tb_TextChanged(sender As Object, e As EventArgs) Handles tb.TextChanged
  310.  
  311.     End Sub
  312.  
  313.     Private Sub bKlammaauf_Click(sender As Object, e As EventArgs) Handles bKlammaauf.Click
  314.         tb.Text += "("
  315.     End Sub
  316.  
  317.     Private Sub bKlammazu_Click(sender As Object, e As EventArgs) Handles bKlammazu.Click
  318.         tb.Text += ")"
  319.     End Sub
  320.  
  321.  
  322.  
  323.  
  324.     Private Function ParseExpression(expression As String) As Double
  325.         Dim parts() As String = expression.Split("+"c, "-"c, "*"c, "/"c)
  326.  
  327.         Dim numbers As New List(Of Double)()
  328.         For Each part As String In parts
  329.             Dim num As Double
  330.             If Double.TryParse(part, NumberStyles.Any, CultureInfo.InvariantCulture, num) Then
  331.                 numbers.Add(num)
  332.             Else
  333.                 ' Wenn das Parsen fehlschlägt, könnte es ein Problem mit dem Dezimaltrennzeichen (Komma) geben
  334.                 ' Hier kannst du entsprechende Fehlerbehandlung vornehmen
  335.                 Throw New FormatException("Ungültiges Zahlenformat")
  336.             End If
  337.         Next
  338.  
  339.         Dim operators As New List(Of Char)()
  340.         For Each c As Char In expression
  341.             If IsOperator(c) Then
  342.                 operators.Add(c)
  343.             End If
  344.         Next
  345.  
  346.         Dim result As Double = numbers(0)
  347.         For i As Integer = 0 To operators.Count - 1
  348.             If operators(i) = "+"c Then
  349.                 result += numbers(i + 1)
  350.             ElseIf operators(i) = "-"c Then
  351.                 result -= numbers(i + 1)
  352.             ElseIf operators(i) = "*"c Then
  353.                 result *= numbers(i + 1)
  354.             ElseIf operators(i) = "/"c Then
  355.                 result /= numbers(i + 1)
  356.             End If
  357.         Next
  358.  
  359.         Return result
  360.     End Function
  361.  
  362.  
  363.  
  364.  
  365.  
  366.  
  367.  
  368.     Private Function EvaluateExpression(expression As String) As Double
  369.         Dim parenRegex As New Regex("\(([^\(\)]+)\)")
  370.  
  371.         While parenRegex.IsMatch(expression)
  372.             expression = parenRegex.Replace(expression, Function(match)
  373.                                                             Dim innerExpression As String = match.Groups(1).Value
  374.                                                             Dim innerResult As Double = ParseExpression(innerExpression)
  375.                                                             Return innerResult.ToString()
  376.                                                         End Function)
  377.         End While
  378.  
  379.         Dim result As Double = ParseExpression(expression)
  380.         Return result
  381.     End Function
  382.  
  383.  
  384.  
  385.     Private Function IsOperator2(c As Char) As Boolean
  386.         Return "+-*/^s".Contains(c)
  387.     End Function
  388.  
  389.     Private Function FindOperatorWithHighestPrecedence2(operators As List(Of Char)) As Integer
  390.         For i As Integer = 0 To operators.Count - 1
  391.             If operators(i) = "^"c Or operators(i) = "s"c Then
  392.                 Return i
  393.             End If
  394.         Next
  395.         For i As Integer = 0 To operators.Count - 1
  396.             If operators(i) = "*"c Or operators(i) = "/"c Then
  397.                 Return i
  398.             End If
  399.         Next
  400.         ' Return the index of the first operator, which will be "+" or "-"
  401.         Return 0
  402.     End Function
  403.  
  404.  
  405.  
  406.     Private Sub NewMethod(sqrtRegex As Regex)
  407.         input = sqrtRegex.Replace(input, Function(match)
  408.                                              Dim innerExpression As String = match.Groups(1).Value
  409.                                              Dim result As Double = EvaluateExpression(innerExpression)
  410.                                              If result >= 0 Then
  411.                                                  Return Math.Sqrt(result)
  412.                                              Else
  413.                                                  ' Wenn das Ergebnis negativ ist, wird eine Exception ausgelöst
  414.                                                  Throw New ArithmeticException("Negatives Ergebnis")
  415.                                              End If
  416.                                          End Function)
  417.     End Sub
  418.  
  419.     Private Function IsOperator(c As Char) As Boolean
  420.         Return c = "+"c OrElse c = "-"c OrElse c = "*"c OrElse c = "/"c OrElse c = "^"c OrElse c = "s"c
  421.     End Function
  422.  
  423.     Private Function FindOperatorWithHighestPrecedence(operators As List(Of Char)) As Integer
  424.         Dim highestPrecedence As Integer = 0
  425.         Dim index As Integer
  426.  
  427.         For i As Integer = 0 To operators.Count - 1
  428.             Dim precedence As Integer = GetOperatorPrecedence(operators(i))
  429.             If precedence > highestPrecedence OrElse (precedence = highestPrecedence AndAlso operators(i) = "^"c) Then
  430.                 highestPrecedence = precedence
  431.                 index = i
  432.             End If
  433.         Next
  434.  
  435.         Return index
  436.     End Function
  437.  
  438.     Private Function GetOperatorPrecedence(op As Char) As Integer
  439.         If op = "^"c Then
  440.             Return 3
  441.         ElseIf op = "*"c OrElse op = "/"c Then
  442.             Return 2
  443.         ElseIf op = "+"c OrElse op = "-"c Then
  444.             Return 1
  445.         End If
  446.         Return 0
  447.     End Function
  448.  
  449.     Private Sub bPow_Click(sender As Object, e As EventArgs) Handles bPow.Click
  450.         tb.Text += "^"
  451.     End Sub
  452.  
  453.     Private Sub btnSqrt_Click(sender As Object, e As EventArgs) Handles btnSqrt.Click
  454.         Dim inputExpression As String = tb.Text
  455.         Dim evaluatedExpression As Double = EvaluateExpression(inputExpression)
  456.  
  457.         If evaluatedExpression >= 0 Then
  458.             tb.Text = Math.Sqrt(evaluatedExpression).ToString()
  459.         Else
  460.             tb.Text = "Error: Negative result"
  461.         End If
  462.     End Sub
  463.  
  464.     Private Sub besc_Click(sender As Object, e As EventArgs) Handles besc.Click
  465.         Me.Close()
  466.  
  467.     End Sub
  468.  
  469.  
  470.     Private Function GetNextToken(ByRef expression As String) As String
  471.         Dim token As New StringBuilder()
  472.  
  473.         While expression.Length > 0 AndAlso expression(0) = " "c
  474.             expression = expression.Substring(1)
  475.         End While
  476.  
  477.         If expression.Length = 0 Then
  478.             Return String.Empty
  479.         End If
  480.  
  481.         If IsOperator(expression(0)) Then
  482.             token.Append(expression(0))
  483.             expression = expression.Substring(1)
  484.         ElseIf expression(0) = "("c Then
  485.             Dim openParentheses As Integer = 1
  486.             token.Append(expression(0))
  487.             expression = expression.Substring(1)
  488.  
  489.             While openParentheses > 0
  490.                 If expression(0) = "("c Then
  491.                     openParentheses += 1
  492.                 ElseIf expression(0) = ")"c Then
  493.                     openParentheses -= 1
  494.                 End If
  495.                 token.Append(expression(0))
  496.                 expression = expression.Substring(1)
  497.             End While
  498.         Else
  499.             While expression.Length > 0 AndAlso (Char.IsDigit(expression(0)) OrElse expression(0) = ","c)
  500.                 token.Append(expression(0))
  501.                 expression = expression.Substring(1)
  502.             End While
  503.         End If
  504.  
  505.         Debug.WriteLine("Parsed token: " & token.ToString())
  506.  
  507.         Return token.ToString()
  508.     End Function
  509.  
  510.  
  511.  
  512. End Class
  513.  
Add Comment
Please, Sign In to add comment