Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h> /* for atof() */
- #include <ctype.h>
- #include <math.h>
- #define MAXOP 100 /* max size of operator or operand */
- #define MAXVAL 100 /* maximum depth of val stack */
- #define NUMBER '0' /* signal that a number was found */
- #define BUFSIZE 100
- #define COMMAND 'A' /* signal that a command was found */
- int getop(char []);
- void push(double f);
- double pop(void);
- int getch(void);
- void ungetch(int);
- int sp = 0;
- double val[MAXVAL];
- char buf[BUFSIZE]; /* buffer for ungetch */
- int bufp = 0; /* next free position in buf */
- /* push: push f onto value stack */
- void push(double f)
- {
- if (sp < MAXVAL)
- val[sp++] = f;
- else
- printf("error: stack full, can't push %g\n", f);
- }
- /* pop: pop and return top value from stack */
- double pop(void)
- {
- if (sp > 0)
- return val[--sp];
- else {
- printf("error: stack empty\n");
- return 0.0;
- }
- }
- /* getop: get next character or numeric operand */
- int getop(char s[])
- {
- int i, c;
- while ((s[0] = c = getch()) == ' ' || c == '\t')
- ;
- s[1] = '\0';
- if (isalpha(c)) {
- while (isalpha(s[++i] = c = getch()))
- ;
- s[i] = '\0';
- printf("Entered string: %s\n", s);
- return COMMAND;
- }
- if (!isdigit(c) && c != '.' && c != '-')
- return c; /* not a number */
- i = 0;
- if (c == '-') { /* check to see if we have a neg number or a minus operator */
- s[++i] = c = getch();
- if (!isdigit(c)) /* if the next char is a number it's a neg num, else not */
- return '-';
- }
- if (isdigit(c)) { /* collect integer part */
- while (isdigit(s[++i] = c = getch()))
- ;
- }
- if (c == '.') /* collect fraction part */
- while (isdigit(s[++i] = c = getch()))
- ;
- s[i] = '\0';
- if (c != EOF)
- ungetch(c);
- return NUMBER;
- }
- int getch(void) /* get a (possibly pushed-back) character */
- {
- return (bufp > 0) ? buf[--bufp] : getchar();
- }
- void ungetch(int c) /* push character back on input */
- {
- if (bufp >= BUFSIZE)
- printf("ungetch: too many characters\n");
- else
- buf[bufp++] = c;
- }
- /* reverse Polish calculator */
- main()
- {
- int type, temp, i, sp_mark;
- double op2;
- char s[MAXOP];
- while ((type = getop(s)) != EOF) {
- switch (type) {
- case NUMBER:
- push(atof(s));
- break;
- case COMMAND:
- printf("s is: %s\n", s);
- if (s == "sin") {
- printf("sin command received\n");
- push(sin(pop()));
- printf("\t%.8g\n", val[sp-1]);
- }
- break;
- case '+':
- push(pop() + pop());
- printf("\t%.8g\n", val[sp-1]);
- break;
- case '*':
- push(pop() * pop());
- printf("\t%.8g\n", val[sp-1]);
- break;
- case '-':
- op2 = pop();
- push(pop() - op2);
- printf("\t%.8g\n", val[sp-1]);
- break;
- case '/':
- op2 = pop();
- if (op2 != 0.0) {
- push(pop() / op2);
- printf("\t%.8g\n", val[sp-1]);
- }
- else
- printf("error: zero divisor\n");
- break;
- case '%':
- op2 = pop();
- if (op2 != 0.0) {
- push(fmod(pop(), op2));
- printf("\t%.8g\n", val[sp-1]);
- }
- else
- printf("error: zero divisor\n");
- break;
- /* ignore the rest of the cases, they're left over from previous exercise */
- case 'c': /* clears the stack */
- sp = 0;
- printf("All values cleared\n");
- break;
- case 's': /* shows the top values on the stack */
- if (sp > 1)
- printf("The top elements on the stack are %.8g and %.8g\n",
- val[sp-1], val[sp-2]);
- else if (sp == 1)
- printf("The only element on the stack is %.8g\n", val[sp-1]);
- else
- printf("The stack is empty\n");
- break;
- case 'S': /* swaps the top two elements */
- if (sp > 1) {
- temp = val[sp-1];
- val[sp-1] = val[sp-2];
- val[sp-2] = temp;
- }
- else
- printf("Error: Not enough elements on stack\n");
- break;
- case 'd': /* duplicates the stack */
- if (sp > 0 && sp * 2 < MAXVAL - 1) {
- for (i = 0, sp_mark = sp; i < sp_mark; i++)
- push(val[i]);
- }
- else if (sp == 0)
- printf("Error: Stack is empty");
- else
- printf("Error: Insufficient space on stack");
- break;
- case '\n':
- /* printf("\t%.8g\n", pop()); */
- printf("\n");
- break;
- default:
- printf("error: unknown command %s\n", s);
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement