标题:求助C programming language (second edition)中逆波兰计算器程序中的问题
只看楼主
googlellc
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2010-11-5
结帖率:0
已结贴  问题点数:5 回复次数:4 
求助C programming language (second edition)中逆波兰计算器程序中的问题
#include <stdio.h>
#include <stdlib.h> /* for atof() */
#define MAXOP 100 /* max size of operand or operator */
#define NUMBER '0' /* signal that a number was found */
int getop(char []);
void push(double);
double pop(void);
/* reverse Polish calculator */
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
              printf("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 /* maximum depth of val stack */
int sp = 0;         /* next free stack position */
double val[MAXVAL]; /* value stack */
/* 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;
   }
}
#include <ctype.h>
int getch(void);
void ungetch(int);
/* getop: get next character or numeric operand */
int getop(char s[])
{
   int i, c;
   while ((s[0] = c = getchar()) == ' ' || c == '\t')
       ;
   s[1] = '\0';
   if (!isdigit(c) && c != '.')
       return c;    /* not a number */
   i = 0;
  if (isdigit(c))   /* collect integer part */
     while (isdigit(s[++i] = c = getchar()))
        ;
  if (c == '.')     /* collect fraction part */
     while (isdigit(s[++i] = c = getchar()))
        ;
  s[i] = '\0';

  return NUMBER;
}
 当运行这段程序后,输入 6  7* 返回值为7如果输入为6 7 *结果又正确为42
正确的程序是
#include <stdio.h>
#include <stdlib.h> /* for atof() */
#define MAXOP 100 /* max size of operand or operator */
#define NUMBER '0' /* signal that a number was found */
int getop(char []);
void push(double);
double pop(void);
/* reverse Polish calculator */
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
              printf("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 /* maximum depth of val stack */
int sp = 0;         /* next free stack position */
double val[MAXVAL]; /* value stack */
/* 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;
   }
}
#include <ctype.h>
int getch(void);
void ungetch(int);
/* 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 (!isdigit(c) && c != '.')
       return c;    /* not a number */
   i = 0;
  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;
}
#define BUFSIZE 100
char buf[BUFSIZE];    /* buffer for ungetch */
int bufp = 0;        /* next free position in buf */
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;
}
输入6 7*得到结果为42
小弟刚刚接触C 不太明白里面getch() 以及ungetch() 两个函数的意义,其中在getop()函数中 if (c != EOF)才执行ungetch()函数
搜索更多相关主题的帖子: programming 波兰 计算器 edition language 
2010-11-05 16:34
googlellc
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2010-11-5
得分:0 
希望高手指导下,才注册金币不够,多多谅解
2010-11-05 16:37
五当家
Rank: 12Rank: 12Rank: 12
等 级:火箭侠
威 望:2
帖 子:1112
专家分:3674
注 册:2010-10-20
得分:5 
getch()表示输入一个字符.

经验积累中............
2010-11-05 17:11
googlellc
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2010-11-5
得分:0 
麻烦高手指点下,我指的可是getch() 不是getchar()
而且你可以把我贴的两段程序运行一下...麻烦五当家了呀
2010-11-05 17:17
googlellc
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2010-11-5
得分:0 
而且很奇怪的一个现象就是getch() 中的return (bufp > 0) ? buf[--bufp] : getchar();这个程序,我的理解是 bufp > 0 是假所以执行的是buf[--bufp]也就是返回的值,是个什么呢?
2010-11-05 17:21



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-325088-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.755379 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved