Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Countday - countday.c

countday.c

Caricato da: Netarrow
Scarica il programma completo

  1. /***************************************************************************
  2.  *   Copyright (C) 2006 by Matteo "netarrow" Tomasulo                      *
  3.  *   netarrow@gmail.com                                                    *
  4.  *                                                                         *
  5.  *   This program is free software; you can redistribute it and/or modify  *
  6.  *   it under the terms of the GNU General Public License as published by  *
  7.  *   the Free Software Foundation; either version 2 of the License, or     *
  8.  *   (at your option) any later version.                                   *
  9.  *                                                                         *
  10.  *   This program is distributed in the hope that it will be useful,       *
  11.  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
  12.  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
  13.  *   GNU General Public License for more details.                          *
  14.  *                                                                         *
  15.  *   You should have received a copy of the GNU General Public License     *
  16.  *   along with this program; if not, write to the                         *
  17.  *   Free Software Foundation, Inc.,                                       *
  18.  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
  19.  ***************************************************************************/
  20.  
  21.  
  22.  
  23.  
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26.  
  27. /*
  28.   Se sta compilando su windows, il comando per pulire
  29.   lo schermo è cls.
  30. */
  31. #ifdef _WIN32
  32. #define CLEAR "cls"
  33. #else
  34. #define CLEAR "clear"
  35. #endif
  36.  
  37. /*
  38.  Struttura che rappresenta la data.
  39. */
  40. typedef struct Data {
  41.   long anno;
  42.   long mese;
  43.   long giorno;
  44. } Data;
  45.  
  46. /*
  47.  Questa funziona calcola quanti giorni sono passati dal 4000 a.C
  48.  alla data immessa.
  49. */
  50. long getDay(Data data) {
  51.    long a, b, giorni_passati;
  52.    if(data.mese < 3) {
  53.       data.anno -= 1;
  54.       data.mese += 12;
  55.    }
  56.  
  57.    a = (long) (data.anno / 100);
  58.    b = 2 - a + ( (long) (a / 4));
  59.  
  60.    giorni_passati = (long) (365.25*(data.anno + 4716)) + (30.6001*(data.mese + 1)) + data.giorno + b -  1524;
  61.  
  62.   return giorni_passati;
  63. }
  64.  
  65. int main(int argc, char *argv[])
  66. {
  67.   Data arrivo, partenza;
  68.   signed long differenza;
  69.  
  70.   system(CLEAR);  
  71.  
  72.   printf("Data di partenza: \n\n");
  73.  
  74.   printf("Inserire anno: ");
  75.   scanf("%d", &partenza.anno);
  76.   printf("Inserire mese: ");
  77.   scanf("%d", &partenza.mese);
  78.   printf("Inserire giorno: ");
  79.   scanf("%d", &partenza.giorno);
  80.  
  81.   system(CLEAR);
  82.  
  83.   printf("Data di arrivo: \n\n");
  84.  
  85.   printf("Inserire anno: ");
  86.   scanf("%d", &arrivo.anno);
  87.   printf("Inserire mese: ");
  88.   scanf("%d", &arrivo.mese);
  89.   printf("Inserire giorno: ");
  90.   scanf("%d", &arrivo.giorno);
  91.  
  92.   differenza = getDay(arrivo) - getDay(partenza);
  93.  
  94.   if(differenza == 0) {
  95.     printf("%s\n", "La data è la stessa");
  96.  
  97.         /*
  98.      Su windows è meglio mettere una pausa per bloccare l'output
  99.     */
  100.     #ifdef _WIN32
  101.       system("pause");
  102.     #endif
  103.  
  104.     return EXIT_SUCCESS;
  105.   }
  106.  
  107.   /*
  108.    Facciamo controlli sul risultato per adattare il testo da stampare.
  109.   */
  110.   printf("%s %d %s\n", abs(differenza) == 1 ? differenza < 0 ? \
  111.   "E' passato":"Manca":differenza < 0 ? "Sono passati":"Mancano", abs(differenza), \
  112.   abs(differenza) == 1 ? "giorno":"giorni");
  113.  
  114. /*
  115.  Come per il caso di prima, meglio mettere una pausa
  116. */
  117. #ifdef _WIN32
  118.   system("pause");
  119. #endif
  120.  
  121.   return EXIT_SUCCESS;
  122. }