#include <bits/stdc++.h> int gcd(int a,int b) { a=abs(a); b=abs(b); if(a<b) { int temp=a; a=b; b=temp; } while(b!=0) { int temp=a%b; a=b; b=temp; } return a; } int main() { char str[1000]; while(gets(str)!=NULL) { int a=0,b=0,c=0,d=0; int biaoji=0; int fuhao; for(int i=0; i<strlen(str); i++) { if(str[i]=='/' || str[i]=='-' ||str[i]=='+') { biaoji++; if(str[i]=='-') { fuhao=0; } else if(str[i]=='+') { fuhao=1; } } else if(biaoji==0) { a=a*10+str[i]-'0'; } else if(biaoji==1) { b=b*10+str[i]-'0'; } else if(biaoji==2) { c=c*10+str[i]-'0'; } else if(biaoji==3) { d=d*10+str[i]-'0'; } } int resa=a*d; int resc=c*b; int resb=b*d; int resd=d*b; if(fuhao==0) { resa=resa-resc; } else { resa=resa+resc; } if(resa==0) { printf("0\n"); } else if(abs(resa)%abs(resb)==0) { printf("%d\n",resa/resb); } else { printf("%d/%d\n",resa/gcd(resa,resb),resb/gcd(resa,resb)); } } } /* F:\temp\22492177.25707\Main.cc:2:25: error: bits/stdc++.h: No such file or directory F:\temp\22492177.25707\Main.cc: In function 'int gcd(int, int)': F:\temp\22492177.25707\Main.cc:5: error: 'abs' was not declared in this scope F:\temp\22492177.25707\Main.cc: In function 'int main()': F:\temp\22492177.25707\Main.cc:24: error: 'gets' was not declared in this scope F:\temp\22492177.25707\Main.cc:24: error: 'NULL' was not declared in this scope F:\temp\22492177.25707\Main.cc:28: error: 'strlen' was not declared in this scope F:\temp\22492177.25707\Main.cc:57: error: 'printf' was not declared in this scope F:\temp\22492177.25707\Main.cc:58: error: 'abs' was not declared in this scope F:\temp\22492177.25707\Main.cc:59: error: 'printf' was not declared in this scope F:\temp\22492177.25707\Main.cc:61: error: 'printf' was not declared in this scope */ |
Double click to view unformatted code.