Parsing a Expression String

DOTNETCoder33

Member
Joined
Jun 18, 2010
Messages
15
Programming Experience
Beginner
hey

i have a problem just now and am stuck on this subject


im doing a simple calculatro and was happy with the way i done things .. a ew member variables and a properties:

Private MVAR_Number1 As Decimal
Private MVAR_Number2 As Decimal
Private MVAR_Operator As Char

Public Property Number1() As Decimal
Get
Return MVAR_Number1
End Get
Set(ByVal value As Decimal)
MVAR_Number1 = value
End Set
End Property

Public Property Number2() As Decimal
Get
Return MVAR_Number2
End Get
Set(ByVal value As Decimal)
MVAR_Number2 = value
End Set
End Property

Public Property Operator_Selected() As Char
Get
Return MVAR_Operator
End Get
Set(ByVal value As Char)
MVAR_Operator = value
End Set
End Property

Public Function Result() As Decimal
If Operator_Selected = "+" Then
Return Number1 + Number2
ElseIf Operator_Selected = "-" Then
Return Number1 - Number2
ElseIf Operator_Selected = "*" Then
Return Number1 * Number2
ElseIf Operator_Selected = "/" Then
Return Number1 / Number2
End If

End Function



BUT now the book im reading into wants complicate things up a bit and i dont have a bloomin clue!! :( it just seems all he has done is make things a heck of a lot more complex for something that is supposed to be so simple!



he is now saying "we can break up the expression" .. why would you want to do that tho if it already works?



he want to add this:

Dim Operators() As Char = ("+", "-", "*", "/")
Dim OpPos as Decimal

OpPos = Expr.IndexOfAny(Operators)

MvarNumber1 = CType (Expr.Substring (0,OpPos), Decimal

then goes similar for Number 2 ect


Just drives me mad how complex this is all getting for something so simple, fell like giving it all up !! :( dont have a clue what the heck hes on about




not in a good mood today coz of this .. as you can tell ...
 
It's all about scalability. You might write some simple code to complete a simple task but, when the task becomes twice as complex you find yourself having to write 4 times as much code, and when it becomes 10 times as complex you have to write 100 times as much code. You need to develop a style where the code may be a little more complex to begin with but it it will handle any number of problems from very simple to very complex with little or no change. It's a stitch in time to save none. Do a little extra work early on and you do less work overall.
 
It's all about scalability. You might write some simple code to complete a simple task but, when the task becomes twice as complex you find yourself having to write 4 times as much code, and when it becomes 10 times as complex you have to write 100 times as much code. You need to develop a style where the code may be a little more complex to begin with but it it will handle any number of problems from very simple to very complex with little or no change. It's a stitch in time to save none. Do a little extra work early on and you do less work overall.

Thanks for the reply; i still dont understand what Parsing is?
 
Yes John, i did ask. I said "he is now saying "we can break up the expression" .. why would you want to do that tho if it already works?



I have searched for a good while now and the Wiki articles. Most of the replys on here seemt o be refered to Wiki, but i have already done that, so i posted on here to see if i can get a "human answer" someone explaining it from their point of view on a given example. Wiki is good but it covers the vast majoirty of the subject matter, my example is more specific; even with me searching i still cant understand it. Also, do you think i should go for a course at Uni ... a BSc Honours Degree in Computing? ... i am studying just now but it is all Self Study and i dont have a tutuor ... :( i find it difficult, So was thinking; i could go to Uni get the degree THEN have a solid understanding of the programming concpets and return to do the Microsoft Certs after or even during if i then feel upto it. Would you agree?


You able to help?


Thanks John, and thanks for the Previous reply from the MVP .... great status to have, well done.
 
Your code works on expressions like "1+1" and "4-2", but what about "2*5+6/3"? Like I said, you want to build scalable code that will work for the general case and not have to be reworked any time the target changes slightly. What's better: write specific code for two numbers, three numbers, four numbers, etc, etc, or write one set of code that will work no matter how many numbers you have?
 
Your code works on expressions like "1+1" and "4-2", but what about "2*5+6/3"? Like I said, you want to build scalable code that will work for the general case and not have to be reworked any time the target changes slightly. What's better: write specific code for two numbers, three numbers, four numbers, etc, etc, or write one set of code that will work no matter how many numbers you have?

I see how this is working but still unsure of how the actual code functions and how it can manipulate the data to give those results. I know it Analyses the data but dont understand the code layout and how VB interprets it?


thanks for the reply tho
 
Back
Top