Question: Store only one integer in SDate class-the value of the computed serial date. When writing the implementation, call existing serial date functions to perform all
Store only one integer in SDate class-the value of the computed serial date. When writing the implementation, call existing serial date functions to perform all low-level serial Julian date computations. If bad paremeters are detected by the (), member function , do not update the current serial date value, simply return false. Do not print any error messages from set () member function, let the caller deal with the problem. Complie and test class implementation against the main () function provided in SDate_main.cpp
SDate,h
/* * @topic S-0326-09-02-35 Assignment a9 Serial date class * @brief class SDate */
#ifndef SDATE_H_INCLUDED_ #define SDATE_H_INCLUDED_
#include "serial_functions.h"
class SDate { int serial_date; public: // Function to set calendar date. Returns: // -- false if given month/day/year are invalid // -- true if everything is okay. bool set(int mm, int dd, int yyyy);
//---------------------------------------------- // functions to manipulate serial value //---------------------------------------------- void sDate::serial(int sDate); // set serial date
int sDate::serial(); // get serial date
void sDate::add(int days); // add days to the serial value
//---------------------------------------------- // functions to change or get calendar parts: //---------------------------------------------- void sDate::set_month(int mm); // set month
int sDate::month(); // get month
void set_day(int dd); // set day
int day(); // get day
void set_year(int yyyy); // set year
int year(); // get year
};//class SDate
#endif //SDATE_H_INCLUDED_
serial_functions.h
/* * @topic S-0326-09-02-35 Assignment a9 Serial date class * @brief class SDate */
#ifndef SDATE_H_INCLUDED_ #define SDATE_H_INCLUDED_
#include "serial_functions.h"
class SDate { int serial_date; public: // Function to set calendar date. Returns: // -- false if given month/day/year are invalid // -- true if everything is okay. bool set(int mm, int dd, int yyyy);
//---------------------------------------------- // functions to manipulate serial value //---------------------------------------------- void serial(int sDate); // set serial date
int serial(); // get serial date
void add(int days); // add days to the serial value
//---------------------------------------------- // functions to change or get calendar parts: //---------------------------------------------- void set_month(int mm); // set month
int month(); // get month
void set_day(int dd); // set day
int day(); // get day
void set_year(int yyyy); // set year
int year(); // get year
};//class SDate
#endif //SDATE_H_INCLUDED_
std_lib_facilities.h
/* std_lib_facilities.h */
/* simple "Programming: Principles and Practice using C++ (second edition)" course header to be used for the first few weeks. It provides the most common standard headers (in the global namespace) and minimal exception/error support.
Students: please don't try to understand the details of headers just yet. All will be explained. This header is primarily used so that you don't have to understand every concept all at once.
By Chapter 10, you don't need this file and after Chapter 21, you'll understand it
Revised April 25, 2010: simple_error() added
Revised November 25 2013: remove support for pre-C++11 compilers, use C++11:
#ifndef H112 #define H112 251113L
#include #include
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
typedef long Unicode;
//------------------------------------------------------------------------------
using namespace std;
template
struct Range_error : out_of_range { // enhanced vector range error reporting int index; Range_error(int i) :out_of_range("Range error: " + to_string(i)), index(i) { } };
// trivially range-checked vector (no iterator checking): template< class T> struct Vector : public std::vector
#ifdef _MSC_VER // microsoft doesn't yet support C++11 inheriting constructors Vector() { } explicit Vector(size_type n) :std::vector
T& operator[](unsigned int i) // rather than return at(i); { if (i<0 || this->size() <= i) throw Range_error(i); return std::vector
// disgusting macro hack to get a range checked vector: #define vector Vector
// trivially range-checked string (no iterator checking): struct String : std::string { using size_type = std::string::size_type; // using string::string;
char& operator[](unsigned int i) // rather than return at(i); { if (i<0 || size() <= i) throw Range_error(i); return std::string::operator[](i); }
const char& operator[](unsigned int i) const { if (i<0 || size() <= i) throw Range_error(i); return std::string::operator[](i); } };
namespace std {
template<> struct hash
} // of namespace std
struct Exit : runtime_error { Exit() : runtime_error("Exit") {} };
// error() simply disguises throws: inline void error(const string& s) { throw runtime_error(s); }
inline void error(const string& s, const string& s2) { error(s + s2); }
inline void error(const string& s, int i) { ostringstream os; os << s << ": " << i; error(os.str()); }
template
inline void keep_window_open() { cin.clear(); cout << "Please enter a character to exit "; char ch; cin >> ch; return; }
inline void keep_window_open(string s) { if (s == "") return; cin.clear(); cin.ignore(120, ' '); for (;;) { cout << "Please enter " << s << " to exit "; string ss; while (cin >> ss && ss != s) cout << "Please enter " << s << " to exit "; return; } }
// error function to be used (only) until error() is introduced in Chapter 5: inline void simple_error(string s) // write ``error: s and exit program { cerr << "error: " << s << ' '; keep_window_open(); // for some Windows environments exit(1); }
// make std::min() and std::max() accessible on systems with antisocial macros: #undef min #undef max
// run-time checked narrowing cast (type conversion). See ???. template
// random number generators. See 24.7.
inline int randint(int min, int max) { static default_random_engine ran; return uniform_int_distribution<>{min, max}(ran); }
inline int randint(int max) { return randint(0, max); }
//inline double sqrt(int x) { return sqrt(double(x)); } // to match C++0x
// container algorithms. See 21.9.
template
template
template
template
template
template
#endif //H112
SDate.cpp
#include "SDate.h"
// Function to set calendar date. Returns: // -- false if given month/day/year are invalid // -- true if everything is okay. bool SDate::set(int mm, int dd, int yyyy) { serial_date = serial_julian_date(mm, dd, yyyy);
return true; }
//---------------------------------------------- // functions to manipulate serial value //---------------------------------------------- void SDate::serial(int sDate) { serial_date = sDate; } // set serial date
int SDate::serial() { return 0; } // get serial date
void SDate::add(int days) { } // add days to the serial value
//---------------------------------------------- // functions to change or get calendar parts: //---------------------------------------------- void SDate::set_month(int mm) { return 0; } // set month
int SDate::month() { return 0; } // get month
void SDate::set_day(int dd) { } // set day
int SDate::day() { return 0; } // get day
void SDate::set_year(int yyyy) { } // set year
int SDate::year() { return 0; } // get year
SDate_main.cpp
/* * @topic S-0326-09-02-55 Assignment a9 Serial date class * @brief Assignment a9 test driver program */
#include
int main() { SDate date; // 7/4/1776 2369916 date.set( 7, 4, 1776 ); assert( date.serial() == 2369916 );
date.serial( 2369916 ); assert( date.serial() == 2369916 ); assert( date.month() == 7 ); assert( date.day() == 4 ); assert( date.year() == 1776 );
date.add( 1 ); // 7/5/1776 assert( date.serial() == 2369917 ); assert( date.month() == 7 ); assert( date.day() == 5 ); assert( date.year() == 1776 );
// 12/31/2000 2451910 date.set( 12, 31, 2000 ); assert( date.serial() == 2451910 ); assert( date.month() == 12 ); assert( date.day() == 31 ); assert( date.year() == 2000 );
date.add( 1 ); // 1/1/2001 assert( date.month() == 1 ); assert( date.day() == 1 ); assert( date.year() == 2001 );
assert( date.set( 2, 29, 2000 ) ); // leap year assert( date.month() == 2 ); assert( date.day() == 29 ); assert( date.year() == 2000 );
// Test validation assert( !date.set( 2, 29, 2001 ) ); // bad day assert( !date.set( 13, 31, 2001 ) ); // bad month assert( !date.set( 1, 32, 2001 ) ); // bad day
std::cout << "Oll Korrect! "; keep_window_open(); return 0; }
serial_function.cpp
/* * @topic S-0326-09-01-20 Serial date mock-up for Assignment a9 * @brief mock-up definitions for serial date functions */
#include "serial_functions.h"
// CIS-155 // Please note: // This is a mock-up implementation of the serial date. // It is used for Assignment a9 demo -- Serial date class
int serial_julian_date( int Month, int Day, int Year ) { // return integer in format YYYYMMDD return Year * 10000 + Month * 100 + Day;
// The formulas to get back to the calendar parts:
// int Day = nDate % 100; // int Month = ( nDate - Day ) / 100 % 100; // int Year = ( nDate - Month * 100 - Day ) / 10000; }//serial_julian_date()
int serial_2_day( int nDate ) { return nDate % 100; }//serial_2_day()
int serial_2_month( int nDate ) { return ( nDate - serial_2_day( nDate ) ) / 100 % 100; }//serial_2_month()
int serial_2_year( int nDate ) { return ( nDate - serial_2_month( nDate ) * 100 - serial_2_day( nDate ) ) / 10000; }//serial_2_year()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
