Added example with variable arrays.
[C-Programming-Examples.git] / calc_adv.c
blob7993e4ee16b3e47c65030ecba4338ad0a5398597
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <ctype.h>
4 #include <math.h>
6 #define MAXOP 100
7 #define NUMBER '0'
8 #define MAXVAL 100
9 #define BUFSIZE 100
11 void dup_stack(void);
12 int getop(char[]);
13 void push(double);
14 double pop(void);
15 void print_stack(void);
16 void reverse(void);
17 int getch(void);
18 void ungetch(int);
20 char buf[BUFSIZE];
21 int bufp = 0;
22 int sp = 0;
23 double val[MAXVAL];
24 static int ungetchar = '\0';
26 int main()
28 int type;
29 double op2;
30 char s[MAXOP];
32 while((type = getop(s)) != EOF)
34 switch(type)
36 case NUMBER:
37 push(atof(s));
38 break;
39 case '#':
40 dup_stack();
41 break;
42 case '?':
43 print_stack();
44 break;
45 case '~':
46 reverse();
47 break;
48 case '+':
49 push(pop() + pop());
50 break;
51 case '*':
52 push(pop() * pop());
53 break;
54 case '-':
55 op2 = pop();
56 push(pop() - op2);
57 break;
58 case '/':
59 op2 = pop();
60 if(op2 != 0.0)
61 push(pop() / op2);
62 else
63 printf("error: zero divisor\n");
64 break;
65 case '%':
66 op2 = pop();
67 if(op2 != 0.0)
69 push((int)(pop()) % (int)(op2));
70 } else {
71 printf("error: zero divisor\n");
73 break;
74 case 'c':
75 push(cos(pop()));
76 break;
77 case 'p':
78 push((int)pow(pop(),pop()));
79 break;
80 case 't':
81 push(tan(pop()));
82 break;
83 case 'q': // square root
84 push(sqrt(pop()));
85 break;
86 case 'r':
87 push(rand());
88 break;
89 case 's':
90 push(sin(pop()));
91 break;
92 case '\n':
93 printf("\t%.8g\n", pop());
94 break;
95 default:
96 printf("error: unknown command %s\n", s);
97 break;
100 return 0;
103 /* push f onto value stack */
104 void push(double f)
106 if(sp < MAXVAL)
107 val[sp++] = f;
108 else
109 printf("error: stack full\n");
112 /* pop and return top value from stack */
113 double pop(void)
115 if(sp > 0)
116 return val[--sp];
117 else
119 printf("stack empty\n");
120 return 0.0;
124 /* duplicate value at top of stack */
125 void dup_stack()
127 int temp = pop();
128 push(temp);
129 push(temp);
132 /* reverse two vales at top of stack */
133 void reverse()
135 double temp1, temp2;
136 temp1 = pop();
137 temp2 = pop();
138 push(temp1);
139 push(temp2);
142 /* print top two values on stack */
143 void print_stack()
145 if(sp > 0)
146 printf("%f\t%f\n", val[sp], val[sp-1]);
147 // else
148 // printf("Stack is empty!\n");
151 /* get next operator or numberic operand */
152 int getop(char s[])
154 int i, c;
156 while ((s[0] = c = getch()) == ' ' || c == '\t')
158 s[1] = '\0';
159 if(!isdigit(c) && c != '.')
160 return c;
161 i = 0;
162 if(isdigit(c))
163 while(isdigit(s[++i] = c = getch()))
165 if(c == '.')
166 while(isdigit(s[++i] = c = getch()))
168 s[i] = '\0';
169 if(c != EOF)
170 c = ungetchar;
171 return NUMBER;
174 int getch(void)
176 return (bufp > 0) ? buf[--bufp] : getchar();
179 /* NO LONGER NEEDED
180 void ungetch(int c)
182 if(bufp >= BUFSIZE)
183 printf("ungetch: too many characters\n");
184 else
185 buf[bufp++] = c;