Assigned project for FYS 292

Contents

 

Project Title : Calendar

Back to top

Project description :

 

Back to top

Show Source Code

 

// This program will implement two functions.

//First, it can be used to obtain a calendar for a known year.

//Second, it can be used to obtain the weekday for a known date.

 

#include <iostream.h>

#include <string>

using namespace std;

 

typedef unsigned short int usi;

char menu(); // prototype of subfunction menu() to generate a menu

class Calendar // prototype of self-defined new class Calendar

{

private:

usi weekdays1_loop; //from 0 to 6: 0-Sun,1-Mon,2-Tue,3-Wed,4-Thu,5-Fri,6-Sat

usi weekdays2_loop; //from 1 to 7: 7-sun,1-mon,2-Tue,3-wed,4-Thu,5-Fri,6-Sat

usi monthdays_loop; //from 1 to 28/29/30/31

usi month, date; // stand for user-entered value of month, date

int year; // stand for usr-entered value of year

usi month_loop, date_loop; // looped value of month, date

int year_loop; // looped value of year

bool leap; // used for judging leap year

int start_weekday; // stand for starting weekday of a known year

string weekday_table;

public:

Calendar(); // constructors

bool IfLeapYear(int year); // leap year, return true; otherwise return false

void GetYearCalendar(); //print out a yearly calendar for a known year

void MonthLayOut(usi month_days); //arrange the days in a month

void MonthCalendar(usi month, bool leap); //print out a monthly calender

void GetWeekDay(); // find the weekday for a known date

};

 

 

Calendar :: Calendar() // initialization of the class

{

year = -1; month = 0; date = 0 ; // used for judging input errors

year_loop = 0; month_loop = 1; date_loop = 1; //initial loop value

start_weekday = -1 ;

weekdays2_loop = 6;

leap = true;

weekday_table = " S M T W T F S";

}

 

bool Calendar :: IfLeapYear (int year)

{

if (year % 4 == 0) leap = true;

else leap = false;

if ((year % 100 == 0) && (year %400 != 0)) leap = false;

 

return leap;

}

 

 

void Calendar :: MonthLayOut (usi month_days)

{

++month_days;

bool first = true;

monthdays_loop = 1;

 

for (usi a = 0 ; a < start_weekday+1 ; a++)

cout << " ";

 

weekdays1_loop = start_weekday;

while (monthdays_loop < month_days)

{

if (first == false) cout << " ";

for ( ; weekdays1_loop < 7 ; weekdays1_loop++)

{

if (monthdays_loop < month_days)

{

if (monthdays_loop < 10)

cout << monthdays_loop << " ";

else

cout << monthdays_loop << " ";

start_weekday = weekdays1_loop+1;

}

monthdays_loop++;

}

 

weekdays1_loop = 0;

cout << endl;

first = false;

}

cout << endl;

}

 

void Calendar :: MonthCalendar (usi month, bool leap)

{

switch (month)

{

case 1:

cout << "\t\tJanuary" << endl << weekday_table << endl;

MonthLayOut(31); break;

case 2:

cout << "\t\tFebruary" << endl << weekday_table << endl;

if (leap == true) MonthLayOut(29);

else MonthLayOut(28); break;

case 3:

cout << "\t\tMarch" << endl << weekday_table << endl;

MonthLayOut(31); break;

case 4:

cout << "\t\tApril" << endl << weekday_table << endl;

MonthLayOut(30); break;

case 5:

cout << "\t\tMay" << endl << weekday_table << endl;

MonthLayOut(31); break;

case 6:

cout << "\t\tJune" << endl << weekday_table << endl;

MonthLayOut(30); break;

case 7:

cout << "\t\tJuly" << endl << weekday_table << endl;

MonthLayOut(31); break;

case 8:

cout << "\t\tAugust" << endl << weekday_table << endl;

MonthLayOut(31); break;

case 9:

cout << "\t\tSeptember" << endl << weekday_table << endl;

MonthLayOut(30); break;

case 10:

cout << "\t\tOctober" << endl << weekday_table << endl;

MonthLayOut(31); break;

case 11:

cout << "\t\tNoveber" << endl << weekday_table<<endl;

MonthLayOut(30); break;

case 12:

cout << "\t\tDecember"<<endl<< weekday_table<<endl;

MonthLayOut(31); break;

}

 

}

 

 

