Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Parse a string of text in order to perform a calculation
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Coldkick




PostPosted: Sun Oct 03, 2010 11:20 am   Post subject: Parse a string of text in order to perform a calculation

What is it you are trying to achieve?
I am trying to create a calculator program which when you enter an equation it will solve it or graph it.


What is the problem you are having?
I want to be able to have a user enter for example, "3**2-(4/5+3)" and it will parse each individual part and calculate it.


Describe what you have tried to solve this problem
I have tried making sets of flexible arrays for numbers and symbols and setting them as each individual character in the string.

Please specify what version of Turing you are using
Turing 4.1.1a


Thanks to anyone who can help. Smile
Sponsor
Sponsor
Sponsor
sponsor
DtY




PostPosted: Sun Oct 03, 2010 9:37 pm   Post subject: RE:Parse a string of text in order to perform a calculation

There are two steps here:

First, you want to tokenize the input, in your example you would split it up into
- 3
- **
- 2
- -
- (
- 4
- /
- 5
- +
- 3
- )
This is pretty easy, and you'll probable do it at the same time as the next step (rather than just splitting it up into an array or something, you'd just grab the next token, process it, repeat).

Next, which will be more difficult is that you want to transform these tokens into something a computer can process. To do this, you'll probably be using the Shunting Yard Algorithm to transform the expression into RPN.

I would suggest getting a basic understanding of RPN before attempting this. Here's an RPN calculator you can play around with. Just learn to do basic multistep equations in RPN, don't need to be able to do anything too fancy, the shunting yard algorithm will be doing that for you.

Just two notes to help you out
- That calculator I linked to has a stack depth of four. That's good enough for an RPN calculator, but if a program is converting from algebraic notation it will probably not be enough. This is just because it's rather difficult to get the computer to do operations in the most efficient way; the shunting yard algorithm wont. The expression you entered would have three items on the stack at one point, put a person who knows what they are doing can do it with only two items on the stack.

- I would suggest using `^` for powers, rather than `**`, this is just to make tokenizing easier, because you wont have to deal with any operators that are more than one character long.

To help you with RPN, here's what your expression would look like in RPN:

Na?ve method (what your algorithm will use):
3 2 ** 4 5 / 3 + -

Optimized for small stack (what a human might enter):
4 5 / 3 + 3 2 ** swap -

The swap operator here is a common RPN operator, it takes two items off the stack and puts them back on in the reverse order (a b => b a)
Coldkick




PostPosted: Tue Oct 05, 2010 6:10 pm   Post subject: RE:Parse a string of text in order to perform a calculation

Thanks, I'll give you some bits once I figure it out. lol. For now +1 Karma.

EDIT: NVM need 25 post count.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: