-
Notifications
You must be signed in to change notification settings - Fork 0
/
calc.h
70 lines (62 loc) · 1.67 KB
/
calc.h
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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
double evaluate(const char* expression)
{
char* endptr;
double result = strtod(expression, &endptr);
if (errno == ERANGE) return NAN;
while (*endptr != '\0')
{
while (isspace(*endptr)) endptr++;
char op = *endptr++;
switch (op)
{
case 'r': result = sqrt(result); break;
case 'l': result = log(result); break;
case 'S': result = asin(result); break;
case 'C': result = acos(result); break;
case 'T': result = atan(result); break;
case 'c':
if (*endptr == 'h') { endptr++; result = cosh(result); }
else result = cos(result);
break;
case 't':
if (*endptr == 'h') { endptr++; result = tanh(result); }
else result = tan(result);
break;
case 's':
if (*endptr == 'h') { endptr++; result = sinh(result); }
else result = sin(result);
break;
case '+': case '-': case '*': case '/': case '^': case '%': case 'b': case 'n':
{
double operand = strtod(endptr, &endptr);
if (errno == ERANGE) return NAN;
switch (op)
{
case '+': result += operand; break;
case '-': result -= operand; break;
case '*': result *= operand; break;
case '/':
if (operand != 0) result /= operand;
else return NAN;
break;
case '^': result = pow(result, operand); break;
case '%': result = operand / 100.0 * result; break;
case 'b':
if (operand != 0) result = log(result) / log(operand);
else return NAN;
break;
case 'n': result = pow(result, 1.0 / operand); break;
default: return NAN;
}
}
break;
default:
return NAN;
}
}
return result;
}