void Calendar :: GetYearCalendar()

{

while (year < 0)

{

cout << "Enter a year : ";

cin >> year;

}

 

cout << endl;

cout << " 0-Sun, 1-Mon, 2-Tue, 3-Wed, 4-Thu, 5-Fri, 6-Sat " << endl;

 

while (start_weekday < 0 || start_weekday > 6)

{

cout << "Enter the starting weekday of the year (0-6): ";

cin >> start_weekday;

}

 

cout << "\n\tCalendar of year " << year << endl<< endl;

 

leap = IfLeapYear(year); // Check if leap year

 

for (month = 1 ; month < 13 ; month++)

{

MonthCalendar (month,leap);

}

 

}

 

 

void Calendar :: GetWeekDay()

{

while ( year < 0)

{

cout << "Enter a year : ";

cin >> year;

}

cout << endl;

while (month < 1 || month > 12)

{

cout << "Enter a month(1-12): ";

cin >> month;

}

cout << endl;

while (date < 1 || date > 31)

{

cout << "Enter a date(1-31): ";

cin >> date;

}

while (year != year_loop || month != month_loop || date != date_loop)

{

weekdays2_loop++;

if (weekdays2_loop == 8) weekdays2_loop = 1;

date_loop++;

if (date_loop == 29 && month_loop == 2 && leap == false) { date_loop = 1; month_loop = 3;}

if (date_loop == 30 && month_loop == 2 && leap == true) { date_loop = 1; month_loop = 3;}

if (date_loop == 31 && month_loop == 4) {date_loop = 1; month_loop = 5;}

if (date_loop == 31 && month_loop == 6) {date_loop = 1; month_loop = 7;}

if (date_loop == 31 && month_loop == 9) {date_loop = 1; month_loop = 10;}

if (date_loop == 31 && month_loop == 11) {date_loop = 1; month_loop = 12;}

if (date_loop == 32 && month_loop == 1 ) {date_loop = 1; month_loop = 2;}

if (date_loop == 32 && month_loop == 3 ) {date_loop = 1; month_loop = 4;}

if (date_loop == 32 && month_loop == 5 ) {date_loop = 1; month_loop = 6;}

if (date_loop == 32 && month_loop == 7 ) {date_loop = 1; month_loop = 8;}

if (date_loop == 32 && month_loop == 8 ) {date_loop = 1; month_loop = 9;}

if (date_loop == 32 && month_loop == 10 ) {date_loop = 1; month_loop = 11;}

if (date_loop == 32 && month_loop == 12 )

{

date_loop = 1; month_loop = 1;

year_loop++ ;

leap = IfLeapYear (year_loop);

}

}

cout << endl << endl;

if (date < 10) cout << "0";

cout << date << "/";

if (month < 10) cout << "0";

cout << month << "/" << year << " is ";

if (weekdays2_loop == 1) cout << "Monday" << endl << endl;

if (weekdays2_loop == 2) cout << "Tuesday" << endl << endl;

if (weekdays2_loop == 3) cout << "Wednesday" << endl << endl;

if (weekdays2_loop == 4) cout << "Thursday" << endl << endl;

if (weekdays2_loop == 5) cout << "Friday" << endl << endl;

if (weekdays2_loop == 6) cout << "Saturday" << endl << endl;

if (weekdays2_loop == 7) cout << "Sunday" << endl << endl;

}

 

 

char menu()

