Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1.  
  2. import java.util.ArrayList;
  3. import java.util.Objects;
  4.  
  5. class TestFraction {
  6. public static void main(String[] args) {
  7. StaticFraction f1 = StaticFraction.getFraction(1, 2);
  8. //StaticFraction f2 = StaticFraction.getFraction(1, 2);
  9. // System.out.println(f1);
  10. StaticFraction.print();
  11. }
  12. }
  13.  
  14. public class StaticFraction {
  15. final int numerator, denom;
  16.  
  17. static ArrayList<StaticFraction> f;
  18. FinalFraction add = new FinalFraction(1, 1);
  19. private static int tos = 0;//стрелочка нужна для for each стат методе
  20.  
  21. StaticFraction(int a, int b) {
  22. int[] result = red(a, b);
  23. a = result[0];
  24. b = result[1];
  25. if (a * b > 0) {
  26. numerator = Math.abs(a);
  27. denom = Math.abs(b);
  28. } else if (a * b == 0) {
  29. numerator = 0;
  30. denom = 1;
  31. } else {
  32. numerator = -Math.abs(a);
  33. denom = Math.abs(b);
  34. }
  35. }
  36.  
  37. StaticFraction(String s) {
  38. int a, b;
  39. s.trim(); //удаляем пробелы
  40. int d = s.indexOf('/'); //индекс слэша
  41. if (d < 0) {
  42. a = Integer.parseInt(s);
  43. b = 1;
  44. int[] result = red(a, b);
  45. a = result[0];
  46. b = result[1];
  47. } else {
  48. String s1 = s.substring(0, d);
  49. String s2 = s.substring(d + 1);
  50. a = Integer.parseInt(s1);
  51. b = Integer.parseInt(s2);
  52. }
  53. numerator = a;
  54. denom = b;
  55. }
  56.  
  57. public int getDenom() {
  58. return denom;
  59. }
  60.  
  61. public int getNumerator() {
  62. return numerator;
  63. }
  64.  
  65. private int[] red(int a, int b) {
  66. int c = nod(a, b);
  67. int n = a / c;
  68. int d = b / c;
  69. int[] result = {n, d};
  70. return result;
  71. }
  72.  
  73. @Override
  74. public boolean equals(Object o) {
  75. if (this == o) return true;
  76. if (!(o instanceof FinalFraction)) return false;
  77. FinalFraction that = (FinalFraction) o;
  78. return getNumerator() == that.getNum() &&
  79. getDenom() == that.getDen();
  80. }
  81.  
  82. @Override
  83. public int hashCode() {
  84. return Objects.hash(getNumerator(), getDenom());
  85. }
  86.  
  87. private int nod(int a, int b) {
  88. a = Math.abs(a);
  89. b = Math.abs(b);
  90. int max = Math.max(a, b);
  91. int nod;
  92. for (int i = max; i > 1; i--)
  93. if (a % i == 0 && b % i == 0) {
  94. nod = i;
  95. return nod;
  96. }
  97. return 1;
  98. }
  99.  
  100. static StaticFraction getFraction(int a, int b) {
  101. if (tos == 1)
  102. for (int i = 0; i < f.size(); i++)
  103. if (f.get(i).getNumerator() == a && f.get(i).getDenom() == b) return f.get(i);
  104. tos = 1;
  105. StaticFraction frac = new StaticFraction(a, b);
  106. f.add(frac);
  107. return frac;
  108.  
  109.  
  110. }
  111.  
  112. @Override
  113. public String toString() {
  114. return numerator +
  115. "/" + denom;
  116. }
  117.  
  118. static void print() {
  119. for (int i = 0; i < f.size(); i++) {
  120. System.out.println(f.get(i));
  121. }
  122. }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement