-
Notifications
You must be signed in to change notification settings - Fork 1
/
Calendar.c
255 lines (226 loc) · 7.41 KB
/
Calendar.c
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#include<stdio.h>
#include<conio.h>
#include<windows.h>
int getkey();
void display(int,int,int,int[]);
void calendar(int,int);
//-------------- GOTO function definition ----------------------
void gotoxy(int x,int y)
{
COORD coord;
coord.X=x;
coord.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
}
//------------ Change color -------------------
void SetColor(int ForgC)
{
WORD wColor;
//We will need this handle to get the current background attribute
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
//We use csbi for the wAttributes word.
if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
{
//Mask out all but the background attribute, and add in the forgournd color
wColor = (csbi.wAttributes & 0xF0) + (ForgC & 0x0F);
SetConsoleTextAttribute(hStdOut, wColor);
}
return;
}
char *month[]={"January","February","March","April","May","June","July","August","September","October","November","December"};
char *week[]={"Mon","Tue","Wed","Thu","Fri","Sat","Sun"};
int main()
{
int nmonth,nyr,ch;
enteryear:
while(1)
{
printf("Enter year and month(number):" );
scanf("%d%d",&nyr,&nmonth);
if(nyr<1945)
{
//If year less than 1945
printf("\n\t Please enter year above 1945\n");
continue;
}
else
{
//If year greater than equal to 1945
calendar(nyr,nmonth);
}
while(1)
{
gotoxy(20,20);printf("(*) Use LEFT, RIGHT, UP and DOWN arrow.");
gotoxy(20,22);printf("(*) Press P to go to particular year and month.");
gotoxy(20,24);printf("(*) Press ESC to Exit.");
ch=getkey();
switch(ch)
{
case 80: //-------- DOWN ARROW -----------
//Increasing month
if(nmonth==12)
{
//Jump to next year if month is december
nmonth=1;
nyr++;
}
else
{
nmonth++;
}
calendar(nyr,nmonth);
break;
case 77: //-------- RIGHT ARROW ----------
//Increasing Year
nyr++;
calendar(nyr,nmonth);
break;
case 72: //------- UP ARROW -------------
// Decreasing Month
if(nmonth==1)
{
// Jump to previous year if month is january
nyr--;
nmonth=12;
}
else
nmonth--;
calendar(nyr,nmonth);
break;
case 75: //-------- LEFT ARROW ----------
//Decreasing year
if(nyr==1945)
{
//If year is 1945 and we click left arrow than
gotoxy(15,2);printf("Year below 1945 not available");
}
else
{
nyr--;
calendar(nyr,nmonth);
}
break;
case 27: //--------- ESC KEY ------------
//Exit program
system("cls");
gotoxy(50,14);printf("Exiting program.\n\n\n\n\n");
exit(0);
case 112://---------- p KEY ------------
//Go to particular year and month
system("cls");
goto enteryear;
}
}
break;
}
getch();
return 0;
}
//============== DISPLAYING THE CALENDAR ===================
void display(int nyr,int nmonth,int tdays,int days[])
{
int i,j,pos;
SetColor(12); //Color red
gotoxy(56,6);printf("%s %d",month[nmonth-1],nyr); //Heading year and month dispalying
for(i=0,pos=30;i<7;i++,pos+=10)
{
if(i==6)
SetColor(9); //Sunday color blue
else
SetColor(10); //Others day color green
gotoxy(pos,8);printf("%s",week[i]);
}
SetColor(15); //setting the color white
tdays++; //incrementing the tdays by 1
if(tdays==0 || tdays==7)
pos=91; //if tdays is 0 or 7, position is sunday
if(tdays==1)
pos=31; //if tdays is 1, position is monday
if(tdays==2)
pos=41; //if tdays is 2, position is tuesday
if(tdays==3)
pos=51; //if tdays is 3, position is wednesday
if(tdays==4)
pos=61; //if tdays is 4, position is thursday
if(tdays==5)
pos=71; //if tdays is 5, position is friday
if(tdays==6)
pos=81; //if tdays is 6, position is saturday
for(i=0,j=10,pos;i<days[nmonth-1];i++,pos+=10)
{
if(pos==91)
SetColor(8); //Changing color to dark grey for sunday
else
SetColor(7); //Changing color to white for all days
gotoxy(pos,j);printf("%d",i+1);
if(pos==91)
{
pos=21; //Moving position to monday
j++; //Increasing j by 10 if position is sunday
}
}
SetColor(5); //Changing color to purple
//Drawing horizontal line
for(i=22;i<102;i++)
{
gotoxy(i,4);printf("%c",196);
gotoxy(i,17);printf("%c",196);
}
//Drawing all the corner of the rectangle
gotoxy(21,4);printf("%c",218);
gotoxy(102,4);printf("%c",191);
gotoxy(21,17);printf("%c",192);
gotoxy(102,17);printf("%c",217);
//Drawing vertical line
for(i=5;i<17;i++)
{
gotoxy(21,i);printf("%c",179);
gotoxy(102,i);printf("%c",179);
}
SetColor(11); //Changing color to aqua
//Drawing left and the right navigation symbol
gotoxy(24,11);printf("%c",174);
gotoxy(98,11);printf("%c",175);
}
//============== ARROW KEY ===============
int getkey()
{
int ch;
ch=getch();
if(ch==0)
{
printf("zero");
ch=getch();
return ch;
}
return ch;
}
//============ Calculating ====================
void calendar(int nyr,int nmonth)
{
int days[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int tdays=0,k,myear;
system("cls");
myear=nyr-1; //Decreasing year by 1
if(myear>=1945)
{
for(k=1945;k<=myear;k++)
{
if(k%4==0) //If the year is a leap year than total no of days is 366
tdays=tdays+366; //counting all the days till nyr - 1
else //If the year is a leap year than total no of days is 365
tdays=tdays+365; //counting all the days till nyr - 1
}
}
if(nyr%4==0)
days[1]=29; //changing value in days array from 28 to 29 since leap year
else
days[1]=28; //changing value in days array from 29 to 28 since not a leap year
for(k=0;k<(nmonth-1);k++)
{
tdays=tdays+days[k]; //Adding nmonth-1 days to tdays
}
tdays=tdays%7; //Finding the remainder of tdays so it can calculate the position to display
display(nyr,nmonth,tdays,days); //Passing the value to display
}