Sunday, 1 September 2013

Make calculator in .net

There are number of calculators available . If you want to make a Basic calculator in .NET Then you can surf a number of Calculators code on the Internet . But If you have used the calculators come in Microsoft-Windows PC's Then you can notice these are best , easy to operate and can accept no of operators one time as these are advanced calculators . So make a calculator similar to these calculators come in Microsoft-Windows PC's is logical way of developing a calculator. I have tried a simple calculator That only does Addition , Substration, Division and Multiplication of integers and Fractional numbers .

Download this Basic Calculator from Link Below :-


Download calculator.exe

Snapshot of Calculator



Code To Make A Basic Calculator 

Public Class Form1

    Public operation As String
    Public cnt, mulcnt, divcnt, divoccur
    Public ploccur, minoccur, multyoccur As Boolean
    
Private Sub function1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click, Button8.Click, Button7.Click, Button6.Click, Button5.Click, Button4.Click, Button3.Click, Button2.Click, Button12.Click, Button1.Click, Button10.Click, Button11.Click, Button13.Click, Button14.Click
        If Len(TextBox1.Text) < 11 Then
            TextBox1.Text = TextBox1.Text & sender.text
        Else
            MsgBox("Max Limit Reached")
        End If
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ploccur = False
        minoccur = False
        multyoccur = False
        mulcnt = 0
        divcnt = 0
        divoccur = False
        cnt = 0
    End Sub

    Private Sub Button16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button16.Click
        TextBox1.Text = 0
        ploccur = False
        minoccur = False
        multyoccur = False
        mulcnt = 0
        divcnt = 0
        divoccur = False
        cnt = 0
    End Sub

    Private Sub Button15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button15.Click
        If Not TextBox1.Text.Contains(".") Then
            TextBox1.Text = TextBox1.Text & "."
        End If
    End Sub

    Private Sub Button17_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button17.Click
       
        Dim a As String
        Dim num, num1
        num1 = 0
        
For i = 1 To Len(TextBox1.Text)

            a = Mid(TextBox1.Text, i, 1)

  If a = "+" Then
               ploccur = True
                num1 = num1 + num

           ElseIf a = "-" Then
                minoccur = True
                If cnt > 0 Then
                    num1 = num1 - num

                ElseIf cnt = 0 Then
                    If Len(TextBox1.Text) = 3 Then
                        num1 = num - num1
                    Else
                        num1 = num1 - num

                    End If
                End If
                cnt = cnt + 1

            ElseIf a = "*" Then
                multyoccur = True
                If mulcnt = 0 Then
                    num1 = num1 * 1
                Else
                    num1 = num1 * num
                End If

                mulcnt = mulcnt + 1

            ElseIf a = "/" Then
                divoccur = True
                If divcnt = 0 Then
                    If Len(TextBox1.Text) = 3 Then
                        num1 = num / 1
                    Else
                        num1 = num1 / 1
                    End If

                Else
                    num1 = num1 / num
                End If
                divcnt = divcnt + 1

            Else
                num = 0
                num = num & a

                If ploccur = True Then
                    num1 = num1 + num
                    ploccur = False
                    num = 0
                End If

                If minoccur = True Then
                    If cnt > 0 Then
                        num1 = num1 - num
                        minoccur = False
                        num = 0
                    ElseIf cnt = 0 Then
                        If Len(TextBox1.Text) = 3 Then
                            num1 = num - num1
                        Else
                            num1 = num1 - num
                        End If
                        minoccur = False
                        num = 0
                    End If
                End If

                If multyoccur = True Then
                    num1 = num1 * num
                    multyoccur = False
                    num = 0
                End If

                If divoccur = True Then
                    If divcnt = 0 Then
                        divoccur = True
                        num1 = num1 / num
                        divoccur = False
                        num = 0
                    Else
                        num1 = num1 / num
                         divoccur = False
                        num = 0
                    End If

                End If
            End If
         Next
         TextBox1.Text = num1
    End Sub
End Class

Share this

4 Responses to "Make calculator in .net"