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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
|
//Dekota Joe
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
bool leap(int);
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);
protected:
int month;
int day;
int year;
};
class nameDate : public dateType
{
public:
nameDate();
nameDate(int, int, int);
nameDate(const nameDate&);
void setDate(int, int, int);
void setName();
friend ostream &operator <<(ostream &, const nameDate &);
friend istream &operator >>(istream &, const nameDate &);
protected:
string name;
void setName(string);
};
ostream& operator<<(ostream &os, const dateType &d) {
os << d.month << "/" << d.day << "/" << d.year;
return os;
}
istream& operator >>(istream &is, dateType &d) {
is >> d.month >> d.day >> d.year;
return is;
}
ostream& operator<<(ostream &outFile, const nameDate &d)
{
outFile << d.month << "/" << d.day << "/" << d.year << " - " << d.name;
return outFile;
}
int main() {
dateType d1;
cout << "Testing default constructor..." << endl;
d1.printDate(cout);
cout << "\nTesting preset constructor..." << endl;
dateType d2(9, 13, 2000);
d2.printDate(cout);
ofstream outFile;
outFile.open("dateProgram.txt");
if (outFile.fail()) {
cout << "\n\nFailed to open \'dateProgram.txt\'\n" << endl;
system("pause");
return 0;
}
outFile << "Dekota Brown" << endl << endl;
outFile << "\nTesting default constructor..." << endl;
d1.printDate(outFile);
outFile << "\nTesting preset constructor..." << endl;
d2.printDate(outFile);
cout << "\nTesting setDate..." << endl;
outFile << "\nTesting setDate..." << endl;
d1.setDate(3, 4, 1997);
d1.printDate(cout);
d1.printDate(outFile);
cout << "\nTesting overloaded insertion operator..." << endl;
dateType d(9, 11, 2001);
cout << d;
outFile << "\nTesting overloaded insertion operator..." << endl;
outFile << d;
ifstream inFile;
inFile.open("inputDates.txt");
if (inFile.fail()) {
cout << "\n\nFailed to open \'inputDates.txt\'\n" << endl;
system("pause");
return 0;
}
cout << endl << endl;
outFile << endl << endl;
cout << "Testing name of date constructor..." << endl;
outFile << "Testing name of date constructor..." << endl;
cout << endl;
for (int i = 0; i < 10; i++) {
inFile >> d1;
nameDate name;
outFile << d1 << endl;
cout << d1 << "\t" << name << endl;
}
outFile.close();
inFile.close();
cout << endl << endl;
system("pause");
return 0;
}
dateType::dateType() {
month = 1;
day = 1;
year = 0000;
}
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& out)const {
out << month << "/" << day << "/" << year << endl;
}
bool leap(int year)
{
return(year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
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;
case 5: total = total + 1;
break;
case 8: total = total + 2;
break;
case 6: total = total + 4;
break;
case 9:
case 12: total = total + 5;
break;
case 7:
case 4: total = total + 6;
break;
}
if (month < 3 && leap(year))
total--;
total = total + (day - 1);
total = total % 7;
switch (total) {
case 0: name = "Saturday";
break;
case 1: name = "Sunday";
break;
case 2: name = "Monday";
break;
case 3: name = "Tuesday";
break;
case 4: name = "Wednesday";
break;
case 5: name = "Thursday";
break;
case 6: name = "Friday";
break;
}
}
void nameDate::setDate(int m, int d, int y) {
month = m;
day = d;
year = y;
setName();
}
nameDate::nameDate() {
month = 1;
day = 1;
year = 0000;
name = "Saturday";
}
|