-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFraction.java
More file actions
141 lines (121 loc) · 3.76 KB
/
Fraction.java
File metadata and controls
141 lines (121 loc) · 3.76 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
129
130
131
132
133
134
135
136
137
138
139
140
141
package fractionpackage;
public class Fraction {
/**
* Class Fraction contains two fields of Long type - the upper and down part of the fraction.
* All mathematical operations, realised in methods, were coded in the following way: output is
* the Fraction, but the operation is called by the left operand.
*/
//класс дробей
//fields of class Fraction
private long upperPart = 1;
private long downPart = 1;
//constructors
public Fraction() {}
public Fraction(long up, long down) {
this.upperPart = up;
this.downPart = down;
}
//methods
public void setFraction(long up, long down) {
upperPart = up;
downPart = down;
}
public long[] getFraction() {
long fract[] = new long[2];
fract[0] = this.upperPart;
fract[1] = this.downPart;
return fract;
}
//функция вывода дроби в виде строки
public String printFraction() {
return (upperPart + "/" + downPart);
}
//функция суммы дробей
public Fraction fractSum(Fraction right) {
Fraction result = new Fraction();
long rightFr[] = new long[2];
long upRes = 1;
long downRes = 1;
long coef = 1;
rightFr = right.getFraction();
if (this.downPart == rightFr[1]) {
upRes = this.upperPart + rightFr[0];
downRes = this.downPart;
result.setFraction(upRes, downRes);
}
else if (this.downPart % rightFr[1] == 0) {
coef = this.downPart / rightFr[1];
upRes = coef * rightFr[0] + this.upperPart;
downRes = this.downPart;
result.setFraction(upRes, downRes);
}
else if (rightFr[1] % this.downPart == 0) {
coef = rightFr[1] / this.downPart ;
upRes = coef * this.upperPart + rightFr[0];
downRes = rightFr[1];
result.setFraction(upRes, downRes);
}
else {
upRes = rightFr[1] * this.upperPart + this.downPart * rightFr[0];
downRes = this.downPart * rightFr[1];
result.setFraction(upRes, downRes);
}
return result;
}
//функция разности дробей
public Fraction fractDiff(Fraction right) {
Fraction result = new Fraction();
long rightFr[] = new long[2];
long upRes = 1;
long downRes = 1;
long coef = 1;
rightFr = right.getFraction();
if (this.downPart == rightFr[1]) {
upRes = this.upperPart - rightFr[0];
downRes = this.downPart;
result.setFraction(upRes, downRes);
}
else if (this.downPart % rightFr[1] == 0) {
coef = this.downPart / rightFr[1];
upRes = this.upperPart - coef * rightFr[0];
downRes = this.downPart;
result.setFraction(upRes, downRes);
}
else if (rightFr[1] % this.downPart == 0) {
coef = rightFr[1] / this.downPart ;
upRes = coef * this.upperPart - rightFr[0];
downRes = rightFr[1];
result.setFraction(upRes, downRes);
}
else {
upRes = rightFr[1] * this.upperPart - this.downPart * rightFr[0];
downRes = this.downPart * rightFr[1];
result.setFraction(upRes, downRes);
}
return result;
}
//функция произведения дробей
public Fraction fractMult(Fraction right) {
Fraction result = new Fraction();
long rightFr[] = new long[2];
long upRes = 1;
long downRes = 1;
rightFr = right.getFraction();
upRes = this.upperPart * rightFr[0];
downRes = this.downPart * rightFr[1];
result.setFraction(upRes, downRes);
return result;
}
//функция частного (деления) дробей
public Fraction fractDiv(Fraction right) {
Fraction result = new Fraction();
long rightFr[] = new long[2];
long upRes = 1;
long downRes = 1;
rightFr = right.getFraction();
upRes = this.upperPart * rightFr[1];
downRes = this.downPart * rightFr[0];
result.setFraction(upRes, downRes);
return result;
}
}