0% found this document useful (0 votes)
24 views4 pages

Calendar Code

This C++ program uses Zeller's congruence algorithm to calculate the day of the week for any given date and displays a calendar for the selected month and year. It takes month and year input from the user, calculates the starting day using Zeller's algorithm, displays the calendar with formatting and color, and allows navigation between months and years using arrow keys. It also provides options to print the calendar to a file or enter a new month/year.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views4 pages

Calendar Code

This C++ program uses Zeller's congruence algorithm to calculate the day of the week for any given date and displays a calendar for the selected month and year. It takes month and year input from the user, calculates the starting day using Zeller's algorithm, displays the calendar with formatting and color, and allows navigation between months and years using arrow keys. It also provides options to print the calendar to a file or enter a new month/year.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <iostream.

h>
#include<fstream.h>
#include<string.h>
#include <conio.h>
#include <stdlib.h>
#include <dos.h>
#define LEAP_YEAR ((Year%4==0 && Year%100 != 0)||Year%400==0)

int MonthDay[] = {31,28,31,30,31,30,31,31,30,31,30,31};


char *MonthName[]={"January","February","March","April","May","June","July",
"August","September","October","November","December"};
char *MonthName1[]={"JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP",
"OCT","NOV","DEC"};

getkey()
{
union REGS i,o;
while(!kbhit());
i.h.ah = 0;
int86(22,&i,&o);
return (o.h.ah);
}
/*================ FUNCTION TO CALCULATE ZELLER'S ALGORITHM =============*/
int getZeller(int Month,int Year)
{
int Day = 1, ZMonth, ZYear, Zeller;
if(Month < 3)
ZMonth = Month+10;
else
ZMonth = Month-2;
if(ZMonth > 10)
ZYear = Year-1;
else
ZYear = Year;
Zeller = ((int)((13*ZMonth-1)/5)+Day+ZYear%100+
(int)((ZYear%100)/4)-2*(int)(ZYear/100)+
(int)(ZYear/400)+77)%7;
return Zeller;
}
/*=======================================================================*/
void PrintFile(int M,int Y, int Z)
{
int i,j;
char filename[12];
char stryear[5];
ifstream stream;

strcpy(filename,MonthName1[M-1]);
itoa(Y,stryear,10);
strcat(filename,stryear);
strcat(filename,".txt");

if(!stream)
{
cout<<"\nError-cannot create file.";
getch();
exit(1);
}
cout<<"\n\t\t\t"<<MonthName[M-1]<<" "<<Y<<"\n\n\t";
for(i=1;i<=100;i++)
cout<<"-";
cout<<"\n\tSUN\tMON\tTUE\tWED\tTHU\tFRI\tSAT\n\t";
for(i=1;i<=100;i++)
cout<<"-";
/* setting starting position */
cout<<"\n";
for(i = 1; i <= Z; i++)
cout<<"\t -";
j = Z;
/* printing dates */
for(i = 1; i <= MonthDay[M-1]; i++)
{
if(j++ % 7 == 0)
cout<<"\n";
cout<<"\t "<<i;
}
cout<<"\n\t";
for(i=1;i<=100;i++)
cout<<"-";
stream.close();
}
void printchar(char c)
{
int i=1;
cout<<"\n\t";
for(i=1;i<=51;i++)
cout<<c;
cout<<"\n";

/*======================= MAIN FUNCTION =================================*/


void main(void)
{
int Month, Year, Zeller;
int i, j, KeyCode;

Top: /* goto statement */


textcolor(WHITE);
clrscr();
cout<<"\n\tThis program shows calendar of \n";
cout<<"\n\ta given month. Enter month, year...format is mm-yyyy.\n";
/* taking input */
while(1)
{
// fflush(stdin);
cout<<"\n\n\tEnter mm-yyyy:";
cin>>Month>>Year;
if(Year < 0)
{
cout<<"\nInvalid year value...";
continue;
}
if(Year < 100)
Year += 1900;
if(Year < 1582 || Year > 4902)
{
cout<<"\nInvalid year value...";
continue;
}
if(Month < 1 || Month > 12)
{
cout<<"\nInvalid month value...";
continue;
}
break;
} /* End of while loop */
do
{
/* calculating day of 1st date of given month */
Zeller = getZeller(Month,Year);
clrscr();
cout<<"\n\n\t\t\t";

/* printing the corresponding month name */


textbackground(Month);
cout<<MonthName[Month-1]<<" "<<Year<<"\n";
textbackground(BLACK);

/* adjusting February in case of Leap Year */


MonthDay[1] = LEAP_YEAR ? 29 : 28;

/* giving output */
printchar('-');

textcolor(12); /* LIGHTRED */
cout<<"\t";
cprintf("SUN");
textcolor(LIGHTGREEN);
cprintf(" MON TUE WED THU FRI SAT");
textcolor(7);

printchar('-');

/* setting starting position */


for(i = 1; i <= Zeller; i++)
cout<<"\t -";
j = Zeller;
/* printing dates */
for(i = 1; i <= MonthDay[Month-1]; i++)
{
if(j++ % 7 == 0)
{
cout<<"\n\t";
textcolor(12);
cprintf("%2d",i);
textcolor(WHITE);
}
else
cout<<"\t"<< i;
}

printchar('-');
cout<<"\n\n\t\t(*) Use Left,Right,Up & Down Arrow.";
cout<<"\n\n\t\t(*) Press I for New Month & Year.";
cout<<"\n\n\t\t(*) Press P for Print to File.";
cout<<"\n\n\t\t(*) Press ESC for Exit.\n\n\n\t\t";

textcolor(WHITE);
textbackground(BLACK);

KeyCode = getkey(); /* getting Key Code */


if(KeyCode == 72) /* Up Arrow */
Year++;
if(KeyCode == 80) /* Down Arrow */
Year--;
if(KeyCode == 77) /* Right Arrow */
{
Month++;
if(Month > 12)
{
Month = 1;
Year++;
}
}
if(KeyCode == 75) /* Left Arrow */
{
Month--;
if(Month < 1)
{
Month = 12;
Year--;
}
}
if(KeyCode == 25) /* Code of P */
PrintFile(Month,Year,Zeller);
if(KeyCode == 23) /* Code of I */
goto Top;
}while(KeyCode != 1); /* End of do-while loop */
exit(0);
}

You might also like