
Patrick B. answered 04/18/21
Math and computer tutor/teacher
using namespace std;
#include <iostream>
#include <fstream>
#include <string>
#include <string.h>
#include <stdlib.h>
#include <vector>
#include <ctype.h>
typedef struct _TDate
{
int month;
int year;
int day;
} *TDate;
int MonthNum(string monthName)
{
int iReturn=0;
if (monthName.compare("JANUARY"))
{
iReturn=1;
}
if (monthName.compare("FEBRUARY"))
{
iReturn=2;
}
if (monthName.compare("MARCH"))
{
iReturn=3;
}
if (monthName.compare("APRIL"))
{
iReturn=4;
}
if (monthName.compare("MAY"))
{
iReturn=5;
}
if (monthName.compare("JUNE"))
{
iReturn=6;
}
if (monthName.compare("JULY"))
{
iReturn=7;
}
if (monthName.compare("AUGUST"))
{
iReturn=8;
}
if (monthName.compare("SEPTEMBER"))
{
iReturn=9;
}
if (monthName.compare("OCTOBER"))
{
iReturn=10;
}
if (monthName.compare("NOVEMBER"))
{
iReturn=11;
}
if (monthName.compare("DECEMBER"))
{
iReturn=1;
}
return(iReturn);
}
bool IsLeapYear (int year)
{
bool boolReturn=false;
if (year%4==0)
{
boolReturn = (year%400!=0);
}
return(boolReturn);
}
void Uppercase(char * str)
{
int n=strlen(str);
for (int iLoop=0; iLoop<n; iLoop++)
{
str[iLoop]= toupper(str[iLoop]);
}
}
int validDate(TDate dateRec)
{
int iReturn=0;
if ((dateRec->day<1) || (dateRec->month<1) || (dateRec->year<1) || (dateRec->day>31) || dateRec->month>12)
{
iReturn=-1;
}
else
{
switch (dateRec->month)
{
case 4:
case 9:
case 6:
case 11:
{
if (dateRec->day >30)
{
iReturn -= 2;
}
break;
}
case 2:
{
if (IsLeapYear(dateRec->year))
{
if (dateRec->day>29)
{
iReturn-=3;
}
}
else
{
if (dateRec->day>28)
{
iReturn-=3;
}
}
break;
}
default:
{
if (dateRec->day>31)
{
iReturn=-4;
}
}
} //switch
}
return(iReturn);
}
int parseDate(string dateStr, TDate dateRec)
{
int iReturn=0;
char monthStr[25];
char dayStr[25];
char yearStr[25];
char monthDayStr[255];
int commaPos = dateStr.find(",");
if (commaPos>-1)
{
strcpy(monthDayStr,
dateStr.substr(0,commaPos-1).c_str()
);
strcpy(monthStr,strtok(monthDayStr," /t"));
strcpy(dayStr,strtok(NULL," \t"));
Uppercase(monthStr);
strcpy(yearStr,
dateStr.substr(commaPos+1).c_str()
);
//cout << " month >" << monthStr << "<" << endl;
//cout << " day >" << dayStr << "<" << endl;
//cout << " year >" << yearStr << "<" << endl;
dateRec->month = MonthNum(monthStr);
dateRec->day = atoi(dayStr);
dateRec->year = atoi(yearStr);
}
else
{
iReturn=-1;
}
return(iReturn);
}
Go()
{
string inbuff="?";
struct _TDate dateRec;
vector<struct _TDate> vecDates;
while (inbuff.compare("-1"))
{
cout << "input the date :>";
getline(cin,inbuff);
if (parseDate(inbuff,&dateRec)==0)
{
if (validDate(&dateRec)==0)
{
vecDates.push_back(dateRec);
}
}
}
int n=vecDates.size();
for (int iLoop=0; iLoop<n; iLoop++)
{
struct _TDate curDate;
curDate = vecDates[iLoop];
cout << curDate.month << "/" << curDate.day << "/" << curDate.year << endl;
}
}
int main()
{
Go();
}