一道模拟题
但是由于我姿势不对导致代码非常丑
需要注意的就是 -a 与 a 前面没有系数的情况
1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <cstdlib> 5 #include <iostream> 6 using namespace std ; 7 8 int l,k,b,i,x ; 9 char s[1011] ; 10 char ch,ss ; 11 double ans ; 12 int f1,f2,f,w ; 13 14 int main() 15 { 16 scanf("%s",s+1) ; 17 l = strlen(s+1) ; 18 f1 = 0 ; f2 = 0 ; 19 /* 20 ans = 0/-1; 21 //printf("%.3lf\n\n",ans) ; 22 k = -1 ; 23 b = 0 ; 24 ans=b; 25 ans = ans/k; 26 printf("%.3lf\n\n",ans) ; 27 k=0; 28 */ 29 for(i=1;i<=l;) 30 { 31 f = 1 ; f2 = 0 ; x = 0 ; w = 0 ; 32 while((s[i]<'0'||s[i]>'9')&&i<=l) 33 { 34 if(s[i]=='-') f = -1 ; 35 if(s[i]=='=') f1= 1; 36 if(s[i]>='a'&&s[i]<='z') 37 { 38 f2 = 1 ; 39 ss = s[ i ] ; 40 i++ ; 41 x = 1 ; 42 w = 1 ; 43 break ; 44 } 45 i++ ; 46 } 47 while(s[i]>='0'&&s[i]<='9'&&i<=l&&w==0) 48 { 49 x = x*10+s[ i ]-48 ; 50 i++ ; 51 } 52 53 x = x*f ; 54 if(s[i]>='a'&&s[i]<='z') 55 { 56 f2 = 1 ; 57 ss = s[ i ] ; 58 i++ ; 59 } 60 //cout<<x<<endl<<endl ; 61 //cout<<endl<<f1<<endl<<f2<<endl<<endl ; 62 if(f2==0) 63 if(f1==0) 64 b-=x ; 65 else b+=x; 66 else 67 if(f1==0) 68 k+=x; 69 else k-=x; 70 //cout<<endl<<k<<endl<<b<<endl<<endl ; 71 } 72 //cout<<k<<endl<<b<<endl ; 73 cout<<ss<<'='; 74 ans = b ; 75 ans = ans/k; 76 if(b==0) 77 { 78 printf("0.000\n") ; 79 return 0 ; 80 } 81 printf("%.3lf",ans) ; 82 return 0 ; 83 }