SDUT - 2133 数据结构实验之栈与队列三:后缀式求值

#include <stdio.h>
#include <string.h>
int a[1005];
int p = 0;
int main()
{
    char c;
    while((c = getchar()) != EOF)
    {
        if(c == '#')
        {
            printf("%d\n", a[p - 1]);
            break;
        }
        else if(c == '*')
        {
            int t1 = a[p - 1];
            p--;
            int t2 = a[p - 1];
            p--;
            a[p++] = t1 * t2;
        }
        else if(c == '/')
        {
            int t1 = a[p - 1];
            p--;
            int t2 = a[p - 1];
            p--;
            a[p++] = t2 / t1;
        }
        else if(c == '+')
        {
            int t1 = a[p - 1];
            p--;
            int t2 = a[p - 1];
            p--;
            a[p++] = t1 + t2;
        }
        else if(c == '-')
        {
            int t1 = a[p - 1];
            p--;
            int t2 = a[p - 1];
            p--;
            a[p++] = t2 - t1;
        }
        else
        {
            a[p++] = c - '0';
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Miracle_QSH/article/details/81812543