描述程序中的逆波兰式是怎么生成的
- 提问者网友:喜遇你
- 2021-05-24 09:42
- 二级知识专家网友:强势废物
- 2021-05-24 09:50
#include <stdio.h>
#include <stdlib.h>
#define MAXOP 100
#define NUMBER '0'
int getOp(char []);
void push(double);
double pop(void);
main() { int type; double op2; char s[MAXOP]; while ((type = getOp(s)) != 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 puts("error: zero divisor\n"); break; case '\n': printf("\t%.8g\n", pop()); break; default: printf("error: unknown command %s\n", s); break; } } return 0;
}
#define MAXVAL 100
int sp = 0;
double val[MAXVAL];
void push(double f) { if (sp < MAXVAL) val[sp++] = f; else printf("error: stack full, can't push %g\n", f);
}
double pop(void) { if (sp > 0) return val[--sp]; else { puts("error: stack empty\n"); return 0.0; }
}
#include <ctype.h>
int getChar_localVersion(void);
void ungetChar_localVersion(int c);
int getOp(char s[]) { int i, c; while ((s[0] = c = getChar_localVersion()) == ' ' || c == '\t') ; s[1] = '\0'; if (!isdigit(c) && c != '.' && c != '-') return c; i = 0; if (c == '-') if(isdigit(s[1] = c = getChar_localVersion())) ++i; else { ungetChar_localVersion(c); s[1] = '\0'; return s[0]; } if (isdigit(c)) while (isdigit(s[++i] = c = getChar_localVersion())) ; if (c == '.') while (isdigit(s[++i] = c = getChar_localVersion())) ; s[i] = '\0'; if (c != EOF) ungetChar_localVersion(c); return NUMBER;
}
#define BUFSIZE 100
char buf[BUFSIZE];
int bufp = 0;
int getChar_localVersion(void) { return (bufp > 0) ? buf[--bufp] : getchar();
}
void ungetChar_localVersion(int c) { if (bufp >= BUFSIZE) puts("ungetChar_localVersion: too many characters\n"); else buf[bufp++] = c;
}
参考资料:The C Programming Language, 2nd Edition, 1988 ( Brian Kernighan & Dennis Ritchie )
- 1楼网友:安稳不如野
- 2021-05-24 10:38
这个用C语言是很方便的!可以直接运行的!
#include <stdio.h> #include <stdlib.h> #define MAXOP 100 #define NUMBER '0' int getOp(char []); void push(double); double pop(void); main() { int type; double op2; char s[MAXOP]; while ((type = getOp(s)) != 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 puts("error: zero divisor\n"); break; case '\n': printf("\t%.8g\n", pop()); break; default: printf("error: unknown command %s\n", s); break; } } return 0; } #define MAXVAL 100 int sp = 0; double val[MAXVAL]; void push(double f) { if (sp < MAXVAL) val[sp++] = f; else printf("error: stack full, can't push %g\n", f); } double pop(void) { if (sp > 0) return val[--sp]; else { puts("error: stack empty\n"); return 0.0; } } #include <ctype.h> int getChar_localVersion(void); void ungetChar_localVersion(int c); int getOp(char s[]) { int i, c; while ((s[0] = c = getChar_localVersion()) == ' ' || c == '\t') ; s[1] = '\0'; if (!isdigit(c) && c != '.' && c != '-') return c; i = 0; if (c == '-') if(isdigit(s[1] = c = getChar_localVersion())) ++i; else { ungetChar_localVersion(c); s[1] = '\0'; return s[0]; } if (isdigit(c)) while (isdigit(s[++i] = c = getChar_localVersion())) ; if (c == '.') while (isdigit(s[++i] = c = getChar_localVersion())) ; s[i] = '\0'; if (c != EOF) ungetChar_localVersion(c); return NUMBER; } #define BUFSIZE 100 char buf[BUFSIZE]; int bufp = 0; int getChar_localVersion(void) { return (bufp > 0) ? buf[--bufp] : getchar(); } void ungetChar_localVersion(int c) { if (bufp >= BUFSIZE) puts("ungetChar_localVersion: too many characters\n"); else buf[bufp++] = c;