Newsgroup: it.comp.lang.visual-basic
Titolo: Extracting Variables From Strings

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

 wrote:

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.


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

 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.

 wrote in message
news:4040E251...@swko.dot.net...

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

 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

 the routine I posted since I key into if for parsing purposes.
E avanti... qualcun altro che deve cross-postare lingua straniera in un NG
italiano scordandosi le piů elementari regole della netiquette e del
quoting?
Translation:
Please, go on, someone else wanna cross-postin' in stranger language in an
Italian NG, forgetting the base rules of the netiquette and quoting?
Yankee, please, go home ;)
--
Math Parser : 
http://www.neodatatype.net
Sito Comune : 
http://www.it-lang-vb.net