-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhorizontalcalendar.cpp
124 lines (99 loc) · 2.77 KB
/
horizontalcalendar.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* The following code prints the calendar of the year input by the user */
#include<bits/stdc++.h>
#define f(i,a,n) for(int i=a ;i<n ;i++)
using namespace std;
/* The integer function dayofweek returns a number corresponding to the day at which 1st January of input year falls i.e. day at which 01/01/.... occurs
Day Value of n
Monday 1
Tuesday 2
Wednesday 3
Thursday 4
Friday 5
Saturday 6
Sunday 0 */
int dayofweek(int y)
{
int d=1,m=1;
static int t[] = { 0, 3, 2, 5, 0, 3,
5, 1, 4, 6, 2, 4 };
y -= m < 3; //If m<3, return y-1, else return y
//Using Tomohiko Sakamoto’s Algorithm
return ( y + y / 4 - y / 100 + y / 400 + t[m - 1] + d) % 7;
}
//The boolean function checkYear is used to check is the input year is leap year or not. If it is a leap year, true is returned, else false is returned.
bool checkYear(int year)
{
if (year % 400 == 0)
return true;
if (year % 100 == 0)
return false;
if (year % 4 == 0)
return true;
return false;
}
int main()
{
int y;
cout<<"Enter year : ";
cin>>y;
int n = dayofweek(y);
// cout<<n<<endl<<endl;
/* The following lines modify the value of n to give the following result
Day Value of n
Monday 1
Tuesday 2
Wednesday 3
Thursday 4
Friday 5
Saturday 6
Sunday 7 */
n%=7;
if(!n)
n=7;
int m=1,d=31,febday;
//Set containing all months of the year
string month[12]={"January", "February" ,"March" , "April" , "May" ,"June" ,"July" , "August" ,"September" ,"October" ,"November" ,"December"};
int i=1;
// If input year is leap year, number of days in Februrary are 29, else 28.
if(checkYear(y))
febday=29;
else
febday=28;
while(i<=12)
{
//Print Current Month of the year
cout<<month[i-1]<<" "<<y<<endl;
//Print all days of the week
int j=1;
cout<<"Mon \t"<<"Tue \t"<<"Wed \t"<<"Thu \t"<<"Fri \t"<<"Sat \t"<<"Sun \t"<<endl;
//Print tab until the day corresponding to 01/../.... is reached
f(k,1,n)
cout<<"\t";
//If Current month is February, set number of days as determined in line 72:75
if(i==2)
d=febday;
//Run 'for loop' from 1 to d+1 where d is the number of days in that month
f(k,1,d+1)
{
//Print dates while adjusting space for two digit numbers
if(k<10)
cout<<k<<" \t";
else
cout<<k<<" \t";
n++;
//Reset day of the week to Monday (i.e. 1) if it becomes greater than Sunday (i.e. 7)
if(n>7)
{
n=1;
cout<<"\n";
}
}
//If Current month is July, set number of days for next iteration as 31 (August), else set to 30
if(d==31 && i!=7)
d=30;
else
d=31;
cout<<endl<<endl;
i++;
}
}