一个C的逆波兰式计算器的问题

本文介绍了一个简易计算器的实现方法,使用C语言通过堆栈操作完成基本的算术运算,包括加、减、乘、除等,并详细解释了getop、push及pop等关键函数的工作原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <stdio.h>
#include <stdlib.h>


#define MAXOP 100
#define NUMBER '0'


int getop(char []);
void push(double);
double pop(void);


// 调试的时候输入  12 5 + 4 2 - *   中间用空格隔开,以回车结束 


int main(int argc, char *argv[])
{
        int type;
        double op2;
        char s[MAXOP];        
     
        while((type=getop(s))!='\n'){         
                    
                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;
                default:
                        printf("error: unknown command %s\n",s);
                        break;
                }
        }
        
        if (type=='\n') printf("\t%.8g\n",pop());
        
        system("pause");
        return 0;
}


/******************************************
        push && pop
******************************************/
#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{
                printf("error: stack empty\n");
                return 0.0;
        }
}



/********************************************
        getop
********************************************/


#include <ctype.h>
int getch(void);
void ungetch(int);


int getop(char s[])
{
        int i,c;
        while( (s[0]=c=getch()) ==' ' || c=='\t')
                ;
        s[1]='\0';
        if (!isdigit(c) && 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';
                /*
         原书中用的 != EOF 我为了方便改为 != '\n' 
         全代码中 唯一这个地方调用了  ungetch , 但是我把下面
         这行注释掉程序也能运行,结果不会出错
         那 为什么要设计 ungetch 函数?   
        */
        if (c!='\n') ungetch(c); 


        return NUMBER;
}


/********************************************
        getch && ungetch
********************************************/


#define BUFSIZE 100
char buf[BUFSIZE];
int bufp=0;


int getch(void)
{     
       return (bufp > 0) ? buf[--bufp] : getchar();
}


void ungetch(int c)
{
        if (bufp>=BUFSIZE)
                printf("error in ungetch: too many characters\n");
        else
                buf[bufp++]=c;
}


the c programming language 第四章 的一个例子.

注:小知识点.

1. getchar()   和   getch()  的区别

   getchar: Reads a character from standard input.

   getch  : Gets a character from the console without echo.

两者的区别是: getch()函数不将读入的字符回显在显示屏幕上, getchar()函数等待输入直到按回车才结束,  回车前的所有输入字
符都会逐个显示在屏幕上。但只有第一个字符作为函数的返回值。

2.ungetch : Pushes back the last character read from the console.

#include <conio.h>
#include <ctype.h>
#include <stdio.h>

int main( void )
{
   char buffer[100];
   int count = 0;
   int ch;

   ch = _getche();
   while( isspace( ch ) )      // Skip preceding white space.
      ch = _getche();
   while( count < 99 )         // Gather token.
   {
      if( isspace( ch ) )      // End of token.
         break;
      buffer[count++] = (char)ch;
      ch = _getche();
   }
   _ungetch( ch );            // Put back delimiter.
   buffer[count] = '\0';      // Null terminate the token.
   printf( "\ntoken = %s\n", buffer );
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值