#include <stdio.h>#include <math.h>#include <ctype.h>#define NUMBER '0'#define MAXLEN 1000#define BUFSIZE 100int getch(void);void ungetch(int);int getline(char *);void push(double );double pop(void );void ungets(char *);double atof(char s[]);char buf[BUFSIZE];int bufp = 0;#define MAXVAL 100int sp = 0;double val[MAXVAL];main(int argc,char *argv[]){char s[MAXLEN];double op2;while(--argc > 0){ungets(" ");ungets(*++argv);switch(getline(s)){case NUMBER : push(atof(s));break;case '+' :push(pop() + pop());break;case '-' :op2 = pop();push(pop() - op2);break;case '*' :push(pop() * pop());break;case '/' :op2 = pop();if(op2 != 0.0)push(pop() / op2);else printf("error : zero divisor \n");break;default :printf("erro : unkown command %s\n",s);argc = 1;break;}}printf("\t%.8g\n",pop());return 0;}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()));s[i] = '\0';if(c != EOF)ungetch(c);return NUMBER;}int getch(void){return (bufp > 0) ? buf[--bufp] : getchar();}void ungeth(int c){if(bufp >= BUFSIZE)printf("ungetch : too many characters\n");elsebuf[bufp++] = c;}void push(double f){if(sp < MAXVAL)val[sp++] = f;elseprintf("error: stack full , can't push %g\n");}double pop(){if(sp > 0)return val[--sp];else{printf("error: stack empty\n");return 0.0;}}double atof(char s[]){double val,power;int i,sign;for(i = 0; isspace(s[i]);i++);sign = (s[i] == '-') ? -1 : 1;if(s[i] == '+' ||s[i] == '-')i++;for(val = 0.0;isdigit(s[i]);i++)val = 10.0 * val + s[i] - '0';if(s[i] == '.'){i++;for(power = 1.0 ;isdigit(s[i]);i++){val = 10.0 * val + s[i] - '0';power *= 10.0 ;}}return sign * val / power;}
如下代碼,為什么會(huì)出現(xiàn)這個(gè)錯(cuò)誤呢?求解釋!
寶慕林4294392
2023-03-17 22:18:46