#include<iostream> #include<cstring> #include<algorithm> #include<queue> #include<stdio.h> #include<string.h> #include<cmath> using namespace std; int ming(int a,int b) { int cache; while(b>0){ cache=a%b; a=b; b=cache; } return a; } int main() { string str; while(cin>>str) { int a,b,x,y; a=str[0]-'0'; b=str[2]-'0'; x=str[4]-'0'; y=str[6]-'0'; if(str[3]=='+') { a=a*y; x=x*b; int cnt=b*y; int tem=a+x; // cout<<cnt<<" "<<tem<<endl; if(tem==0) cout<<0<<endl; else{ cout<<tem/ming(cnt,tem); if(cnt/ming(cnt,tem)!=1) cout<<"/"<<cnt/ming(cnt,tem); cout<<endl;} } else { a=a*y; x=x*b; int cnt=b*y; int tem=a-x; if(tem==0) cout<<0; else { if(tem<0){ tem=-tem; cout<<"-"<<tem/ming(cnt,tem); } else if(tem>0) cout<<tem/ming(cnt,tem); } if(cnt/ming(cnt,tem)!=1) cout<<"/"<<cnt/ming(cnt,tem); cout<<endl; } } return 0; } |
Double click to view unformatted code.