基于栈的后缀表达式求值

基于栈的后缀算术表达式求值

描述

从键盘上输入一个后缀表达式,试编写算法计算表达式的值。规定:后缀表达式的长度不超过一行,以“=”作为输入结束,操作数之间用空格分隔,操作符只可能有+、−、*、

输入

多组数据,每组数据一行,对应一个后缀算术表达式,每个表达式均以“=”结尾。当表达式只有一个“=”时,输入结束。

输出

多组数据,每组数据一行,对应一个后缀算术表达式,每个表达式均以“=”结尾。当表达式只有一个“=”时,输入结束。

输入样例 1
1 2+8 2-7 4-/*=
1 2+=
1 2/=
=
输出样例 1
6.00
3.00
0.50
AC 代码
#include <iostream>
#include <cstring>

#define MAX 100

using namespace std;

typedef struct
{
    
    
    double *base;
    double *top;
    int
    maxSize;
}stack;

void initStack (stack &s)
{
    
    
    s.base = new double [MAX];
    s.top = s.base;
    s.maxSize = MAX;
}

void push(stack &s, double e)
{
    
    
    *(s.top ++) = e;   
}

void pop(stack &s)
{
    
    
    s.top --;
}

double top(stack s)
{
    
    
    return *(s.top - 1);
}

int main()
{
    
    
/*
后缀表达式的基本思想是只要遇见了字符,就取出data栈的两个数据进行运算
*/
    char s[100];
    while (true)           // 这里由于需要读入空格,因此用cin读入不方便(遇见空格自动停止)
    {
    
    
        cin.getline(s, 100);
        if (s[0] == '=')
            break;
        stack ds;
        initStack(ds);

        int x = 0, e = 0, flag = 0;
        for (int i = 0; s[i] != '='; i ++)
        {
    
    
            if ('0' <= s[i] && s[i] <= '9')
            {
    
    
                x = x * 10 + (s[i] - '0');
                if (e)
                    e *= 10;
                
                flag = 1;
            }
            else if (s[i] == '.')
                e = 1;
            
            else 
            {
    
    
                if (flag)
                {
    
    
                    double data = x;
                    if (e)
                        data /= e;
                    push(ds, data);
                    
                    flag = e = x = 0;
                }
                else if (s[i] == ' ')
                    continue;
                    
                    switch (s[i])
                    {
    
    
                        case '+':
                        {
    
    
                            double a = top(ds);
                            pop(ds);
                            double b = top(ds);
                            pop(ds);
                            push(ds, b + a);
                            break;
                        }
                        case '-':
                        {
    
    
                            double a = top(ds);
                            pop(ds);
                            double b = top(ds);
                            pop(ds);
                            push(ds, b - a);
                            break;  
                        }
                        case '*':
                        {
    
    
                            double a = top(ds);
                            pop(ds);
                            double b = top(ds);
                            pop(ds);
                            push(ds, b * a);
                            break; 
                        }
                        case '/':
                        {
    
    
                            double a = top(ds);
                            pop(ds);
                            double b = top(ds);
                            pop(ds);
                            push(ds, b / a);
                            break; 
                        }
                    }
            }
        }
        printf("%.2lf\n", top(ds));
    }
    
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_57095511/article/details/121260277