locked
Calculator IN MY PROGRAM RRS feed

  • Question

  • Hello

    I want to make a calculator with certain specifications for my software

    And stopped when two problems

    1- The user is not allowed to put two marks together, for example this problem

    22 ++ 13/*15 * - + 16

    2- How can I perform the calculation if one line is like the next section, no matter how long the calculation is, for example

    22 + 13-15 * 48/12 + 14 + 65 + 98 + 65 =
    Monday, August 6, 2018 7:29 AM

Answers

  • Hi

    The following method ensures that you cannot enter + - */ in an adjacent position.

        Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
            For i = 0 To TextBox1.Text.Length - 2
                If (TextBox1.Text(i) = "+" Or TextBox1.Text(i) = "-" Or TextBox1.Text(i) = "*" Or TextBox1.Text(i) = "/") And (TextBox1.Text(i + 1) = "+" Or TextBox1.Text(i + 1) = "-" Or TextBox1.Text(i + 1) = "*" Or TextBox1.Text(i + 1) = "/") Then
                    'TextBox1.Text = TextBox1.Text.Substring(0, TextBox1.Text.Length - 1)
                    TextBox1.Text = TextBox1.Text.Remove(TextBox1.Text.Length - 1, 1)
                    TextBox1.Select(TextBox1.TextLength, 0)
                End If
    
            Next
        End Sub

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by ahmeddc Monday, August 6, 2018 10:29 AM
    • Unmarked as answer by ahmeddc Monday, August 6, 2018 10:34 AM
    • Marked as answer by ahmeddc Monday, August 6, 2018 10:35 AM
    Monday, August 6, 2018 9:50 AM

