1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
#include "DayOfYear.h"
#include <cmath>
#include <iostream>
using namespace std;
//Set days of each month into an array
const int DayOfYear::MonthDay[] = {31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
//Set the name of each month into an array
const string DayOfYear::Month[] = {"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December"};
//************************************************
// Overloaded prefix ++ operator. Causes the *
// inched member to be incremented. Returns the *
// incremented object. *
//************************************************
DayOfYear DayOfYear::operator++()
{
++day;
simplify();
return *this;
}
//*************************************************
// Overloaded postfix ++ operator. Causes the *
// inches member to be incremented. Returns the *
// value of the object before the increment. *
//*************************************************
DayOfYear DayOfYear::operator++(int)
{
DayOfYear temp(months, day);
day++;
simplify();
//cout<<day;
return temp;
}
//*******************************************************
// Overloaded postfix -- operator. *
//*******************************************************
DayOfYear DayOfYear::operator--(int)
{
string month;
DayOfYear temp(month, day);
day--;
if (day == -1) // <-------------- fix to ... if (day ==-1)
{ day = day + 365; }
return temp;
}
void DayOfYear::simplify()
{
string month;
//Incrementors
for(int index=0;index<12;index++){
month=DayOfYear::Month[index];
if (month== "April" ||month== "June" || month=="September" ||month== "November" && day > 30)
{
month += (day / 30);
day = (day % 30);
}
else if ( month =="January"||month=="March"||month=="May" ||month=="July" ||month=="August" ||month=="October" && day > 31)
{
month += (day / 31);
day = day % 31;
}
else if ( month == "Feburary" && day>28 )
{
month += (day / 28);
day = day % 28;
}
else if (month == "December" && day > 31)
{
month = (day / 31);
day = day % 31;
}
}
}
/*
//Decrementors
else if (month== "April" ||month== "June" || month=="September" ||month== "November" && day < 1)
{
month -= ((abs(day) / 30) + 1);
day = day - (abs(day) % 30);
}
else if (month=="March"||month=="May" ||month=="July" ||month=="August" ||month=="October"||month == "December" && day < 1)
{
month -= ((abs(day) / 31) +1);
day = 31 - (abs(day) % 31);
}
else if (month == "Feburary" && day < 1)
{
month -= ((abs(day) / 28) +1);
day = 28 - (abs(day) % 28);
}
else if (month =="January" && day < 1)
{
month -= ((abs(day) / 31) + 1);
day = 31 - (abs(day) % 31);
}
}
}
*/
|