Advertisement
Guest User

Untitled

a guest
Oct 17th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. public class Main {
  2.  
  3.  
  4. public static void main(String[] args) {
  5. RationalFraction r1 = new RationalFraction(1, -2);
  6. System.out.println("1 = " + r1);
  7.  
  8. RationalFraction r2 = new RationalFraction(1, 3);
  9. System.out.println("2 = " + r2);
  10.  
  11. RationalFraction r3 = r1.add(r2);
  12. System.out.println("3 = 1+2 = " + r3);
  13.  
  14. RationalFraction r4 = r3.sub(r1);
  15. System.out.println("4 = 3-1 = " + r4);
  16.  
  17. r4.sub2(r1);
  18. System.out.println("4 = 4-1 = " + r4);
  19.  
  20. r3.add2(r2);
  21. System.out.println("3 = 3+2 = " + r3);
  22.  
  23. RationalFraction r5 = r1.mult(r2);
  24. System.out.println("5 = 1*2 = " + r5);
  25.  
  26. r3.mult2(r1);
  27. System.out.println("3 = 3*1 = " + r3);
  28.  
  29. r3.div2(r1);
  30. System.out.println("3 = 3/1 = " + r3);
  31.  
  32. RationalFraction r6 = r3.div(r1);
  33. System.out.println("6 = 3/1 = " + r6);
  34.  
  35. System.out.println("1 = "+r1.value());
  36.  
  37. System.out.println("целая часть 1 = "+r1.numberPart());
  38. RationalFraction r7 = new RationalFraction(-1,2);
  39. System.out.println("7 = "+r7);
  40. System.out.println("7 == 1? "+ r1.equals(r7));
  41. }
  42. }
  43.  
  44.  
  45. public class RationalFraction {
  46. int chisl, znam;
  47.  
  48. public RationalFraction() {
  49. chisl = 0;
  50. znam = 1;
  51. }
  52.  
  53. public RationalFraction(int c, int z) {
  54. chisl = c;
  55. znam = z;
  56. reduce();
  57. }
  58.  
  59. int gcd(int a, int b) {
  60. while (a != 0) {
  61. b %= a;
  62. int t = b;
  63. b = a;
  64. a = t;
  65. }
  66. return b;
  67. }
  68.  
  69. void reduce() {
  70. int g = gcd(chisl, znam);
  71. chisl /= g;
  72. znam /= g;
  73. if (znam < 0) {
  74. znam *= -1;
  75. chisl *= -1;
  76. }
  77. }
  78.  
  79. public RationalFraction add(RationalFraction r) {
  80. int obshznam = r.znam * znam;
  81. int newchisl = chisl * r.znam + r.chisl * znam;
  82. RationalFraction ans = new RationalFraction(newchisl, obshznam);
  83. ans.reduce();
  84. return ans;
  85. }
  86.  
  87. public void add2(RationalFraction r) {
  88. chisl *= r.znam;
  89. chisl += znam * r.chisl;
  90. znam *= r.znam;
  91. reduce();
  92. }
  93.  
  94. public RationalFraction sub(RationalFraction r) {
  95. int obshznam = r.znam * znam;
  96. int newchisl = chisl * r.znam - r.chisl * znam;
  97. RationalFraction ans = new RationalFraction(newchisl, obshznam);
  98. ans.reduce();
  99. return ans;
  100. }
  101.  
  102. public void sub2(RationalFraction r) {
  103. chisl *= r.znam;
  104. chisl -= znam * r.chisl;
  105. znam *= r.znam;
  106. reduce();
  107. }
  108.  
  109. public RationalFraction mult(RationalFraction r) {
  110. RationalFraction ans = new RationalFraction(chisl * r.chisl, znam * r.znam);
  111. ans.reduce();
  112. return ans;
  113. }
  114.  
  115. public void mult2(RationalFraction r) {
  116. chisl *= r.chisl;
  117. znam *= r.znam;
  118. reduce();
  119. }
  120.  
  121. public RationalFraction div(RationalFraction r) {
  122. return mult(new RationalFraction(r.znam, r.chisl));
  123. }
  124.  
  125. public void div2(RationalFraction r) {
  126. mult2(new RationalFraction(r.znam, r.chisl));
  127. }
  128.  
  129.  
  130. public String toString() {
  131. return chisl + "/" + znam;
  132. }
  133.  
  134. public double value() {
  135. return 1.0 * chisl / znam;
  136. }
  137.  
  138. public boolean equals(RationalFraction r) {
  139. return r.chisl == chisl && r.znam == znam;
  140. }
  141.  
  142. public int numberPart() {
  143. return chisl / znam;
  144. }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement