Created
October 21, 2017 15:37
-
-
Save hyperion0201/169ff2159855f03031da857941953579 to your computer and use it in GitHub Desktop.
1760311_BTVN_Tuan06 - Bai06
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Ham.h" | |
int CheckLeap(int year){ | |
int leap; | |
if (year % 4 == 0 || (year % 4 == 0 && year % 100 != 0)) leap = 1; | |
else leap = 0; | |
return leap; | |
} | |
int DayInMonth(int month, int year){ | |
int nday; | |
switch (month) | |
{ | |
case 1: case 3: case 5: case 7:case 8: case 10: case 12: nday = 31; break; | |
case 4:case 6: case 9: case 11: nday = 30; | |
case 2: if (CheckLeap(year) == 1) nday = 29; | |
else nday = 28; | |
default: | |
break; | |
} | |
return nday; | |
} | |
int inSeason(int month){ | |
int season; | |
switch (month) | |
{ | |
case 1: case 2: case 3: season = 1; break; | |
case 4: case 5: case 6: season = 2; break; | |
case 7: case 8: case 9: season = 3; break; | |
case 10: case 11: case 12: season = 4; break; | |
default: | |
break; | |
} | |
return season; | |
} | |
int DistanceFromBegin(int day, int month, int year){ | |
int distance = 0; | |
for (int i = 1; i < month; i++){ | |
if (month>1) | |
distance += DayInMonth(i, year); | |
else return day; | |
} | |
return distance + day; | |
} | |
void NextDay(int day, int month, int year, int &nextday, int &nextmonth, int &nextyear){ | |
if (day == 28 || day == 29 || day == 30 || day == 31) { | |
nextday = 1; | |
if (month == 12){ | |
nextyear = year+1; | |
nextmonth = 1; | |
} | |
else { | |
nextmonth = month + 1; | |
nextyear = year; | |
} | |
} | |
else { | |
nextday = day+1; | |
nextmonth = month; | |
nextyear = year; | |
} | |
} | |
void PreviousDay(int day, int month, int year, int &preday, int &premonth, int &preyear){ | |
if (day == 1){ | |
if (month == 1){ | |
premonth = 12; | |
preyear = year-1; | |
preday = DayInMonth(premonth, preyear); | |
} | |
else { | |
premonth = month-1; | |
preyear = year; | |
preday = DayInMonth(premonth, preyear); | |
} | |
} | |
else { | |
premonth = month; | |
preyear = year; | |
preday = day-1; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef BAI06_ | |
#define BAI06_ | |
#include <stdio.h> | |
#include <conio.h> | |
int CheckLeap(int year); | |
int DayInMonth(int month, int year); | |
int inSeason(int month); | |
int DistanceFromBegin(int day, int month, int year); | |
void NextDay(int day, int month, int year, int &nextday, int &nextmonth, int &nextyear); | |
void PreviousDay(int day, int month, int year, int &preday, int &premonth, int &preyear); | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Ham.h" | |
void main(){ | |
int day, month, year, nextday, nextmonth, nextyear, preday, premonth, preyear; | |
printf("Input day :"); | |
scanf_s("%d", &day); | |
printf("Input month : "); | |
scanf_s("%d", &month); | |
printf("Input year : "); | |
scanf_s("%d", &year); | |
if (CheckLeap(year) == 1) printf("Leap year!"); | |
else printf("Not a leap year!"); | |
printf("\nThe day in month is : %d \n", DayInMonth(month, year)); | |
printf("\n This month belongs to season %d\n", inSeason(month)); | |
printf("\n Distance from begin is : %d\n", DistanceFromBegin(day, month, year)); | |
NextDay(day, month, year, nextday, nextmonth, nextyear); | |
printf("The next day is : %d/%d/%d\n", nextday, nextmonth, nextyear); | |
PreviousDay(day, month, year, preday, premonth, preyear); | |
printf("The previous day is : %d/%d/%d\n", preday, premonth, preyear); | |
_getch(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment