Friday, March 29

Simple Calculator Using VB.Net

This project was given to us during our mid-term examination and is coded using VB.Net 2010.
It is consists of three forms.

FORM 1:
Components:
  • TextBoxes:   Display and MemIndicate.
  • Buttons:        btnPosNeg, btnClear, btnDelete, btnMemRet, btnMemClear, btnMemAdd, btnMemMinus, btnMemStore, btnMultiply, btnDivide, btnAdd, btnMinus, btnEquals, btnDot, Button0, Button1, Button2, Button3, Button4, Button5, Button6, Button7, Button8, Button9.




Code:

Imports System.Math
Public Class Form1
    Dim Operation As String
    Dim Dot As Boolean = False
    Dim thereIsOp As Boolean = False
    Dim num1 As Double
    Dim successiveOp As Boolean = False
    Dim btnEqualsClicked As Boolean = False
    Dim Memory As Double = Nothing
    Dim digits As Integer = Nothing

    Private Sub btnMultiply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMultiply.Click
        performOpAction("multiply")
    End Sub

    Private Sub btnDivide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDivide.Click
        performOpAction("divide")
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        performOpAction("add")
    End Sub

    Private Sub btnMinus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMinus.Click
        performOpAction("minus")
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Call addToDisplay(1)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Call addToDisplay(2)
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Call addToDisplay(3)
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Call addToDisplay(4)
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Call addToDisplay(5)
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Call addToDisplay(6)
    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        Call addToDisplay(7)
    End Sub

    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        Call addToDisplay(8)
    End Sub

    Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
        Call addToDisplay(9)
    End Sub

    Private Sub Button0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button0.Click
        Call addToDisplay(0)
    End Sub

    Private Sub btnDot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDot.Click
        If thereIsOp = True Then
            Display.Text = 0
            thereIsOp = False
        End If
        If Dot = False Then
            Display.Text = Display.Text & "."
            Dot = True
        End If
        btnEqualsClicked = False
    End Sub

    Private Sub btnEqual_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEquals.Click
        If successiveOp = True Then
            Display.Text = performOperation()
        End If
        thereIsOp = True
        successiveOp = False
        btnEqualsClicked = True
    End Sub

    Private Function performOperation()
        Dim num As Double
        If Operation = "multiply" Then
            num = num1 * Display.Text
        ElseIf Operation = "divide" Then
            num = num1 / Display.Text
        ElseIf Operation = "add" Then
            num = num1 + Display.Text
        ElseIf Operation = "minus" Then
            num = num1 - Display.Text
        End If
        Dot = False
        Return num
    End Function

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        Display.Text = 0
        num1 = Nothing
        Operation = "none"
        digits = 0
        Dot = False
        btnEqualsClicked = False
    End Sub

    Private Sub btnDelete_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        If btnEqualsClicked = False Then
            If Display.Text.Length > 1 Then
                Display.Text = Display.Text.Substring(0, Display.Text.Length - 1)
                digits -= 1
            Else
                Display.Text = Nothing
                Display.Text = 0
                digits = 0
            End If
        End If
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub btnposNeg_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPosNeg.Click
        Display.Text = Display.Text * -1
    End Sub

    Private Sub btnPercent_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPercent.Click
        Display.Text = (Display.Text / 100) * num1
        Dot = False
        thereIsOp = True
    End Sub

    Private Sub btnSqrt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSqrt.Click
        Display.Text = Sqrt(Display.Text)
        Dot = False
        thereIsOp = True
    End Sub

    Private Sub btnMemRet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMemRet.Click
        Display.Text = Memory
        thereIsOp = True
        digits = 0
        Dot = False
        btnEqualsClicked = True
    End Sub

    Private Sub btnMemClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMemClear.Click
        Memory = Nothing
        MemIndicate.Text = Nothing
        Display.Text = Int(Display.Text)
        thereIsOp = True
        digits = 0
        Dot = False
        btnEqualsClicked = True
    End Sub

    Private Sub btnMemAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMemAdd.Click
        Memory = Memory + Display.Text
        If Memory = 0 Then
            MemIndicate.Text = Nothing
        Else
            MemIndicate.Text = "M"
        End If
        Display.Text = Int(Display.Text)
        thereIsOp = True
        digits = 0
        Dot = False
        btnEqualsClicked = True
    End Sub

    Private Sub btnMemMinus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMemMinus.Click
        Memory = Memory - Display.Text
        If Memory = 0 Then
            MemIndicate.Text = Nothing
        Else
            MemIndicate.Text = "M"
        End If
        Display.Text = Int(Display.Text)
        thereIsOp = True
        digits = 0
        Dot = False
        btnEqualsClicked = True
    End Sub

    Private Sub btnMemStore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMemStore.Click
        If Display.Text <> "0" Then
            If Display.Text <> "0." Then
                Memory = Display.Text
                MemIndicate.Text = "M"
            Else
                Display.Text = "0"
            End If
        End If
        thereIsOp = True
        digits = 0
        Dot = False
        btnEqualsClicked = True
    End Sub
    Private Sub addToDisplay(ByVal toAdd As Integer)
        If digits < 16 Then
            If thereIsOp = True Then
                Display.Text = Nothing
                thereIsOp = False
            End If
            If Display.Text = "0" Then
                Display.Text = Nothing
            End If
            Display.Text = Display.Text & toAdd
            digits += 1
        End If
        btnEqualsClicked = False
    End Sub
    Private Sub performOpAction(ByVal OperatorIs As String)
        If successiveOp = True Then
            Display.Text = performOperation()
        End If
        Operation = OperatorIs
        successiveOp = True
        thereIsOp = True
        num1 = Display.Text
        Display.Text = num1
        digits = 0
        Dot = False
    End Sub

    Private Sub Display_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Display.TextChanged
        Dim x As Integer = 0
        For i = 0 To Display.Text.Length - 1
            If Display.Text.Substring(i, 1) = "." Then
                x += 1
            End If
        Next
        If x > 0 Then
            Dot = True
        Else
            Dot = False
        End If
    End Sub
