Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #
- #include <stdio.h>
- #include <ctype.h>
- #include <string.h>
- #include <math.h>
- #define MAXOP 100
- #define NUMBER '\0'
- #define BUFSIZE 100
- #define MAXVAL 100
- int sp = 0;
- double val[MAXVAL];
- char buf[BUFSIZE];
- int bufp = 0;
- void push(double f) {
- if (sp < MAXVAL) {
- val[sp++] = f;
- }
- else {
- printf("error: stack full can't push");
- }
- }
- double pop(void) {
- if (sp > 0) {
- return val[--sp];
- }
- else {
- printf("error: stack empty can't pop");
- return 0.0;
- }
- }
- int getch(void) {
- return (bufp > 0) ? buf[--bufp] : getchar();
- }
- void ungetch(int c) {
- if (bufp >= BUFSIZE) {
- printf("ungetch: too many characters\n");
- }
- else {
- buf[bufp++] = c;
- }
- }
- int getop(char s[]) {
- int i, c;
- while ((s[0] = c = getch()) == ' ' || c == '\t') {
- }
- s[1] = '\0';
- if (!isdigit(c) && c != '.') {
- return c;
- }
- i = 0;
- if (!isdigit(c)) {
- while (isdigit(s[++i] = c = getch())) {
- }
- }
- if (c == '.') {
- while (isdigit(s[++i] = c = getch())) {
- }
- }
- }
- int main(void) {
- int type;
- double op2;
- int sign = 1;
- char s[MAXOP];
- while ((type = getop(s)) != EOF) {
- if (type == '-') {
- sign = -1;
- continue;
- }
- switch (type) {
- case NUMBER:
- push(atof(s) * sign);
- break;
- case '+':
- push(pop() + pop());
- break;
- case '-':
- op2 = pop();
- push(pop() - op2);
- break;
- case '/':
- op2 = pop();
- if (op2 != 0.0) {
- push(pop() / op2);
- break;
- }
- else {
- printf("Error: zero divisor\n");
- break;
- }
- case '\n':
- printf("\t%.8g\n", pop());
- break;
- case '%':
- op2 = pop();
- push(fmod(pop(), op2));
- break;
- case 'P':
- printf("%c", buf[bufp - 1]);
- break;
- case 'D':
- push(buf[bufp - 1]);
- break;
- case 'S':
- int c = buf[bufp - 1];
- buf[bufp - 1] = buf[bufp - 2];
- buf[bufp - 2] = c;
- break;
- case 'V':
- for (int i = 0;i < BUFSIZE - 1;i++) {
- buf[i] = 0;
- }
- break;
- case 'B':
- push(sin(pop()));
- break;
- case 'N':
- op2 = pop();
- push(pow(pop(), op2));
- break;
- case 'L':
- break;
- default:
- printf("error: unknown command %s\n", s);
- break;
- }
- sign = 1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement