表达式
作者:互联网
链接:https://ac.nowcoder.com/acm/contest/5795/A
来源:牛客网
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.示例1
It's guaranteed that the absolute value after each step is always between 10−1010^{-10}10−10 and 101010^{10}1010.
输入
复制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\n"); 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\n"); 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\n"); 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\n",ans); }
标签:Little,ch,return,int,Gyro,char,表达式 来源: https://www.cnblogs.com/lipu123/p/13045737.html