• Post Reply Bookmark Topic Watch Topic
  • New Topic

Conversion of string into an expression  RSS feed

 
sri kam
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have a strings of the form as "i*1+2"

now I have to convert that strings into an expresion
i*i+2, where each is an individual token.


Thank you
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sri kam:
Hello,

I have a strings of the form as "i*1+2"

now I have to convert that strings into an expresion
i*i+2, where each is an individual token.

Thank you


Refer to "Introduction to Data Structures" Bhagat Singh
http://www.amazon.com/exec/obidos/tg/detail/-/0314852999/002-7008479-5241611?v=glance#product-details
This book throws some light on stack based implementation of a string tokenizer . may be it's helpful to you.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Parsing a string to an expression can get very complex. Is this for a class? Do you have any hints on how to start?
 
Fisher Daniel
Ranch Hand
Posts: 582
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I dont know whether it is proper solution for your case or not.
I think you can use StringTokenizer to parse that string...

So first, you create StringTokenizer object and parse that string with delimiter '*'.
After that you create second StringTokenizer object and parse the first result with delimiter '+'.
And now you have 3 string... 'i', '1' and '2'

Correct me if I am wrong...
Hope this help
daniel
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!