#include<stdio.h> #include<math.h> #include<string.h> #include<stdlib.h> //#include<algorithm> //#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)); } } } |
Double click to view unformatted code.