End Class

FORM 2:

Component: MenuStrip1




Code:

Public Class Form2
    Public standard As New Form1
    Public scientific As New Form3
    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        standard.TopLevel = False
        standard.Location = New System.Drawing.Point(0, 27)
        Me.Controls.Add(standard)
        scientific.TopLevel = False
        scientific.Location = New System.Drawing.Point(0, 27)
        Me.Controls.Add(scientific)
        standard.Show()
    End Sub

    Private Sub StandardToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StandardToolStripMenuItem.Click
        Me.Size = New System.Drawing.Size(262, 333)
        standard.Show()
        scientific.Hide()
    End Sub

    Private Sub ScientificToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ScientificToolStripMenuItem.Click
        Me.Size = New System.Drawing.Size(403, 324)
        scientific.Show()
        standard.Hide()
    End Sub
End Class


FORM 3:

Components: Includes all components in form1. (Mod is btnPercent), btnlog, btnReciprocal, btnA, btnB, btnC, btnD, btnE, btnF, RadioButton1 (Bin), RadioButton2 (Dec), RadioButton3 (Oct), RadioButton4 (Hex), btnSine, btnCosine, btnTangent, btnCubeRoot, btnNthRoot, btnSquare, btnCube, btnNthPower, GroupBox1 and GroupBox2.




Code:

Imports System.Math

