C operations with variables

Is there a relatively simple way to use math operators with string variables in Objective-C?

For instance:

The string "x * x" should return "x ^ 2"

The string "x / x" should return "1"

I am looking for something that actually uses the variable name "x" without an assigned numerical value and returns an answer in terms of "x".

+5
source share
3 answers

If you put the value "x" as a string, you can evaluate it as:

NSString *string=@"3*3";
NSExpression *expression = [NSExpression expressionWithFormat:string];
float result = [[expression expressionValueWithObject:nil context:nil] floatValue];
NSLog(@"%f", result);

Besides,

NSString *string=@"34*(2.56+1.79)-42/1.5";
NSNumber *result=[NSExpression expressionWithFormat:string];
NSLog(@"%@", result);

Change any data type floatto intetc. according to your requirement

+1
source

Objective C lib, , .

, C ( , Objective C ANSI C, ).

C maths libs:

http://www.mathtools.net/C_C__/Mathematics/

, Mathomatic .

C Objective C:

  • C Objective C

  • Objective C C (, , :

+1

GiNaC (www.ginac.de). openource. . OSX Xcode, ++. 1. Ginac, CLN Pkg-Config (http://www.freedesktop.org/wiki/Software/pkg-config/). 2. Pkg-Config /usr/local. 3. CLN 4. Ginac

Then get to you. Project settings in Xcode have chosen your target and placed the path to the header files in the header search path in the build settings. Then go to the section "Phase assembly" - "Linking binary files to libraries" click "+" and select "Add another", then select the compiled library file libginac.a This is all I think. Remember to include the ginac.h file in the header file of your class.

#include <ginac.h>
0
source

All Articles