P. 1
calender

calender

|Views: 0|Likes:

More info:

Published by: Gurpartap Singh Gill on Apr 10, 2011
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOCX, PDF, TXT or read online from Scribd
See more
See less

04/10/2011

pdf

text

original

#include <iostream.h> #include <stdlib.h> #include <ctype.h> #include <time.

h> int getYear(); // has the user enter a valid year bool isLeap(int year); // check for leap years void dayName(); // prints the names for the day of the week void monthNameHeader(int year); // puts head for the month name int startDay(int year); // decides what week day Jan starts on int monthCount(int counter); // how many days are in each month void newMonth(int startDOW); void printAll(int year); to screen // what day of the week new month starts on // puts everything together, prints

int year = 0; per 0 int counter = 1; days in month int startDOW, wrap, Saturday daysInMonth; int weekNumber = 0; int main() {

// uesr inputed year || rand gen // counter for month name & # // day of the week Jan starts on // check for if weekday is // total days in each month // flag for first week of the month

year = getYear(); printAll(year);

// has user enter year number

return 0; }

int getYear() //prompts the user to enter a valid year { char c; srand(time(NULL)); cout << "Enter the year, or 0 for and random year: "; do { // gets whole number value cin.get(c); if(isdigit(c)) { year=year*10; year +=(int)(c-'0');

} } while(c!='\n'); if (year == 0) // if no response or 0 are enter, random a year { year = rand() % 8600 + 1400; cout << "\nThe random year " << year << " will be evaluated\n\n"; } return year; } bool isLeap(int year) // checking for possible leap year { if (year % 400 == 0) return true; if (year % 100 == 0) return false; if (year % 4 == 0) return true; return false; // else return false } void dayName() { cout << " S M T W T F S" << endl; cout << "---------------------" << endl; } void monthNameHeader(int year) { switch (counter) { case 1: cout << " break; case 2: cout << " break; case 3: cout << " break; case 4: cout << " break; case 5: cout << " break; case 6: cout << "

January " << year << endl;

February " << year << endl;

March" << year << endl;

April " << year << endl;

May " << year << endl;

June " << year << endl;

break; case 7: cout << " break; case 8: cout << " break; case 9: cout << " break; case 10: cout << " break; case 11: cout << " break; case 12: cout << " break; } } int monthCount(int counter) // how many days are in the month { switch (counter) { case 1: daysInMonth = 31; // current month days break; case 2: possible leap year if(isLeap(year)) daysInMonth = 29; if(!isLeap(year)) daysInMonth = 28; break; case 3: daysInMonth = 31; break; case 4: daysInMonth = 30; break; case 5: daysInMonth = 31; break; // checks for

July " << year << endl;

August " << year << endl;

September " << year << endl;

October " << year << endl;

November " << year << endl;

December " << year << endl;

case 6: daysInMonth = 30; break; case 7: daysInMonth = 31; break; case 8: daysInMonth = 31; break; case 9: daysInMonth = 30; break; case 10: daysInMonth = 31; break; case 11: daysInMonth = 30; break; case 12: daysInMonth = 31; break; } } int startDay(int year) { startDOW = (year + (year - 1 ) /4 - (year - 1) / 100 + (year - 1) /400) %7; return startDOW; // formula for what DoWeek year starts on } void printAll(int year) { for (counter = 1; counter <= 12; counter++) { monthNameHeader(year); month day dayName(); prints the name of days if (counter==1) wrap = startDay(year) ; on else // // what day Jan starts // prints

startDOW = wrap; other months start on cout << " ";

// what day

for (int loopCount = 0; loopCount < startDOW; loopCount++) { cout << " "; // how many space to indent new month } monthCount(counter); month for (int dayCounter=1;dayCounter<=daysInMonth; dayCounter++) { if (wrap == 7) return { cout << "\n "; wrap = 0; of week counter weekNumber++; week of month } if (dayCounter<10) single digit days cout << " "; cout << dayCounter << " "; # wrap++; } // // cout << "\nthis month starts on day number " << startDOW; *testing* cout << "\ndays in this month are " << daysInMonth; *testing* cout << "\n\n"; system("PAUSE"); cout << endl; } // end BIG for loop //prints the day //adds space for //no longer first //resets day //if Saturday, carriage // how many days in

}

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->