View Code of Problem 3494

#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;
        cout<<tem/ming(cnt,tem)<<"/"<<cnt/ming(cnt,tem)<<endl;
       }
       else {
        a=a*y;
        x=x*b;
        int cnt=b*y;
        int tem=a-x;
        if(tem<0){
                tem=-tem;
       // cout<<cnt<<" "<<tem<<endl;
        cout<<"-"<<tem/ming(cnt,tem)<<"/"<<cnt/ming(cnt,tem)<<endl;
       }
       else if(tem>0)
        cout<<tem/ming(cnt,tem)<<"/"<<cnt/ming(cnt,tem)<<endl;
        else cout<<0<<endl;
       }
   }
   return 0;
}

Double click to view unformatted code.


Back to problem 3494