Kanjut SHELL
Server IP : 172.16.15.8  /  Your IP : 3.135.193.193
Web Server : Apache
System : Linux zeus.vwu.edu 4.18.0-553.27.1.el8_10.x86_64 #1 SMP Wed Nov 6 14:29:02 UTC 2024 x86_64
User : apache ( 48)
PHP Version : 7.2.24
Disable Function : NONE
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : ON
Directory (0705) :  /home/jljustice/

[  Home  ][  C0mmand  ][  Upload File  ]

Current File : /home/jljustice/monthex.cpp
//-------------------------------------------------------------
// Computer Programming Assignment #5                         |
//                                                            |
// Written By: Jared Justice                                  | 
// Due Date: November 10, 2008                                |
//                                                            |         
// Purpose: To produce a calender based on what year and      |
//          month the user gives. Also determines the start   | 
//          day of each month. Leap is also taken into        |
//          account when the year is given.                   |
//-------------------------------------------------------------

#include <iostream>
#include <iomanip>
using namespace std;


// LeapYear displays nothing
//---------------------------------------------
// It sees if the year is a leap year or not. |
//---------------------------------------------

bool LeapYear(int year)
{
 int  rem;
 rem = year % 4;
 
 if( rem == 0)
  return true;
 else 
  return false;
}

//-------------------------------------------------------------
// DaysInMonth determines the number of days of the month     |
// Note:  ignores leap year                                   |
//-------------------------------------------------------------
// Accepts:  number (1 - 12) representing month               |
// Returns:  number of days in that month                     |
//-------------------------------------------------------------

int DaysInMonth (int monthnum,int year)
{
    int numdays;  // temp variable for days in month
        

    switch (monthnum)
    {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:   numdays = 31;  break;
        case 2:   if(LeapYear(year) == true)
                    numdays = 29;
                   else
                     numdays = 28;
                   break;
        case 4:
        case 6:
        case 9:
        case 11:   numdays = 30;  break;
    }

    return numdays;
}

//-------------------------------------------------------------
// DisplayMonthName displays the month name to standard output|
//-------------------------------------------------------------
// Accepts:  number (1 - 12) representing month               |
// Returns:  nothing                                          |
//-------------------------------------------------------------

void DisplayMonthName (int monthnum)
{
    switch (monthnum)
    {
        case 1:  cout << "January";  break;
        case 2:  cout << "February";  break;
        case 3:  cout << "March";  break;
        case 4:  cout << "April";  break;
        case 5:  cout << "May";  break;
        case 6:  cout << "June";  break;
        case 7:  cout << "July";  break;
        case 8:  cout << "August";  break;
        case 9:  cout << "September";  break;
        case 10:  cout << "October";  break;
        case 11:  cout << "November";  break;
        case 12:  cout << "December";  break;
    }
}

//-------------------------------------------------------
// CalculateDayofWeek is a function to determine the    |
// start day of the month.                              |
//-------------------------------------------------------
int CalculateDayofWeek (int month, int day, int year)
{
   int dayofweek,
       phase1,    // the calculations to
       phase2,    // get the last digits
       phase3,    // of the year.
       code,      //used in the switch to determine the month
       final;     // the end result


    phase1 = year % 100;
    phase2 = phase1 / 4;
    phase3 = phase1 + phase2;

   switch (month)
   {
    case  1:
    case 10: code = 6;
             break;
    case  2:
    case  3:
    case 11: code = 2;
             break;
    case  4:
    case  7: code = 5;
             break;
    case  5: code = 0;
             break;
    case  6: code = 3; 
             break;
    case  8: code = 1;
             break;
    case  9:
    case 12: code = 4;
             break;
   }   

   if (year >= 1700)
     final = phase3 + code + 5;
   else if (year >= 1800)
     final = phase3 + code + 3;
   else if (year >= 1900) 
     final = phase3 + code + 1;
   else if (year >= 2100)
     final = phase3 + code + 5;
   else if (year >= 2200 || year <= 2299)
     final = phase3 + code + 3;

   if(LeapYear(year) == true && code == 2)
     final -= 1;
   else if(LeapYear(year) == true && code == 1)
     final -= 1;
 
   dayofweek = (final + 4) % 7;
  
   return dayofweek; 
}

