home *** CD-ROM | disk | FTP | other *** search
- /*.t|Clist demonstration using K&R pages 74-79*//*.function .author Kernighan & Ritchie*/
- /* A $2,000 desktop calculator that isn't even programmable! */
- /* This listing produced using the following command line:
- CL -G -H -C -PBI DESKTOP
- */
-
- /* .noincl Don't include the (long) standard headers */
- #include <stdio.h>
- #include <math.h>
- /* .incl Include the other files */
-
- #define MAXOP 20
- #define NUMBER '0'
- #define TOOBIG '9'
-
- #include <stack.c>
- #include <getop.c>
- #include <getch.c>
-
- main()
- {
- int type;
- char s[MAXOP];
- double op2, atof(), pop(), push();
-
- while ((type = getop(s,maxop)) != 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("Zero divisor popped\n");
- break;
- case '=':
- printf("\t%f\n",push(pop()));
- break;
- case 'c':
- clear();
- break;
- case TOOBIG:
- printf("%.20s ... is too long\n");
- break;
- default:
- printf("unknown command %c\n",type);
- break;
- }
- }
-
- /* end of program, desktop.c */
-