• codevs5164 逆波兰表达式


    题目描述 Description

    逆波兰表达式是一种把运算符前置的算术表达式(又叫前缀表达式),例如普通的表达式2 + 3的逆波兰表示法为+ 2 3。逆波兰表达式的优点是运算符之间不必有优先级关系,也不必用括号改变运算次序,例如(2 + 3) * 4的逆波兰表示法为* + 2 3 4。本题求解逆波兰表达式的值,其中运算符包括+ - * /四个。

    输入描述 Input Description

    输入为一行,其中运算符和运算数之间都用空格分隔,运算数是浮点数。

    输出描述 Output Description

    输出为一行,表达式的值。

    值应该为浮点数并保留6位小数。

    测试数据保证单精度与双精度都可以通过

    样例输入 Sample Input

       

    * + 11.0 12.0 + 24.0 35.0
    样例输出 Sample Output

       

    1357.000000
    数据范围及提示 Data Size & Hint

    C/C++语言可以使用stdlib.h(C)或cstdlib(C++)里的

    double atof( const char *str );

    把字符串变为double浮点数

    ↑这个是函数原型↑

    pascal的嘛……

    我不知道有没有……

    所以P的看着办吧

    /*
    递归计算
    */
    #include<iostream>
    #include<cstdio>
    #include<string>
    #include<cstring>
    #include<algorithm>
    #include<stack>
    #include<cstdlib>
    #define ll int
    #define fo(i,l,r) for(int i = l;i <= r;i++)
    #define fd(i,l,r) for(int i = r;i >= l;i--)
    using namespace std;
    const int maxn = 1050;
    ll read(){
        ll x=0,f=1;
        char ch=getchar();
        while(!(ch>='0'&&ch<='9')){if(ch=='-')f=-1;ch=getchar();};
        while(ch>='0'&&ch<='9'){x=x*10+(ch-'0');ch=getchar();};
        return x*f;
    }
    stack<double> aa,bb;
    stack<char> cc;
    char a[100],t,op;
    double ans;
    double get_f(){
        int pos = 0;
        char tmp = a[0];
        while((tmp>='0'&&tmp<='9') || tmp == '.'){
            a[pos++] = tmp;
            tmp = getchar();
        }
        return atof(a);
    }
    inline char get_in(){
        char tmp;
        tmp = getchar();
        while(tmp == ' ') tmp = getchar();
        return tmp;
    }
    double cal(char col){
        double num,numa,numb;
        t = get_in();
        if(t>='0'&&t<='9'){
            memset(a,0,sizeof(a));
            a[0] = t;
            numa = get_f();
        }else{
            numa = cal(t);
        }
        t = get_in();
        if(t>='0'&&t<='9'){
            memset(a,0,sizeof(a));
            a[0] = t;
            numb = get_f();
        }else{
            numb = cal(t);
        }
        if(col == '+') num = numa + numb;
        if(col == '-') num = numa - numb;
        if(col == '*') num = numa * numb;
        if(col == '/') num = numa / numb;
        return num;
    }
    int main(){
        freopen("gg.in","r",stdin);
        t = get_in();
        ans = cal(t);
        printf("%.6lf",ans);
        return 0;
    } 
  • 相关阅读:
    简单自定义控件在view下可以运行在传统模式下运行显示空白
    symbian菜单2运行时更换菜单实现右键菜单功能
    得到文件路径信息 TParse
    Symbian菜单:创建一个最简单的菜单
    RFs 目录操作
    作品[RFileRead/WriteStream 练习] for s60 fp2
    对话框的练习
    文件浏览工具s60第二版
    转描述符
    得到驱动器信息
  • 原文地址:https://www.cnblogs.com/hyfer/p/6003733.html
Copyright © 2020-2023  润新知