The following C++ code is used to testing for a leap year. However, this test is
ID: 3635578 • Letter: T
Question
The following C++ code is used to testing for a leap year. However, this test is not quite correct. A year is a leap year if the year is divisible by 4 unless the year is divisible by 100. Of the years divisible by 100, the only ones that are leap years are those divisible by 400. Thus, the year 1900 was not a leap year but the year 2000 was a leap year. Recode the function Validate_Date() to incorporate the correct test for a leap year.// dem09-7. Cpp
//This prophram demonstrates the use of structures as function
//arguments and function return values
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cctype>
Using namespace std;
Struct DATE_STRUCT
[
Int month,
day,
year;
] ;
DATE_STRUCT String_To_MDY(char*);
bool Validate_Date(DATE_STRUCT);
char* Strchcpy(char*, char*, int);
int main ()
[
char date_string [11];
DATE_STRUCT mdy_date;
char response [2];
cout << endl << endl;
cout << “Do you want to convert and validate a date?(Y/N) : “ ;
cin.getline(response, 2);
while (toupper(*response) == ‘Y’)
[
cout << endl;
cout << “Enter the date in mm/dd/yyyy form: “;
cout<< “Month: “ << setw(4) << mdy_date.month << endl;
cout << “Day: “<< setw(4) << mdy_date.day << endl;
cout << “Year: “ <<setw(4) << mdy_date.year << endl;
if ( Validate_Date(mdy_date) )
{
cout << endl;
cout << “The date is valid. “” << endl;
}
Else
{
cout << endl;
cout << “The date is invalid. “” << endl;
}
cout << endl << endl;
cout << “Do you want to convert and validate a date? (Y/N) : “ ;
cin.getline(response, 2);
}
return 0;
} // End of main ()
DATE_STRUCT String_To_MDY (char* date_ptr)
{
DATE_STRUCT mdy_date;
char month [3]
day[3]
year[5]
char* ch_ptr;
ch_ptr = date_ptr;
ch_ptr = Strchpy(month, ch_ptr, ‘/’);
++ch_ptr;
ch_ptr = Strchpy (day, ch_ptr, ‘/’);
++ch_ptr;
Strchpy(year, ch_ptr, ‘’);
mdy_date.month = atoi (month);
mdy_date.day = atoi (day);
mdy_date.year = atoi (year) ;
return mdy_date;
} // End of String_To_MDY ()
char* Strchpy(char* target, char* source, int ch)
{
while (source* != ch && *source != ‘’ )
{
*target = *source;
++target;
++source;
}
*target = ‘’;
Return source;
} // Wnd of Strchcpy ()
bool Validate_Date)DATE_STRUCT date)
{
int ny_days [13] = {0,31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} ;
int ly_days [13] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} ;
if (date.month < 1 || date.month > 12)
return false;
if (date.day < 1)
return false;
if (date.year % 4 == 0)
{
If (date.day > ly_days[date.month])
return false;
}
else
if (date.day > ny_days [date.month])
return false;
if (date.year < 1980)
return false;
return true;
} // End of Validate_Date()
Explanation / Answer
// dem09-7. Cpp
//This prophram demonstrates the use of structures as function
//arguments and function return values
#include
#include
#include
#include
Using namespace std;
Struct DATE_STRUCT
[
Int month,
day,
year;
] ;
DATE_STRUCT String_To_MDY(char*);
bool Validate_Date(DATE_STRUCT);
char* Strchcpy(char*, char*, int);
int main ()
[
char date_string [11];
DATE_STRUCT mdy_date;
char response [2];
cout << endl << endl;
cout << “Do you want to convert and validate a date?(Y/N) : “ ;
cin.getline(response, 2);
while (toupper(*response) == ‘Y’)
[
cout << endl;
cout << “Enter the date in mm/dd/yyyy form: “;
cout<< “Month: “ << setw(4) << mdy_date.month << endl;
cout << “Day: “<< setw(4) << mdy_date.day << endl;
cout << “Year: “ <if ( Validate_Date(mdy_date) )
{
cout << endl;
cout << “The date is valid. “” << endl;
}
Else
{
cout << endl;
cout << “The date is invalid. “” << endl;
}
cout << endl << endl;
cout << “Do you want to convert and validate a date? (Y/N) : “ ;
cin.getline(response, 2);
}
return 0;
} // End of main ()
DATE_STRUCT String_To_MDY (char* date_ptr)
{
DATE_STRUCT mdy_date;
char month [3]
day[3]
year[5]
char* ch_ptr;
ch_ptr = date_ptr;
ch_ptr = Strchpy(month, ch_ptr, ‘/’);
++ch_ptr;
ch_ptr = Strchpy (day, ch_ptr, ‘/’);
++ch_ptr;
Strchpy(year, ch_ptr, ‘’);
mdy_date.month = atoi (month);
mdy_date.day = atoi (day);
mdy_date.year = atoi (year) ;
return mdy_date;
} // End of String_To_MDY ()
char* Strchpy(char* target, char* source, int ch)
{
while (source* != ch && *source != ‘’ )
{
*target = *source;
++target;
++source;
}
*target = ‘’;
Return source;
} // Wnd of Strchcpy ()
bool Validate_Date)DATE_STRUCT date)
{
int ny_days [13] = {0,31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} ;
int ly_days [13] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} ;
if (date.month < 1 || date.month > 12)
return false;
if (date.day < 1)
return false;
if (((date.year % 4 == 0)&&(date.year % 100 != 0))||(date.year % 400 == 0)) // condition is rewritten as required..
{
If (date.day > ly_days[date.month])
return false;
//printf("Leap Year");
}
else{
if (date.day > ny_days [date.month])
return false;
}
return true;
} // End of Validate_Date()