Calcolo tra due date in Cpp

#include <iostream>
#include <cassert>

int firstDay; //Giorno della prima data
int firstMonth; //Mese della prima data
int firstYear; //Anno prima data
int secDay; //Giorno seconda data
int secMonth; //mese seconda data
int secYear; //anno seconda data

int month[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; /* mesi dell'anno
* in giorni
*/
int passedDayF; //giorni trascorsi nella prima data
int passedDayS; //giorni trascorsi seconda data
int remainingF; //giorni rimanenti nella prima data
int remainingS; //giorni rimanenti seconda data
int totDays; //risultato differenza fra le due date
int passedYear; //anni trascorsi dalla prima alla seconda data
int bisYear(0); //quanti anni bisestili ?


int main(){
std::cout << "Inserisci primo giorno: ";
std::cin >> firstDay;

assert(firstDay > 0); /*<------------ NOTA */
assert(firstDay <= 31); /*<---------- NOTA */

std::cout << "Inserisci primo mese: ";
std::cin >> firstMonth;

assert(firstMonth > 0); /*<---------- NOTA */
assert(firstMonth <= 12); /*<-------- NOTA */

std::cout << "Inserisci primo anno: ";
std::cin >> firstYear;

assert(firstYear > 0); /*<----------- NOTA */

std::cout << "Inserisci secondo giorno: ";
std::cin >> secDay;

assert(secDay > 0); /*<-------------- NOTA */
assert(secDay <= 31); /*<------------ NOTA */

std::cout << "Inserisci secondo mese: ";
std::cin >> secMonth;

assert(secMonth > 0); /*<------------ NOTA */
assert(secMonth <= 12); /*<---------- NOTA */

std::cout << "Inserisci secondo anno: ";
std::cin >> secYear;

assert(secYear > 0); /*<------------ NOTA */

assert(firstYear <= secYear); /*<---- NOTA */

/*>>>>>> CONTROLLO ANNI BISESTILI <<<<<<<*/
std::cout << "Considerando che: \n";

for (int i=firstYear; i<=secYear; i++){
if (((i % 4) == 0 && (i % 100) != 0) || (i % 400) == 0) {
std::cout << "Il " << i << " e' un anno bisestile!\n";
bisYear += 1;
} else {
std::cout << "Il " << i << " non e' un anno bisestile!\n";
}
}


/*>>>>>> GG TRASCORSI NELLA PRIMA DATA <<<<<<<<*/
for (int i=0; i < firstMonth; i++){
passedDayF += month[i];
}

passedDayF += firstDay;

std::cout << "\nSono passati: " << passedDayF << " gg nella prima data\n";

/*>>>>>> GG TRASCORSI SECONDA DATA <<<<<<<*/
for (int i=0; i<secMonth; i++) {
passedDayS += month[i];
}

passedDayS += secDay;

std::cout << "Sono passati: " << passedDayS << " gg nella seconda data!\n\n";

/*>>>>>>> CALCOLO GIORNI RESTANTI X COMPLETARE L'ANNO <<<<<<<<*/
remainingF = 365 - passedDayF; //prima data
remainingS = 365 - passedDayS; //seconda data

std::cout << "Rimangono : " << remainingF << " gg per completare l'anno (prima data)\n";
std::cout << "Rimangono : " << remainingS << " gg per completare l'anno (seconda data)\n\n";

passedYear = secYear - firstYear;

std::cout << "Tra le due date sono passati: " << passedYear << " anni!\n\n";

/* ---> Secondo gli anni trascorsi adatto il risultato <---- */
if (passedYear == 0)
totDays = ((365 + bisYear) - passedDayF) - remainingS;

if (passedYear == 1)
totDays = ((730 + bisYear) - passedDayF) - remainingS;

if (passedYear >= 2)
totDays = ((((passedYear+1) * 365) + bisYear) - passedDayF) - remainingS;

std::cout << "La differenza in gg tra le date e' di: " << totDays << " gg\n\n";
system ("pause");
return (0);
}

Commenti

Post popolari in questo blog

Simulazioni di reti (con Cisco Packet Tracer)

Esercizi sulla rappresentazione della virgola mobile IEEE 754 (Floating Point)