Advertisement
fcamuso

Untitled

Oct 25th, 2020 (edited)
479
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 KB | None | 0 0
  1. using System;
  2. using System.Drawing;
  3.  
  4. namespace overload_op_funzioni
  5. {
  6. class Frazione
  7. {
  8. public int Num { get; set; } = 0;
  9.  
  10. private int den;
  11. public int Den
  12. {
  13. get => den;
  14. set
  15. {
  16. if (value == 0) throw new ArgumentException("Denominatore zero");
  17. den = value;
  18. }
  19. }
  20. public Frazione() { Den = 1; }
  21. public Frazione(int num, int den)
  22. { Num = num; Den = den; }
  23.  
  24. public Frazione(int n) : this(n, 1) { }
  25. public Frazione(string s)
  26. {
  27.  
  28. if (string.IsNullOrEmpty(s)) return;
  29.  
  30. //if (s == null || s == "") return;
  31.  
  32. string[] dati = s.Split('/'); //da "25ert/7" -> ["25", "7"]
  33.  
  34. try
  35. {
  36. Num = int.Parse(dati[0]);
  37. }
  38. catch (FormatException e)
  39. {
  40. throw new FormatException("Stringa non nel formato 'numero/numero' o 'numero'");
  41. }
  42.  
  43. if (dati.Length == 1) // "25"
  44. Den = 1;
  45. else
  46. {
  47. try
  48. {
  49. Den = int.Parse(dati[1]);
  50. }
  51. catch (FormatException e)
  52. {
  53. throw new FormatException("Stringa non nel formato 'numero/numero' o 'numero'");
  54. }
  55. }
  56. }
  57.  
  58. public override string ToString() { return $"{Num}/{Den}"; }
  59.  
  60. public static Frazione operator +(Frazione f1, Frazione f2)
  61. {
  62.  
  63. return new Frazione(f1.Num * f2.Den + f2.Num * f1.Den, f1.Den * f2.Den);
  64.  
  65. }
  66.  
  67. //public static Frazione operator +(Frazione f1, int n)
  68. //{
  69. // return f1 + new Frazione(n, 1);
  70. //}
  71.  
  72. public static implicit operator Frazione(int n) => new Frazione(n);
  73.  
  74. public static explicit operator Frazione(string s) => new Frazione(s);
  75.  
  76. public static bool operator ==(Frazione f1, Frazione f2)
  77. {
  78. if (f1 is null || f2 is null) return false;
  79. return f1.Num * f2.Den == f2.Num * f1.Den;
  80. }
  81.  
  82. public static bool operator !=(Frazione f1, Frazione f2)
  83. {
  84. //return !(f1 == f2);
  85. return !(f1.Num * f2.Den == f2.Num * f1.Den);
  86. }
  87.  
  88. }
  89.  
  90. class Giocatore
  91. {
  92. public string Cognome { get; set; }
  93. public string Nome { get; set; }
  94.  
  95. public string Matricola { get; set; }
  96.  
  97. public Giocatore(string c, string n, string m)
  98. {
  99. Cognome = c; Nome = n; Matricola = m;
  100. }
  101. }
  102.  
  103. class Partita
  104. {
  105. public Giocatore G1 { get; set;}
  106. public Giocatore G2 { get; set; }
  107.  
  108. public Partita(Giocatore g1, Giocatore g2)
  109. {
  110. G1 = g1; G2 = g2;
  111. }
  112.  
  113. public static bool operator ==(Partita p1, Partita p2) =>
  114. (p1.G1 == p2.G1 && p1.G2 == p2.G2) || (p1.G1 == p2.G2 && p1.G2 == p2.G1);
  115. public static bool operator !=(Partita p1, Partita p2) => !(p1== p2);
  116.  
  117. public override bool Equals(object altra)
  118. {
  119. if (altra is null || altra.GetType() != typeof(Partita)) return false;
  120.  
  121. //meglio
  122. //if (altra is null || !(altra is Partita)) return false;
  123.  
  124. return this.G1.Matricola == ((Partita) altra).G1.Matricola &&
  125. this.G2.Matricola == ((Partita) altra).G2.Matricola;
  126. }
  127.  
  128. }
  129.  
  130.  
  131. class Program
  132. {
  133. static void Main(string[] args)
  134. {
  135. Partita p1 =
  136. new Partita(new Giocatore("Rossi", "Mario", "abc"), new Giocatore("Verdi", "Sandro", "xyz"));
  137.  
  138. Partita p2 =
  139. new Partita(new Giocatore("Rossi", "Mario", "abc"), new Giocatore("Verdi", "Sandro", "xyz"));
  140.  
  141. Console.WriteLine(p1 == p2);
  142. Console.WriteLine(p1.Equals(p2));
  143.  
  144.  
  145. Frazione f1 = new Frazione(3, 2); // 3/2
  146. Frazione f2 = new Frazione("4/5"); // 4/5
  147.  
  148. if (f1 == null) Console.WriteLine("null");
  149.  
  150.  
  151. Frazione f3 = f1 + f2;
  152. Frazione f4 = 6; // = new Frazione(6)
  153.  
  154. //Console.WriteLine(3 + f1);
  155. //Console.WriteLine(f1 + 3);
  156.  
  157. Frazione f5 = (Frazione)"4/5";
  158. //Console.WriteLine(f1 + "paperino");
  159.  
  160. f1 += f2; //f1 = f1 + f2
  161. Console.WriteLine(f1);
  162.  
  163. // == !=
  164. //< > <= >=
  165. //Console.WriteLine(f1 == f2);
  166. //Console.WriteLine(f1 != f2);
  167. //Console.WriteLine( (Frazione)"3/7" == (Frazione)"6/14") ;
  168. }
  169. }
  170. }
  171.  
  172.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement