Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Drawing;
- namespace overload_op_funzioni
- {
- class Frazione
- {
- public int Num { get; set; } = 0;
- private int den;
- public int Den
- {
- get => den;
- set
- {
- if (value == 0) throw new ArgumentException("Denominatore zero");
- den = value;
- }
- }
- public Frazione() { }
- public Frazione(int num, int den)
- { Num = num; Den = den; }
- public Frazione(int n) : this(n, 1) { }
- public Frazione(string s) {
- if (string.IsNullOrEmpty(s)) return;
- //if (s == null || s == "") return;
- string[] dati = s.Split('/'); //da "25ert/7" -> ["25", "7"]
- try
- {
- Num = int.Parse(dati[0]);
- }
- catch (FormatException e)
- {
- throw new FormatException("Stringa non nel formato 'numero/numero' o 'numero'");
- }
- if (dati.Length == 1) // "25"
- Den = 1;
- else
- {
- try
- {
- Den = int.Parse(dati[1]);
- }
- catch (FormatException e)
- {
- throw new FormatException("Stringa non nel formato 'numero/numero' o 'numero'");
- }
- }
- }
- public string ToString() { return $"{Num}/{Den}"; }
- }
- class Program
- {
- static void Main(string[] args)
- {
- int n1 = 5, n2=7;
- int n3 = n1 + n2;
- n3++;
- Frazione f1 = new Frazione(1,3); // 1/3
- Frazione f2 = new Frazione("25/4yy"); //25/1
- //f1 += new Frazione("1/3");
- //Frazione f3 = f1 + f2;
- Console.WriteLine($"{f1.ToString()} {f2.ToString()}");
- string s = null;
- //Frazione f3 = new Frazione(s);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement