Please write the code using C++ and you Can download the files from here https:/
ID: 3839226 • Letter: P
Question
Please write the code using C++ and you Can download the files from here
https://ufile.io/7540a
https://ufile.io/94b50
A. Start with Chrono.h and Chrono.cpp ; for now put a dummy "return sunday;" or "return d;" in the last 3 functions on pages 337-338. Add a prefix ++ operator to the Date class by putting the declaration in the Date class in Chrono.h and the definition in Chrono.cpp. The prefix ++ operator should change a date to "tomorrow" using the following
pseudocode:
Date& operator++(){
//add 1 to d //tomorrow, unless we were at the end of the month
//if is_date is false
// //need to change to first of next month
// set d to 1
// if m is December
// //need to change to next year too
// set m to January
// increment y
// else
// increment m
return *this;
}
Write a main which repeatedly reads a Date with cin >> in a while statement, increments the Date with your ++, and prints "Tomorrow is" and the new value of the Date using cout <<.
B. Write another seperate code to Add a postfix ++ operator to part A, and modify the main to show that it works, by putting
cin >> d1;
d2 = d1; //d2 == old value of d1
d3 = d1++; //d3 == old value of d1, d1 == new value of d1
if(d2 != d3)error("error in postfix ++");
if(d2 == d1)error("error in postfix ++");
if(d1 != ++d2)error("error in postfix ++");
cout << "Tomorrow is " << d1 << ' ';
in a loop. Put the main in hw4pr4.cpp and turn in all 3 files in one zip file according to your TA's instructions. Note: To declare a postfix ++ member function, use
Date operator++(int);
The "int" is not used, it just tells the C++ compiler this is postfix ++.
Explanation / Answer
As you have not provided me full code here is my attempt for providing you with operator ++
// Add this in Chrono.cppp
void Date::add_day(int n)
{
++d;
if(!is_date(y, m, d)) {
d = 1;
if(m == dec) {
m = jan;
++y;
}
else
m = Month(m+1);
}
}
Date& operator++(Date& d)
{
d.add_day(1);
return d;
}
Date operator++(Date& d1, int)
{
Date d2 = d1;
d1.add_day(1);
return d2;
}
// Add this in Chrono.h
Date& operator++(Date& d);
Date operator++(Date& d1, int);
// Chrono.cpp
#include "Chrono.h"
namespace Chrono {
// member function definitions:
//------------------------------------------------------------------------------
Date::Date(int yy, Month mm, int dd)
: y(yy), m(mm), d(dd)
{
if (!is_date(yy,mm,dd)) throw Invalid();
}
//------------------------------------------------------------------------------
const Date& default_date()
{
static const Date dd(2001,Date::jan,1); // start of 21st century
return dd;
}
//------------------------------------------------------------------------------
Date::Date()
:y(default_date().year()),
m(default_date().month()),
d(default_date().day())
{
}
//------------------------------------------------------------------------------
void Date::add_day(int n)
{
++d;
if(!is_date(y, m, d)) {
d = 1;
if(m == dec) {
m = jan;
++y;
}
else
m = Month(m+1);
}
}
//------------------------------------------------------------------------------
void Date::add_month(int n)
{
// ...
}
//------------------------------------------------------------------------------
void Date::add_year(int n)
{
if (m==feb && d==29 && !leapyear(y+n)) { // beware of leap years!
m = mar; // use March 1 instead of February 29
d = 1;
}
y+=n;
}
//------------------------------------------------------------------------------
// helper functions:
bool is_date(int y, Date::Month m, int d)
{
// assume that y is valid
if (d<=0) return false; // d must be positive
int days_in_month = 31; // most months have 31 days
switch (m) {
case Date::feb: // the length of February varies
days_in_month = (leapyear(y))?29:28;
break;
case Date::apr: case Date::jun: case Date::sep: case Date::nov:
days_in_month = 30; // the rest have 30 days
break;
}
if (days_in_month<d) return false;
return true;
}
//------------------------------------------------------------------------------
bool leapyear(int y)
{
// See exercise ???
return false;
}
//------------------------------------------------------------------------------
bool operator==(const Date& a, const Date& b)
{
return a.year()==b.year()
&& a.month()==b.month()
&& a.day()==b.day();
}
//------------------------------------------------------------------------------
bool operator!=(const Date& a, const Date& b)
{
return !(a==b);
}
//------------------------------------------------------------------------------
ostream& operator<<(ostream& os, const Date& d)
{
return os << '(' << d.year()
<< ',' << d.month()
<< ',' << d.day()
<< ')';
}
//------------------------------------------------------------------------------
istream& operator>>(istream& is, Date& dd)
{
int y, m, d;
char ch1, ch2, ch3, ch4;
is >> ch1 >> y >> ch2 >> m >> ch3 >> d >> ch4;
if (!is) return is;
if (ch1!='(' || ch2!=',' || ch3!=',' || ch4!=')') { // oops: format error
is.clear(ios_base::failbit); // set the fail bit
return is;
}
dd = Date(y,Date::Month(m),d); // update dd
return is;
}
//------------------------------------------------------------------------------
Date& operator++(Date& d)
{
d.add_day(1);
return d;
}
//------------------------------------------------------------------------------
Date operator++(Date& d1, int)
{
Date d2 = d1;
d1.add_day(1);
return d2;
}
//------------------------------------------------------------------------------
enum Day {
sunday, monday, tuesday, wednesday, thursday, friday, saturday
};
//------------------------------------------------------------------------------
Day day_of_week(const Date& d)
{
// ...
return sunday;
}
//------------------------------------------------------------------------------
Date next_Sunday(const Date& d)
{
// ...
return d;
}
//------------------------------------------------------------------------------
Date next_weekday(const Date& d)
{
// ...
return d;
}
//------------------------------------------------------------------------------
} // Chrono
//------------------------------------------------------------------------------
// main() is declared in hw4pr2 and hw4pr4
int main()
{
Chrono::Date date;
while(cin >> date)
{
date++;
cout << "Tomorrow is " << date;
}
Chrono::Date d1;
while(cin >> d1)
{
Chrono::Date d2 = d1; //d2 == old value of d1
Chrono::Date d3 = d1++; //d3 == old value of d1, d1 == new value of d1
if(d2 != d3) cout << "error in postfix ++";
if(d2 == d1) cout < "error in postfix ++";
if(d1 != ++d2) cout << "error in postfix ++";
cout << "Tomorrow is " << d1 << ' ';
}
return 0;
}
//------------------------------------------------------------------------------
// Chrono.h
#include <iostream>
using namespace std;
//------------------------------------------------------------------------------
namespace Chrono {
//------------------------------------------------------------------------------
class Date {
public:
enum Month {
jan=1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec
};
class Invalid { }; // to throw as exception
Date(int y, Month m, int d); // check for valid date and initialize
Date(); // default constructor
// the default copy operations are fine
// non-modifying operations:
int day() const { return d; }
Month month() const { return m; }
int year() const { return y; }
// modifying operations:
void add_day(int n);
void add_month(int n);
void add_year(int n);
private:
int y;
Month m;
int d;
};
//------------------------------------------------------------------------------
bool is_date(int y, Date::Month m, int d); // true for valid date
//------------------------------------------------------------------------------
bool leapyear(int y); // true if y is a leap year
//------------------------------------------------------------------------------
bool operator==(const Date& a, const Date& b);
bool operator!=(const Date& a, const Date& b);
//------------------------------------------------------------------------------
ostream& operator<<(ostream& os, const Date& d);
istream& operator>>(istream& is, Date& dd);
//------------------------------------------------------------------------------
Date& operator++(Date& d);
Date operator++(Date& d1, int);
//------------------------------------------------------------------------------
} // Chrono