All replies

  • Hi

    1- The user is not allowed to put two marks together, for example this problem

    22 ++ 13/*15 * - + 16

     Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
            For i = 0 To TextBox1.Text.Length - 2
                If (TextBox1.Text(i) = "+" Or TextBox1.Text(i) = "-" Or TextBox1.Text(i) = "*" Or TextBox1.Text(i) = "/") And (TextBox1.Text(i + 1) = "+" Or TextBox1.Text(i + 1) = "-" Or TextBox1.Text(i + 1) = "*" Or TextBox1.Text(i + 1) = "/") Then
    
                End If
            Next
        End Sub

    2.The second question is also detailed, I can't understand.

    22 + 13-15 * 48/12 + 14 + 65 + 98 + 65 =217??

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, August 6, 2018 8:04 AM
  • Hi

    1- The user is not allowed to put two marks together, for example this problem

    22 ++ 13/*15 * - + 16

     Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
            For i = 0 To TextBox1.Text.Length - 2
                If (TextBox1.Text(i) = "+" Or TextBox1.Text(i) = "-" Or TextBox1.Text(i) = "*" Or TextBox1.Text(i) = "/") And (TextBox1.Text(i + 1) = "+" Or TextBox1.Text(i + 1) = "-" Or TextBox1.Text(i + 1) = "*" Or TextBox1.Text(i + 1) = "/") Then
    
                End If
            Next
        End Sub

    2.The second question is also detailed, I can't understand.

    22 + 13-15 * 48/12 + 14 + 65 + 98 + 65 =217??

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    At first I thank you for your reply

    1- Code is incomplete and does not work You put more than one tag together and add without problems
    I do not want to have two tags OR MORE ONLY ONE TAGS

    2.The second question is also detailed, I can't understand.

    22 + 13-15 * 48/12 + 14 + 65 + 98 + 65 =217??

    I do not mean the final value but I mean the way to get the value if the process is in this form in one line IN TEXTBOX  In the previous format

    22 + 13-15 * 48/12 + 14 + 65 + 98 + 65 =??

    Monday, August 6, 2018 8:24 AM
  • Hi

    You can try this code:

    Public Class Form1
        Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
            'If TextBox1.Text.Length > 1 Then
            For i = 0 To TextBox1.Text.Length - 2
                If (TextBox1.Text(i) = "+" Or TextBox1.Text(i) = "-" Or TextBox1.Text(i) = "*" Or TextBox1.Text(i) = "/") And (TextBox1.Text(i + 1) = "+" Or TextBox1.Text(i + 1) = "-" Or TextBox1.Text(i + 1) = "*" Or TextBox1.Text(i + 1) = "/") Then
                    MsgBox("error")
    
                End If
    
            Next
            'End If
    
        End Sub
        Private lists As List(Of String) = Nothing
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            lists = New List(Of String)()
            Dim str As String = TextBox1.Text
            Dim temp As String = ""
            Dim index As Integer = 0, sum As Integer = str.Length
    
            For Each c As Char In str
                index += 1
    
                If c <> "+"c AndAlso c <> "-"c AndAlso c <> "*"c AndAlso c <> "/"c Then
                    temp += c
                Else
                    lists.Add(temp)
                    lists.Add(c.ToString())
                    temp = ""
                End If
    
                If index = sum Then
                    lists.Add(temp)
                End If
            Next
    
            index = 0
    
            Do
                Dim type1 As String = lists(index + 1)
    
                If type1 = "+" OrElse type1 = "-" Then
    
                    If lists.Count < 4 Then
                        Make(index, lists(index), lists(index + 2), type1)
                    Else
                        Dim type2 As String = lists(index + 3)
    
                        If type2 = "*" OrElse type2 = "/" Then
                            Make(index + 2, lists(index + 2), lists(index + 4), type2)
                        Else
                            Make(index, lists(index), lists(index + 2), type1)
                        End If
                    End If
                Else
                    Make(index, lists(index), lists(index + 2), type1)
                End If
    
                If lists.Count = 1 Then
                    textBox2.Text = lists(0)
                    Exit Do
                End If
            Loop While 1 = 1
    
        End Sub
        Private Sub Make(ByVal index As Integer, ByVal noStr1 As String, ByVal noStr2 As String, ByVal type As String)
            Dim tempSum As Double = 0, no1 As Double = Convert.ToDouble(noStr1), no2 As Double = Convert.ToDouble(noStr2)
    
            Select Case type
                Case "+"
                    tempSum = no1 + no2
                Case "-"
                    tempSum = no1 - no2
                Case "*"
                    tempSum = no1 * no2
                Case "/"
                    tempSum = no1 / no2
            End Select
    
            lists(index + 2) = tempSum.ToString()
            lists.RemoveAt(index)
            lists.RemoveAt(index)
        End Sub
    
    End Class

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, August 6, 2018 8:54 AM
  • all thanks Alex

    The problem with the first part is that I want to delete the extra tag and not the error message


    • Edited by ahmeddc Monday, August 6, 2018 9:33 AM
    Monday, August 6, 2018 9:31 AM
  • Hi

    The following method ensures that you cannot enter + - */ in an adjacent position.

        Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
            For i = 0 To TextBox1.Text.Length - 2
                If (TextBox1.Text(i) = "+" Or TextBox1.Text(i) = "-" Or TextBox1.Text(i) = "*" Or TextBox1.Text(i) = "/") And (TextBox1.Text(i + 1) = "+" Or TextBox1.Text(i + 1) = "-" Or TextBox1.Text(i + 1) = "*" Or TextBox1.Text(i + 1) = "/") Then
                    'TextBox1.Text = TextBox1.Text.Substring(0, TextBox1.Text.Length - 1)
                    TextBox1.Text = TextBox1.Text.Remove(TextBox1.Text.Length - 1, 1)
                    TextBox1.Select(TextBox1.TextLength, 0)
                End If
    
            Next
        End Sub

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by ahmeddc Monday, August 6, 2018 10:29 AM
    • Unmarked as answer by ahmeddc Monday, August 6, 2018 10:34 AM
    • Marked as answer by ahmeddc Monday, August 6, 2018 10:35 AM
    Monday, August 6, 2018 9:50 AM