Activity 5: Parsing an expression string in Visual Basic .NET

Encoder QR-Code in Visual Basic .NET Activity 5: Parsing an expression string
Activity 5: Parsing an expression string
Creating QR In VB.NET
Using Barcode creator for .NET framework Control to generate, create QR-Code image in Visual Studio .NET applications.
There are various methods within the String class that allow us to break it into separate sub-strings The Chars() property is an array of single Char types, and can be used to access individual characters (such as * ) However, we need to know the index of a particular character to retrieve it The IndexOf() function can do this for us Given the expression 4*10 , IndexOf("*") will return 1 (counting from 0 as the rst character of the string) We could therefore use IndexOf() to determine the position of the operator, and from this extract the characters to the left of it (the rst number) and the characters to the right of it (the second number) However, at the point where we are trying to determine the position of the operator, we don t know which speci c operator will be in the string A similar method, IndexOfAny() can solve this problem for us, since it takes an array of operators, and returns the position of the rst occurrence of any of them Given the string expression Expr, containing 4*10 , the following statements (Figure A43) will retrieve the two numbers, n1 and n2, and the operator, op:
Making Bar Code In VB.NET
Using Barcode generation for .NET framework Control to generate, create barcode image in .NET applications.
Dim operators() As Char = {"+", " ", "*", "/"} OpPos = ExprIndexOfAny (operators) n1 = CType(ExprSubstring(0, OpPos), Decimal) n2 = CType(ExprSubstring(OpPos + 1), Decimal) op = ExprSubstring(OpPos, 1)
Quick Response Code Drawer In Visual C#.NET
Using Barcode printer for VS .NET Control to generate, create QR Code JIS X 0510 image in VS .NET applications.
Breaking up an expression
QR Code Creator In .NET
Using Barcode maker for ASP.NET Control to generate, create QR Code 2d barcode image in ASP.NET applications.
Figure A43
QR Code JIS X 0510 Encoder In Visual Studio .NET
Using Barcode maker for Visual Studio .NET Control to generate, create QR Code image in .NET applications.
Note that CType() is being used to convert the number substrings to Decimal type directly The operation of splitting up an expression into constituent parts is called parsing, and is commonly used in programming to break strings into elements that are easier to deal with individually Now that we have this ability, we can greatly simplify the use of a Calculator object by creating a constructor into which we can pass an expression to be evaluated This is shown in Listing A47
Bar Code Drawer In Visual Basic .NET
Using Barcode creator for VS .NET Control to generate, create bar code image in Visual Studio .NET applications.
Class Calculator Private mvarNumber1 As Decimal Private mvarNumber2 As Decimal Private mvarOperator As Char Public Sub New(ByVal Expr As String)
Barcode Creator In VB.NET
Using Barcode creator for .NET Control to generate, create bar code image in .NET applications.
4 n Objects in Visual Basic NET
Code 39 Extended Creation In Visual Basic .NET
Using Barcode creator for VS .NET Control to generate, create Code 3/9 image in .NET applications.
Dim OpPos As Integer Dim operators() As Char = {"+", " ", "*", "/"} OpPos = ExprIndexOfAny(operators) mvarNumber1 = CType(ExprSubstring(0, OpPos), Decimal) mvarNumber2 = CType(ExprSubstring(OpPos + 1), Decimal) mvarOperator = ExprSubstring(OpPos, 1) End Sub 'Other code for the class ' End Class Listing A47: A constructor for the Calculator class
Code 128C Creation In VB.NET
Using Barcode creation for VS .NET Control to generate, create USS Code 128 image in .NET framework applications.
Now our Calculator class is much easier to use, since we can assign all of the input expression in a single statement and then simply display the result The full listing of the class and a Sub Main() to use it is given in Listing A48
ECC200 Generator In Visual Basic .NET
Using Barcode encoder for Visual Studio .NET Control to generate, create Data Matrix ECC200 image in VS .NET applications.
Module Calc Class Calculator Private mvarNumber1 As Decimal Private mvarNumber2 As Decimal Private mvarOperator As Char Public Sub New(ByVal Expr As String) Dim OpPos As Integer Dim operators() As Char = {"+", " ", "*", "/"} OpPos = ExprIndexOfAny(operators) mvarNumber1 = CType(ExprSubstring(0, OpPos), _ Decimal) mvarNumber2 = CType(ExprSubstring(OpPos + 1), _ Decimal) mvarOperator = ExprSubstring(OpPos, 1) End Sub Public Property Number1() As Decimal Get Return mvarNumber1 End Get Set(ByVal Value As Decimal) mvarNumber1 = Value End Set End Property Public Property Number2() As Decimal Get Return mvarNumber2 End Get Set(ByVal Value As Decimal) mvarNumber2 = Value End Set End Property
Drawing GTIN - 13 In Visual Studio .NET
Using Barcode maker for ASP.NET Control to generate, create GTIN - 13 image in ASP.NET applications.
Practical Activities
Code 3 Of 9 Decoder In VS .NET
Using Barcode recognizer for .NET Control to read, scan read, scan image in VS .NET applications.
Public Property Operator() As Char Get Return mvarOperator End Get Set(ByVal Value As Char) mvarOperator = Value End Set End Property Public Function Result() As Decimal If Operator = "+" Then Return Number1 + Number2 End If If Operator = " " Then Return Number1 Number2 End If If Operator = "*" Then Return Number1 * Number2 End If If Operator = "/" Then Return Number1 / Number2 End If End Function End Class Sub Main() Dim Expr As String ConsoleWrite( _ "Enter an expression (number op number): ") Expr = ConsoleReadLine() Dim Calc As New Calculator(Expr) ConsoleWriteLine("{0} {1} {2} = {3}", _ CalcNumber1, CalcOperator, _ CalcNumber2, CalcResult) End Sub End Module Listing A48: The full calculator program
Scan UPC - 13 In Visual Studio .NET
Using Barcode recognizer for Visual Studio .NET Control to read, scan read, scan image in .NET applications.
Draw Barcode In C#
Using Barcode creation for VS .NET Control to generate, create bar code image in VS .NET applications.
Make Bar Code In Visual Studio .NET
Using Barcode maker for ASP.NET Control to generate, create barcode image in ASP.NET applications.
UPC-A Supplement 2 Generator In Visual Studio .NET
Using Barcode encoder for VS .NET Control to generate, create UPC-A image in VS .NET applications.
Painting EAN-13 In Java
Using Barcode creation for Java Control to generate, create GTIN - 13 image in Java applications.