-
Notifications
You must be signed in to change notification settings - Fork 0
/
1034.cpp
99 lines (86 loc) · 1.92 KB
/
1034.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>
#include <math.h>
#include <string>
using namespace std;
long long gcd(long long a,long long b)
{
if(b==0)
return a;
else
return gcd(b,a%b);
}
long long lcd(long long a,long long b)
{
return a*b/gcd(a,b);
}
struct frac
{
int fh=1;
long long fz;
long long fm;
};
string dealFrac(frac f)
{
if(f.fz<0&&f.fm>0||f.fm<0&&f.fz>0)
{
f.fh=-1;
f.fz=abs(f.fz);
f.fm=abs(f.fm);
}
int p=gcd(f.fz,f.fm);
f.fz/=p;
f.fm/=p;
if(f.fz==0)
return "0";
string s;
if(f.fz/f.fm>0)
{
s=s+to_string(f.fz/f.fm);
if(f.fm!=1)
s=s+' '+to_string(abs(f.fz%f.fm))+"/"+to_string(f.fm);
}
else
s=to_string(f.fz)+"/"+to_string(f.fm);
if(f.fh==-1)
s="(-"+s+')';
return s;
}
int main()
{
frac f1,f2;
scanf("%lld/%lld %lld/%lld",&f1.fz,&f1.fm,&f2.fz,&f2.fm);
long long d1=gcd(f1.fz,f1.fm);
f1.fz/=d1;
f1.fm/=d1;
long long d2=gcd(f2.fz,f2.fm);
f2.fz/=d2;
f2.fm/=d2;
frac plus;
//cout<<f1.fz<<" "<<f1.fm<<" "<<f2.fz<<" "<<f2.fm<<endl;
plus.fh=f1.fh*f2.fh;
plus.fz=f1.fz*f2.fm+f2.fz*f1.fm;
plus.fm=f1.fm*f2.fm;
//cout<<plus_fz<<" "<<plus_fm<<endl;
// long long d3=gcd(plus_fm,plus_fz);
// plus_fm/=d3;
// plus_fz/=d3;
frac minus;
minus.fh=f1.fh*f2.fh;
minus.fz=f1.fz*f2.fm-f2.fz*f1.fm;
minus.fm=f1.fm*f2.fm;
frac times;
times.fz=f1.fz*f2.fz;
times.fm=f1.fm*f2.fm;
frac divided;
divided.fh=f1.fh*f2.fh;
divided.fz=f1.fz*f2.fm;
divided.fm=f2.fz*f1.fm;
cout<<dealFrac(f1)<<" "<<"+"<<" "<<dealFrac(f2)<<" "<<"="<<" "<<dealFrac(plus)<<endl;
cout<<dealFrac(f1)<<" "<<"-"<<" "<<dealFrac(f2)<<" "<<"="<<" "<<dealFrac(minus)<<endl;
cout<<dealFrac(f1)<<" "<<"*"<<" "<<dealFrac(f2)<<" "<<"="<<" "<<dealFrac(times)<<endl;
if(divided.fm==0)
cout<<dealFrac(f1)<<" "<<"/"<<" "<<dealFrac(f2)<<" "<<"="<<" "<<"Inf"<<endl;
else
cout<<dealFrac(f1)<<" "<<"/"<<" "<<dealFrac(f2)<<" "<<"="<<" "<<dealFrac(divided)<<endl;
return 0;
}