0% found this document useful (0 votes)
22 views

Calendar C

This C++ program contains functions to calculate calendar dates and determine the day of the week for a given date. It includes functions to check if a year is a leap year, determine the number of days in a given month and year, and calculate the day of the week that the 13th of a month falls on. The main function uses these functions to calculate and output the number of times the 13th of each month from 1900 to 2099 falls on a Friday.

Uploaded by

charlesvc
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Calendar C

This C++ program contains functions to calculate calendar dates and determine the day of the week for a given date. It includes functions to check if a year is a leap year, determine the number of days in a given month and year, and calculate the day of the week that the 13th of a month falls on. The main function uses these functions to calculate and output the number of times the 13th of each month from 1900 to 2099 falls on a Friday.

Uploaded by

charlesvc
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

http://www.dzone.

com/snippets/calendar-calculation-c

view source
print?
01.#include <iostream>
02.#include <fstream>
03.#include <string>
04.
05.using namespace std;
06.
07.bool isLeapYear(int year) {
08.

bool retval = false;

09.

if (year % 100 == 0) {

10.

if (year % 400 == 0) {

11.

retval = true;

12.
13.

}
} else if (year % 4 == 0) {

14.

retval = true;

15.

16.
17.}

return retval;

18.
19.int daysInMonth(int month, int year) {
20.

switch (++month) {

21.

case 4:

22.

case 6:

23.

case 9:

24.

case 11:

25.

return 30;

26.

break;
case 2:

27.

if (isLeapYear(year)) {

28.

return 29;

29.

} else return 28;

30.
31.

break;

32.

default:

33.

return 31;

34.

break;

35.
36.}

37.
38.int getFirstDayOfMonth(int month, int year, int day) {

1 of 3

39.

int retval = -1;

40.

int numdays;

41.

if (month == 0) {

08/18/2012 05:38 PM

http://www.dzone.com/snippets/calendar-calculation-c

42.
43.

numdays = daysInMonth(11, year-1);


} else {

44.

numdays = daysInMonth(month-1, year);

45.

46.

int offset = numdays % 7;

47.

if (month == 0 && year == 1900) {

48.
49.

retval = 1;
} else {

50.
51.

retval = (day + offset) % 7;


}

52.
53.
54.}

return retval;

55.
56.int getThirteenth(int firstday) {
57.
58.}

return ((13 - firstday) % 7);

59.
60.int main() {
61.

ofstream fout ("friday.out");

62.

ifstream fin ("friday.in");

63.
64.

int numYears;

65.

fin >> numYears;

66.

int year = 1900;

67.

int firstDay[numYears][12];

68.

int numThirteenths[7];

69.

int day;

70.
71.

for (int z = 0; z < 7; ++z) numThirteenths[z] = 0;

72.
73.
74.

for (int x = 0; x < numYears; ++x) {


for (int month = 0; month < 12; month++) {

75.

if (year == 1900 && month == 0) {

76.

firstDay[x][month] = 1;

77.
78.

} else if (month == 0) {

79.
firstDay[x-1][11]);
80.

firstDay[x][month] = getFirstDayOfMonth(month, year,


} else {

81.
firstDay[x][month-1]);

2 of 3

firstDay[x][month] = getFirstDayOfMonth(month, year,

82.

83.

++numThirteenths[((firstDay[x][month] + 5) % 7)];

08/18/2012 05:38 PM

http://www.dzone.com/snippets/calendar-calculation-c

84.

85.

++year;

86.

87.
88.

fout << numThirteenths[6] << " ";

89.

for (int y = 0; y < 5; ++y) fout << numThirteenths[y] << " ";

90.

fout << numThirteenths[5] << endl;

91.
92.
93.}

3 of 3

return 0;

08/18/2012 05:38 PM

You might also like