PDA

View Full Version : String as equation?




wesg
Sep 28, 2010, 10:37 PM
Has anyone had success turning strings into equations?

ie. take a stored string of 2*5+6 and output 16



(marc)
Sep 29, 2010, 02:29 AM
You'll have to write a parser.

EDIT: This might not be trivial. I googled "c equation parser" and found this (http://www.ubasics.com/simple_c_equation_parser).

dejo
Sep 29, 2010, 08:54 AM
Wirelessly posted (Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7)

Perhaps you can use JavaScript's eval() function.

PhoneyDeveloper
Sep 29, 2010, 09:39 AM
This has been an assignment in CS courses since 1962. I think you'll be able to find some source code online.

expression evaluation code

http://www.google.com/search?q=expression+evaluation+code&hl=en&num=100&lr=&ft=i&cr=&safe=off

wesg
Sep 29, 2010, 04:33 PM
Thanks for all the suggestions. I'm going to look into storing them as actual values... we'll see how it goes.