用栈求后缀表达式地的值


float calPostFix(char exp[])
{
    float s [maxSize];int top = -1;
    int i = 0;
    while(exp[i] != '\0')
    {
        if ('0'<= exp[i] && exp[i] <= '9')
            s[++top] = exp[i] - '0';
        else if (exp[i] == '+' || exp[i] == '-' || 
                exp[i] == '*' || exp[i] == '/'   )
        {
            float opnd1,pand2,result;
            chat op;    //接受运算符
            int flag;    //判断计算是否成功
            opand2 = s[top--];
            opand1 = s[top--];
            op = exp[i];
            flag = calSub(opand1, op, opand2, result);
            if(flag == 0)    //如果不成功
            {
                std::count<<"ERROR" << std::end1; //puts("ERROR")
                break;
            }
            s[++top] == result;
        }
        ++i;
    }
    return s[top];
}

posted @ 2021-02-28 11:13  当时不杂  阅读(50)  评论(0编辑  收藏  举报