Chinaunix首页 | 论坛 | 博客
  • 博客访问: 522344
  • 博文数量: 252
  • 博客积分: 6057
  • 博客等级: 准将
  • 技术积分: 1635
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-21 10:17
文章分类

全部博文(252)

文章存档

2013年(1)

2012年(1)

2011年(32)

2010年(212)

2009年(6)

分类: C/C++

2010-04-18 16:52:18

/* reverse Polish calculator */
#include
#include /* 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);
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
");
break;
case '':printf("\t%.8g", pop());
break;
default: printf("error: unknown command %s", s);
break;
}
}
return 0;
}
/*************************************************************************
Definition of push() and pop().
*************************************************************************/
#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
", f);
}
/* pop: pop and return top value from stack */
double pop(void) {
if (sp > 0)
return val[--sp];
else {
puts("error: stack empty");
return 0.0;
}
}
/*************************************************************************
Definition of getOp().
*************************************************************************/
#include
int getChar_localVersion(void);
void ungetChar_localVersion(int c);
/* getOp: get next character or numeric operand */
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;/* not a number */
i = 0;
if (c == '-')/* is valid sign if followed by a digit */
if(isdigit(s[1] = c = getChar_localVersion()))
   ++i;
else {
ungetChar_localVersion(c);
s[1] = '\0';
return s[0];
}
if (isdigit(c))/* collect integer part */
while (isdigit(s[++i] = c = getChar_localVersion()));
if (c == '.') /* collect fraction part */
while (isdigit(s[++i] = c = getChar_localVersion()));
  s[i] = '\0';
  if (c != EOF)
  ungetChar_localVersion(c);
  return NUMBER;
}
/*************************************************************************
Definition of getChar_localVersion() and ungetChar_localVersion().
*************************************************************************/
#define BUFSIZE 100
char buf[BUFSIZE];  /* buffer for ungetChar_localVersion */
int bufp = 0;       /* next free position in buf */
/* getChar_localVersion: get a (possibly pushed-back) character */
int getChar_localVersion(void) {
return (bufp > 0) ? buf[--bufp] : getchar();
}
/* ungetChar_localVersion: push character back on input */
void ungetChar_localVersion(int c) {
if (bufp >= BUFSIZE)
puts("ungetChar_localVersion: too many characters
");
else
buf[bufp++] = c;
}
阅读(822) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~