逆波兰表示法——栈的理解

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int a,b,top = 0;
char s[100];
void push(int x)
{
    s[++top] = x;
}
int pop()
{
    top--;
    return s[top+1];
}
int main()
{

    while(scanf("%s",s)!=EOF)
    {
        if(s[0]=='+')
        {
            a = pop();
            b = pop();
            push(a+b);
        }
        else if(s[0]=='-')
        {
            a = pop();
            b = pop();
            push(a-b);
         } 
         else if(s[0]=='*')
         {
            a = pop();
            b = pop();
            push(a*b);
         }
         else
            push(atoi(s));//用来将字符串形式的数字转化为整形数值。 
    }
    printf("%d\n",pop());
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qiulianshaonv_wjm/article/details/82427438