Kanjut SHELL
Server IP : 172.16.15.8  /  Your IP : 3.145.56.150
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/snbittner/../drsparks/

[  Home  ][  C0mmand  ][  Upload File  ]

Current File : /home/snbittner/../drsparks/monthex.cpp
//-------------------------------------------------------------
// Project to illustrate user-defined functions
// Dale Sparks
// November 12, 2008
//-------------------------------------------------------------

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

//-------------------------------------------------------------
// LeapYear is here to make the days in Feb com out right on 
// the right years.
//-------------------------------------------------------------

bool LeapYear (int year)
{   
   int rem;
   bool leap;
 
   rem = year % 4;

   if (rem == 0)
       if (year == 1700 || year == 1800 || year == 1900 || year == 2100)
           leap = false;
        else
           leap = true;
   else
     leap = false;
    
return leap;
}

//-------------------------------------------------------------
// DaysInMonth determines the number of days of the month
//-------------------------------------------------------------
// 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;
    }
}

//-------------------------------------------------------------
// DisplayTitleAndHeading is here to produce the output that
// shows the month name and year, also the days on the week
// starting with sunday.
//-------------------------------------------------------------

void DisplayTitleAndHeadings (int monthnum, int year)
{

  cout << "\n\n";
  cout << "                       "; 
  DisplayMonthName (monthnum);
  cout << "  " << year;
    
  cout << "\n  Sun      Mon     Tues      Wed    Thurs      Fri      Sat";
   
}

//-------------------------------------------------------------
// StartingDay is here to produce the right starting day of the
// week for each month inbetween 1700 and 2299
//-------------------------------------------------------------

int StartingDay (int monthnum, int year)
{
    int yeartwo;
    int num;
    int quarter;
    int added;
    int firstday;
    int numone;
    int cent;

    if (1700 <= year && year <= 1799)
      {
       yeartwo = year - 1700;
       cent = 5;   
      }
    else if (1800 <= year && year <= 1899) 
      {
       yeartwo = year - 1800;
       cent = 3;
      }
    else if (1900 <= year && year <= 1999)
      {
       yeartwo = year - 1900;
       cent = 1;
      }
    else if (2000 <= year && year <= 2099)
      {
       yeartwo = year - 2000;
       cent = 0;
      }
    else if (2100 <= year && year <= 2199)
      {
       yeartwo = year - 2100;
       cent = 5;
      }
    else if (2200 <= year && year <= 2299)
      {
       yeartwo = year - 2200;
       cent = 3;
      }

    quarter = yeartwo / 4;
    numone = yeartwo + quarter;

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

   added = numone + num + 1 + cent;
   firstday = added % 7;

   return firstday;
}

//-------------------------------------------------------------
// DisplayFirstLine is here to start the day of the week in the
// right spot and to make sure the week does not go past sat 
// on this line.
//-------------------------------------------------------------

void DisplayFirstLine (int startdayofweek)
{
  int i;
  cout << endl;
  
  for (i=0; i < startdayofweek; i++)
        cout << "         ";
  for (i = 1; i <= (7 - startdayofweek); i++)
   {
        cout << setw(5) << i;
        cout << "    ";
   }

  cout << endl;
}

//-------------------------------------------------------------
// DisplayFullWeek is set up to display the full weeks of a
// of a given month.
//-------------------------------------------------------------
 
void DisplayFullWeek (int startdateofweek)
{
  int i;
  
  for (i = startdateofweek; i <= (startdateofweek + 6); i++)
   {
        cout << setw(5) << i;
        cout << "    ";
   }

  cout << endl;
} 

//-------------------------------------------------------------
// DisplayLastWeek is here to make sure the calender ends on 
// the right day for that month.
//-------------------------------------------------------------

void DisplayLastWeek (int startdateofweek, int numdaysinmonth)
{
   int i;

   for(i = startdateofweek; i <= numdaysinmonth; i++)
   {
        cout << setw(5) << i;
        cout << "    ";
   }
   cout << endl;
}


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

int main ( )
{

// --- Variables

    int monthnum,            // month number (1 - 12) entered by user
        numdays,             // number of days in month
        begdayofmonth,       // first day of the month that was chosen
        begdateofnextweek,   // first day of the first week for the month
        year;                // year in which the calendar is made for

// --- Get month number from user

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

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

    cout << "Enter Year:                   ";
    cin >> year;

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

// --- Get beginning day

    begdayofmonth = StartingDay (monthnum, year);

// --- Display output sentence

    DisplayTitleAndHeadings (monthnum, year);

    DisplayFirstLine(begdayofmonth);
    begdateofnextweek = 8 - begdayofmonth;
    DisplayFullWeek(begdateofnextweek);
    begdateofnextweek += 7;
    DisplayFullWeek(begdateofnextweek);
    begdateofnextweek += 7;
    DisplayFullWeek(begdateofnextweek);
    begdateofnextweek += 7;


  if (numdays - begdateofnextweek >= 7)
{
    DisplayFullWeek(begdateofnextweek);
    begdateofnextweek += 7;
}

    DisplayLastWeek(begdateofnextweek, numdays);    

    return 0;
}

Stv3n404 - 2023