|
Programming Diploma in Assembler, Fortran, Basic C++, JavaScript,HTML, Visual Basic 6.0
//Michael Wetherwax //Date Class implementaion - C++ #include #include #include #include #include #include #include "Date.h" #include"Person.h"
const int Date::monthDays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
Date::Date() //:month(0),day(0),year(0) {} Date::~Date() {}
void Date::getDate() { Date tday; char buffer3[3]; char buffer4[3]; char buffer5[5]; int answer = 0; // int mo, da, yr, thisyear; do { cout<<"\nEnter month number : "; cin<<buffer3; answer = isValidbmoDigit(buffer3); if (answer == 1) strcpy (m, buffer3); mo = atoi(m); cout<<"Mo = "<<mo; } while((answer == 0) || ((mo > 12) || (mo < 1))); answer = 0; do { cout<<"\nEnter day number : "; cin<<buffer4; answer = isValidbdayDigit(buffer4); if (answer == 1) strcpy (d, buffer4);
da = atoi(d); cout<<"Da = "<<da; } while((answer == 0) || ((da > 31) || (da < 1))); answer = 0;
do { cout<<"\nEnter year number(xxxx) : "; cin<<buffer5; answer = isValidbyearDigit(buffer5); if (answer == 1) { strcpy (y, buffer5); } yr = atoi(y); cout<<"yr = "<<yr; tday = tday.callToday(); thisyear = tday.printYear(); if (yr > thisyear) { cout<<"\nInvalid year!"; } } while ((answer == 0) || (yr > thisyear)); }
int Date::isValidbmoDigit(char numb[] ) { int digit3 = 0; int nondigit3 = 0; for (unsigned j = 0; j < strlen(numb); j++) { if (isdigit(numb[j])) digit3++; if (! isdigit(numb[j])) nondigit3++; } if ((nondigit3 > 0)||(digit3 >2)) { cout<<"Month must be no more than 2 numbers. Enter again!\n"; return 0; } else return 1; }
int Date::isValidbdayDigit(char numb[] ) { int digit4 = 0; int nondigit4 = 0; for (unsigned j = 0; j < strlen(numb); j++) { if (isdigit(numb[j])) digit4++; if (! isdigit(numb[j])) nondigit4++; } if ((nondigit4 > 0)||(digit4 > 2)) { cout<<"Day must be no more than 2 . Enter again!\n"; return 0; } else return 1; }
int Date::isValidbyearDigit(char numb[] ) { int digit5 = 0; int nondigit5 = 0; for (unsigned j = 0; j < strlen(numb); j++) { if (isdigit(numb[j])) digit5++; if (! isdigit(numb[j])) nondigit5++; } if ((nondigit5 > 0)||(digit5 != 4)) { cout<<"Year must be 4 numbers. Enter again!\n"; return 0; } else return 1; } void Date::printDate() { cout <<mo << "/" <<da<< "/ " <<yr<<"\n"; }
Date Date::callToday() { Date today; struct tm *tptr; time_t t1; time(&t1); tptr = localtime(&t1); tptr-> tm_mon; tptr-> tm_mday; tptr-> tm_year; today.month = (*tptr).tm_mon + 1; today.day = (*tptr).tm_mday;
today.year = (*tptr).tm_year + 1900; //border('*', 60, 1); //cout<<"\nToday is : "<<today.month<<" "<<today.day <<" "<<today.year<<endl; return today; }
void Date::printToday() { Date today2; struct tm *tptr; time_t t1; time(&t1); tptr = localtime(&t1); tptr-> tm_mon; tptr-> tm_mday; tptr-> tm_year; today2.month = (*tptr).tm_mon + 1; today2.day = (*tptr).tm_mday; today2.year = (*tptr).tm_year + 1900; border('*', 60, 1); cout<<"\nToday is : "<<today2.month<<" "<<today2.day <<" "<<today2.year<<endl; //return today; }
int Date::printYear() { return year; }
Date Date:: operator- (Date d1) { int yr =0; int mon = 0; int x1 = 0, x2 = 0, diff, i = 0; Date result; x1 = (year * 365) + getmonthDays() + day; x2 = (d1.year * 365) + d1.getmonthDays() +d1.day; diff = x1 - x2;
while (diff >= 365) { yr++; diff-65; } while (diff > 28) { mon++; diff -= monthDays[i]; i++; } result.year = yr; result.month = mon; result.day = diff; return result; }
int Date::operator> (Date d1) { int x1, x2; x1 = (year * 365) + getmonthDays() + day; x2 = (d1.year * 365) + d1.getmonthDays() + d1.day; if (x1 > x2) return 1; else return 0; }
int Date::operator==(Date d1) { if((year == d1.year) && (month == d1.month) && (day == d1.day)) return 1; else
return 0; }
void Date::operator = (Date d1) { month = d1.month; day = d1.day; year = d1.year; }
int Date::getmonthDays(){ int total = 0; for(int i = 0; i < month -1; i++) total += monthDays[i]; if(((year % 400) == 0) || ((year % 100 ) && (year % 4 == 0))) total = total + 1; return total;
|