{

char option;

 

cout << endl << endl;

cout << " ----------------W---E---L---C---O---M---E----------------------- " << endl;

cout << " | This is a calendar program which can be used to obtain a | " << endl;

cout << " | yearly calendar or to obtain the weekday of a known date. | " << endl;

cout << " | | " << endl;

cout << " | Authored by : Lu Zhao, Dept. of Physics, UiB, Bergen, Norway | " << endl;

cout << " | Date: June, 2002 | " << endl;

cout << " ---------------------------------------------------------------- " << endl;

cout << endl;

cout << " ***************************************** " << endl;

cout << " * 1. Get a calendar of a known year * " << endl;

cout << " * 2. Get the weekday of a known date * " << endl;

cout << " ***************************************** " << endl;

cout << endl;

cout << " Please select (1/2): ";

cin >> option; cout << endl;

while ((option != '1') && (option != '2')) // Input error checking

{

cout << " Invalid input, please only enter 1 or 2 : ";

cin >> option; cout << endl;

}

return option;

}

 

 

 

 

//-------------------------START OF MAIN PROGRAM --------------------------------

 

void main()

{

Calendar MyCalendar;

char choice;

 

choice = menu(); // choose to use which function of the program

switch (choice)

{

case '1' :

MyCalendar.GetYearCalendar(); break; // draw a yearly calendar

case '2' :

MyCalendar.GetWeekDay(); break; // determine the weekday of the given date

}

}

 

Back to top

Show running result1

 

----------------W---E---L---C---O---M---E-----------------------

| This is a calendar program which can be used to obtain a |

| yearly calendar or to obtain the weekday of a known date. |

| |

| Authored by : Lu Zhao, Dept. of Physics, UiB, Bergen, Norway |

| Date: June, 2002 |

----------------------------------------------------------------

 

*****************************************

* 1. Get a calendar of a known year *

* 2. Get the weekday of a known date *

*****************************************

 

Please select (1/2): 1

 

Enter a year : 2002

 

0-Sun, 1-Mon, 2-Tue, 3-Wed, 4-Thu, 5-Fri, 6-Sat

Enter the starting weekday of the year (0-6): 2

 

Calendar of year 2002

 

January

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

 

February

S M T W T F S

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28

 

March

S M T W T F S

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

 

April

S M T W T F S

1 2 3 4 5 6

7 8 9 10 11 12 13

14 15 16 17 18 19 20

21 22 23 24 25 26 27

28 29 30

 

May

S M T W T F S

1 2 3 4

5 6 7 8 9 10 11

12 13 14 15 16 17 18

19 20 21 22 23 24 25

26 27 28 29 30 31

 

June

S M T W T F S

1

2 3 4 5 6 7 8

9 10 11 12 13 14 15

16 17 18 19 20 21 22

23 24 25 26 27 28 29

30

 

July

S M T W T F S

1 2 3 4 5 6

7 8 9 10 11 12 13

14 15 16 17 18 19 20

21 22 23 24 25 26 27

28 29 30 31

 

August

S M T W T F S

1 2 3

4 5 6 7 8 9 10

11 12 13 14 15 16 17

18 19 20 21 22 23 24

25 26 27 28 29 30 31

 

September

S M T W T F S

 

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

 

October

S M T W T F S

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

 

Noveber

S M T W T F S

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

 

December

S M T W T F S

 

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

 

Back to top

Show running results2

 

----------------W---E---L---C---O---M---E-----------------------

| This is a calendar program which can be used to obtain a |

| yearly calendar or to obtain the weekday of a known date. |

| |

| Authored by : Lu Zhao, Dept. of Physics, UiB, Bergen, Norway |

| Date: June, 2002 |

----------------------------------------------------------------

 

*****************************************

* 1. Get a calendar of a known year *

* 2. Get the weekday of a known date *

*****************************************

 

Please select (1/2): 2

 

Enter a year : 2002

 

Enter a month(1-12): 6

 

Enter a date(1-31): 3

 

 

03/06/2002 is Monday

 

Back to top

 

Last revised: 13.06.2002