REM written by Daniel R. Spohn 'This text is filler 'sdfdsfsdfsdf '10/02/03 Option Strict On Public Class Homework3 Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code " Public Sub New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub 'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Friend WithEvents txtInput As System.Windows.Forms.TextBox Friend WithEvents btnCalculate As System.Windows.Forms.Button Friend WithEvents lblCalculated As System.Windows.Forms.Label Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu Friend WithEvents mnuFile As System.Windows.Forms.MenuItem Friend WithEvents mnuHelp As System.Windows.Forms.MenuItem Friend WithEvents mnuFileClear As System.Windows.Forms.MenuItem Friend WithEvents mnuFileExit As System.Windows.Forms.MenuItem Friend WithEvents mnuHelpAbout As System.Windows.Forms.MenuItem Friend WithEvents ToolTip1 As System.Windows.Forms.ToolTip Private Sub InitializeComponent() Me.components = New System.ComponentModel.Container() Me.txtInput = New System.Windows.Forms.TextBox() Me.btnCalculate = New System.Windows.Forms.Button() Me.lblCalculated = New System.Windows.Forms.Label() Me.MainMenu1 = New System.Windows.Forms.MainMenu() Me.mnuFile = New System.Windows.Forms.MenuItem() Me.mnuFileClear = New System.Windows.Forms.MenuItem() Me.mnuFileExit = New System.Windows.Forms.MenuItem() Me.mnuHelp = New System.Windows.Forms.MenuItem() Me.mnuHelpAbout = New System.Windows.Forms.MenuItem() Me.ToolTip1 = New System.Windows.Forms.ToolTip(Me.components) Me.SuspendLayout() ' 'txtInput ' Me.txtInput.Location = New System.Drawing.Point(56, 32) Me.txtInput.Name = "txtInput" Me.txtInput.Size = New System.Drawing.Size(304, 20) Me.txtInput.TabIndex = 0 Me.txtInput.Text = "" Me.ToolTip1.SetToolTip(Me.txtInput, "Enter an Expression such as ""5+3"" or ""20 \ 5 * 2""") ' 'btnCalculate ' Me.btnCalculate.Location = New System.Drawing.Point(168, 120) Me.btnCalculate.Name = "btnCalculate" Me.btnCalculate.Size = New System.Drawing.Size(80, 32) Me.btnCalculate.TabIndex = 1 Me.btnCalculate.Text = "&Calculate" ' 'lblCalculated ' Me.lblCalculated.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.lblCalculated.Location = New System.Drawing.Point(128, 80) Me.lblCalculated.Name = "lblCalculated" Me.lblCalculated.Size = New System.Drawing.Size(160, 16) Me.lblCalculated.TabIndex = 2 ' 'MainMenu1 ' Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuFile, Me.mnuHelp}) ' 'mnuFile ' Me.mnuFile.Index = 0 Me.mnuFile.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuFileClear, Me.mnuFileExit}) Me.mnuFile.Text = "&File" ' 'mnuFileClear ' Me.mnuFileClear.Index = 0 Me.mnuFileClear.Text = "&Clear" ' 'mnuFileExit ' Me.mnuFileExit.Index = 1 Me.mnuFileExit.Text = "E&xit" ' 'mnuHelp ' Me.mnuHelp.Index = 1 Me.mnuHelp.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuHelpAbout}) Me.mnuHelp.Text = "&Help" ' 'mnuHelpAbout ' Me.mnuHelpAbout.Index = 0 Me.mnuHelpAbout.Text = "&About" ' 'Homework3 ' Me.AcceptButton = Me.btnCalculate Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(416, 181) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.lblCalculated, Me.btnCalculate, Me.txtInput}) Me.Menu = Me.MainMenu1 Me.Name = "Homework3" Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen Me.Text = "Integer Arithmetic" Me.ResumeLayout(False) End Sub #End Region Dim mintStart As Integer = 1 Dim mintIndex As Integer = 1 Dim mstrOperator As String Dim blnError As Boolean = False Private Function ValidInput(ByVal A As String) As Boolean 'This function calls the RemoveSpaces function and the Invalid Characters function Dim blnValid As Boolean Dim strSpacesRemoved As String If InvalidCharacters(txtInput.Text) > 0 Then blnValid = False Else txtInput.Text = RemoveSpaces(txtInput.Text) blnValid = True End If Return blnValid End Function Private Function RemoveSpaces(ByVal A As String) As String 'this function removes uneeded spaces from the expression Dim IntIndex As Integer = 1 Dim NewString As String For IntIndex = 1 To A.Length If " " <> A.Substring(IntIndex - 1, 1) Then NewString &= A.Substring(IntIndex - 1, 1) End If Next Return NewString End Function Private Function InvalidCharacters(ByVal A As String) As Integer 'This Function Checks the Expression for invalid characters Dim intIndex As Integer = 1 Dim intReturn As Integer Dim intLength As Integer = A.Length While (intIndex <= intLength) If (Strings.InStr("+-*\^0123456789 ", A.Substring(intIndex - 1, 1), 0) = 0) Then mintStart = intIndex - 1 intReturn = intIndex End If intIndex += 1 End While Return intReturn End Function Private Function GetOperand(ByVal A As String) As Integer 'This function gets the first operand Dim StrCharacter As String = "" Dim StrOperand As String Dim IntOperand As Integer Do Until ((mintIndex - 1) = A.Length) StrCharacter = A.Substring(mintIndex - 1, 1) Select Case StrCharacter Case "+", "-" If mintIndex = 1 Then StrOperand = StrCharacter Else Return IntOperand End If Case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" StrOperand &= StrCharacter Try IntOperand = CInt(StrOperand) Catch myErr As OverflowException MessageBox.Show("Number to Large to be Calculated") blnError = True mintIndex = A.Length End Try Case Else 'mintAnswer = IntOperand Return IntOperand End Select mintIndex += 1 Loop Return IntOperand End Function Private Function GetOperatorthenOperand(ByVal A As String) As Integer 'This function gets and operator, then an operand If blnError = False Then Dim strCharacter As String strCharacter = A.Substring(mintIndex - 1, 1) mstrOperator = strCharacter mintIndex += 1 Dim StrOperand As String Dim intOperand As Integer Dim blnOperand As Boolean = False Do Until ((mintIndex - 1) = A.Length) strCharacter = A.Substring(mintIndex - 1, 1) Select Case strCharacter Case "+", "-" If blnOperand = False Then StrOperand = strCharacter Else Return intOperand End If Case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" StrOperand &= strCharacter Try intOperand = CInt(StrOperand) Catch myErr As OverflowException MessageBox.Show("Number to Large to be Calculated") blnError = True End Try blnOperand = True Case Else Return intOperand End Select mintIndex += 1 Loop Return intOperand End If End Function Private Function EvaluateExpression(ByVal A As String) As Integer ' this expression calls getoperand and getoperatorthenoperand functions ' Also it decides whether to add, subtract, divide or multiply the operands If blnError = False Then Dim intOperand1 As Integer Try intOperand1 = GetOperand(A) Do Until ((mintIndex - 1) = A.Length) Dim intOperand2 As Integer = 0 intOperand2 = GetOperatorthenOperand(A) Select Case mstrOperator Case "+" intOperand1 = intOperand1 + intOperand2 Case "-" intOperand1 = intOperand1 - intOperand2 Case "\" intOperand1 = intOperand1 \ intOperand2 Case "*" intOperand1 = intOperand1 * intOperand2 Case "^" intOperand1 = CInt(intOperand1 ^ intOperand2) End Select Loop Catch myErr As OverflowException MessageBox.Show("Number to Large to be Calculated") blnError = True Exit Function Catch myErr As DivideByZeroException MessageBox.Show("Cannot Divide by Zero") blnError = True Exit Function End Try Return intOperand1 End If End Function Private Sub mnuFileClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileClear.Click lblCalculated.Text = "" With txtInput .Clear() .Focus() End With End Sub Private Sub mnuFileExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileExit.Click Me.Close() End Sub Private Sub mnuHelpAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuHelpAbout.Click MessageBox.Show("This Program was written by: Daniel R. Spohn") End Sub Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click Dim strInput As String If ValidInput(txtInput.Text) Then strInput = txtInput.Text strInput = CStr(EvaluateExpression(strInput)) If blnError = False Then lblCalculated.Text = strInput End If Else MessageBox.Show("Invalid Input- Please Enter Only Operands and Operators") With txtInput .Select(mintStart, 1) .Focus() End With End If mintIndex = 1 blnError = False End Sub End Class