Advertisement
fcamuso

Untitled

Jul 30th, 2020
756
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.34 KB | None | 0 0
  1. using System;
  2.  
  3. namespace oop1
  4. {
  5.   //class Data
  6.   //{
  7.   //  //stato interno (variabili)
  8.   //  int gg = 1, mm=1, aa=1;  //field (campo)
  9.  
  10.   //  //metodi (funzioni)
  11.   //  static bool Bisestile(int anno)
  12.   //  {
  13.   //    return anno % 400 == 0 || (anno % 4 == 0 && anno % 100 != 0);
  14.   //  }
  15.  
  16.   //  static int GiorniAnno(int anno)
  17.   //  { return 365 + (Bisestile(anno) ? 1 : 0); }
  18.  
  19.   //  static int GiorniMese(int mese, int anno) //parametri formali
  20.   //  {
  21.   //    switch (mese)
  22.   //    {
  23.   //      case 4:
  24.   //      case 6:
  25.   //      case 9:
  26.   //      case 11:
  27.   //        return 30;
  28.  
  29.   //      case 2:
  30.   //        return 28 + (anno % 400 == 0 || (anno % 4 == 0 && anno % 100 != 0) ? 1 : 0);
  31.  
  32.   //      default:
  33.   //        return 31;
  34.   //    }
  35.   //  }
  36.  
  37.   //  static bool DataCorretta(int g, int m, int a)
  38.   //  {
  39.   //    return a > 0 && m > 0 && m < 13 && g > 0 && g <= GiorniMese(m, a);
  40.   //  }
  41.  
  42.   //  static int DifferenzaDateInGiorni(int g1, int m1, int a1, int g2, int m2, int a2)
  43.   //  {
  44.   //    if (m1 == m2 && a1 == a2) return Math.Abs(g2 - g1);
  45.  
  46.   //    //se non in ordine cronologico, inverti le date
  47.   //    if (a1 > a2 || (a1 == a2 && m1 > m2))
  48.   //    {
  49.   //      Scambia(ref g1, ref g2); Scambia(ref m1, ref m2); Scambia(ref a1, ref a2);
  50.   //    }
  51.  
  52.   //    int differenza = GiorniMese(m1, a1) - g1; //a fine mese
  53.  
  54.   //    //sommo i giorni dei mesi intermedi
  55.   //    if (a1 == a2)
  56.   //      for (int mese = m1 + 1; mese < m2; mese++) differenza += GiorniMese(mese, a1);
  57.   //    else
  58.   //    {
  59.  
  60.   //      //mesi anno di partenza fino a fine anno
  61.   //      for (int mese = m1 + 1; mese <= 12; mese++) differenza += GiorniMese(mese, a1);
  62.  
  63.   //      //anni intermedi
  64.   //      for (int anno = a1 + 1; anno < a2; anno++) differenza += GiorniAnno(anno);
  65.  
  66.   //      //mesi anno finale escluso ultimo
  67.   //      for (int mese = 1; mese < m2; mese++) differenza += GiorniMese(mese, a2);
  68.   //    }
  69.  
  70.   //    //sommo i giorni restanti
  71.   //    return differenza + g2;
  72.   //  }
  73.  
  74.   //  static void Scambia(ref int a, ref int b)
  75.   //  {
  76.   //    int temp = a; a = b; b = temp;
  77.   //  }
  78.   //}
  79.  
  80.   class Data
  81.   {// membri interni
  82.  
  83.     //field
  84.     private int g = 1;
  85.     private int m = 1;
  86.     private int a = 1;
  87.  
  88.     //metodi
  89.  
  90.     public int GetGiorno() //getter
  91.     {
  92.       return g;
  93.     }
  94.  
  95.     public bool SetGiorno(int nuovo) //setter
  96.     {
  97.       if (nuovo > GiorniMese())
  98.         return false;
  99.       else
  100.       {
  101.         g = nuovo;
  102.         return true;
  103.       }
  104.     }
  105.  
  106.     int GiorniMese() //parametri formali
  107.     {
  108.       switch (m)
  109.       {
  110.         case 4:
  111.         case 6:
  112.         case 9:
  113.         case 11:
  114.           return 30;
  115.  
  116.         case 2:
  117.           return 28 + (a % 400 == 0 || (a % 4 == 0 && a % 100 != 0) ? 1 : 0);
  118.  
  119.         default:
  120.           return 31;
  121.       }
  122.     }
  123.  
  124.   }
  125.  
  126.   class Program
  127.   {
  128.  
  129.     static bool Bisestile(int anno)
  130.     {
  131.       return anno % 400 == 0 || (anno % 4 == 0 && anno % 100 != 0);
  132.     }
  133.  
  134.     static public int DifferenzaGiorni3(int g1, int m1, int a1, int g2, int m2, int a2)
  135.     {
  136.       int[] days = new int[] { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
  137.       int y, n1, n2;
  138.       y = a1 - 1;
  139.       n1 = y * 365 + y / 4 - y / 100 + y / 400 + days[m1 - 1] + g1 + (Bisestile(a1) && m1 > 2 ? 0 : -1);
  140.       y = a2 - 1;
  141.       n2 = y * 365 + y / 4 - y / 100 + y / 400 + days[m2 - 1] + g2 + (Bisestile(a2) && m2 > 2 ? 0 : -1);
  142.       return Math.Abs(n2 - n1);
  143.     }
  144.  
  145.     static public int DifferenzaGiorni4(int g1, int m1, int a1, int g2, int m2, int a2)
  146.     {
  147.       int[] days = new int[] { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
  148.       int y, n1, n2;
  149.       y = a1 - 1;
  150.       n1 = y * 365 + y / 4 - y / 100 + y / 400 + days[m1 - 1] + g1 + (Bisestile(a1) && m1 > 2 ? 0 : -1);
  151.       y = a2 - 1;
  152.       n2 = y * 365 + y / 4 - y / 100 + y / 400 + days[m2 - 1] + g2 + (Bisestile(a2) && m2 > 2 ? 0 : -1);
  153.       return Math.Abs(n2 - n1);
  154.     }
  155.  
  156.  
  157.     static void Main(string[] args)
  158.     {
  159.       Data d1 = new Data();
  160.  
  161.       Console.WriteLine(d1.GetGiorno());
  162.  
  163.       if (d1.SetGiorno(45))
  164.         Console.WriteLine("Data modificata!");
  165.       else
  166.         Console.WriteLine("Giorno non valido, data NON modificata ...");
  167.  
  168.       d1.SetGiorno(12);
  169.       //d1.GetGiorno = 12;
  170.  
  171.       int n = 34 + d1.GetGiorno() * 2;
  172.  
  173.     }
  174.   }
  175. }
  176.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement