-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtfrac.cpp
More file actions
128 lines (120 loc) · 2.63 KB
/
tfrac.cpp
File metadata and controls
128 lines (120 loc) · 2.63 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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include "tfrac.h"
#include "tanumber.h"
TFrac::TFrac()
{
this->up = 0;
this->down = 1;
}
TFrac::TFrac(int a, int b){
int temp = gcd(a, b);
a /= abs(temp);
b /= abs(temp);
this->up = a;
this->down = b;
}
TFrac::TFrac(const QString &str) {
int separator = str.indexOf("|");
this->up = str.left(separator).toInt();
this->down = str.right(separator).toInt();
}
int TFrac::gcd(int a, int b){
if (b == 0)
return a;
else
return gcd (b, a % b);
}
void TFrac::evk(){
int temp = gcd(up, down);
up /= abs(temp);
down /= abs(temp);
}
TFrac* TFrac::copy(){
TFrac* d = new TFrac();
d->up = this->up;
d->down = this->down;
return d;
}
TFrac& TFrac::operator+(TANumber& other){
TFrac pi = static_cast<TFrac&>(other);
TFrac* d = new TFrac();
d->up = this->up*pi.down+this->down*pi.up;
d->down = this->down * pi.down;
d->evk();
return *d;
}
TFrac& TFrac::operator-(){
TFrac* d = new TFrac();
d->up = -this->up;
d->down = this->down;
return *d;
}
TFrac& TFrac::operator-(TANumber& other){
TFrac pi = static_cast<TFrac&>(other);
TFrac* d = new TFrac();
d->up = this->up*pi.down-this->down*pi.up;
d->down = this->down * pi.down;
d->evk();
return *d;
}
TFrac& TFrac::operator*(TANumber& other){
TFrac pi = static_cast<TFrac&>(other);
TFrac* d = new TFrac();
d->up = this->up*pi.up;
d->down = this->down *pi.down;
d->evk();
return *d;
}
TFrac& TFrac::sqr(){
TFrac* d = new TFrac();
d->up = this->up * this->up;
d->down =this->down * this->down;
d->evk();
return *d;
}
TFrac& TFrac::rev() {
TFrac* d = new TFrac();
d->up = this->down;
d->down = this->up;
d->evk();
return *d;
}
TFrac& TFrac::operator/(TANumber& other){
TFrac pi = static_cast<TFrac&>(other);
TFrac* d = new TFrac();
d->up = this->up*pi.down;
d->down = this->down * pi.up;
if(d->down < 0){
d->down = -d->down;
d->up = -d->up;
}
d->evk();
return *d;
}
bool TFrac::operator==(TANumber& other) {
TFrac pi = static_cast<TFrac&>(other);
if (this->up == pi.up && this->down == pi.down)
return true;
else
return false;
}
bool TFrac::operator>(TFrac &other) {
this->up *= other.down;
other.up *= this->down;
if (this->up > other.up)
return true;
else
return false;
}
bool TFrac::isZero(){
if(this->up == 0) return true;
else return false;
}
TFrac& TFrac::get(){
return *this;
}
void TFrac::set(TANumber& A){
*this = static_cast<TFrac&>(A);
}
FEditor& TFrac::getRedactor(){
return *(new FEditor());
}