-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunc.cpp
More file actions
46 lines (45 loc) · 1.33 KB
/
Copy pathfunc.cpp
File metadata and controls
46 lines (45 loc) · 1.33 KB
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
#include "Fraction.h"
#include <iostream>
using namespace std;
ostream& operator<<(ostream& cout, Fraction& R1) {
if (R1.fenzi == R1.fenmu) {
std::cout << 1 << endl;
return cout;
}
if (R1.fenzi == 0) {
std::cout << 0 << endl;
return cout;
}
std::cout << R1.fenzi << "/" << R1.fenmu << std::endl;
return cout;
}
Fraction Fraction::operator + (const Fraction& R1) {
int fenmu = lcm(this->fenmu, R1.fenmu);
int fenzi = (this->fenzi) * (fenmu / this->fenmu) + (R1.fenzi) * (fenmu / R1.fenmu);
Fraction sum(fenzi, fenmu);
sum = yuefen(sum);
return sum;
}
Fraction Fraction::operator - (const Fraction& R1) {
int fenmu = lcm(this->fenmu, R1.fenmu);
int fenzi = (this->fenzi) * (fenmu / this->fenmu) - (R1.fenzi) * (fenmu / R1.fenmu);
Fraction sum(fenzi, fenmu);
sum = yuefen(sum);
return sum;
}
Fraction Fraction::operator * (const Fraction& R1) {
Fraction sum(this->fenzi * R1.fenzi, this->fenmu * R1.fenmu);
sum = yuefen(sum);
return sum;
}
Fraction Fraction::operator / (const Fraction& R1) { return Fraction((*this) * (Fraction(R1.fenmu, R1.fenzi))); }
Fraction Fraction::yuefen(Fraction R1) {
int m = R1.fenzi;
int n = R1.fenmu;
int x = gcd(m, n);
R1.fenzi /= x;
R1.fenmu /= x;
return R1;
}
int Fraction::lcm(int a, int b) { return a / gcd(a, b) * b; }
int Fraction::gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }