Added an example with a struct.
[C-Programming-Examples.git] / ex_5-10.c
bloba69ebc7e7fe669c8602678b8b86dc3c662d592ff
1 #include <ctype.h>
2 #include <stdio.h>
3 #include <stdlib.h>
5 #define STACKSIZE 100
7 double stack[STACKSIZE];
8 int stkh = 0;
10 void error(const char *err)
12 fprintf(stderr, "ERROR: %s\n", err);
13 exit(EXIT_FAILURE);
16 void push(double val)
18 if(stkh == STACKSIZE)
19 error("Stack Overflow.");
20 stack[stkh] = val;
21 ++stkh;
24 double pop()
26 if(stkh == 0)
27 error("Stack Empty.");
28 return stack[--stkh];
31 int main(int argc, char *argv[])
33 int i = 0;
34 for(i = 1; i < argc; ++i)
36 switch(argv[i][0])
38 case '\0':
39 error("No Arguments Sent.");
40 break;
41 case '1':
42 case '2':
43 case '3':
44 case '4':
45 case '5':
46 case '6':
47 case '7':
48 case '8':
49 case '9':
50 case '0':
51 push(atof(argv[i]));
52 break;
53 case '+':
54 push(pop()+pop());
55 break;
56 case 'x':
57 push(pop()*pop());
58 break;
59 default:
60 error("Unknown Operand.");
61 break;
64 printf("%f\n", pop());
65 return 1;