|
#4:WA
- #include<iostream>
- #include<iomanip>
- #include<string>
- using namespace std;
- string s;
- int a=1,b,sl,sr,k;
- double eps=1e-5;
- char ch;
- int main()
- {
- cin>>s;
- while(k<s.size())
- {
- if(!(s[k]>='0'&&s[k]<='9')&&s[k+1]>='a'&&s[k+1]<='z')
- s=s.substr(0,k+1)+'1'+s.substr(k+1);
- else if(k==0&&s[k]>='a'&&s[k]<'z')s='1'+s;
- k++;
- }
- int mid=s.find('=');
- int l=0,r=mid+1,len=s.size();
- while(l<mid)
- {
- if(s[l]=='-')a=0-a,l++;
- while(s[l]>='0'&&s[l]<='9')
- {
- b=b*10+(s[l]-'0');
- l++;
- }
- if(s[l]>='a'&&s[l]<='z')
- {
- sl+=a*b;
- ch=s[l];
- b=0,l++;
- }
- else
- {
- sr-=a*b;
- b=0;
- if(s[l]=='-')a=-1;
- else if(s[l]=='+')a=1;
- l++;
- }
- }
- a=1,b=0;
- while(r<=len)
- {
- if(s[r]=='-')a=0-a,r++;
- while(s[r]>='0'&&s[r]<='9')
- {
- b=b*10+(s[r]-'0');
- r++;
- }
- if(s[r]>='a'&&s[r]<='z')
- {
- sl-=a*b;
- ch=s[r],b=0;
- r++;
- }
- else
- {
- sr+=a*b;
- b=0;
- if(s[r]=='-')a=-1;
- else if(s[r]=='+')a=1;
- r++;
- }
- }
- double ans=sr*1.0/double(sl);
- cout<<ch<<'=';
- if(ans>-eps&&ans<eps)cout<<"0.000";
- else cout<<fixed<<setprecision(3)<<ans;
- return 0;
- }
复制代码 |
|