forked from natikgadzhi/XNMaths
-
Notifications
You must be signed in to change notification settings - Fork 1
/
GCMathParser.m
214 lines (152 loc) · 3.06 KB
/
GCMathParser.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
//
// GCMathParser.m
//
// Created by graham on 28/08/2007.
//
#import "GCMathParser.h"
@implementation GCMathParser
static struct init s_MathFunctions[]=
{
"sin", sin,
"cos", cos,
"tan", tan,
"log", log10,
"log2", log2,
"ln", log,
"exp", exp,
"abs", fabs,
"sqrt", sqrt,
"asin", asin,
"acos", acos,
"atan", atan,
"sinh", sinh,
"cosh", cosh,
"tanh", tanh,
"asinh", asinh,
"acosh", acosh,
"atanh", atanh,
"ceil", ceil,
"floor", floor,
"round", round,
"trunc", trunc,
"rint", rint,
"near", nearbyint,
"dtor", degtorad,
"rtod", radtodeg,
0, 0
};
+ (double) evaluate:(NSString*) expression
{
return [[self parser] evaluate:expression];
}
+ (GCMathParser*) parser
{
return [[[GCMathParser alloc] init] autorelease];
}
- (id) init
{
if ((self = [super init]) != nil )
{
int i;
symbol *ptr;
_st = NULL;
_expr = NULL;
// install mathematical functions in symbol table
for ( i = 0; s_MathFunctions[i].fname != 0; i++ )
{
ptr = [self initSymbol:s_MathFunctions[i].fname ofType:FUNCTION];
ptr->value.func = s_MathFunctions[i].fnct;
}
// also include a named constant for pi
[self setSymbolValue:pi forKey:@"pi"];
}
return self;
}
- (void) dealloc
{
// free symbol table
symbol *ns, *s;
s = _st;
while( s )
{
ns = s->next;
free( s->name );
free( s );
s = ns;
}
[_expr release];
[super dealloc];
}
- (double) evaluate:(NSString*) expression
{
[expression retain];
[_expr release];
_expr = expression;
_result = 0.0;
yyparse( self );
// return the result...
return _result;
}
- (NSString*) expression
{
return _expr;
}
- (const char*) expressionCString
{
return [[self expression] cString];
}
- (void) setSymbolValue:(double) value forKey:(NSString*) key
{
symbol* p;
p = [self getSymbol:key];
if ( p == NULL )
p = [self initSymbol:[key cString] ofType:VAR];
if ( p )
p->value.var = value;
}
- (double) symbolValueForKey:(NSString*) key
{
symbol* s = [self getSymbol:key];
if ( s )
return s->value.var;
else
return 0.0;
}
// private methods called internally:
- (symbol*) getSymbol:(NSString*) key
{
return [self getSymbolForCString:[key cString]];
}
- (symbol*) getSymbolForCString:(const char*) name
{
symbol *ptr;
for ( ptr = _st; ptr != NULL; ptr = ptr->next )
{
if ( strcmp( ptr->name, name ) == 0 )
return ptr;
}
return NULL;
}
- (symbol*) initSymbol:(const char*) name ofType:(int) type
{
symbol* ptr = (symbol*) malloc ( sizeof( symbol ));
ptr->name = (char*) malloc( strlen( name ) + 1 );
strcpy( ptr->name, name );
ptr->type = type;
ptr->value.var = 0; // preset value to 0 even if type is a function
ptr->next = _st;
_st = ptr;
return ptr;
}
- (void) setResult:(double) result
{
_result = result;
}
@end
#pragma mark -
@implementation NSString (ExpressionParser)
- (double) evaluateMath
{
return [GCMathParser evaluate:self];
}
@end