Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

use c++ to write this program This system of programs implements and tests a cla

ID: 3812715 • Letter: U

Question

use c++ to write this program

This system of programs implements and tests a class that encapsulates various functions related to dates. He are the specifications 1. Store the month, day, and year in separate private integer variables 2. Your My Date class will have two constructors. The first one takes no arguments and sets the date to March 27, 2017. The second takes a string in the form MM/DD/YYYY and sets the date to that 3. Provide accessor functions for the month, day, and year. The mutator functions do not do error checking, since that is beyond the scope of this assignment. Your driver program, described below, will do limited error checking 4. Provide a function called SetDate that takes a string in the form MM/DDYYYY and sets the new date. This will return true if the date is valid and false if it is not. However, you can assume that the string will always be in the correct format, with slashes between the components. For example, the function should return false for 3/32/2017 5. Provide a function ToString that takes no parameters and returns the date as a string in the form MM/DD/YYYY 6. Provide an add function that takes an integer as its parameter and adds that number of days to the given date. It returns a string in the form MM/DD/YYYY with the new date 7. Provide a subtract function that takes an integer as its parameter and subtracts that number of days from the given date

Explanation / Answer

Here is the class for you:

// Date.h
#ifndef DATE_H
#define DATE_H
#include <iostream>
#include <string>
#include <ctime>
#include <sstream>
#include <iomanip>
#include <fstream>
using namespace std;
static string name[] = { "", "Jan","Feb", "Mar", "Apr","May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
static string week[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
static int days[2][13] = { { 0,31,28,31,30,31,30,31,31,30,31,30,31 },{ 0,31,29,31,30,31,30,31,31,30,31,30,31 } };
class Date {
int *day;
int *month;
int *year;

public:
Date(); // default c'tor set to the date of
Date(int dd, int mm, int yy);
Date(Date &other); // copy c'tor
Date(int yy); // use the Jan 1st as the default day and month
Date(int doy, int yy); // doy=day of the year
~Date(); // d'tor
int getDay();
int getMonth();
int getYear();
// overload operators
Date & operator= (Date & rhs); // assignment operator
Date & operator++(); // prefix ++
Date & operator ++ (int); // postfix ++
Date & operator --(); // prefix --
Date & operator -- (int); // postfix --
int operator - (Date & other); // returns the number of days between this and other
Date & operator + (int n); // n can be positive or negative
bool operator > (Date & rhs);
bool operator < (Date & rhs);
bool operator ==(Date & rhs);
friend ostream & operator << (ostream & out, Date& rhs);
friend istream &operator >> (istream & in, Date &rhs);

// other auxilary functions
int isLeap(int year); // return 0 if given year is not a leap year, 1 otherwise
int day_code(int year); // return the day code for the new year day of given year [ 0 - 6 ] 0 means sunday ..
int day_code(int dd, int mm, int yy);
int day_code(); // return the day code for the new year day of this->year
void print(int yy, ostream& out); // print the calendar for the given year
void print(int yy, int mm, ostream& out); // print the calendar for the give n month of a given year
string toString(); // mm/dd/yyyy format
string toString(int type); // Jan 01 , 2017 or SUn Jan 01, 2017
int dayOfYear(); //
};
Date::Date() { // default c'tor
// put your code here
month = new int(1);
day = new int(1);
year = new int(2017);
}
Date::Date(int dd, int mm, int yy) {
// put your code here
// this->day = new int(dd);
// this->month = new int(mm);
// this->year = new int(yy);
day = new int(dd);
month = new int(mm);
year = new int(yy);
}
Date::Date(Date &other) {
// put your code here
// month = other.month;
// day = other.day;
// year = other.year;
day = new int(other.getDay());
month = new int(other.getMonth());
year = new int(other.getYear());
}
Date::Date(int yy) { // use the Jan 1st as the default day and month
// put your code here
day = new int(1);
month = new int(1);
year = new int(yy);
}
Date::Date(int doy, int yy) { // doy=day of the year
// put your code here
day = new int(doy);
month = new int(1);
year = new int(yy);
}
Date::~Date() { // d'tor
// put your code here
delete(month);
delete(day);
delete(year);
}
int Date::getDay() {
return *day;
}
int Date::getMonth() {
return *month;
}
int Date::getYear() {
return *year;
}
// overload operators
Date & Date::operator= (Date & rhs) { // assignment operator
// put your code here
if (this != &rhs) {
month =new int(rhs.getMonth());
day = new int(rhs.getDay());
year = new int(rhs.getYear());
}
return *this;
}

bool Date::operator > (Date & rhs) {
// put your code here
if (*year != rhs.getYear())
return(*year > rhs.getYear());
else if (*month != rhs.getMonth())
return (*month > rhs.getMonth());
else
return (*day > rhs.getDay());
}
bool Date::operator <(Date & rhs) {
// put your code here
if (*year != rhs.getYear())
return(*year < rhs.getYear());
else if (*month != rhs.getMonth())
return (*month < rhs.getMonth());
else
return (*day < rhs.getDay());
}
Date & Date::operator++() { // prefix ++
// put your code here
int dayofmonth[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (isLeap(*year)) dayofmonth[2] = 29;
if (*month == 12 && *day == 31) {
*day = 1;
*month = 1;
*year += 1;
}else if (*month != 12 && *day == dayofmonth[*month]) {
*day = 1;
*month += 1;
}else
*day += 1;
return *this;
}
Date & Date::operator ++ (int) { // postfix ++
// put your code here
int daysofmonth[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (isLeap(*year)) daysofmonth[2] = 29;
Date *tmp;
tmp = new Date(*this);
  
if (*month == 12 && *day == 31) {
*day = 1;
*month = 1;
*year += 1;
}else if (*month != 12 && *day == daysofmonth[*month]) {
*day = 1;
*month += 1;
}else
*day += 1;
return *tmp;
}
Date & Date::operator --() { // prefix --
// put your code here
int dayofmonth[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (isLeap(*year)) dayofmonth[2] = 29;
if (*month == 1 && *day == 1) {
*day = 31;
*month = 12;
*year -= 1;
}else if (*month != 1 && *day == 1) {
*month -= 1;
*day = dayofmonth[*month];
}
else
*day -= 1;
return *this;
}
Date & Date::operator -- (int) { // postfix --
// put your code here
int dayofmonth[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (isLeap(*year)) dayofmonth[2] = 29;
Date *tmp;
tmp = new Date(*this);
if (*month == 1 && *day == 1) {
*day = 31;
*month = 12;
*year -= 1;
}else if (*month != 1 && *day == 1) {
*month -= 1;
*day = dayofmonth[*month];
}else
*day -= 1;
return *tmp;
}
int Date::operator - (Date & other) { // returns the number of days between this and other
// put your code here
int i = 0;
while (operator >(other)) {
operator--();
i++;
}
while (operator <(other)) {
operator++();
i--;
}
return i;
}

Date & Date::operator + (int n) { // n can be positive or negative
// put your code here
for (int i = 0; i < abs(n); i++) {
operator++();
}
return *this;
}
bool Date::operator ==(Date & rhs) {
// put your code here
return(*month == rhs.getMonth() && *day == rhs.getDay() && *year == rhs.getYear());
}
ostream & operator << (ostream & out, Date& rhs) {
out << rhs.toString() << endl;
return out;
}
istream &operator >> (istream & in, Date &rhs) {
in >> *(rhs.month) >> *(rhs.day) >> *(rhs.year);
return in;
}
// other auxilary functions
int Date::isLeap(int year) {
// put your code here
return ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0));
}
int Date::day_code(int year) {
// put your code here
return (year + (year - 1) / 4 - (year - 1) / 100
+ (year - 1) / 400) % 7;
}

int Date::day_code(int dd, int mm, int yy) {
// put your code here
int d = 0;
int daycode;
int daysofmonth[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (isLeap(yy)) daysofmonth[2] = 29;
  
for (int i = 1; i < mm; i++)
d += daysofmonth[i];
  
d += dd;
daycode = (day_code(yy) + d - 1) % 7;
return daycode;
}
int Date::day_code() {
// put your code here
return day_code(*year);
}
void Date::print(int yy, ostream& out) { // print the calendar for the given year
// put your code here
int c, d;
int daysofmonth[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (isLeap(yy)) daysofmonth[2] = 29;
out << year;
for (int i = 1; i < 13; i++) {
out << endl << endl << daysofmonth[i] << endl << endl;
out << setw(3) << "Sun" << setw(5) << "Mon" << setw(5) << "Tue" << setw(5) << "Wed" << setw(5) << "Thu" << setw(5) << "Fri" << setw(5) << "Sat" << endl;
int j = 1;
c = day_code(j, i, yy);
if (c == 0) out << setw(3) << j;
else {
out << setw(3 + 5 * c) << j;
}
for (int j = 2; j < daysofmonth[i] + 1; j++) {
d = day_code(j, i, yy);
if (d % 7 == 0) {
out << endl;
out << setw(3) << j;
}
else out << setw(5) << j;
}
}
}

void Date::print(int yy, int mm, ostream& out) {// print the calendar for the give n month of a given year
// put your code here
int c, d;
int daysofmonth[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (isLeap(yy)) daysofmonth[2] = 29;
out << year;
out << month;
for (int i = 1; i < 13; i++) {
if (daysofmonth[i]==mm) {
// out << endl << endl << daysofmonth[i] << endl << endl;
out << setw(3) << "Sun" << setw(5) << "Mon" << setw(5) << "Tue" << setw(5) << "Wed" << setw(5) << "Thu" << setw(5) << "Fri" << setw(5) << "Sat" << endl;
int j = 1;
c = day_code(j, i, yy);
if (c == 0) out << setw(3) << j;
else {
out << setw(3 + 5 * c) << j;
}
for (int j = 2; j < daysofmonth[i] + 1; j++) {
d = day_code(j, i, yy);
if (d % 7 == 0) {
out << endl;
out << setw(3) << j;
}
else out << setw(5) << j;
}
}
}
}

string Date::toString() { // mm/dd/yyyy format
ostringstream convert; // stream used for the conversion
convert << *month << "/" << *day << "/" << *year;
return convert.str();
}

string Date::toString(int type) { // type 1 : Jan 01 , 2017 or type 2: SUn Jan 01, 2017
// put your code here
string tmp;
char buf[80];
int week_day = day_code(*year);
sprintf_s(buf, "%d %d , %d", *month, *day, *year);
if (type == 1)
tmp = string(buf);
else if (type == 2)
tmp = string(buf + week[week_day]);

  
return tmp;
  
}
int Date::dayOfYear() {
// put your code here
int doy = 0;
int daysofmonth[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (isLeap(*year)) daysofmonth[2] = 29;
for (int i = 1; i < *month; i++) {
doy += daysofmonth[i];
}
doy += *day;
return doy;
}
#endif