Public Class Form3
    'Declaration and initialization of variables
    Dim Operation As String
    Dim Dot As Boolean = False
    Dim thereIsOp As Boolean = False
    Dim num1 As Double
    Dim successiveOp As Boolean = False
    Dim btnEqualsClicked As Boolean = False
    Dim Memory As Double = Nothing
    Dim digits As Integer = Nothing
    Dim previous As String = "dec"
    Dim present As String = "dec"

    Private Sub btnMultiply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMultiply.Click
        performOpAction("multiply") 'Operator is multiply
    End Sub

    Private Sub btnDivide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDivide.Click
        performOpAction("divide") 'Operator is divide
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        performOpAction("add") 'Operator is add
    End Sub

    Private Sub btnMinus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMinus.Click
        performOpAction("minus") 'Operator is minus
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Call addToDisplay("1")
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Call addToDisplay("2")
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Call addToDisplay("3")
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Call addToDisplay("4")
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Call addToDisplay("5")
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Call addToDisplay("6")
    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        Call addToDisplay("7")
    End Sub

    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        Call addToDisplay("8")
    End Sub

    Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
        Call addToDisplay("9")
    End Sub

    Private Sub Button0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button0.Click
        Call addToDisplay("0")
    End Sub

    Private Sub btnDot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDot.Click
        'if opertor has been selected, the display will be overwritten instead of adding dot in the end
        If thereIsOp = True Then
            Display.Text = 0
            thereIsOp = False
        End If

        If Dot = False Then 'if dot is not yet present, it will be added in the display
            Display.Text = Display.Text & "."
            Dot = True
        End If
        btnEqualsClicked = False
    End Sub

    Private Sub btnEqual_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEquals.Click
        If successiveOp = True Then
            Display.Text = performOperation() 'perform the operation function
        End If
        thereIsOp = True
        successiveOp = False
        btnEqualsClicked = True
    End Sub

    Private Function performOperation()
        'operation process
        Dim num As Double
        Dim num2 As Double
        If RadioButton1.Checked = True Then
            num2 = binTOdec()
        ElseIf RadioButton2.Checked = True Then
            num2 = Display.Text
        ElseIf RadioButton3.Checked = True Then
            num2 = octTOdec()
        ElseIf RadioButton4.Checked = True Then
            num2 = hexTOdec()
        End If
        If Operation = "multiply" Then
            num = num1 * num2
        ElseIf Operation = "divide" Then
            num = num1 / num2
        ElseIf Operation = "add" Then
            num = num1 + num2
        ElseIf Operation = "minus" Then
            num = num1 - num2
        ElseIf Operation = "root" Then
            num = Display.Text ^ (1 / num1)
        ElseIf Operation = "power" Then
            num = num1 ^ num2
        ElseIf Operation = "modulus" Then
            num = num1 Mod num2
        End If
        Dot = False
        Return num
    End Function

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        'clear the display and set some variables to initial value
        Display.Text = 0
        num1 = Nothing
        Operation = "none"
        digits = 0
        Dot = False
        btnEqualsClicked = False
    End Sub

    Private Sub btnDelete_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        'delete one by one
        If btnEqualsClicked = False Then
            If Display.Text.Length > 1 Then
                Display.Text = Display.Text.Substring(0, Display.Text.Length - 1)
                digits -= 1
            Else
                Display.Text = Nothing
                Display.Text = 0
                digits = 0
            End If
        End If
    End Sub

    Private Sub btnposNeg_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPosNeg.Click
        Display.Text = Display.Text * -1
    End Sub

    Private Sub btnPercent_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPercent.Click
        performOpAction("modulus")
    End Sub

    Private Sub btnSqrt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSqrt.Click
        Display.Text = Sqrt(Display.Text)
        Dot = False
        thereIsOp = True
    End Sub

    Private Sub btnMemRet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMemRet.Click
        Display.Text = Memory
        thereIsOp = True
        digits = 0
        Dot = False
        btnEqualsClicked = True
    End Sub

    Private Sub btnMemClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMemClear.Click
        Memory = Nothing
        MemIndicate.Text = Nothing
        Display.Text = Int(Display.Text)
        thereIsOp = True
        digits = 0
        Dot = False
        btnEqualsClicked = True
    End Sub

    Private Sub btnMemAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMemAdd.Click
        Memory = Memory + Display.Text
        If Memory = 0 Then
            MemIndicate.Text = Nothing
        Else
            MemIndicate.Text = "M"
        End If
        Display.Text = Int(Display.Text)
        thereIsOp = True
        digits = 0
        Dot = False
        btnEqualsClicked = True
    End Sub

    Private Sub btnMemMinus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMemMinus.Click
        Memory = Memory - Display.Text
        If Memory = 0 Then
            MemIndicate.Text = Nothing
        Else
            MemIndicate.Text = "M"
        End If
        Display.Text = Int(Display.Text)
        thereIsOp = True
        digits = 0
        Dot = False
        btnEqualsClicked = True
    End Sub

    Private Sub btnMemStore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMemStore.Click
        If Display.Text <> "0" Then
            If Display.Text <> "0." Then
                Memory = Display.Text
                MemIndicate.Text = "M"
            Else
                Display.Text = "0"
            End If
        End If
        thereIsOp = True
        digits = 0
        Dot = False
        btnEqualsClicked = True
    End Sub
    Private Sub addToDisplay(ByVal toAdd As Char)
        If digits < 16 Then
            If thereIsOp = True Then
                Display.Text = Nothing
                thereIsOp = False
            End If
            If Display.Text = "0" Then
                Display.Text = Nothing
            End If
            Display.Text = Display.Text & toAdd
            digits += 1
        End If
        btnEqualsClicked = False
    End Sub
    Private Sub performOpAction(ByVal OperatorIs As String)
        If successiveOp = True Then
            Display.Text = performOperation()
        End If
        If RadioButton1.Checked = True Then
            num1 = binTOdec()
        ElseIf RadioButton2.Checked = True Then
            num1 = Display.Text
            Display.Text = num1
        ElseIf RadioButton3.Checked = True Then
            num1 = octTOdec()
        ElseIf RadioButton4.Checked = True Then
            num1 = hexTOdec()
        End If
        Operation = OperatorIs
        successiveOp = True
        thereIsOp = True
        digits = 0
        Dot = False
    End Sub

    Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
        disableButtons()
        present = "bin"
        Call baseConverter()
        previous = "bin"
        thereIsOp = True
    End Sub
    Private Sub disableButtons()
        btnSine.Enabled = False
        btnCosine.Enabled = False
        btnTangent.Enabled = False
        btnCubeRoot.Enabled = False
        btnReciprocal.Enabled = False
        btnNthRoot.Enabled = False
        btnSqrt.Enabled = False
        btnlog.Enabled = False
        btnSquare.Enabled = False
        btnCube.Enabled = False
        btnNthPower.Enabled = False
        btnA.Enabled = False
        btnB.Enabled = False
        btnC.Enabled = False
        btnD.Enabled = False
        btnE.Enabled = False
        btnF.Enabled = False
        btnPosNeg.Enabled = False
        btnMemRet.Enabled = False
        btnMemClear.Enabled = False
        btnMemMinus.Enabled = False
        btnMemAdd.Enabled = False
        btnMemStore.Enabled = False
        btnPercent.Enabled = False
        btnlog.Enabled = False
        btnReciprocal.Enabled = False
        btnDot.Enabled = False
        Button2.Enabled = False
        Button3.Enabled = False
        Button4.Enabled = False
        Button5.Enabled = False
        Button6.Enabled = False
        Button7.Enabled = False
        Button8.Enabled = False
        Button9.Enabled = False
        btnAdd.Enabled = False
        btnMultiply.Enabled = False
        btnDivide.Enabled = False
        btnMinus.Enabled = False
    End Sub

    Private Sub RadioButton3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton3.CheckedChanged
        disableButtons()
        present = "oct"
        Call baseConverter()
        previous = "oct"
        Button2.Enabled = True
        Button3.Enabled = True
        Button4.Enabled = True
        Button5.Enabled = True
        Button6.Enabled = True
        Button7.Enabled = True
        thereIsOp = True
    End Sub

    Private Sub RadioButton4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton4.CheckedChanged
        disableButtons()
        present = "hex"
        Call baseConverter()
        previous = "hex"
        Button2.Enabled = True
        Button3.Enabled = True
        Button4.Enabled = True
        Button5.Enabled = True
        Button6.Enabled = True
        Button7.Enabled = True
        Button8.Enabled = True
        Button9.Enabled = True
        btnA.Enabled = True
        btnB.Enabled = True
        btnC.Enabled = True
        btnD.Enabled = True
        btnE.Enabled = True
        btnF.Enabled = True
        thereIsOp = True
    End Sub
    Private Sub enableButtons()
        btnSine.Enabled = True
        btnCosine.Enabled = True
        btnTangent.Enabled = True
        btnCubeRoot.Enabled = True
        btnReciprocal.Enabled = True
        btnNthRoot.Enabled = True
        btnSqrt.Enabled = True
        btnlog.Enabled = True
        btnSquare.Enabled = True
        btnCube.Enabled = True
        btnNthPower.Enabled = True
        btnPosNeg.Enabled = True
        btnMemRet.Enabled = True
        btnMemClear.Enabled = True
        btnMemMinus.Enabled = True
        btnMemAdd.Enabled = True
        btnMemStore.Enabled = True
        btnPercent.Enabled = True
        btnlog.Enabled = True
        btnReciprocal.Enabled = True
        btnDot.Enabled = True
        Button2.Enabled = True
        Button3.Enabled = True
        Button4.Enabled = True
        Button5.Enabled = True
        Button6.Enabled = True
        Button7.Enabled = True
        Button8.Enabled = True
        Button9.Enabled = True
        btnAdd.Enabled = True
        btnMultiply.Enabled = True
        btnDivide.Enabled = True
        btnMinus.Enabled = True
    End Sub

    Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
        enableButtons()
        present = "dec"
        Call baseConverter()
        previous = "dec"
        thereIsOp = True
    End Sub
    Private Sub baseConverter()
        If previous <> present Then
            Try
                If previous = "dec" And present = "bin" Then
                    Display.Text = Convert.ToString(CType(Display.Text, Long), 2)
                ElseIf previous = "dec" And present = "oct" Then
                    Display.Text = Convert.ToString(CType(Display.Text, Long), 8)
                ElseIf previous = "dec" And present = "hex" Then
                    Display.Text = Convert.ToString(CType(Display.Text, Long), 16)
                ElseIf previous = "bin" And present = "dec" Then
                    Display.Text = Convert.ToString(binTOdec(), 10)
                ElseIf previous = "oct" And present = "dec" Then
                    Display.Text = Convert.ToString(octTOdec(), 10)
                ElseIf previous = "hex" And present = "dec" Then
                    Display.Text = Convert.ToString(hexTOdec(), 10)
                ElseIf previous = "bin" And present = "oct" Then
                    Display.Text = Convert.ToString(binTOdec(), 8)
                ElseIf previous = "bin" And present = "hex" Then
                    Display.Text = Convert.ToString(binTOdec(), 16)
                ElseIf previous = "oct" And present = "bin" Then
                    Display.Text = Convert.ToString(octTOdec(), 2)
                ElseIf previous = "oct" And present = "hex" Then
                    Display.Text = Convert.ToString(octTOdec(), 16)
                ElseIf previous = "hex" And present = "bin" Then
                    Display.Text = Convert.ToString(hexTOdec(), 2)
                ElseIf previous = "hex" And present = "oct" Then
                    Display.Text = Convert.ToString(hexTOdec(), 8)
                End If
            Catch
            End Try
        End If
    End Sub

    Private Sub btnSine_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSine.Click
        Display.Text = Sin((Display.Text * PI) / 180)
        Dot = False
        thereIsOp = True
    End Sub

    Private Sub btnCosine_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCosine.Click
        Display.Text = Cos((Display.Text * PI) / 180)
        Dot = False
        thereIsOp = True
    End Sub

    Private Sub btnTangent_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTangent.Click
        Display.Text = Tan((Display.Text * PI) / 180)
        Dot = False
        thereIsOp = True
    End Sub

    Private Sub btnCubeRoot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCubeRoot.Click
        Display.Text = Display.Text ^ (1 / 3)
        Dot = False
        thereIsOp = True
    End Sub

    Private Sub btnNthRoot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNthRoot.Click
        performOpAction("root")
    End Sub

    Private Sub btnlog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnlog.Click
        Display.Text = Log10(Display.Text)
        Dot = False
        thereIsOp = True
    End Sub

    Private Sub btnReciprocal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReciprocal.Click
        Display.Text = 1 / Display.Text
        Dot = False
        thereIsOp = True
    End Sub

    Private Sub btnSquare_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSquare.Click
        Display.Text = Display.Text ^ 2
        Dot = False
        thereIsOp = True
    End Sub

    Private Sub btnCube_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCube.Click
        Display.Text = Display.Text ^ 2
        Dot = False
        thereIsOp = True
    End Sub

    Private Sub btnNthPower_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNthPower.Click
        performOpAction("power")
    End Sub

    Private Sub Display_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Display.KeyDown

    End Sub
    Private Function binTOdec()
        Dim Store As String = Display.Text
        Dim len As Integer = Store.Length
        Dim x As Integer = 1
        Dim total As Long = 0
        Dim counter As Integer = len - 1
        For i = 0 To len - 1
            total = total + (Store.Substring(counter, 1) * x)
            x *= 2
            counter -= 1
        Next
        Return total
    End Function
    Private Function octTOdec()
        Dim len As Integer = Display.Text.Length
        Dim x As Integer = 0
        Dim total As Long = 0
        Dim counter As Integer = len - 1
        For i = 0 To len - 1
            total = total + (Display.Text.Substring(counter, 1) * (8 ^ x))
            x += 1
            counter -= 1
        Next
        Return total
    End Function
    Private Function hexTOdec()
        Dim len As Integer = Display.Text.Length
        Dim x As Integer = 0
        Dim total As Long = 0
        Dim counter As Integer = len - 1
        Dim num As Integer = 0
        For i = 0 To len - 1

            If Display.Text.Substring(counter, 1).ToLower = "a" Then
                num = 10
            ElseIf Display.Text.Substring(counter, 1).ToLower = "b" Then
                num = 11
            ElseIf Display.Text.Substring(counter, 1).ToLower = "c" Then
                num = 12
            ElseIf Display.Text.Substring(counter, 1).ToLower = "d" Then
                num = 13
            ElseIf Display.Text.Substring(counter, 1).ToLower = "e" Then
                num = 14
            ElseIf Display.Text.Substring(counter, 1).ToLower = "f" Then
                num = 15
            Else
                num = CType(Display.Text.Substring(counter, 1), Integer)
            End If
            total = total + (num * (16 ^ x))
            x += 1
            counter -= 1
        Next
        Return total
    End Function

    Private Sub btnA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnA.Click
        Call addToDisplay("A")
    End Sub

    Private Sub btnB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnB.Click
        Call addToDisplay("B")
    End Sub

    Private Sub btnC_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnC.Click
        Call addToDisplay("C")
    End Sub

    Private Sub btnD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnD.Click
        Call addToDisplay("D")
    End Sub

    Private Sub btnE_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnE.Click
        Call addToDisplay("E")
    End Sub

    Private Sub btnF_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnF.Click
        Call addToDisplay("F")
    End Sub

    Private Sub Display_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Display.TextChanged
        Dim x As Integer = 0
        For i = 0 To Display.Text.Length - 1
            If Display.Text.Substring(i, 1) = "." Then
                x += 1
            End If
        Next
        If x > 0 Then
            Dot = True
        Else
            Dot = False
        End If
    End Sub

