-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rational.cpp
93 lines (78 loc) · 2.24 KB
/
Rational.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
//
// Keita Nonaka
//
// This is Rational class, which calculate rational numbers.
// This class is for implementing methods.
//
#include "project6.h"
#include <iostream>
using namespace std;
int numeratorF = 0; // final numerator, global variable
int denominatorF = 0; // final denominator, global variable
char sign = '+'; // sign, global variable
Rational::Rational() { // constructor
numerator = 0;
denominator = 0; // doesnt matter if denominator is assigned 0
}
Rational::Rational(int a, int b) { // constructor
int GCD = gcd(a, b);
numerator = a / GCD;
denominator = b / GCD;
reduce(numerator, denominator);
}
Rational::~Rational() {
numerator = 0;
denominator = 0;
}
int Rational::gcd(int a, int b){ // gcd
if(b == 0)
return a;
return gcd(b, a % b);
}
void Rational::add(Rational obj) { // addition
numeratorF = numerator * obj.denominator + obj.numerator * denominator;
denominatorF = denominator * obj.denominator;
sign = '+';
}
void Rational::sub(Rational obj) { // subtraction
obj.negate();
this->add(obj);
sign = '-';
}
void Rational::mul(Rational obj) { // multiplication
numeratorF = numerator * obj.numerator;
denominatorF = denominator * obj.denominator;
sign = '*';
}
void Rational::div(Rational obj) { // division
obj.reciprocal();
this->mul(obj);
sign = '/';
}
void Rational::reciprocal() { // reciprocal
int numer = numerator;
numerator = denominator;
denominator = numer;
}
void Rational::negate() { // negation
numerator = -numerator;
}
void Rational::reduce(int& numer, int& denom){
if (denom < 0) {
numer = -numer;
denom = -denom;
}
}
void Rational::finally() { // instead of reduce
int GCD = gcd(numeratorF, denominatorF);
numeratorF /= GCD;
denominatorF /= GCD;
reduce(numeratorF, denominatorF);
}
void Rational::print(Rational obj) {
finally();
cout << numerator << "/" << denominator << " " << sign << " " << obj.numerator << "/" << obj.denominator << " = ";
cout << numeratorF << "/" << denominatorF << endl;
cout << numerator / (float)denominator << " " << sign << " " << obj.numerator / (float)obj.denominator << " = ";
cout << numeratorF / (float)denominatorF << endl;
}