Thursday, March 31, 2005

Calender Generation in Visual C++

#include iostream
using namespace std;
#include string
#include iomanip

void DayList()
{
//PURPOSE: display the initials of the days of the week
//PRECONDITION: week should start from Friday
//POSTCONDTION: the initials should be displayed horizontally with constant width between the initials
cout << setw(3) << "F";
cout << setw(3) << "S";
cout << setw(3) << "S";
cout << setw(3) << "M";
cout << setw(3) << "T";
cout << setw(3) << "W";
cout << setw(3) << "T" << "\n";
}


int febdays(int year)
{
//PURPOSE: Calculate the number of days in February for a given year
//PRECONDITION: Check whether the year is a leap or not
//POSTCONDTION: return the correct number of days in February for that year
if(year%100 == 0)
{
if(year%400 == 0)
{
return 29;
}
else
return 28;
}
else if(year%4 == 0)
return 29;
else
return 28;
}



int daysInMonth(int month, int year)
{
//PURPOSE: Assign number of days for a particular month
//PRECONDITION: Check for leap years
//POSTCONDITION: Return the number of days in a month for that year
int MonthDays = 0;
switch(month)
{
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
MonthDays = 31;
return MonthDays;
break;
case 4: case 6: case 9: case 11:
MonthDays = 30;
return MonthDays;
break;
case 2:
MonthDays = febdays(year);
return MonthDays;
break;
}
return 0;
}

int dayOfWeek(int month, int day, int year)
//PURPOSE: compute the day of the week for a date
//PRECONDITION: month, year, and day
//POSTCONDITION:the day number of the date (0=Sunday)
{
int century; // century
int EffectOfMonth; // days caused by month
int LeapYears; // days caused by leap years
int EffectOfCentury;// days caused by century
int TotalDays; // total days in date
int WeekDay; // day of week for given date

century = year/100;
year = year - century * 100;

//Apply Reverend Zeller's Algorithm
//Step 1: Adjust year so 00 becomes 100
if (year == 00) {
year = 100;
century = century - 1;
}

//Step 2: Adjust month so March == 1
if ((month < 3) && (year==1)) {
century=century - 1;
year=100;
}
else if (month < 3) {
year=year - 1;
}

//Step 3: Count the days
month=(month + 9) % 12 + 1;
EffectOfMonth=(13*month - 1) / 5;
LeapYears=year / 4;
EffectOfCentury=century / 4;
TotalDays=EffectOfMonth + LeapYears +
EffectOfCentury + day +
year + 5*century;

//Step 4: Compute and return day of week
WeekDay=TotalDays % 7;

return WeekDay;
}


string monthName(int month)
//PURPOSE: Determine the name of the month
//PRECONDITION: Match the number of the month to its name
//POSTCONDITION: Return the name of the respective month
{
switch(month)
{
case 1:
return " January";
break;
case 2:
return " February";
break;
case 3:
return " March";
break;
case 4:
return " April";
break;
case 5:
return " May";
break;
case 6:
return " June";
break;
case 7:
return " July";
break;
case 8:
return " August";
break;
case 9:
return " September";
break;
case 10:
return " October";
break;
case 11:
return " November";
break;
case 12:
return " December";
break;
}
}

int displayMonth(int year, int month, int day)
//PURPOSE: Display calender for the month
//PRECONDITION: Dates should correspond to the correct days
//POSTCONDITION: return 0 if successful
{
int a = 0;
for(month = 1; month <=12; month++){
cout << monthName(month);
cout << "\n" << "\n";
DayList();
day = 1;
if((dayOfWeek(month, day, year))== 0){
cout << setw(9) << day;
day = day + 1;
a = 9;
for(day = 2; day <= daysInMonth(month, year); day++){
cout << setw(3) << day;
a = a + 3;
if(a == 21){
a = 0;
cout << "\n";
}
}
cout << "\n" << "\n";
}
else if((dayOfWeek(month, day, year))== 1){
cout << setw(12) << day;
day = day + 1;
a = 12;
for(day = 2; day <= daysInMonth(month, year); day++){
cout << setw(3) << day;
a = a + 3;
if(a == 21){
a = 0;
cout << "\n";
}
}
cout << "\n" << "\n";
}
else if((dayOfWeek(month, day, year))== 2){
cout << setw(15) << day;
day = day + 1;
a = 15;
for(day = 2; day <= daysInMonth(month, year); day++){
cout << setw(3) << day;
a = a + 3;
if(a == 21){
a = 0;
cout << "\n";
}
}
cout << "\n" << "\n";
}
else if((dayOfWeek(month, day, year))== 3){
cout << setw(18) << day;
day = day + 1;
a = 18;
for(day = 2; day <= daysInMonth(month, year); day++){
cout << setw(3) << day;
a = a + 3;
if(a == 21){
a = 0;
cout << "\n";
}
}
cout << "\n" << "\n";
}
else if((dayOfWeek(month, day, year))== 4){
cout << setw(21) << day;
day = day + 1;
a = 21;
for(day = 2; day <= daysInMonth(month, year); day++){
if(a == 21){
a = 0;
cout << "\n";
}
cout << setw(3) << day;
a = a + 3;

}
cout << "\n" << "\n";
}
else if((dayOfWeek(month, day, year))== 5){
cout << setw(3) << day;
day = day + 1;
a = 3;
for(day = 2; day <= daysInMonth(month, year); day++){
cout << setw(3) << day;
a = a + 3;
if(a == 21){
a = 0;
cout << "\n";
}
}
cout << "\n" << "\n";
}
else if((dayOfWeek(month, day, year))== 6){
cout << setw(6) << day;
day = day + 1;
a = 6;
for(day = 2; day <= daysInMonth(month, year); day++){
cout << setw(3) << day;
a = a + 3;
if(a == 21){
a = 0;
cout << "\n";
}
}
cout << "\n" << "\n";
}
}
return 0;
}

int main()
//PURPOSE: Prompt user for an year
//PRECONDITION: Check whether the entered date is valid, display error messages if it is not correct
//POSTCONDTION: return 0 to indicate success
{
int month = 0 , year = 0, day = 0;
cout << "Enter an year between 1582 and 4092: ";
cin >> year;
if((year <> 4092)){
cout << ">>Invalid Year<<\n";
cout << ">>Year should be between 1582 and 4092<<\n";
}
while((year >= 1582)&&(year <= 4092)){
displayMonth(year, month, day);
cout << "Enter an year between 1582 and 4092: ";
cin >> year;
cout << "\n" << "\n";
}
return 0;
}

0 Comments:

Post a Comment

<< Home