|
| 1 | +/*---------------------------------------------------------------------------------------------------------------------- |
| 2 | + Rational sınıfı |
| 3 | +----------------------------------------------------------------------------------------------------------------------*/ |
| 4 | +package org.csystem.math; |
| 5 | + |
| 6 | +public class Rational { |
| 7 | + private int m_a, m_b; |
| 8 | + |
| 9 | + private static Rational add(int a1, int b1, int a2, int b2) |
| 10 | + { |
| 11 | + return new Rational(a1 * b2 + a2 * b1, b1 * b2); |
| 12 | + } |
| 13 | + |
| 14 | + private static Rational sub(int a1, int b1, int a2, int b2) |
| 15 | + { |
| 16 | + return add(a1, b1, -a2, b2); |
| 17 | + } |
| 18 | + |
| 19 | + private static Rational mul(int a1, int b1, int a2, int b2) |
| 20 | + { |
| 21 | + return new Rational(a1 * a2, b1 * b2); |
| 22 | + } |
| 23 | + |
| 24 | + private static Rational div(int a1, int b1, int a2, int b2) |
| 25 | + { |
| 26 | + return mul(a1, b1, b2, a2); |
| 27 | + } |
| 28 | + |
| 29 | + private static void control(int a, int b) |
| 30 | + { |
| 31 | + if (b == 0) { |
| 32 | + if (a == 0) |
| 33 | + throw new RationalException("Belirsiz", RationalExceptionStatus.INDEFINITE); |
| 34 | + |
| 35 | + throw new RationalException("Tanımsız", RationalExceptionStatus.UNDEFINED); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + private void simplify() |
| 40 | + { |
| 41 | + int a = Math.abs(m_a); |
| 42 | + int b = m_b; |
| 43 | + int min = Math.min(a, b); |
| 44 | + |
| 45 | + for (int i = min; i >= 2; --i) { |
| 46 | + if (a % i == 0 && b % i == 0) { |
| 47 | + m_a /= i; |
| 48 | + m_b /= i; |
| 49 | + break; |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + private void setSigns() |
| 55 | + { |
| 56 | + if (m_b < 0) { |
| 57 | + m_a = -m_a; |
| 58 | + m_b = -m_b; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + private void set(int a, int b) |
| 63 | + { |
| 64 | + m_a = a; |
| 65 | + if (m_a == 0) { |
| 66 | + m_b = 1; |
| 67 | + return; |
| 68 | + } |
| 69 | + m_b = b; |
| 70 | + this.setSigns(); |
| 71 | + this.simplify(); |
| 72 | + } |
| 73 | + |
| 74 | + public Rational() |
| 75 | + { |
| 76 | + m_b = 1; |
| 77 | + } |
| 78 | + |
| 79 | + public Rational(int a) |
| 80 | + { |
| 81 | + m_a = a; |
| 82 | + m_b = 1; |
| 83 | + } |
| 84 | + |
| 85 | + public Rational(int a, int b) |
| 86 | + { |
| 87 | + control(a, b); |
| 88 | + this.set(a, b); |
| 89 | + } |
| 90 | + |
| 91 | + public int getNumerator() |
| 92 | + { |
| 93 | + return m_a; |
| 94 | + } |
| 95 | + |
| 96 | + public void setNumerator(int val) |
| 97 | + { |
| 98 | + if (m_a == val) |
| 99 | + return; |
| 100 | + |
| 101 | + this.set(val, m_b); |
| 102 | + } |
| 103 | + |
| 104 | + public int getDenominator() |
| 105 | + { |
| 106 | + return m_b; |
| 107 | + } |
| 108 | + |
| 109 | + public void setDenominator(int val) |
| 110 | + { |
| 111 | + if (m_b == val) |
| 112 | + return; |
| 113 | + |
| 114 | + control(m_a, val); |
| 115 | + this.set(m_a, val); |
| 116 | + } |
| 117 | + |
| 118 | + public void setRational(int a, int b) |
| 119 | + { |
| 120 | + if (a == m_a || b == m_b) |
| 121 | + return; |
| 122 | + |
| 123 | + control(a, b); |
| 124 | + this.set(a, b); |
| 125 | + } |
| 126 | + |
| 127 | + public double getRealValue() |
| 128 | + { |
| 129 | + return (double)m_a / m_b; |
| 130 | + } |
| 131 | + |
| 132 | + //add methods |
| 133 | + public static Rational add(int val, Rational r) |
| 134 | + { |
| 135 | + return add(val, 1, r.m_a, r.m_b); |
| 136 | + } |
| 137 | + |
| 138 | + public Rational add(int val) |
| 139 | + { |
| 140 | + return add(m_a, m_b, val, 1); |
| 141 | + } |
| 142 | + |
| 143 | + public Rational add(Rational r) |
| 144 | + { |
| 145 | + return add(m_a, m_b, r.m_a, r.m_b); |
| 146 | + } |
| 147 | + |
| 148 | + //sub methods |
| 149 | + public static Rational sub(int val, Rational r) |
| 150 | + { |
| 151 | + return sub(val, 1, r.m_a, r.m_b); |
| 152 | + } |
| 153 | + |
| 154 | + public Rational sub(int val) |
| 155 | + { |
| 156 | + return sub(m_a, m_b, val, 1); |
| 157 | + } |
| 158 | + |
| 159 | + public Rational sub(Rational r) |
| 160 | + { |
| 161 | + return sub(m_a, m_b, r.m_a, r.m_b); |
| 162 | + } |
| 163 | + |
| 164 | + //multiply methods |
| 165 | + public static Rational mul(int val, Rational r) |
| 166 | + { |
| 167 | + return mul(val, 1, r.m_a, r.m_b); |
| 168 | + } |
| 169 | + |
| 170 | + public Rational mul(int val) |
| 171 | + { |
| 172 | + return mul(m_a, m_b, val, 1); |
| 173 | + } |
| 174 | + |
| 175 | + public Rational mul(Rational r) |
| 176 | + { |
| 177 | + return mul(m_a, m_b, r.m_a, r.m_b); |
| 178 | + } |
| 179 | + |
| 180 | + //div methods |
| 181 | + public static Rational div(int val, Rational r) |
| 182 | + { |
| 183 | + return div(val, 1, r.m_a, r.m_b); |
| 184 | + } |
| 185 | + |
| 186 | + public Rational div(int val) |
| 187 | + { |
| 188 | + return div(m_a, m_b, val, 1); |
| 189 | + } |
| 190 | + |
| 191 | + public Rational div(Rational r) |
| 192 | + { |
| 193 | + return div(m_a, m_b, r.m_a, r.m_b); |
| 194 | + } |
| 195 | + |
| 196 | + //inc methods |
| 197 | + public void inc() |
| 198 | + { |
| 199 | + this.inc(1); |
| 200 | + } |
| 201 | + |
| 202 | + public void inc(int val) |
| 203 | + { |
| 204 | + m_a += val * m_b; |
| 205 | + } |
| 206 | + |
| 207 | + //dec methods |
| 208 | + public void dec() |
| 209 | + { |
| 210 | + this.dec(1); |
| 211 | + } |
| 212 | + |
| 213 | + public void dec(int val) |
| 214 | + { |
| 215 | + this.inc(-val); |
| 216 | + } |
| 217 | + |
| 218 | + //pow method |
| 219 | + public Rational pow(int n) |
| 220 | + { |
| 221 | + return new Rational((int)Math.pow(m_a, n), (int)Math.pow(m_b, n)); |
| 222 | + } |
| 223 | + |
| 224 | + public String toString() |
| 225 | + { |
| 226 | + return String.format("%d / %d = %.2f", m_a, m_b, this.getRealValue()); |
| 227 | + } |
| 228 | +} |
0 commit comments