Evaluate Postfix Expression_后缀表达式求解

6-2 Evaluate Postfix Expression(25 分)
Write a program to evaluate a postfix expression. You only have to handle four kinds of operators: +, -, x, and /.
Format of functions:

ElementType EvalPostfix( char *expr );
where expr points to a string that stores the postfix expression. It is guaranteed that there is exactly one space between any two operators or operands. The function EvalPostfix is supposed to return the value of the expression. If it is not a legal postfix expression, EvalPostfix must return a special value Infinity which is defined by the judge program.
Sample program of judge:

#include 
#include 

typedef double ElementType;
#define Infinity 1e8
#define Max_Expr 30   /* max size of expression */

ElementType EvalPostfix( char *expr );

int main()
{
    ElementType v;
    char expr[Max_Expr];
    gets(expr);
    v = EvalPostfix( expr );
    if ( v < Infinity )
        printf("%f\n", v);
    else
        printf("ERROR\n");
    return 0;
}

/* Your function will be put here */
Sample Input 1:

11 -2 5.5 * + 23 7 / -
Sample Output 1:

-3.285714
Sample Input 2:

11 -2 5.5 * + 23 0 / -
Sample Output 2:

ERROR
Sample Input 3:

11 -2 5.5 * + 23 7 / - *
Sample Output 3:

ERROR
Special thanks to Sirou Zhu (朱思柔) for providing a valuable test case.
后缀表达式求值,简单模拟
没啥好说的

#include 
#include 
#include
#include
#include
#include
using namespace std;
typedef double ElementType;
#define Infinity 1e8
#define Max_Expr 30   /* max size of expression */

ElementType EvalPostfix(char *expr)
{
    double stk[1111], p1, p2;
    int len = strlen(expr), i, j, f = 0,top=-1;
    char *tmp;
    for (i = 0; expr[i]; i++)
    {
        if (!i)
        {
            if (expr[0] == '-' && expr[1] == '\0')
                return Infinity;
            if (!isdigit(expr[0])&&expr[0]!='-')
                f = 1;
        }
        else if (expr[i] != '.' && !isdigit(expr[i]))
        {
            f = 1;
        }
    }
    expr[len] = ' ', expr[len + 1] = '\0';
//  cout << f << endl;
    if (!f)
        return atof(expr);
    i = 0;// 11 -2 5.5 * +23 7 / -
    while (expr[i]!='\0')
    {
        if (expr[i] == ' ')
        {
            expr[i++] = '\0';
            tmp = expr;
            expr += i;
            i = 0;
            if (!strchr("0123456789.+-*/", tmp[0]))
                return Infinity;
            if (strchr("*/+-", tmp[0]) && tmp[1] == '\0')
            {
                if (top - 2 < -1)
                    return Infinity;
                p1 = stk[top--];
                p2 = stk[top--];
                if (tmp[0] == '+')
                    stk[++top] = p1 + p2;
                else if (tmp[0] == '-')
                    stk[++top] = p2 - p1;
                else if (tmp[0] == '*')
                    stk[++top] = p2*p1;
                else if (tmp[0] == '/')
                {
                    if (p1 == 0)
                        return Infinity;
                    stk[++top] = p2 / p1;
                }
            }
            else
                stk[++top] = atof(tmp);
        }
        else i++;
    }
    if (top - 1 == -1)  
        return stk[0];
    return Infinity;
}

int main()
{
    ElementType v;
    char expr[Max_Expr];
    gets_s(expr);
    v = EvalPostfix(expr);
    if (v < Infinity)
        printf("%f\n", v);
    else
        printf("ERROR\n");
    return 0;
}

你可能感兴趣的:(nit_大作业)