zoukankan      html  css  js  c++  java
  • 表达式

    链接:https://ac.nowcoder.com/acm/contest/5795/A
    来源:牛客网

    During this winter vacation, Little Gyro went for a journey to Praha——a brand new country in the world. After arriving, Little Gyro really found a lot of differences between Praha and our country. One of the differences is based on the maths problems, which often makes Little Gyro very headache.
    However, Little Gyro was invited to complete a math quiz someday, the information relating to this shown as the following:
    What follows are some simple arithmetic problems. Therefore, it should be quite easy for you to get the correct answers. However, you are in a different country named Praha and the symbols for multiplication, addition, division, and subtraction follow a different logic, but fortunately all the arithmetic expressions in this country still follow the regular arithmetic rules. They are as follows:

    It's considered that Little Gyro can't find the symbol '×' or '÷' on his laptop keyboard, so he always uses the symbol '*' to represent the symbol '×' as well as the symbol '/' to represent the symbol '÷'.
    Due to calculating the arithmetic problems which seems to be a little bit difficult for Little Gyro, he wrote a simple program to solve those problems perfectly at last. Now Little Gyro sends this task to you.

    输入描述:

    Each input file only contains one test case.
    For each case, each line contains a simple arithmetic expression S (no more than 5×10510^5105 characters) from the different country Praha, which only contains non-negative integers (no more than 10510^5105), calculating signs (only contains '+', '-', '*', '/') and spaces.

    输出描述:

    For each test case, output the value of the given arithmetic expression (keep 2 decimal places). Especially, in the division operation, if the divisor is 0, output "Cannot be divided by 0"(without quotes) in the single line.
    It's guaranteed that the absolute value after each step is always between 10−1010^{-10}1010 and 101010^{10}1010.
    示例1

    输入

    复制
    1 + 1 / 1 * 1 - 1

    输出

    复制
    1.00
    示例2

    输入

    复制
    2/0+0-3*4

    输出

    复制
    Cannot be divided by 0
    注意加减乘除换了
    #pragma GCC optimize(2)
     
    #include<bits/stdc++.h>
     
    using namespace std;
    typedef long long ll;
    //c(n,k)*c(m,k)*k! 
    inline int read()
    {
        int 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;
    }
    const int maxn=5e5+10;
    const int mod=1e9+7;
    stack<char>c;
    stack<double>v;   
    int n;
    char a[maxn];
    char s[maxn];
    ll cnt,val[maxn];
    int check(char a,char b){
        if(a=='('||b=='('){
            return 0;
        }
        if(a=='+'||a=='-'){
            return 1;
        }
        if(a=='*'&&(b=='*'||b=='/')){
            return 1;
        }
        if(a=='/'&&(b=='*'||b=='/')){
            return 1;
        }
        return 0;
    }
    double dd(double x,char p,double y){
        if(p=='+'){
            return x+y;
        }
        else if(p=='-'){
            return x-y;
        }
        else if(p=='*'){
            return x*y;
        }
        else if(p=='/'){
            if(y==0){
                return -1;
            }
            else
                return (double)x/(double)y; 
        } 
    }
    int main(){
        memset(s,0,sizeof(s));
        string vi;
        getline(cin,vi);
        n=vi.size();
        cnt=0;
         for(int i=0;i<n;i++){
            if(vi[i]!=' '){
                if(vi[i]=='-') a[cnt++]='*';
                else if(vi[i]=='/') a[cnt++]='+';
                else if(vi[i]=='+') a[cnt++]='/';
                else if(vi[i]=='*') a[cnt++]='-';
                else a[cnt++]=vi[i];
            }
        }
        a[cnt]=')';
        c.push('(');
        int i=0;
        int ct=0;
        while(i<=cnt){
            if(a[i]>='0'&&a[i]<='9'){
                double x=0;
                while(a[i]>='0'&&a[i]<='9'){
                    x=x*10+(a[i]-'0');
                    i++;
                }
                val[ct++]=x;
            }
            else{
                if(a[i]==')'){
                    while(!c.empty()&&c.top()!='('){
                        s[ct++]=c.top();
                        c.pop();
                    }
                    c.pop();
                }
                else{
                    while(!c.empty()&&check(a[i],c.top())){
                        s[ct++]=c.top();
                        c.pop();
                    }
                    c.push(a[i]);
                }
                i++;
            }
        }
        for(i=0;i<ct;i++){
            if(!s[i]){
                v.push(val[i]);
            }
            else{
                double y=v.top();
                v.pop();
                double x=v.top();
                v.pop();
                double pp=dd(x,s[i],y);
                if(pp==-1){
                    printf("Cannot be divided by 0
    ");
                    return 0;
                }
                v.push(pp);
            }
        }
        printf("%.2f",v.top());
    }
    #pragma GCC optimize(2)
    #include<bits/stdc++.h> 
    using namespace std;
    typedef long long ll;
    //c(n,k)*c(m,k)*k! 
    inline int read()
    {
        int 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;
    }
    const int maxn=1e6+10;
    const int mod=1e9+7;
    char a[maxn];
    int main(){
        int n=0;
        double p,t;
        string vi;
        getline(cin,vi);    
        int len=vi.size();
        for(int i=0;i<len;i++){
            if(vi[i]!=' '){
                a[n++]=vi[i];
            }
        }
        double ans=0;
        int flag1=1,flag2=0,flag3=1,flag4=0,flag5=0;
        for(int i=0;i<n;i++){
            t=1.0;
            p=0.0;
            flag5=0;
            flag3=1;
            flag4=0;
            while(a[i]!='/'&&a[i]!='*'&&i<n){
                if(a[i]=='-'||a[i]=='+'){
                    if(flag3==1&&flag4==0){
                        flag5=1;
                        t=(1.0)*t*p;
                        p=0;
                    }   
                    else if(flag4==1&&flag3==0){
                        flag5=1;
                        if(p==0){
                            printf("Cannot be divided by 0
    ");
                            return 0;
                        }
                        t=(1.0)*t/p;
                        p=0;
                    }
                    if(a[i]=='-'){
                        flag3=1;
                        flag4=0;
                    }
                    else if(a[i]=='+'){
                        flag4=1;
                        flag3=0;
                    }
                    i++;
                    continue;
                }
                p=p*10+(a[i]-'0');
                i++;
            }
            if(flag3==1&&flag4==0&&flag5==1){
                t=(1.0)*t*p;
                p=0;
            }   
            else if(flag4==1&&flag3==0&&flag5==1){
                if(p==0){
                    printf("Cannot be divided by 0
    ");
                    return 0;
                }
                t=(1.0)*t/p;
                p=0;
            }
            if(flag5==1){
                p=t;
                flag5=0;
            }
            if(flag1==1&&flag2==0){
                ans=(1.0)*(ans+p);
            } 
            if(flag1==0&&flag2==1){
                ans=(1.0)*(ans-p);
            }
            if(a[i]=='/'){
                flag1=1;
                flag2=0;
            }
            if(a[i]=='*'){
                flag1=0;
                flag2=1;
            }
            p=0;//
        }
        printf("%.2lf
    ",ans);
          
    }
  • 相关阅读:
    【设计模式+原型理解】第四章:函数的三种角色+原型链终结版
    【设计模式+原型理解】第三章:javascript五种继承父类方式
    【设计模式+原型理解】第二章:基于构造函数扩展出来的原型模式
    【设计模式+原型理解】第一章:使用Javascript来巧妙实现经典的设计模式
    【Javascript】JS遍历数组的三种方法:map、forEach、filter
    mysql_8.0.12环境配置
    Vue搭建环境
    Flask学习:根据WTF实现登录
    Flask学习:简单的实现登录功能(表单,消息显示)
    Flask学习:模板的使用(变量,控制器,过滤器)
  • 原文地址:https://www.cnblogs.com/lipu123/p/13045737.html
Copyright © 2011-2022 走看看