Code:
#include<windows.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
struct
{
int day;
int month;
int year;
}calender;
void clrscr()
{
COORD coordScreen = { 0, 0 };
DWORD cCharsWritten;
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD dwConSize;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hConsole, &csbi);
dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten);
GetConsoleScreenBufferInfo(hConsole, &csbi);
FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
SetConsoleCursorPosition(hConsole, coordScreen);
}
void gotoxy(int x, int y)
{
COORD point;
point.X = x; point.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),point);
}
//////////////////////////////////////////////////////////////////////////////////////////////
//
// isleapyear()
//
// checks whether year is a leap year in the gregorian calender
// a year is leap if it is divisable by four but not if divisible by 100
// unless it is divisible by 400
//
// returns true if leap and false if not
//
//////////////////////////////////////////////////////////////////////////////////////////////
bool isleapyear()
{
if ((calender.year%400==0) || ((calender.year %4==0) && (calender.year%100 !=0)))
{
return true; // its a leap year
}
else
{
return false; // its not a leap year
}
}
//////////////////////////////////////////////////////////////////////////////////////////////
//
// int howmanydays()
//
// returns number of days in month
//
//////////////////////////////////////////////////////////////////////////////////////////////
int howmanydays()
{
static const int daysinmonth[12]={31,28,31,30,31,30,31,31,30,31,30,
31};
if((calender.month==2)&& isleapyear())
{
return 29; // feb has 29 days in a leap year
}
return daysinmonth[calender.month-1];
}
//////////////////////////////////////////////////////////////////////////////////////////////
//
// checkday(int)
//
// checks day is legal for month
//
// in:- day to be tested
//
// returns day if legal and 1 if not legal
//
//////////////////////////////////////////////////////////////////////////////////////////////
int checkday(int testday)
{
if ((testday>0) && (testday<= howmanydays()))
{
return testday; // day is valid for month
}
printf("\n%s%d%s%d%s%d%s\n","Invalid day entered ",testday,"/",calender.month,"/",calender.year,". Setting to the first of the month.");
return 1; // hopefully wont get here but if invalid day entered day is set to 1
}
//////////////////////////////////////////////////////////////////////////////////////////////
//
// int whatdayisfirstofmonth()
//
// uses zeller's algorithm to find out what day the first of the month falls on falls on.
//
// returns 0 to 6 corresponding to sunday to saturday.
//
//////////////////////////////////////////////////////////////////////////////////////////////
int whatdayisfirstofmonth()
{
int f,c,d,m,k;
double g;
c=calender.year/100; // # of centuries
d=calender.year%100; // # of years through century
m=(calender.month+10)%12; // # of month march is 1,feb is 12
k=1; // set the day part to 1 so we get back the day for first of month
if ((calender.month==1)||(calender.month==2))// treat jan and feb as if they were in previous year
{
if (d==0) // if d is 0 then to go back a year d becomes 99 and c become c-1
{
d=99;
c-=1;
}
else
{
d-=1; // jan and feb are treated as previous year
}
}
g=(k + (floor(((13*m)-1)/5)) + d + (floor(d/4)) + (floor(c/4)) - (2*c));
f=(int)g%7; // cast result of algorithm to int to take modulus
if (f<0) // if negative add 7
{
f+=7;
}
return f; // returns 0 to 6 corresponding to sunday to saturday
}
void initcalender(int d,int m,int y)
{
if (y<1582) // church accepted gregorian calender in 1582
{
printf("\n%s%d%s\n","The year ",y," is before the gregorian calender was accepted by the church.Setting to 2001.");
calender.year=2001; // invalid year so set to 2001
}
else
{
calender.year=y; // y is valid so use it to set year
}
if ((m>=1) && (m<=12)) // check month between 1 and 12
{
calender.month=m; // if it is set month
}
else
{
printf("\n%s%d%s\n","The month ",m," is invalid.Setting to month 1");
calender.month=1;
}
calender.day=checkday(d); // validate the day
}
char* newgets(char* buffer,int num)
{
int i;
fgets(buffer,num,stdin);
i=strlen(buffer)-1;
if (buffer[i]=='\n') buffer[i]='\0';
return buffer;
}
void printcalender()
{
int i,j,startday,endday,rows,count;
clrscr();
printf("%s%d%s%d%s%d\n","Date entered was :- ",calender.day,"/",calender.month,"/",calender.year);
gotoxy(4,2);
printf("SUN");
gotoxy(12,2);
printf("MON");
gotoxy(21,2);
printf("TUES");
gotoxy(31,2);
printf("WED");
gotoxy(42,2);
printf("THUR");
gotoxy (50,2);
printf("FRI");
gotoxy(60,2);
printf("SAT");
startday=whatdayisfirstofmonth();
endday=howmanydays();
for (i=0;i<startday;i++)
{
if (i==0)
{
gotoxy(4,4);
printf("-");
}
if (i==1)
{
gotoxy(12,4);
printf("-");
}
if (i==2)
{
gotoxy(21,4);
printf("-");
}
if (i==3)
{
gotoxy(31,4);
printf("-");
}
if (i==4)
{
gotoxy(42,4);
printf("-");
}
if (i==5)
{
gotoxy(50,4);
printf("-");
}
} // end of for loop
rows=4;
count=1;
for(j=startday;j<(startday+endday);j++)
{
if(j%7==0)
{
rows+=2;
gotoxy(4,rows);
}
if(j%7==1) gotoxy(12,rows);
if(j%7==2) gotoxy(21,rows);
if(j%7==3) gotoxy(31,rows);
if(j%7==4) gotoxy(42,rows);
if(j%7==5) gotoxy(50,rows);
if(j%7==6) gotoxy(60,rows);
if(count==calender.day) // set text to bright red if count is the day entered
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED);
}
else
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_GREEN);
}
printf("%d",count);
count ++;
} // end of for loop
printf("\n\n\n");
}
int main()
{
int d,m,y;
char input[20];
char* p;
while (1)
{
clrscr();
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_GREEN);
printf("Welcome to the gregorian calender calculator. v.1.00\n\n");
printf("Use UK format.... day/month/full year. Use the '/' char to delimit your entry...\n\n");
printf("Please enter the date you would like to see the calender for :- ");
newgets(input,20);
printf("\n");
d=atoi(input);
p=strchr(input,'/');
m=atoi(++p);
p=strchr(p,'/');
y=atoi(++p);
initcalender(d,m,y);
system("PAUSE"); // wait for a keypress
printcalender(); // print the calender
printf("Another calender (Y/N) ? ");
newgets(input,20);
if ((input[0]=='N') || (input[0]=='n'))
{
break; // drop out of while loop
}
} // end of while loop
return 0;
}