-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
129 lines (125 loc) · 4.21 KB
/
main.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
#include <stdio.h>
#include <stdlib.h> /* for atof() */
#include <math.h>
#include "calc.h"
#define MAXOP 100 /* max size of operand or operator */
/* reverse Polish calculator */
int main() {
int type;
double op2, op1; // Store the first operand
char s[MAXOP];
extern int sp;
printf("Simple RPN Calculator\n \
Commands:\n \
\t p \t power ( 2 8 p => 16)\n \
\t s \t sin \n \
\t c \t cos \n \
\t t \t tan \n \
\t S \t arc sin (in radians)\n \
\t C \t arc cos (in radians)\n \
\t T \t arc tan (in radians)\n \
\t l \t log10(x) \n \
\t + \t add \n \
\t - \t subtract \n \
\t * \t multiply \n \
\t / \t divide \n \
\t %% \t modulo \n \
\t q \t quit \n \
\t Enter \t calculate \n \
");
printf("Enter an RPN expression: \n");
while ((type = getop(s)) != EOF) {
switch (type) {
case NUMBER:
push(atof(s));
break;
case '+':
op2 = pop();
op1 = pop(); // Get the first operand
while (sp > 0) { // Keep popping and adding until the stack is empty
op1 += pop();
}
push(op1 + op2); // Add the last operand
break;
case '*':
op2 = pop();
op1 = pop(); // Get the first operand
while (sp > 0) { // Keep popping and multiplying until the stack is empty
op1 *= pop();
}
push(op1 * op2); // Multiply the last operand
break;
case '-':
op2 = pop();
op1 = pop(); // Get the first operand
while (sp > 0) { // Keep popping and subtracting until the stack is empty
op1 -= pop();
}
push(op1 - op2); // Subtract the last operand
break;
case '%':
op2 = pop();
op1 = pop(); // Get the first operand
while (sp > 0) { // Keep popping and calculating modulo until the stack is empty
op1 = (int)op1 % (int)pop();
}
if (op2 != 0.0)
push((int)op1 % (int)op2);
else
printf("error: zero divisor\n");
break;
case '/':
op2 = pop();
op1 = pop(); // Get the first operand
while (sp > 0) { // Keep popping and dividing until the stack is empty
op1 /= pop();
}
if (op2 != 0.0)
push(op1 / op2);
else
printf("error: zero divisor\n");
break;
case 's': // sin
push(sin(pop()));
break;
case 'c': // cos
push(cos(pop()));
break;
case 't': // tan
push(tan(pop()));
break;
case 'S': // arc sin
push(asin(pop()));
break;
case 'C': // arc cos
push(acos(pop()));
break;
case 'T': // arc tan
push(atan(pop()));
break;
case 'p': // pow
op2 = pop();
push(pow(pop(), op2));
break;
case 'l': // log10(x)
op1 = pop(); // Get the operand
if (op1 > 0.0) { // Check if it's positive
push(log10(op1));
} else {
printf("error: log10() requires a positive argument\n");
}
break;
case '\n':
printf("\t%.8g\n", pop());
break;
case 'q': // Quit
printf("Exiting...\n");
exit(0); // Use exit(0) to terminate the program
break;
default:
printf("error: unknown command %s\n", s);
break;
}
}
return 0;
}