End Class




Note: It can be shorten by merging the three forms into a single form and by eliminating unwanted lines.

Thursday, September 1

Exercise in clearing the screen and choosing the desired background color.


 .model small
.code
org 100h
mov ax,0600h
mov cx,0000h
mov dx,0c28h
mov bh,40h
int 10h
mov ax,0600h
mov cx,0028h
mov dx,0c50h
mov bh,10h
int 10h
mov ax,0600h
mov cx,0d00h
mov dx,1928h
mov bh,30h
int 10h
mov ax,0600h
mov cx,0d28h
mov dx,1950h
mov bh,20h
int 10h
mov ax,0600h
mov cx,040eh
mov dx,0c42h
mov bh,50h
int 10h
mov ax,0600h
mov cx,0d0eh
mov dx,1442h
mov bh,60h
int 10h
mov ax,0600h
mov cx,081bh
mov dx,1028h
mov bh,70h
int 10h
mov ax,0600h
mov cx,0828h
mov dx,1035h
mov bh,00h
int 10h
mov ah,02h
mov dx,0000h
mov bh,01h
int 10h
int 20h

/*Screen Shot*/


Monday, August 29

Determining the last and the first letter to occur in alphabetical order

.model small
.code
org 100h
jmp main
    parlist label byte
    maxlen db 30
    actlen db ?
    kbdata db 30 dup(' ')
   
    intro db 'Enter string:  ','$'
    first db 'First:  ','$'
    last db 'Last:  ','$'
