zoukankan      html  css  js  c++  java
  • 1470 数据结构:逆波兰表达式

    本题难点有二:

      其一为波兰表达式递归求值算法的理解;

      其二为处理多组数据,scanf()的返回值是成功赋值的变量数量, 发生错误时返回EOF.注意exit()与return的区别

    关于波兰(前缀)表达式、中缀表达式、逆波兰(后缀)表达式的详细介绍,请参考:http://www.cnblogs.com/chenying99/p/3675876.html

    另外,指出本题的一个小错误,应把题目中所有的“逆波兰”改为“波兰”。

    // 测试用例:
    // * - 2 3 4
    // - 3 2
    // - 7 7
    // * + 11.0 12.0 + 24.0 35.0
    #include <iostream>
    #include <cstdlib>
    #include <cmath>
    using namespace std;
    double Evaluate()//波兰(前缀)表达式递归求值算法,需要好好理解
    {
        char a[15];//用于存储每次递归读取的一个非空字符(串)
        if (scanf("%s", &a) == EOF) exit(0);//处理多组数据正常结束的问题
        switch (a[0])
        {
        case '+': return Evaluate() + Evaluate();
        case '-': return Evaluate() - Evaluate();
        case '*': return Evaluate() * Evaluate();
        case '/': return Evaluate() / Evaluate();
        default: return atof(a);
        }
    }
    int main(int argc, char const *argv[])
    {
        //#ifndef _OJ_  //ONLINE_JUDGE
        // freopen("input.txt", "r", stdin);
        // freopen("output.txt", "w", stdout);
        //#endif
        while (1)   printf("%f
    ",Evaluate());
        return 0;
    }
  • 相关阅读:
    ajax_注册
    mysql 二
    mysql基础
    django数据库批量创建
    私有属性
    mysql操作
    @property @classmethod @staticmethod
    python中的__new__方法
    员工信息表-装逼版
    三级菜单
  • 原文地址:https://www.cnblogs.com/swanGooseMan/p/4253713.html
Copyright © 2011-2022 走看看