补充:卡在Case #5和#6上
by Andycode3759 @ 2022-12-03 07:48:10
我之前也卡过,你看一下你的输出如果是在为0的时候的情况
附上我的代码
```cpp
#include<cstdio>
#include<cstring>
char s[100001],c;
bool right=false,multiply=false;
int tmp1,tmp2,sumleft,sumright,sumxleft,sumxright,positive=1;
int main()
{
scanf("%s",s);
for(register int i=0;i<strlen(s);i++)
{
if(s[i]=='='){multiply?sumleft+=tmp1*tmp2*positive:sumleft+=tmp1*positive;right=true,multiply=false,tmp1=0,positive=1;}
else if(s[i]>='0'&&s[i]<='9')multiply?tmp2*=10,tmp2+=s[i]-'0':tmp1*=10,tmp1+=s[i]-'0';
else if(s[i]>='a'&&s[i]<='z'){c=s[i],multiply?right?sumxright+=tmp1*tmp2*positive:sumxleft+=tmp1*tmp2*positive:right?tmp1?sumxright+=positive*tmp1:sumxright++:tmp1?sumxleft+=positive*tmp1:sumxleft++;multiply=false,tmp1=0;}
else if(s[i]=='+'){multiply?right?sumright+=positive*tmp1*tmp2,tmp1=0,positive=1:sumleft+=positive*tmp1*tmp2,tmp1=0,positive=1:right?sumright+=positive*tmp1,tmp1=0,positive=1:sumleft+=positive*tmp1,tmp1=0,positive=1;multiply=false;}
else if(s[i]=='-'){multiply?right?sumright+=positive*tmp1*tmp2,tmp1=0,positive=-1:sumleft+=positive*tmp1*tmp2,tmp1=0,positive=-1:right?sumright+=positive*tmp1,tmp1=0,positive=-1:sumleft+=positive*tmp1,tmp1=0,positive=-1;multiply=false;}
else if(s[i]=='*')multiply=true,tmp2=0;
if(i==strlen(s)-1)multiply?sumright+=tmp1*tmp2*positive:sumright+=tmp1*positive;
}
//printf("%d %d %d %d\n",sumleft,sumright,sumxleft,sumxright);
(sumleft-sumright)?printf("%c=%.3f\n",c,(double)(sumleft-sumright)/(double)(sumxright-sumxleft)):printf("%c=0.000\n",c);
}
```
by 四宫辉夜 @ 2022-12-03 09:25:56