-
Notifications
You must be signed in to change notification settings - Fork 0
/
polish.c
129 lines (114 loc) · 3.08 KB
/
polish.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
/* polish.c */
# include <stdlib.h>
# include <stdio.h>
# include <string.h>
# include "schtack.h"
///////////////////////////////////////////////
//main function
int main ( int argc, char * argv []) {
//do while loop to read characters entered at command line one at a time
//read one character using getchar
//for each character use switch case to determine what stack func
//for 0-9 push
//for operators pop twice saving values, perform operation, push result
//for q break
//for p print top
char c;
stack s;
int a,b;
printf("Welcome to the Reverse Polish Calculator!\n");
printf("Enter single numbers from 0-9 and operators +-*/ to calculate.\n");
printf("At any time press p to print top of stack, or q to quit.\n");
while(1){
printf("Enter number/operator/p/q: \n");
while((c = getchar()) != '\n' && c != 'q'){
//printf("yes");
switch(c){
case '0' :
case '1' :
case '2' :
case '3' :
case '4' :
case '5' :
case '6' :
case '7' :
case '8' :
case '9' :
//push
s = push(s,c - '0');
break;
case '+' :
//top,pop,top,pop,calc,push
b = top(s);
s = pop(s);
a = top(s);
s = pop(s);
printf("%d + %d = %d\n",a,b,a+b);
s = push(s,a+b);
top(s);
break;
case '-' :
//top,pop,top,pop,calc,push
//subtracts b from a, e.g. 52- means 5-2
b = top(s);
s = pop(s);
a = top(s);
s = pop(s);
printf("%d * %d = %d\n",a,b,a-b);
s = push(s,a-b);
top(s);
break;
case '*' :
//top,pop,top,pop,calc,push
b = top(s);
s = pop(s);
a = top(s);
s = pop(s);
printf("%d * %d = %d\n",a,b,a*b);
s = push(s,a*b);
top(s);
break;
case '/' :
//top,pop,top,pop,calc,push
//divides b by a, e.g. 52/ means 5/2
b = top(s);
s = pop(s);
a = top(s);
s = pop(s);
printf("%d / %d = %d\n",a,b,a/b);
s = push(s,a/b);
top(s);
break;
case 'p' :
top(s);
break;
case 'q' :
break;
default :
printf("Invalid command, please try again\n");
break;
}
}
if(c == 'q') break;
}
/*
stack s = new();
int i;
for(i=1;i<=5;i++){
s = push(s,i);
}
s = pop(s);
s = pop(s);
s = pop(s);
s = push(s,6);
s = push(s,7);
s = pop(s);
s = push(s,8);
s = push(s,9);
s = push(s,10);
s = push(s,11);
s = push(s,12);
s = pop(s);
*/
return 0;
}// end of main function