zoukankan      html  css  js  c++  java
  • nyist 128 前缀式计算

    前缀式计算

    时间限制:1000 ms  |  内存限制:65535 KB
    难度:3
    描述

    先说明一下什么是中缀式:

    如2+(3+4)*5这种我们最常见的式子就是中缀式。

    而把中缀式按运算顺序加上括号就是:(2+((3+4)*5))

    然后把运算符写到括号前面就是+(2 *( +(3 4) 5) )

    把括号去掉就是:+ 2 * + 3 4 5

    最后这个式子就是该表达式的前缀表示。

    给你一个前缀表达式,请你计算出该前缀式的值。

    比如:

    + 2 * + 3 4 5的值就是 37

    输入
    有多组测试数据,每组测试数据占一行,任意两个操作符之间,任意两个操作数之间,操作数与操作符之间都有一个空格。输入的两个操作数可能是小数,数据保证输入的数都是正数,并且都小于10,操作数数目不超过500。
    以EOF为输入结束的标志。
    输出
    对每组数据,输出该前缀表达式的值。输出结果保留两位小数。
    样例输入
    + 2 * + 3 4 5
    + 5.1 / 3 7
    样例输出
    37.00
    5.53
    一开始按照输入顺序来操作,发现这样子做会遇到前面的保存的数和后面的数可以进行先计算然后才进栈,但是这个不知道怎么样来
    做,没有做出来。

    后面yy了一下,直接逆序来操作,遇到符号就进行计算,然后进栈,这样AC了

    代码::

    #include <iostream>
    #include <string>
    #include <cstring>
    #include <cstdio>
    #include <cmath>

    using namespace std;

    double judge_char(char ch[])
    {
        int i = 0;
        double sum = 0.0;
        int  flag = 1;
        int k = 1;
        for(i = 0; i < strlen(ch); i++)
        {
            if(ch[i] == '.')
                {
                    flag = 0;

                    continue;
                }

            if(ch[i] != '.' && flag)
            {
                sum = sum *10 + (double)ch[i]-'0';
            }

            if(flag == 0)
            {
                sum += (double)(ch[i]-'0')/pow(10,k);
                k++;
            }
        }
        return sum;
    }
    int main()
    {
        //freopen("Input.txt","r",stdin);
        string ch;
        while(getline(cin,ch))
        {
            double a[505];//数字栈
            char ch2[500];//数字中转站
            memset(a,0,sizeof(a));
            int i;
            int t_a = 0;
            int t_ch2 = 0;
            ch2[0] = '\0';
            for( i = ch.length()-1; i >=0; i--)
               {
                   if(ch[i] != ' ')
                    {
                        if(ch[i] =='+' || ch[i] == '-' || ch[i] == '*' || ch[i] == '/')
                            {
                                t_a--;
                                double x = a[t_a];
                                t_a--;
                                double y = a[t_a];

                                    switch(ch[i])
                                    {
                                        case '+':
                                            a[t_a++] = x+y; break;
                                        case '-':
                                            a[t_a++] = x-y; break;
                                        case '*':
                                            a[t_a++] = x*y; break;
                                        case '/':
                                            a[t_a++] = x/y; break;
                                    }
                            }
                        else
                        {
                            ch2[t_ch2++] = ch[i];
                        }
                    }
                    else
                    {
                        if(ch2[0] == '\0')//判断是否有无数字 没有就不执行字符转换为数字了
                          continue;
                        ch2[t_ch2] = '\0';//在保存数字的末尾加\0
                        char ch3[500];
                        ch3[0] = '\0';//ch3 归为0 以免之前的重复使用
                        int t_ch3 = 0;
                        int jj = 0;
                        for(jj = strlen(ch2)-1; jj >= 0; jj--)
                            ch3[t_ch3++] = ch2[jj];
                        ch3[t_ch3++] = '\0';
                        a[t_a] = judge_char(ch3);
                        t_a++;
                        t_ch2 = 0;
                        ch2[0] = '\0';
                    }

        }

               printf("%.2lf\n",a[0]);
        }

    return 0;
    }


  • 相关阅读:
    DGA域名可以是色情网站域名
    使用cloudflare加速你的网站隐藏你的网站IP
    167. Two Sum II
    leetcode 563. Binary Tree Tilt
    python 多线程
    leetcode 404. Sum of Left Leaves
    leetcode 100. Same Tree
    leetcode 383. Ransom Note
    leetcode 122. Best Time to Buy and Sell Stock II
    天津Uber优步司机奖励政策(12月28日到12月29日)
  • 原文地址:https://www.cnblogs.com/yyroom/p/2951674.html
Copyright © 2011-2022 走看看