PTA Evaluate Postfix Expression

167 阅读1分钟

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 <stdio.h>
#include <stdlib.h>

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

代码:

ElementType EvalPostfix( char *expr ){
    double num[Max_Expr];
    int i = -1;
    for(int j = 0 ; expr[j] != '\0' ; j ++)
    {
        if(expr[j] == '-' && expr[j+1] >= '0')
        {
            char a[100] = {'\0'};
            int t= -1;
            j++;
            while((expr[j] >= '0'&&expr[j] <= '9') || expr[j] == '.')
                a[++t] = expr [j++];
            j--;
            num[++i] = -atof(a); //(将字符串转化为浮点数)
            continue;
        }
        if(expr[j] >= '0'&&expr[j] <= '9')
        {
            char a[100] = {'\0'};
            int t= -1;
            while((expr[j] >= '0'&&expr[j] <= '9') || expr[j] == '.')
                a[++t] = expr [j++];
            j--;
            num[++i] = atof(a); //(将字符串转化为浮点数)
            continue;
        }
        if(expr[j] == '+' || expr[j] == '-' || expr[j] == '*' || expr[j] == '/')
        {
            if(i<1)
                return Infinity;
            switch(expr[j])
            {
                case '+' : {
                    num[i-1] = num[i-1] + num[i];
                    i--;
                    break;
                }
                case '-' :{
                    num[i-1] = num[i-1] - num[i];
                    i--;
                    break;
                }
                case '*' :{
                    num[i-1] = num[i-1] * num[i];
                    i--;
                    break;
                }
                case '/' :{
                    if(num[i] == 0 )
                        return Infinity;
                    else
                    {
                        num[i-1] = num[i-1] / num[i];
                        i--;
                        break;
                    }
                }
            }
        }
    }
    if(i != 0 )
        return Infinity;
    else 
        return num[0];
}

提交结果:

2.png