Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsVB SyntaxEnterprise DevelopmentDatabase AccessControlsCOMWin APICrystal ReportDeploymentGeneralGeneral 2
Related Topics
VB.NET / ASP.NETMS SQL ServerMS AccessOther Database ProductsMore Topics ...

VB Forum / General 2 / February 2004



Tip: Looking for answers? Try searching our database.

Extracting Variables From Strings

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
WStoreyII - 28 Feb 2004 16:01 GMT
i am trying to make a program for matrices
what i am trying to do is i have a textbox where the user
inputs the equations.  What i need to do is to be able to manipulate this
text and assign the coefficents of the variables in the equations to
variablse for instance
if this equation was entered

10w + 7x - 9y + 8z = 10
would produce

a1 = 10
b1 = 7
c1 = -9
d1 = 8
e1 = 10
var1 = w
var2 = x
var3 = y
var4 = z

if any one could help me with this i would be much appreciative

WStoreyII
the Wiz - 28 Feb 2004 16:25 GMT
>i am trying to make a program for matrices
>what i am trying to do is i have a textbox where the user
[quoted text clipped - 19 lines]
>
>WStoreyII

You'll have to write a parser using a loop.  Use Mid$() to step through the text
and pare each number, letter, and operator into separate variables.  The parser
would read from the string until the type of character changed (alpha, numeric,
whitespace, operator), then place that group of characters into a variable.

Unless you specifically need the parts of the expression, you could use an
expression evaluator plug-in to perform the operation.

Even small parsers are a lot of work because of the number of things that must
be tracked. The type of character can be alpha, numeric including formatting
(comma, decimal, exponent, scientific notation), operator (including
parentheses), whitespace, and the set of characters that are invalid in the
format you are using.

I've written one parser (in C++) to use with an existing calculator engine
(textbook code that had its own set of bugs).  If you'd like the C++ code as a
reference, send email (fix the address in this message or go to the page in my
signature and use the webmaster link).

More about me: http://www.jecarter.com/
VB3/VB6/C/PowerBasic source code: http://www.jecarter.com/programs.html
Freeware for the Palm with NS Basic source code: http://nsb.jecarter.com
Drivers for Pablo graphics tablet and JamCam cameras: http://home.earthlink.net/~mwbt/
johnecarter at@at mindspring dot.dot com. Fix the obvious to reply by email.
Rick Rothstein - 28 Feb 2004 17:15 GMT
> i am trying to make a program for matrices
> what i am trying to do is i have a textbox where the user
[quoted text clipped - 17 lines]
>
> if any one could help me with this i would be much appreciative

The following subroutine assumes the equations will always have constant
coefficients; the signs separating the variable and its coefficient will
only be plus or minus signs; the order of the terms is the same as the order
of your variable letter assignments to var1, var2, etc. (you might want to
consider using an array instead of hard-coded, sequential variable names);
the equal sign along with the term following it is always at the end of the
equation; and, finally, if a term is "missing", it will still be shown in
the equation with a coefficient of zero. I'm thinking a routine like this
would work (created off the top of my head):

Sub AssignCoefficients(ByVal Equation As String, _
                    ParamArray Values())
 Dim Index As Long
 Dim Position As Long
 Dim EqualSign As Long
 Dim BeginningOfNumber As Long
 Dim EndOfNumber As Long
 Equation = Replace$(Equation, " ", "")
 EqualSign = InStr(Equation, "=")
 Values(UBound(Values)) = Mid$(Equation, EqualSign + 1)
 Position = 1
 BeginningOfNumber = Position
 Do While Index < UBound(Values)
   Do While Position <= EqualSign
     If Mid$(Equation, Position, 1) Like "[!0-9+-]" Then
       EndOfNumber = Position - 1
       Exit Do
     End If
     Position = Position + 1
   Loop
   Values(Index) = Mid$(Equation, BeginningOfNumber, _
                        EndOfNumber - BeginningOfNumber + 1)
   Index = Index + 1
   Position = Position + 1
   BeginningOfNumber = Position
 Loop
End Sub

And an example of calling it, using the names for the coefficients that you
post showed, would be

Private Sub Command1_Click()
 Dim a1 As Double
 Dim b1 As Double
 Dim c1 As Double
 Dim d1 As Double
 Dim e1 As Double
 ' Assume TextBox with entry of "10w + 7x - 9y + 8z = 10
 AssignCoefficients Text1.Text, a1, b1, c1, d1, e1
 ' Show that the assignments took place
 Debug.Print a1
 Debug.Print b1
 Debug.Print c1
 Debug.Print d1
 Debug.Print e1
End Sub

You might want to consider using an array for those coefficient variables.
If you do, simply removed the Parameter keyword from the Sub declaration,
change the Values type to Double and pass that array into the Sub instead.

Rick - MVP
WStoreyII - 28 Feb 2004 18:09 GMT
> > i am trying to make a program for matrices
> > what i am trying to do is i have a textbox where the user
[quoted text clipped - 80 lines]
>
> Rick - MVP

Could i set up some sort of code so that once the input is entered and
before it is parced it will go through and remove all spaces commas equal
signs everything but numbers variables and then parse it for the individual
variables would that make it easier you think?

WStoreyII
Newbie Programmer Want -A - Be
Duane Bozarth - 28 Feb 2004 18:47 GMT
...

> Could i set up some sort of code so that once the input is entered and
> before it is parced it will go through and remove all spaces commas equal
> signs everything but numbers variables and then parse it for the individual
> variables would that make it easier you think?

Yes, I'd also check for the existence of illegal characters at the same
time. Of course, you could do some of that during the textbox entry
phase as well by not allowing illegal characters, etc.

I agree w/Rick you'd be well served in going to an array syntax rather
than keeping the individual terms.  Depending on the flexibility you
intend to cover, would change the manner in which I would see creating
it, however.
WStoreyII - 28 Feb 2004 18:55 GMT
> ...
>
[quoted text clipped - 11 lines]
> intend to cover, would change the manner in which I would see creating
> it, however.

The problem is that I am still new to programming period and still am trying
to grasp the concept of arrays so if you could point me to some good
tutorials this may help with my project.

Thanks for the help
Rick Rothstein - 28 Feb 2004 18:58 GMT
> Could i set up some sort of code so that once the input is entered and
> before it is parced it will go through and remove all spaces commas equal
> signs everything but numbers variables and then parse it for the individual
> variables would that make it easier you think?

No need to do any removals before processing... let the subroutine do all of
the hard work for you. The routine I posted already removes all of the
spaces. Commas? If those are what you use for decimal points, no. If that is
what you use for "thousand's separators", then yes. Add this line

     Equation = Replace$(Equation, ",", "")

right after the similar statement I used to remove the spaces. I guess you
could remove the equal sign if you wanted, but then you would have to change
the routine I posted since I key into if for parsing purposes.

Rick - MVP
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.