I am looking to create an iOS calculator, just by using a UITextField. So for example; somebody may type in '6 - 3 x 2' in the UITextField then hit "Done" or "Go" etc. This would then calculate the answer and display it in a UIAlertView.
So firstly I believe that the UITextField text has to be split up into different sections so '6' '-' '3' '*' '2' would be the separated strings from the UITextField text input. How would I go about separating the text like mentioned above?
How can I then use this information to calculate the answer and to display it?
Use GCMathParser http://apptree.net/parser.htm
It takes an NSString as an input and evaluates it for you.
As Danh said, you can also use DDMathParser https://github.com/davedelong/DDMathParser
An Example(right from the docs)
NSString* mathstring = @"tan(dtor(45))";
double result = [mathstring evaluateMath]; // returns 0.999999999999...
So you simply send message evaluateMath to the NSString you want to parse. That's it)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With