// DisplayFirstWeek displays the first week of a month
//----------------------------------------------------
// Accepts: numbers 1-6                              |
// Returns: First week of the month                  |
//----------------------------------------------------
void DisplayFirstWeek (int startdayofweek)
{
 int i;
 cout << endl;
 for( i = 0; i < startdayofweek; i++)
   cout << "      ";
 for( i = 1; i <= (7-startdayofweek); i++)
   cout << setw(6) << i;
}
// DisplayFullWeek displays the rest of the month
//------------------------------------------------
// Accepts: numbers                              |
// Returns: the dates of the other days          |
//------------------------------------------------

void DisplayFullWeek (int startdateofweek)
{
 int i;
 cout << "\n";
 for (i = startdateofweek; i <= (startdateofweek + 6); i++)
 cout << setw(6) << i;
}

void DisplayLastWeek (int startdateofweek, int numdaysinmonth)
{
 int i;
 cout << "\n";
 for (i = startdateofweek; i <= numdaysinmonth; i++)
  cout << setw(6) << i;
}

//---------------------------------------------------
// DisplayTitleAndHeadings (int month)              |
//---------------------------------------------------
// Displays the headings for the month and days     |
//---------------------------------------------------

void DisplayTitleAndHeadings (int month)
{

    cout << "\t    " << setw(7);
    cout << "~~~~~~~";
    DisplayMonthName (month);
    cout << "~~~~~~~";
    cout << endl;
    cout <<"   Sun.  Mon.  Tue.  Wed.  Thurs.  Fri.  Sat.  ";
    cout << "\n---------------------------------------------";
}  


//-------------------------------------------------------------
//  Main Function                                             |
//-------------------------------------------------------------

int main ( )
{

// --- Variables

    int monthnum,      // month number (1 - 12) entered by user
        begdayofmonth, // number 0 - 6 for the day of the week
        begdateofnextweek,
        year,
        day,
        numdays;       // number of days in month

// --- Get the year from user
  
    cout << "\nEnter a year: ";
    cin >> year;
// --- Validate the year

   while (year < 1700 || year > 2299)
    {
     cout << "\n\n Invalid Year";
     cout << "\n Please enter a year between 1700 and 2299: ";
     cin >> year;
    }
   
// --- Get month number from user

    cout << "\nEnter a month number (1-12): ";
    cin >> monthnum;
    cout << endl;
// --- Validate month number


    while (monthnum < 1 || monthnum > 12)
    {
       cout << "\n\n!!! Month number must be in range 1 - 12 !!!";
       cout << "\n\nPlease re-enter month number: ";
       cin >> monthnum;
    }


// --- Get number of days in month
    begdayofmonth = CalculateDayofWeek (monthnum, 1, year);
    numdays = DaysInMonth(monthnum, year);

// --- Displays Month and Days of the Week Titles

    DisplayTitleAndHeadings(monthnum);

// --- Display output sentence 
   
    DisplayFirstWeek(begdayofmonth);
    begdateofnextweek = 8-begdayofmonth;
    DisplayFullWeek(8-begdayofmonth);
    begdateofnextweek += 7;
    DisplayFullWeek (begdateofnextweek);
    begdateofnextweek += 7;
    DisplayFullWeek (begdateofnextweek);
    begdateofnextweek +=7;
    if (numdays - begdateofnextweek >= 7)
    {
     DisplayFullWeek (begdateofnextweek);
     begdateofnextweek += 7;
    }
    DisplayLastWeek (begdateofnextweek, numdays);
    cout << endl;
    return 0;
}


Stv3n404 - 2023