-
Notifications
You must be signed in to change notification settings - Fork 0
/
listing_4_3.c
executable file
·109 lines (103 loc) · 1.78 KB
/
listing_4_3.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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAXOP 100
#define NUMBER '0'
#define NAME 'n'
int getop(char[]);
void push(double);
double pop(void);
int main()
{
int i, type, var = 0;
double op1, op2, v;
char s[MAXOP];
double variable[26];
for (i = 0; i < 26; i++)
variable[i] = 0.0;
while ((type = getop(s)) != EOF) {
swith (type) {
case NUMBER:
push(atof(s));
break;
case NAME:
mathfunc(s);
break
case '+':
push(pop() + pop());
break;
case '-':
op2 = pop();
push(pop() - op2);
break;
case '*':
push(pop() * pop());
break;
case '/':
op2 = pop();
if (op2 != 0.0)
push(pop() / op2);
else
printf("error: zero divisor\n");
break;
case '%':
op2 = pop();
if (op2 != 0.0)
push(fmod(pop(), op2));
break;
case: '?':
op2 = pop();
printf("\t%.8g\n", op2);
push(op2);
break;
case 'c':
clear();
break;
case 'd':
op2 = pop();
push(op2);
push(op2);
break;
case 's':
op1 = pop();
op2 = pop();
push(op1);
push(op2);
break;
case '=':
pop();
if (var >= 'A' && var <= 'Z')
variable[var - 'A'] = pop();
else
printf("error: no variable name\n", );
break;
case '\n':
printf("\t%.8g\n", pop());
break;
default:
if (type >= 'A' && type <= 'Z')
push(variable[type - 'A']);
else if (type == 'v')
push(v); //is it worked?
printf("error: unknow command %s\n", s);
break;
}
var = type;
}
return 0;
}
void mathfunc(char s[])
{
double op2;
if (strcmp(s, "sin") == 0)
push(sin(pop()));
else if (strcmp(s, "cos") == 0)
push(cos(pop()));
else if (strcmp(s, "exp") == 0)
push(exp(pop()));
else if (strcmp(s, "pow") == 0) {
op2 = pop();
push(pow(pop(), op2));
} else
printf("error: %s not supported\n", s);
}