-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculate.c
502 lines (494 loc) · 11.5 KB
/
calculate.c
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
/*
* Copyright (C) 1992 by Rush Record
* Copyright (C) 1993 by Charles Sandmann (sandmann@clio.rice.edu)
*
* This file is part of ED.
*
* ED is free software; you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Foundation.
*
* ED is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with ED
* (see the file COPYING). If not, write to the Free Software Foundation, 675
* Mass Ave, Cambridge, MA 02139, USA.
*/
#include "opsys.h"
#include <stdio.h>
#include <stddef.h>
#include <string.h>
#include <math.h>
#include "ctyp_dec.h"
#ifndef PI
#define PI 3.14159265
#endif
#define CONV (PI / 180.0)
#ifndef HUGE_VAL /* ANSI type definition conversion */
#define HUGE_VAL HUGE
#endif
/******************************************************************************\
|Routine: calculate
|Callby: command
|Purpose: Implements a basic calculator.
|Arguments:
| buf is a buffer containing an algebraic expression.
| result is the returned buffer containing the answer, or an error message.
\******************************************************************************/
Int calculate(pbuf,result)
Char *pbuf;
Char *result;
{
static Char *buf = NULL;
static Int bufl = 0;
static Int maxtokens = 0;
static Int *token = NULL;
static double *value,*real_value;
static Char *intrinsic[] =
{
"sinh","cosh","tanh",
"sind","cosd","tand",
"cotd","secd","cscd",
"sin","cos","tan",
"cot","sec","csc",
"arcsind","arccosd","arctand",
"arcsin","arccos","arctan",
"sqrt","fact","exp","ln","log","int","abs",
};
Char *p,*q,c;
double val;
Int i,j,ival;
Int ntokens,nintr,depth,lowest,first,second,operator;
/* ignore empty expressions */
if(!pbuf)
goto empty;
/* expand the local buffer if necessary */
if((i = strlen(pbuf) + 2) > bufl)
{
if(buf)
ifree(buf);
buf = (Char *)imalloc((bufl = i) * sizeof(Char));
}
/* copy to local buffer, making it lowercase */
for(p = pbuf,q = buf;(c = *p) && c != '=';p++)
*q++ = tolower(c);
*q++ = ' ';
*q = '\0';
/* get the number of tokens */
nintr = sizeof(intrinsic)/sizeof(intrinsic[0]); /* number of intrinsic functions */
for(ntokens = 0,p = buf;(c = *p);p++)
switch(c)
{
case '(': case ')': case '+': case '-': case '*': case '/': case '^':
ntokens++;
break;
case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case '.':
if(my_sscanf(p,"%Lf%n",&val,&i) != 1)
{
strcpy(result,"Bad number.");
return(0);
}
p += i - 1;
ntokens++;
break;
case ' ': case '\t':
break;
default:
for(i = 0;i < nintr;i++)
{
if(!strncmp(p,intrinsic[i],strlen(intrinsic[i])))
{
ntokens++;
break;
}
}
if(i == nintr)
{
strcpy(result,"Bad function.");
return(0);
}
p += strlen(intrinsic[i]) - 1;
ntokens++;
}
/* this is an entry point for a spot below where closing parens need to be added to the token array */
expand_tokens:
if((ntokens += 2) > maxtokens) /* add two for enclosing parens */
{
if(token)
{
ifree(token);
ifree(real_value);
}
maxtokens = ntokens;
token = (Int *)imalloc(maxtokens * sizeof(int));
/* value is a double-aligned version of real_value. real_value is kept around for later freeing */
real_value = (double *)imalloc((maxtokens + 1) * sizeof(double));
value = (double *)((((size_t)real_value) + 7) & ~7);
}
token[0] = '('; /* enclose entire expression in parens */
ntokens = 1; /* first paren is first token */
/*
convert string to tokens.
token[i] = 0 if token is a number. In this case, value[i] is the numeric value.
token[i] = <character> if token is an operator <+ - * / ( )>.
token[i] = -n if token is intrinsic function #n (starting with #1).
*/
for(p = buf;(c = *p);p++)
switch(c)
{
case '(': case ')': case '+': case '-': case '*': case '/': case '^':
token[ntokens++] = c;
break;
case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case '.':
token[ntokens] = 0;
my_sscanf(p,"%Lf%n",value + ntokens++,&i);
p += i - 1;
break;
case ' ': case '\t':
break;
default:
for(i = 0;i < nintr;i++)
{
if(!strncmp(p,intrinsic[i],strlen(intrinsic[i])))
{
token[ntokens++] = -1 - i;
break;
}
}
p += strlen(intrinsic[i]) - 1;
}
/* finish with enclosing parenthesis */
token[ntokens++] = ')';
/* now process the tokens, always going to deepest paren depth */
while(ntokens > 1)
{
for(lowest = depth = i = 0;i < ntokens;i++)
{
if(token[i] == '(')
{
if(++depth > lowest)
{
lowest = depth;
first = i;
}
}
else if(token[i] == ')')
{
if(--depth < 0)
{
strcpy(result,"Bad parentheses.");
return(0);
}
}
}
/* make parentheses balance if they don't */
if(depth)
{
if(ntokens + depth > maxtokens)
{
ntokens += depth;
goto expand_tokens;
}
while(depth-- > 0)
token[ntokens++] = ')';
continue;
}
/* find closure of lowest expression */
for(second = first + 1;token[second] != ')';second++);
/* evaluate the expression */
if((i = first + 1) == second)
value[first] = 0;
else
{
if(token[i] == '-')
{
val = -value[++i];
i++;
}
else
val = value[i++];
while(i < second)
{
switch(token[i++])
{
case 0:
strcpy(result,"Two numbers with no operator between.");
return(0);
case '+':
operator = 0;
break;
case '-':
operator = 1;
break;
case '*':
operator = 2;
break;
case '/':
operator = 3;
break;
case '^':
operator = 4;
break;
default:
operator = token[i-1];
break;
}
if(i == second)
{
strcpy(result,"Operator not followed by number.");
return(0);
}
switch(token[i])
{
case 0:
switch(operator)
{
case 0:
val += value[i];
break;
case 1:
val -= value[i];
break;
case 2:
val *= value[i];
break;
case 3:
if(!value[i])
{
strcpy(result,"Divide by zero.");
return(0);
}
val /= value[i];
break;
case 4:
if((val == 0 && value[i] <= 0) || (val < 0 && floor(value[i]) != value[i]))
{
strcpy(result,"Illegal exponentiation.");
return(0);
}
val = pow(val,value[i]);
break;
}
break;
default:
strcpy(result,"Two operators with no number between.");
return(0);
}
i++;
}
/* check for preceeding intrinsic function(s) */
i = first;
while(i > 0)
{
if(token[i - 1] >= 0)
break;
switch(-token[--i])
{
case 1:
if(fabs(val) > log(HUGE_VAL))
{
strcpy(result,"Bad hyperbolic sine.");
return(0);
}
val = sinh(val);
break;
case 2:
if(fabs(val) > log(HUGE_VAL))
{
strcpy(result,"Bad hyperbolic cosine.");
return(0);
}
val = cosh(val);
break;
case 3:
val = tanh(val);
break;
case 4:
val = sin(CONV * val);
break;
case 5:
val = cos(CONV * val);
break;
case 6:
if(val - (double)(90 * (int)(val/90.0)) == 0 && (int)(val/90.0) & 1)
{
strcpy(result,"Bad tangent.");
return(0);
}
val = tan(CONV * val);
break;
case 7:
if(val - (double)(90 * (int)(val/90.0)) == 0 && !((int)(val/90.0) & 1))
{
strcpy(result,"Bad cotangent.");
return(0);
}
val = 1.0/tan(CONV * val);
break;
case 8:
if(val - (double)(90 * (int)(val/90.0)) == 0 && (int)(val/90.0) & 1)
{
strcpy(result,"Bad secant.");
return(0);
}
val = 1.0/cos(CONV * val);
break;
case 9:
if(val - (double)(90 * (int)(val/90.0)) == 0 && !((int)(val/90.0) & 1))
{
strcpy(result,"Bad cosecant.");
return(0);
}
val = 1.0/sin(CONV * val);
break;
case 10:
val = sin(val);
break;
case 11:
val = cos(val);
break;
case 12:
if(val - (double)(90 * (int)(val/90.0)) == 0 && (int)(val/90.0) & 1)
{
strcpy(result,"Bad tangent.");
return(0);
}
val = tan(val);
break;
case 13:
if(val - (double)(90 * (int)(val/90.0)) == 0 && !((int)(val/90.0) & 1))
{
strcpy(result,"Bad cotangent.");
return(0);
}
val = 1.0/tan(val);
break;
case 14:
if(val - (double)(90 * (int)(val/90.0)) == 0 && (int)(val/90.0) & 1)
{
strcpy(result,"Bad secant.");
return(0);
}
val = 1.0/cos(val);
break;
case 15:
if(val - (double)(90 * (int)(val/90.0)) == 0 && !((int)(val/90.0) & 1))
{
strcpy(result,"Bad cosecant.");
return(0);
}
val = 1.0/sin(val);
break;
case 16:
if(val < -1.0 || val > 1.0)
{
strcpy(result,"Bad arcsin.");
return(0);
}
val = asin(val) / CONV;
break;
case 17:
if(val < -1.0 || val > 1.0)
{
strcpy(result,"Bad arccos.");
return(0);
}
val = acos(val) / CONV;
break;
case 18:
val = atan(val) / CONV;
break;
case 19:
if(val < -1.0 || val > 1.0)
{
strcpy(result,"Bad arcsin.");
return(0);
}
val = asin(val);
break;
case 20:
if(val < -1.0 || val > 1.0)
{
strcpy(result,"Bad arccos.");
return(0);
}
val = acos(val);
break;
case 21:
val = atan(val);
break;
case 22:
if(val < 0.0)
{
strcpy(result,"Square root of negative value.");
return(0);
}
val = sqrt(val);
break;
case 23:
if(val <= -1.0)
{
strcpy(result,"Factorial of negative value.");
return(0);
}
else if(val >= 34.0)
{
strcpy(result,"Factorial of value that is too large.");
return(0);
}
for(ival = (int)val,val = 1.0;ival > 0;ival--)
val *= (double)ival;
break;
case 24:
if(fabs(val) > log(HUGE_VAL))
{
strcpy(result,"Bad exponentiation.");
return(0);
}
val = exp(val);
break;
case 25:
if(val <= 0.0)
{
strcpy(result,"Log of non-positive value.");
return(0);
}
val = log(val);
break;
case 26:
if(val <= 0.0)
{
strcpy(result,"Log(base 10) of non-positive value.");
return(0);
}
val = log10(val);
break;
case 27:
val = (double)((int)val);
break;
case 28:
val = (val < 0.0)? -val : val;
break;
} /* end of intrinsic switch */
} /* end of while */
first = i;
} /* end of if(second = first + 1) */
/* replace token[first] with 0, value[first] with val, squeeze out everything up to second inclusive */
token[first] = 0;
value[first] = val;
for(i = second + 1,j = first + 1;i < ntokens;i++,j++)
{
token[i - second + first] = token[i];
value[i - second + first] = value[i];
}
ntokens = j;
/* go for another pass */
} /* end of while(ntokens > 1) */
/* report results */
sprintf(result,"%f",value[0]);
return(1);
empty:
strcpy(result,"Nothing to calculate.");
return(0);
}