zoukankan      html  css  js  c++  java
  • 6-5 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 <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
    ", v);
        else
            printf("ERROR
    ");
        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 ) {
        char t[10];
        double s[100],flag;
        int c = 0,k = 0;
        while(expr[k]) {
            int j = 0;
            while(expr[k] && expr[k] != ' ') t[j ++] = expr[k ++];
            t[j] = 0;
            if(t[0] >= '0' && t[0] <= '9' || t[1] && (t[0] == '-' || t[0] == '+')) {
                s[c] = 0;
                int i = 0;
                flag = 1;
                if(t[i] == '-') {
                    flag = -1;
                    i ++;
                }
                if(t[i] == '+') i ++;
                while(t[i] && t[i] != '.') {
                    s[c] = s[c] * 10 + t[i ++] - '0';
                }
                if(t[i] == '.') {
                    i ++;
                    double d = 0.1;
                    while(t[i]) {
                        s[c] += (t[i ++] - '0') * d;
                        d *= 0.1;
                    }
                }
                s[c] *= flag;
                c ++;
            }
            else {
                if(c < 2) return Infinity;
                switch(t[0]) {
                    case '+':s[c - 2] += s[c - 1];break;
                    case '-':s[c - 2] -= s[c - 1];break;
                    case '*':s[c - 2] *= s[c - 1];break;
                    case '/': if(s[c - 1] == 0) return Infinity;
                              s[c - 2] /= s[c - 1];break;
                }
                c --;
            }
            while(expr[k] && expr[k] == ' ') k ++;
        }
        if(c > 1) return Infinity;
        return s[0];
    }
  • 相关阅读:
    全屏透明遮罩层
    理解Javascript__理解undefined和null
    JS 对象属性相关--检查属性、枚举属性等
    js 空正则匹配任意一个位置
    a 标签 download 和 target 不配合
    Array.prototype.filter(Boolean)
    页面操作表单不会调用表单 value 属性的 set 函数
    Babel6.x的安装
    html 事件处理程序中的代码在执行时,有权访问全局作用域中的任何代码。
    js 常用 DOM 元素宽高
  • 原文地址:https://www.cnblogs.com/8023spz/p/12249628.html
Copyright © 2011-2022 走看看