Advertisement
maxrusmos

Untitled

Mar 7th, 2020
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace FractionOp {
  7. class Fraction {
  8. public double c = 0;
  9. public double z = 0;
  10.  
  11. public Fraction(int c, int z) {
  12. this.c = c;
  13. this.z = z;
  14.  
  15. }
  16.  
  17. public override string ToString() {
  18. return "(" + c.ToString() + "/" + z.ToString() + ")";
  19. }
  20.  
  21. public static Fraction operator +(Fraction a, Fraction b) {
  22. Fraction t = new Fraction(1, 1);
  23. t.c = (a.c * b.z + a.z * b.c);
  24. t.z = a.z * b.z;
  25. Fraction.SetFormat(t);
  26. return t;
  27. }
  28.  
  29. public static Fraction operator -(Fraction a, Fraction b) {
  30. Fraction t = new Fraction(1, 1);
  31. t.c = (a.c * b.z - a.z * b.c);
  32. t.z = a.z * b.z;
  33. Fraction.SetFormat(t);
  34. return t;
  35.  
  36. }
  37.  
  38. public static Fraction operator *(Fraction a, Fraction b) {
  39. Fraction t = new Fraction(1, 1);
  40. t.c = (a.c * b.c);
  41. t.z = a.z * b.z;
  42. Fraction.SetFormat(t);
  43. return t;
  44.  
  45. }
  46.  
  47. public static Fraction operator /(Fraction a, Fraction b) {
  48. Fraction t = new Fraction(1, 1);
  49. t.c = (a.c / b.c);
  50. t.z = a.z / b.z;
  51. Fraction.SetFormat(t);
  52. return t;
  53. }
  54.  
  55. public static Fraction SetFormat(Fraction a) {
  56. double max = 0;
  57. if (a.c > a.z) {
  58. max = Math.Abs(a.z);
  59. } else {
  60. max = Math.Abs(a.c);
  61. }
  62. for (double i = max; i >= 2; i--) {
  63. if ((a.c % i == 0) & (a.z % i == 0)) {
  64. a.c = a.c / i;
  65. a.z = a.z / i;
  66. }
  67. }
  68. if ((a.z < 0)) {
  69. a.c = -1 * (a.c);
  70. a.z = Math.Abs(a.z);
  71. }
  72. return (a);
  73. }
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement