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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
//Letty Jolley
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
using namespace std;
class dateType
{
public:
dateType();
dateType(int, int, int);
void setDate(int, int, int);
void printDate(ostream &)const;
friend ostream &operator<<(ostream& os, const dateType& d);
friend istream& operator>>(istream& is, dateType& d);
private:
int month, day, year;
};
dateType::dateType()
{
month = 1;
day = 1;
year = 00;
}
dateType::dateType(int myMonth, int myDay, int myYear)
{
month = myMonth;
day = myDay;
year = myYear;
}
void dateType::setDate(int myMonth, int myDay, int myYear)
{
month = myMonth;
day = myDay;
year = myYear;
}
void dateType::printDate(ostream & outFile)const
{
outFile << month << '/' << day << '/' << year << endl;
}
ostream&operator<<(ostream& os, const dateType& d)
{
os << d.month << '/' << d.day << '/' << d.year << endl;
return os;
}
istream& operator>>(istream& is, dateType& d)
{
is >> d.month >> d.day >> d.year;
return is;
}
class nameDate : public dateType
{
public:
nameDate();
nameDate(int, int, int);
nameDate(const nameDate&);
void setDate(int, int, int);
bool leapYear(int) const;
friend ostream &operator<<(ostream&, const nameDate&);
friend istream &operator>>(istream& in, nameDate& nd);
private:
int month;
int day;
int year;
string name;
void setName();
};
void nameDate::setDate(int m, int d, int y)
{
month = m;
day = d;
year = y;
setName();
}
bool nameDate::leapYear(int testYear)const
{
if (testYear % 400 == 0 ||
(testYear % 100 != 0 && testYear % 4 == 0))
return true;
else
return false;
}
nameDate::nameDate()
{
month = 1;
day = 1;
year = 0;
name = "Saturday";
}
istream& operator>>(istream & in, nameDate&nd)
{
in >> nd.month >> nd.year;
nd.setName();
return in;
}
void nameDate::setName()
{
int total;
total = year + year / 4 - year / 100 + year / 400 + 1;
switch (month)
{
case 1:
case 10:
break;
case 2:
case 3:
case 11:
total = total + 3;
break;
}
if (month < 3 && leapYear(year))
total--;
total = total + (day - 1);
total = total % 7;
switch (total)
{
case 0: name = "Saturday";
break;
case 1: name = "Saturday";
break;
}
}
int main()
{
dateType d1;
d1.printDate(cout);
dateType d2(1, 10,1900);
d2.printDate(cout);
ofstream outFile;
outFile.open("date2test.txt");
if (outFile.fail())
{
cout << "open failed";
system("pause");
return 0;
}
ifstream inFile("date2test.txt");
outFile << "Letty Jolley" << endl << endl;
outFile << "Testing default constructor..." << endl;
d1.printDate(outFile);
outFile << "Testing second constructor..." << endl;
d2.printDate(outFile);
outFile << "Testing setDate..." << endl;
d1.setDate(3, 16, 1900);
d1.printDate(cout);
d1.printDate(outFile);
outFile << d1;
d1.printDate(cout);
outFile << d1;
cout << d1;
outFile.close();
system("pause");
return 0;
}
|