-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfraction.cpp
79 lines (57 loc) · 1.63 KB
/
fraction.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
#include <cstdio>
#include <algorithm> //swap()使うのに必要
typedef long long int LL;
LL gcd(LL n,LL m){
if(n<m) std::swap(n,m);
if(m==0)return n;
return gcd(m,n%m);
}
//分数。分母に0は入れちゃダメです
class fraction{
public:
LL ue, st; //分子,分母
fraction(){ ue=1; st=1; };
fraction(LL u, LL s){ set(u, s); };
fraction(int u, int s){ set((LL)u, (LL)s); };
void set(LL u, LL s){
LL tmp = gcd(u, s);
ue = u/tmp;
st = s/tmp;
};
fraction operator+( const fraction &f ){
fraction tmp( ue*f.st + f.ue*st, st * f.st );
return tmp;
};
fraction operator-( const fraction &f ){
fraction tmp( ue*f.st - f.ue*st, st * f.st);
return tmp;
};
fraction operator*( const fraction &f ){
fraction tmp( ue * f.ue, st * f.st );
return tmp;
};
fraction operator/( const fraction &f ){
fraction tmp( ue * f.st, st * f.ue);
return tmp;
};
fraction &operator=( const fraction &f ){
ue = f.ue;
st = f.st;
return *this;
};
double real(){ return (double)ue / (double)st; };
};
int main(){
int u1,s1, u2,s2;
scanf("%d %d", &u1, &s1 );
scanf("%d %d", &u2, &s2 );
fraction f1(u1, s1), f2(u2, s2), tmp(1,1);
tmp = f1 + f2;
printf("%lld/%lld + %lld/%lld = %lld/%lld = %f \n", f1.ue,f1.st,f2.ue,f2.st, tmp.ue, tmp.st, tmp.real() );
tmp = f1 - f2;
printf("%d/%d - %d/%d = %lld/%lld = %f \n", u1,s1,u2,s2, tmp.ue, tmp.st, tmp.real() );
tmp = f1 * f2;
printf("%d/%d * %d/%d = %lld/%lld = %f \n", u1,s1,u2,s2, tmp.ue, tmp.st, tmp.real() );
tmp = f1 / f2;
printf("%d/%d / %d/%d = %lld/%lld = %f \n", u1,s1,u2,s2, tmp.ue, tmp.st, tmp.real() );
}