input:
    mov ah,0ah
    lea dx,parlist
    int  21h
    ret
printing:
    mov ah,09h
    int 21h
    ret
clr_scr:
    mov ax,0600h
    mov bh,07h
    mov cx,0000h
    mov dx,184fh
    int 10h
    ret
set_cur:
    mov ah,02h
    mov bh,00h
    int 10h
    ret

main proc near
    call clr_scr
    mov dx,0000h
    call set_cur
    lea dx,intro
    call printing
    mov dx,0305h
    call set_cur
    call input
    mov dx,0505h
    call set_cur
    mov bl,actlen
    mov bh,00h
    mov al,61h
    mov cl,7ah
    mov si,00h
    mov di,00h
    lea dx,first
    call printing
    call compare1
    mov dx,0605h
        call set_cur
    lea dx,last
    call printing
    call compare2
    main endp
    end main

compare1:
mov ch,kbdata[si]
cmp ch,cl
jb below
inc si
cmp si,bx
je print1
jmp compare1

below:
mov cl,ch
mov si,00h
jmp compare1

compare2:
mov ch,kbdata[di]
cmp ch,al
ja above
inc di
cmp di,bx
je print2
jmp compare2

above:
mov al,ch
mov di,00h
jmp compare2

print1:
mov ah,02h
mov dl,cl
int 21h
ret

