⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 date.cpp

📁 《c++大学教程实验指导书》源码
💻 CPP
字号:
// Chapter 7 of C++ How to Program
// Programming Challenge 11 (Solution)

#include <iostream> 

using std::cout; 

#include <cstring> 
#include <ctime>

#include "date.h"

// Date constructor that uses functions from ctime
Date::Date()
{
   struct tm *ptr;       // pointer of type struct tm
                               // which holds calendar time components
   time_t t = time( 0 ); // determine the current calendar time
                         // which is assigned to timePtr
   ptr = localtime( &t ); // convert the current calendar time
                         // pointed to by timePtr into
                               // broken down time and assign it to ptr
   day = ptr->tm_mday;        // broken down day of month
   month = 1 + ptr->tm_mon;    // broken down month since January
   year = ptr->tm_year + 1900; // broken down year since 1900

} // end class Date constructor

// Date constructor that uses day of year, and year
Date::Date( int ddd, int yyyy )
{
   setYear( yyyy );
   convert1( ddd );  // convert to month and day

} // end class Date constructor

// Date constructor that uses month, day and year
Date::Date( int mm, int dd, int yy )
{
   setYear( yy + 1900 );
   setMonth( mm );
   setDay( dd );

} // end class Date constructor

// Date constructor that uses month name, day and year
Date::Date( char *mPtr, int dd, int yyyy )
{
   setYear( yyyy );
   convert3( mPtr );
   setDay( dd );   

} // end class Date constructor

// Set the day
void Date::setDay( int d )
{ 
   day = d >= 1 && d <= daysOfMonth() ? d : 1;

} // end function setDay

// Set the month
void Date::setMonth( int m )
{ 
   month = m >= 1 && m <= 12 ? m : 1;

} // end function setMonth

// Set the year
void Date::setYear( int y ) 
{ 
   year = y >= 1900 && y <= 1999 ? y : 1900;

} // end function setYear

// Print Date in the form: mm/dd/yyyy
void Date::printDateSlash() const
{ 
   cout << month << '/' << day << '/' << year << '\n';

} // end function print DateSlash

// Print Date in the form: monthname dd, yyyy
void Date::printDateMonth() const
{ 
   cout << monthName() << ' ' << day << ", " << year << '\n';

} // end function printDateMonth

// Print Date in the form: ddd yyyy
void Date::printDateDay() const
{ 
   cout << convert2() << ' ' << year << '\n';

} // end function printDateDay

// Return the month name
const char *Date::monthName() const 
{ 
   return monthList( month - 1 );

} // end function monthName

// Return the number of days in the month
int Date::daysOfMonth() const
{ 
   return leapYear() && month == 2 ? 29 : days( month );

} // end function daysOfMonth

// Test for a leap year
bool Date::leapYear() const
{
   if ( year % 400 == 0 || ( year % 4 == 0 && year % 100 != 0 ) )
      return true;

   else
      return false;

} // end function leapYear

// Convert ddd to mm and dd
void Date::convert1( int ddd )  // convert to mm / dd / yyyy
{
   int dayTotal = 0;

   if ( ddd < 1 || ddd > 366 )  // check for invalid day
      ddd = 1;

   setMonth( 1 );

   for ( int m = 1; m < 13 && 
                    ( dayTotal + daysOfMonth() ) < ddd; ++m ) {
      dayTotal += daysOfMonth();
      setMonth( m + 1 );

   } // end for

   setDay( ddd - dayTotal );
   setMonth( m );

} // end function convert1

// Convert mm and dd to ddd
int Date::convert2() const // convert to a ddd yyyy format
{
   int ddd = 0;

   for ( int m = 1; m < month; ++m )
      ddd += days( m );

   ddd += day;
   return ddd;

} // end function convert2

// Convert from month name to month number
// Convert to mm / dd / yyyy
void Date::convert3( const char * const mPtr ) 
{
   bool flag = false; 

   for ( int subscript = 0; subscript < 12; ++subscript )

      if ( !strcmp( mPtr, monthList( subscript ) ) ) {
         setMonth( subscript + 1 );
         flag = true; // set flag
         break;    // stop checking for month

      } // end if 

   if ( !flag )
      setMonth( 1 ); // invalid month default is January

} // end function convert3

// Return the name of the month
const char *Date::monthList( int mm ) const
{
   char *months[] = { "January", "February", "March", "April", 
                     "May", "June", "July", "August", 
                     "September", "October", "November",
                      "December" };

   return months[ mm ];

} // end function monthList

// Return the days in the month
int Date::days( int m ) const 
{
   const int monthDays[] = { 31, 28, 31, 30, 31, 30, 31, 31, 
                             30, 31, 30, 31 };

   return monthDays[ m - 1 ];

} // end function days


/**************************************************************************
 * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice      *
 * Hall. All Rights Reserved.                                             *
 *                                                                        *
 * DISCLAIMER: The authors and publisher of this book have used their     *
 * best efforts in preparing the book. These efforts include the          *
 * development, research, and testing of the theories and programs        *
 * to determine their effectiveness. The authors and publisher make       *
 * no warranty of any kind, expressed or implied, with regard to these    *
 * programs or to the documentation contained in these books. The authors *
 * and publisher shall not be liable in any event for incidental or       *
 * consequential damages in connection with, or arising out of, the       *
 * furnishing, performance, or use of these programs.                     *
 *************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -