View Code
1 #include <stdio.h> 2 #include <string.h> 3 #define m 10 4 #define M 20 5 int gcd(int a, int b) 6 { 7 return !b?a:gcd(b,a%b); 8 } 9 int main() 10 { 11 char s1[m],s[M],s2[M],c; 12 int a,b,k,i,len,t=0; 13 while(~scanf("%s%s%ld%s%s",s,s1,&b,s2,s2)) 14 { 15 t++; 16 len=strlen(s); 17 a=0; 18 k=1; 19 for(i=len-2;i>=0;i--) 20 { 21 if(s[i]<'0' || s[i]>'9') break; 22 a+=(s[i]-'0')*k; 23 k*=10; 24 } 25 if(a==0) a=1; 26 if(i==0 && s[0]=='-') a=-a; 27 if(s1[0]=='+') b=-b; 28 printf("Case %ld:\n",t); 29 k=gcd(a,b); 30 if(k==a || k==-a) printf("%ld\n",b/a); 31 else 32 { 33 if(a/k>0) printf("%ld/%ld\n",b/k,a/k); 34 else printf("-%ld/%ld\n",b/k,-a/k); 35 } 36 } 37 return 0; 38 }
2x + 4 = 0
-12x - 8 = 0
-4x + 6 = 0
x + 1 = 0
-x - 5 = 0
Case 1:
-2
Case 2:
-2/3
Case 3:
3/2
Case 4:
-1
Case 5:
-5