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
|
// INCLUDE:
#include <iostream> // cout, cin
#include <cctype> // toupper
using namespace std;
// CLASS:
class DateTime { // Class contains data about:
private:
int day, month, year; // - A given date and
int hour, minute, second; // - A given time.
bool leapYear(int ye);
int dayNumber(int da, int mo, int ye);
public:
DateTime(); // 2x constructores:
DateTime(int hh, int mm, int ss);
void writeDate(); // Form: dd/mm-yyyy
void writeTime(); // Form: hh:mm:ss
void writeDateTime(); // Form: dd/mm-yyyy hh:mm:ss
void readDate(); // Reads new date.
void readTime(); // Reads new time.
void readDateTime(); // Reads new date and new time.
bool sameDate(const DateTime t); // Same date or not.
DateTime timeDifference(const DateTime tt); // Time difference between two "time"s, same date
int DateDifference(const DateTime tt); // Number of days between two "date"s
};
// Declaration of functions:
void writeMenu();
char readCommand();
int read(const int min, const int max);
// ******************** M A I N P R O G R A M : ************************
int main() {
DateTime dt1, dt2, dt3; // Three DateTime-objects.
char choice; // Users choice/command.
int nr; // Which of dt1 and dt2 should be changed.
writeMenu(); // Writes/prints user-menu.
choice = readCommand(); // Reads users choice/command.
// This i Haven't filled in yet
while (choice != 'Q' && choice != 'A') { // as long as, don't quite:
switch (choice) {
// Prints out both "DateTime"s (dt1 and dt2)(= date + time)
case 'S': /* fill in */ () break;
// Change date (either dt1 or dt2)
case 'D': /* fill in */ () break;
// Change time (either dt1 or dt2)
case 'P': /* fill in */ () break;
// Change DateTime (= date + time) (either dt1 or dt2)
case 'T': /* fill in */ () break;
// checks if dates in dt1 and dt2 are the same
// if not print out a massage saying so.
// But if it is the same date, find the time difference
case 'X': /* fill in */ () break;
// prints number of days between dt1 and dt2
case 'Y': /* fill in */ () break;
default: writeMenu(); break;
}
choice = readCommand(); // Reads again users choice/command.
}
cout << "\n\n";
return 0;
}
// ******************* DEFINITION OF CLASS-FUNCTIONS: ********************
bool DateTime::leapYear(int ye) { // Checks if leap year:
if ((ye % 400 == 0) || ((ye % 4 == 0) && (ye % 100) != 0))
return true; // Leap year if: (dividable by 400)
else // OR (dividable by 4 AND not with 100)
return false;
}
int DateTime::dayNumber(int da, int mo, int ye) {
// Puts up number of days in month.
// Value for February is set later.
int daysPrMonth[12] = { 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int totalDaynr, i;
if (ye < 1600 || ye > 2100) return 0; // not allowed year.
if (mo < 1 || mo > 12) return 0; // not allowed month
// depending on wether or not the year is a leap year,
// the value of February is set:
daysPrMonth[1] = (leapYear(ye)) ? 29 : 28;
if (da < 1 || da > daysPrMonth[mo - 1]) return 0; // not allowed day:
// Guarantied an allowed date !!
totalDaynr = da;
for (i = 0; i < mo - 1; i++) // Calculated the date's daynumber.
totalDaynr += daysPrMonth[i];
return totalDaynr; // Returns daynumber.
}
DateTime::DateTime() // 2x constructores, that sets valus:
:day(1), month(1), year(2000), hour(0), minute(0), second(0)
{
}
DateTime::DateTime(int hh, int mm, int ss)
: day(1), month(1), year(2000), hour(hh), minute(mm), second(ss)
{
}
void DateTime::writeDate() // Form: dd/mm-yyyy
{
cout << day << "/" << month << "-" << year << endl;
}
void DateTime::writeTime() // Form: hh:mm:ss
{
cout << ((hour < 10) ? "0" : "") << hour
<< ((minute < 10) ? "0" : "") << minute
<< ((second < 10) ? "0" : "") << second << endl;
}
void DateTime::writeDateTime() // Form: dd/mm-yyyy hh:mm:ss
{
writeDate();
writeTime();
}
void DateTime::readDate() // reads new date:
{
writeDate();
cout << "Input day (1-31)\n";
day = read(1, 31);
cout << "Input month (1-12)\n";
month = read(1, 12);
cout << "Input year (1600-2100)\n";
year = read(1600, 2100);
dayNumber(day, month, year);
}
void DateTime::readTime() // Reads new time:
{
writeTime();
cout << "Input hour (0-23)\n";
hour = read(0, 23);
cout << "Input minute (0-59)\n";
minute = read(0, 59);
cout << "Input second (0-59)\n";
second = read(0, 59);
}
void DateTime::readDateTime() // Reads new date and new time:
{
readDate();
readTime();
}
bool DateTime::sameDate(const DateTime t) // Same date or not:
{
return(day == t.day && month == t.month && year == t.year)
}
// difference between to "time"s
DateTime DateTime::timeDifference(const DateTime tt)
{
// Gave up here.
// The thought was: turn it into seconds, then back into the "time" format
int timeSec, tempH, tempM, tempS;
timeSec = ((hour * 60) + minute) * 60;
tempM = timeSec / 60;
tempS = timeSec % 60;
tempH = tempM / 60;
tempM = tempM % 60;
return DateTime(tempH, tempM, tempS)
}
// Number of days between "date"s
int DateTime::DateDifference(const DateTime tt)
{ // No idea what do do here
}
// *********************** DEFINITION OF FUNCTIONS: ***********************
void writeMenu() { // Prints the user's choices/commands:
cout << "\n\nFollowing commands are allowed:\n";
cout << "\tS - Prints both \"DateTime\"s\n";
cout << "\tD - Date-input\n";
cout << "\tP - Time-input\n";
cout << "\tT - DateTime-input (= Date + Time)\n";
cout << "\tX - Time-difference (when same date)\n";
cout << "\tY - Date-difference\n";
cout << "\tQ/A - Quit / Avslutt\n";
}
char readCommand() { // Reads user's choice/command:
char ch;
cout << "\n\nGive command (S, D, P, T, X, Y, Q/A): "; // Asks for char
cin >> ch; ch = toupper(ch); // Reads and upcaser char.
return ch; // Returns char/command.
}
// Reads and returns value in [MIN, MAX]:
int read(const int min, const int max) {
int numb; // Written number variable.
cin >> numb; // Reads number.
while (numb < min || numb > max) { // outside allowed interval:
cout << "\tinside interval (" << min << '-' << max << "): ";
cin >> numb;
}
return numb; // Returns allowed number
}
|