-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex4_4.c
executable file
·72 lines (67 loc) · 2.27 KB
/
ex4_4.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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "calc.h"
#define MAXOP 100
/* K&R Ex4_3: Extends reverse polish calculator to add modulus operator and
provisions for negative numbers */
int main()
{
int type, n;
double op2,op1,isint;
char s[MAXOP];
while((type = getop(s)) != EOF){
switch(type) {
case NUMBER:
push(atof(s));
break;
case '+':
push(pop() + pop());
break;
case '*':
push(pop() * pop());
break;
case '-':
op2 = pop();
push(pop() - op2) ;
break;
case '/':
op2 = pop();
if (op2 != 0.0)
push(pop() / op2);
else
printf("error: zero divisor \n");
break;
case '%':
op2 = pop();
op1 = pop();
isint = op2 - floor(op2) + op1 - floor(op1);
if (op2 != 0.0 && isint ==0)
push((int)op1 %(int)op2);
else
printf("illegal operands in % \n");
break;
case '?':
print_top();
break;
case '#':
duplicate();
break;
case '~':
swap_top();
break;
case '!':
clear_stack();
case '\n':
printf("\t%g\n",pop());
break;
default:
if( (n = getmath(type))!= -1)
lib_function(n);
else
printf("error: unknown commmand %s\n",s);
break;
}
}
return 0;
}