Advertisement
fcamuso

Untitled

Oct 18th, 2020 (edited)
502
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.55 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.       return f1.Num * f2.Den == f2.Num * f1.Den;
  79.     }
  80.  
  81.     public static bool operator !=(Frazione f1, Frazione f2)
  82.     {
  83.       //return !(f1 == f2);
  84.       return !(f1.Num * f2.Den == f2.Num * f1.Den);
  85.     }
  86.   }
  87.  
  88.   class Giocatore
  89.   {
  90.     public string Cognome { get; set; }
  91.     public string Nome { get; set; }
  92.  
  93.     public string Matricola { get; set; }
  94.  
  95.     public Giocatore(string c, string n, string m)
  96.     {
  97.       Cognome = c; Nome = n; Matricola = m;
  98.     }
  99.   }
  100.  
  101.   class Partita
  102.   {
  103.     public Giocatore G1 { get; set;}
  104.     public Giocatore G2 { get; set; }
  105.  
  106.     public Partita(Giocatore g1, Giocatore g2)
  107.     {
  108.       G1 = g1; G2 = g2;
  109.     }
  110.  
  111.     public static bool operator ==(Partita p1, Partita p2) => p1.G1 == p2.G1 && p1.G2 == p2.G2;
  112.     public static bool operator !=(Partita p1, Partita p2) => !(p1== p2);  
  113.   }
  114.  
  115.   class Program
  116.   {
  117.     static void Main(string[] args)
  118.     {
  119.       Partita p1 =
  120.         new Partita(new Giocatore("Rossi", "Mario", "abc"), new Giocatore("Verdi", "Sandro", "xyz"));
  121.  
  122.       Partita p2 =
  123.         new Partita(new Giocatore("Rossi", "Mario", "abc"), new Giocatore("Verdi", "Sandro", "xyz"));
  124.  
  125.       Console.WriteLine(p1 == p2);
  126.  
  127.       Frazione f1 = new Frazione(3, 2);    //  3/2
  128.       Frazione f2 = new Frazione("4/5");  //  4/5
  129.  
  130.        
  131.       Frazione f3 = f1 + f2;
  132.       Frazione f4 = 6; // = new Frazione(6)
  133.        
  134.       //Console.WriteLine(3 + f1);
  135.       //Console.WriteLine(f1 + 3);
  136.  
  137.       Frazione f5 = (Frazione)"4/5";
  138.       //Console.WriteLine(f1 + "paperino");
  139.  
  140.       f1 += f2; //f1 = f1 + f2
  141.       Console.WriteLine(f1);
  142.  
  143.       // ==  !=
  144.       //<  >    <=  >=
  145.       //Console.WriteLine(f1 == f2);
  146.       //Console.WriteLine(f1 != f2);
  147.       //Console.WriteLine( (Frazione)"3/7" == (Frazione)"6/14") ;
  148.     }
  149.   }
  150.   }
  151.  
  152.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement