`
carolaif
  • 浏览: 70313 次
  • 性别: Icon_minigender_2
  • 来自: 大连
最近访客 更多访客>>
社区版块
存档分类
最新评论

C程序设计语言(第二版) 4-3 逆波兰计算器

阅读更多

在该程序中加入了取模(%)运算符,并对负数进行处理

 

#include <stdio.h>
#include <stdlib.h>  /* for  atof() */
#include <ctype.h>
   
#define MAXOP   100  /* max size of operand or operator */
#define NUMBER  '0'  /* signal that a number was found */
#define MAXVAL  100  /* maximum depth of val stack */
#define BUFSIZE 100
 
int getop(char []);

void push(double);
double pop(void);

int getch(void);
void ungetch(int);

int sp = 0;          /* next free stack position */
double val[MAXVAL];  /* value stack */
char buf[BUFSIZE];    /* buffer for ungetch */
int bufp = 0;         /* next free position in buf */

int main(void) 
{ 
    int type; 
    double op2; 
    char s[MAXOP]; 
    int flag = true; 
 
	//??从键盘输入的内容,是以' '为单位,依次从缓存读入到数组s[],
	//又由getop(s[])从s中读出,返回给type.
	//例如,从键盘输入 -2 1 +
	//数组s的几个不同的状态分别是:
	//s[0]='-'  s[1]='2'  s[2]='\n'(即10) 
	//s[0]='1' s[2]='\n'
	//s[0]='+' s[2]='\n'
    while((type = getop(s)) != EOF) 
    { 
        switch(type) 
        { 
			 case NUMBER:
               push(atof(s));
               break;
           case '+':
			   double a;
			   double b;
			   a=pop();
			   b=pop();
               push(a+b);
               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 '%': 
                op2 = pop(); 
                if(op2) 
                    push((int)pop()%(int)op2); 
                else 
                    printf("\nError: Division by zero!"); 
                break; 
		   case '\n':
               printf("\t%.8g\n", pop());
               break;
           default:
               printf("error: unknown command %s\n", s);
               break;
  
        } 
    } 
    return EXIT_SUCCESS; 
}

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;
    }
}


int getop(char s[]) 
{ 
    #define PERIOD  '.' 
    int i = 0; 
    int c; 
    int next; 
 
    /* Skip whitespace */ 
    while((s[0] = c = getch()) == ' ' || c == '\t') 
        ; 
    s[1] = '\0'; 
 
    /* Not a number but may contain a unary minus. */ 
	 if(!isdigit(c) && c != PERIOD && c != '-') 
        return c;                
	
    if(c == '-') //如果当前读入的c是'-'
    { 
        next = getch(); //判断下一个输入字符next
        if(!isdigit(next) && next != PERIOD) 
        { 
           return c; //如果next不是数字并且不是小数点,则c为操作符 
        } 
        c = next; //否则,既next是数字
    } 
    else 
    { 
        c = getch(); 
    } 
    /*收集整数部分*/
    while(isdigit(s[++i] = c)) //则把c保存到数组s[]中
            c = getch(); 
    if(c == PERIOD)
		/* 收集小数部分*/ 
        while(isdigit(s[++i] = c = getch())) 
                        ; 
    s[i] = '\0'; 
    if(c != EOF) 
        ungetch(c); 
    return NUMBER; 
}  

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;
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics