PDA

View Full Version : calculating string



T-Bear
08-04-2012, 03:36 PM
Hi, i need a function that gets a string and calculates it and then returns a integer.

For eksample if the string is "5+3" Then the function should return 8.

How can I make this. I guess someone has already done something like this before. Any tips, or codeexamples??
Thanks for help. ;)

AthenaOfDelphi
08-04-2012, 05:28 PM
Hi T-Bear,

Before I provide anything that might resemble a real answer, do you want to know how to do it or do you just need to do it?

The reason I ask is simply because the answer depends on where you're going with it. If it's part of a larger problem and you just need a solution then the answer will be very different to if you want to know how to go about doing it.

Edit:- Another question to go with this... do you want to access variables from within your program so they can be used in the expression you are evaluating?

Regards

T-Bear
08-04-2012, 09:59 PM
@AthenaOfDelphi:
Thanks for the answer. The reason why i need this is that i am making a simple scripting language for a small game. At the moment i can assign a value to a variable, like this:
X := 5
but i want it to be able to calculate expressions and assign that value. Im a little unsure about how to do this, so I hope someone could show some examplecode that does this with numbers only. Then i can make it work with the variables i need :), and i will also learn something from it ;).
Thanks! :D

AthenaOfDelphi
08-04-2012, 11:03 PM
In that case... I would suggest you read a couple of bits. The first discusses Reverse Polish Notation (http://en.wikipedia.org/wiki/Reverse_Polish_notation).

Reverse polish notation (RPN) is a means of representing expressions in a way that is friendly for stack based languages (Forth for example).

2+3 is represented as a sequence of operations such as push(2), push(3) add. Where 2 is pushed onto a stack, 3 is pushed onto a stack and the operator add pops two items from the stack and adds them together before pushing the answer back onto the stack. It gets a little complicated when you start dealing with parenthesis, etc. But there is an algorithm called the Shunting Yard Algorithm (http://en.wikipedia.org/wiki/Shunting-yard_algorithm) which deals with converting an expression to RPN.

Once you've converted an expression, you should look at caching the RPN representation for it. This will save you having to convert it every time. And if you really want to get clever, you can look to pre-process constants and then re-use the optimised RPN.

For example, 2+3+4+5. Let's say this occurs in a loop that is executed a lot, converting it to RPN and then running it every time would be highly wasteful. It's always going to be 14. So instead of converting it to the RPN representation equivalent to push(2), push(3), add, push(4), add, push(5), add you could simply represent it as push(14).

I have used conversion of expressions and then storage of the RPN representation myself (many years ago, Turbo Pascal 7 on the PC side and BBC Basic on an Amstrad NC-100) and it worked a treat. I'm sure there will be other opinions about how to do this, but this is how I would approach it.

Use the shunting yard algorithm to convert the expression to the RPN representation and then produce a stack evaluator to 'run' the resulting RPN.

I hope this helps, if you have more questions obviously ask away :)

Daikrys
08-04-2012, 11:04 PM
you can try to use cqparser (http://www.delphipraxis.net/22764-mathem-parser-bitte-testen.html)
you need to register to download the file but it can calculate strings really fast so it is worth it :)

pitfiend
08-04-2012, 11:47 PM
You can try CalcExpress (http://www.aidaim.com/products/delphi.php#CalcExpress) its a very basic evaluation component, but hey! it's freeware with source code.