print2:
mov ah,02h
mov dl,al
int 21h
ret


/*Screen Shot*/


Tuesday, August 23

Capitalizing The Small Letters In An Input string


.model small
.code
org 100h
jmp main
parlist label byte
maxlen db 40
actlen db ?
kbdata db 40 dup(' ')

labelinput db 'Enter String: ','$'
con db 40 dup(' ')

input:
    mov ah,0ah
    lea dx,parlist
    int  21h
    ret
printing:
    mov ah,09h
    int 21h
    ret
clr_scr:
    mov ax,0600h
    mov bh,07h
    mov cx,0000h
    mov dx,184fh
    int 10h
    ret
set_cur:
    mov ah,02h
    mov bh,00h
    int 10h
    ret

main proc near
    call clr_scr
    mov dx,0000h
    call set_cur
    lea dx,labelinput
    call printing
    mov dx,0305h
    call set_cur
call input
mov dx,0505h
call set_cur
mov bl,actlen
mov bh,00h
mov si,00h
mov di,00h
call process
mov con[di],00100100b
lea dx,con
call printing

mov ah, 4ch
int 21h

main endp
end main
return:
ret
space:
mov con[di],00100000b
inc si
inc di
jmp process
process:
cmp si,bx
je return
mov dl,kbdata[si]
cmp dl,00100000b
je space
cmp dl,60
ja lowerkeys
jb upperkeys
upperkeys:
mov con[di],dl
inc di
inc si
jmp process
lowerkeys:
and dl,11011111b
mov con[di],dl
inc si
inc di
jmp process

