Thursday, March 31, 2005

Day Number of a date

#include
using namespace std;

//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
int febdays(int year)
{

if(year%100 == 0 && year%400==0){
return 29;

}

else if(year%4 == 0){
return 29;
}

else{
return 28;
}
}

//PURPOSE: Calculate the number of days for a given date within that year
//PRECONDITION: Check whether the year is a leap or not
//POSTCONDTION: return the corresponding number of days for that year
int daynum(int month, int day, int year)
{
int yeardays = 0;//declare and initialize variable to hold the day number of the year

if(month == 1){
yeardays = day;
return yeardays;
}

if(month == 2){
yeardays = 31 + day;
return yeardays;
}

if(month == 3){
yeardays = 31 + febdays(year) + day ;
return yeardays;
}

if(month == 4){
yeardays = (31 * 2) + febdays(year) + day;
return yeardays;
}

if(month == 5){
yeardays = (31 * 2) + 30 + febdays(year) + day;
return yeardays;
}

if(month == 6){
yeardays = (31 * 3) + 30 + febdays(year) + day;
return yeardays;
}

if(month == 7){
yeardays = (31 * 3) + (30 * 2) + febdays(year) + day;
return yeardays;
}

if(month == 8){
yeardays = (31 * 4) + (30 * 2) + febdays(year) + day;
return yeardays;
}

if(month == 9){
yeardays = (31 * 5) + (30 * 2) + febdays(year) + day;
return yeardays;
}
if(month == 10){
yeardays = (31 * 5) + (30 * 3) + febdays(year) + day;
return yeardays;
}
if(month == 11){
yeardays = (31 * 6) + (30 * 3) + febdays(year) + day;
return yeardays;
}

if(month == 12){
yeardays = (31 * 6) + (30 * 4) + febdays(year) + day;
return yeardays;
}
return 0;
}

//PURPOSE: Check whether the month entered by the user is valid
//PRECONDITION: Month should be between 1 and 12
//POSTCONDTION: Display error message if the month is invalid
int CheckMonth(int month)
{
if(month <> 12){
cout << ">> Invalid Month <<\n";
cout << "Month should be between 1 and 12 \n";
return 1;
}
return 0;
}


//PURPOSE: Check whether the day entered by the user is valid
//PRECONDITION: Day should be in agreement with the month. e.g September has 30 days not 31
//POSTCONDTION: Display error message if the day is invalid
int CheckDay(int month, int day)
{
if(day <>
cout << ">>Invalid Day<<\n";
cout << "Day has to be a positive number\n";
return 1;
}
if(month == 2){
if(day > 29){
cout << ">> Invalid Day <<\n";
cout << "February has 28 days in a regular year and 29 days in a leap year\n";
return 1;
}
}
if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12){
if(day > 31){
cout << ">> Invalid Day <<\n";
cout << "Day should be between 1 and 31 \n";
return 1;
}

}
else{
if(day > 30){
cout << ">> Invalid Day <<\n";
cout << "Day should be between 1 and 30\n";
return 1;
}
}
return 0;
}

//PURPOSE: Check whether the year entered by the user is valid
//PRECONDITION: Year should be greater than zero
//POSTCONDTION: Display error message if the year is invalid
int CheckYear(int day, int year)
{
if(year <>
cout << ">> Invalid Year <<\n";
cout << "Year has to be positive\n";
return 1;
}
if(day == 29 && febdays(year)!=29){
cout << ">> Invalid Year <<\n";
cout <<>
cout << "Therefore Februaury cannot have 29 days\n";
return 1;
}
return 0;

}



//PURPOSE: Prompt user for the date in month/day/year format
//PRECONDITION: Check whether the entered date is valid, display error messages if it is not correct
//POSTCONDTION: return 0 to indicate success
int main()
{
int month = 0, day = 0, year = 0;
cout << "Enter the month: ";
cin >> month;
if(CheckMonth(month) == 1){
return 0;
}
cout << "Enter the day: ";
cin >> day;
if (CheckDay(month, day) == 1){
return 0;
}
cout << "Enter the year: ";
cin >> year;
if (CheckYear(day,year) == 1){
return 0;
}
cout <<>
return 0;

}

Reverend Zeller's Algorithm

Zeller's algorithm is used to compute the day of the week on which a given date will fall (or fell).

Let A, B, C and D denote integer variables that contain the following values:

  • A = the month of the year, with March having the value 1, April the value 2, . . . , December the value 10, and with January and February being counted as months 11 and 12 of the preceding year (in which case, subtract 1 from C).

  • B = the day of the month (1, 2, 3, . . . , 28, 29, 30, 31).

  • C = the year of the century (this would be 95 for the year 1895).

  • D = the century (this would be 18 for the year 1895).

Next let W, X, Y, Z and R denote integer variables, and compute the following values in this order, using integer arithmetic.

     W = (13A - 1) / 5
X = C / 4
Y = D / 4
Z = W + X + Y + B + C - 2D
R = the remainder obtained when Z is divided by 7.
The value of R represents the day of the week, where r = 0 represents Sunday, r = 1 represents Monday,. . . , and r = 6 represents Saturday. If R turns out to have a negative value such as -4, add 7 to it to get a nonnegative value between 0 and 6.

This above was obtained from http://www.cs.niu.edu/~t90hch1/csci210/zeller.htm

include <>

iostream, string and iomanip should be within <>. When the mentioned words are posted within <> they are not displayed on the screen. No idea why!!!

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;
}