Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make an iOS calculator, by just using a UITextField

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?

like image 646
objc-obsessive Avatar asked Nov 29 '25 16:11

objc-obsessive


1 Answers

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)

like image 126
Nikita Pestrov Avatar answered Dec 02 '25 06:12

Nikita Pestrov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!