/*Screen Shot*/


Monday, August 22

Multiplication Table


#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    long Length,Width,x,y;
    cout<<"\nEnter Length(Horizontal Range_Columns): ";
    cin>>Length;
    cout<<"\nEnter Width(Vertical Range_Rows):       ";
    cin>>Width;
    cout<<"\nTable of Multiplication:\n\n";
    for(x=1;x<=Width;x++){
                          for(y=1;y<=Length;y++){
                                                 cout<<x*y<<" ";
                                                 }
                                                 cout<<"\n";
                                                 }
    cout<<"\n\n\n";
/*Note: Table is not well organized, specially when dealing with great numbers.*/                        
    system("PAUSE");
    return EXIT_SUCCESS;
}

/*Program Preview*/


Prime Factorization


#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    long num,x,qoutient;
    cout<<"Enter Number: ";
    cin>>num;
    cout<<"\n\tPrime Factors of "<<num<<" is : ";
    for(x=num-1;x>0;x--){
                         qoutient=num/x;
                         if(num%x==0){cout<<"  "<<qoutient;num=x;}
                         }
    cout<<"\n\n\n";
    system("PAUSE");
    return EXIT_SUCCESS;
}


/*Program Preview*/


Factorial


#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{  
    long num,x,product=1;
    cout<<"Enter Number : ";
    cin>>num;
    for(x=1;x<=num;x++){
                        product*=x;
                        }
    cout<<"\n\n\tFactorial of "<<num<<" is : "<<product<<"\n\n\n";
    system("PAUSE");
    return EXIT_SUCCESS;
}


/*Program Preview*/