here is some code i think you will find helpful. You shouldn't use it as is but as a basis to learn from.....
Code:
date.h
//Inclusion guard
#ifndef DATE_H
#define DATE_H
class Date
{
private:
int Day;
int Month;
int Year;
// private utility function for class
int CheckDay(int)const; // checks day is legal for month and year
public:
Date(int day=1,int month=1,int year=2001) // defaults to 1/1/2001
{
SetYear(year);
SetMonth(month);
SetDay(day);
}
~Date() {} // do nothing destructor as nothing to clean up.
int GetDay()const { return Day;}
int GetMonth()const { return Month;}
int GetYear()const { return Year;}
void SetDay(int); // validates day
void SetMonth(int); // validates month
void SetYear(int); // validates year
bool IsLeapYear()const; // determines if Year is a leap year
int HowManyDays()const; // returns number of days in Month
}; //end of class declaration
#endif
Code:
date.cpp
//library includes
#include "date.h"
// Class function definitions
bool Date::IsLeapYear()const
{
if ((Year % 400 == 0) || ((Year % 4 == 0) && (Year % 100 != 0)))
{
return true; // its a leap year
}
else
{
return false; // its not a leap year
}
}
int Date::HowManyDays()const
{
if ((Month==2) && IsLeapYear())
{
return 29; // feb has 29 days in a leap year
}
static const int DaysInMonth[12]={31,28,31,30,31,30,31,31,30,31,30,31};
return DaysInMonth[Month-1];
}
int Date::CheckDay(int TestDay)const
{
if ((TestDay > 0) && (TestDay <= HowManyDays() ))
{
return TestDay; // day is valid for month
}
return 1; // if not then set to default value of 1
}
void Date::SetDay(int TestDay)
{
Day=CheckDay(TestDay);
}
void Date::SetMonth(int TestMonth)
{
if (TestMonth >= 1 && TestMonth <= 12)
{
Month=TestMonth;
}
else
{
Month=1; // default value
}
}
void Date::SetYear(int TestYear)
{
if (TestYear > 1582) // Gregorian calender accepted in late 1582
{
Year=TestYear;
}
else
{
Year=2001; // default value
}
}
Code:
CalenderADT.h
// inclusion guard
#ifndef CALENDER_ADT_H
#define CALENDER_ADT_H
// includes
#include <cmath>
#include "date.h"
class CalenderADT // calender abstract data type
{
protected:
Date TheDate;
int WhatDay(int)const;
public:
CalenderADT(Date TestDate) : TheDate(TestDate) {}
virtual ~CalenderADT() {}
virtual void Draw()const = 0;
};
#endif
Code:
CalenderADT.cpp
#include "CalenderADT.h"
//////////////////////////////////////////////////////////////////////////////////////////////
// WhatDay function uses Zellers algorithm to return the day of the week for the given date.
// returns 0 to 6 corresponding to sunday to saturday.
// only valid for the gregorian calender
//
//////////////////////////////////////////////////////////////////////////////////////////////
// details of algorithm are :-
//
// [x] = floor (x)
//
// f=k+[((13*m)-1)/5]+d+[d/4]+[c/4]-(2*c)
//
// where k=day of the month
// m=month number march=1 jan=11 feb=12 etc.
// d=last two digits of year.... if m=1 or m=2 d=d-1 (jan,feb treated as previous year)
// c=first two digits of year i.e. number of centuries.
//
// once we have f we divide by 7 and take remainder.If negative add 7. remainder = day of the week
// 0=sunday 1=monday 6=saturday etc.
//////////////////////////////////////////////////////////////////////////////////////////////
int CalenderADT::WhatDay(int TestDay)const
{
int c=TheDate.GetYear() / 100; // # of centuries
int d=TheDate.GetYear() % 100; // # of years through century
int m=(TheDate.GetMonth() + 10) % 12; // # of month march is 1,feb is 12
int k=TestDay; // day part
if ((TheDate.GetMonth() == 1) || (TheDate.GetMonth() == 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
}
}
double g=(k + (floor(((13 * m) - 1) / 5)) + d + (floor(d / 4)) + (floor(c / 4)) - (2 * c));
int f=static_cast<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
}
Code:
Calender.h
#ifndef CALENDER_H
#define CALENDER_H
// includes
#include "CalenderADT.h"
class Calender:public CalenderADT // console calender class
{
public:
Calender(Date TestDate) : CalenderADT(TestDate) {}
virtual ~Calender() {}
virtual void Draw()const; // draws the calender
};
#endif
Code:
Calender.cpp
// library includes
#include <windows.h>
#include <iostream>
#include <iomanip>
#include "Calender.h"
#include "ScreenFuncs.h"
using namespace std;
// class function definitions
void Calender::Draw()const
{
clrscr();
cout<<"Date entered was :- "<<TheDate.GetDay()<<"/"<<TheDate.GetMonth()<<"/"<<TheDate.GetYear()<<endl;
cout<<endl<<setw(8)<<"SUNDAY"<<setw(8)<<"MONDAY"<<setw(9)<<"TUESDAY"<<setw(11)<<"WEDNESDAY"<<setw(10)
<<"THURSDAY"<<setw(8)<<"FRIDAY"<<setw(10)<<"SATURDAY"<<endl;
int startday=WhatDay(1); // find out what day the first of month was
int endday=TheDate.HowManyDays();
for (int i=0;i<startday;i++)
{
if (i==0) gotoxy(4,4);
if (i==1) gotoxy(12,4);
if (i==2) gotoxy(21,4);
if (i==3) gotoxy(31,4);
if (i==4) gotoxy(42,4);
if (i==5) gotoxy(50,4);
cout<<"-"<<flush;
} // end of for loop
int rows=4;
int count=1;
for(int j=startday;j<(startday+endday);j++)
{
if(j%7==0)
{
if(j==0) rows=2;
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==TheDate.GetDay()) // 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);
}
cout<<count;
count ++;
} // end of for loop
cout<<endl;
}
Code:
ScreenFuncs.h
// function prototypes
#ifndef SCREENFUNCS_H
#define SCREENFUNCS_H
void clrscr(); // clears the screen in a windows console
void gotoxy(int x,int y); // moves the cursor to x,y position in a windows console
#endif
Code:
ScreenFuncs.cpp
// includes
#include <windows.h>
#include "ScreenFuncs.h"
// clearscreen function
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);
}
// gotoxy function
void gotoxy(int x, int y)
{
COORD point;
point.X = x; point.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),point);
}
Code:
main.cpp
#include <windows.h>
#include "Calender.h"
#include "date.h"
#include <iostream>
#include "ScreenFuncs.h"
using namespace std;
int main()
{
while(1)
{
clrscr();
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN);
cout<<"Enter date you want to see the calender for in this format dd/mm/yy :-";
char input_day[4];
char input_month[4];
char input_year[6];
cin.getline(input_day,3,'/'); // get the input as strings
cin.getline(input_month,3,'/');
cin.getline(input_year,5,'\n');
cout<<endl;
int d=atoi(input_day); // convert input to integer
int m=atoi(input_month);
int y=atoi(input_year);
Date TheDate(d,m,y);
CalenderADT* pCal= new Calender(TheDate);
pCal ->Draw();
delete pCal;
cout<<endl<<"Another (y/n)?";
char quit;
cin>>quit;
cin.ignore(80,'\n');
if(quit=='n' || quit=='N') break;
}
return 0;
}