0% found this document useful (0 votes)
7 views4 pages

C++ Solution for Date Overlap Check

The document provides a C++ solution for determining if two date periods overlap. It defines structures for dates and periods, includes functions to compare dates, and reads user input for dates and periods. The main function checks for overlapping periods and outputs the result.

Uploaded by

user.user93.user
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

C++ Solution for Date Overlap Check

The document provides a C++ solution for determining if two date periods overlap. It defines structures for dates and periods, includes functions to compare dates, and reads user input for dates and periods. The main function checks for overlapping periods and outputs the result.

Uploaded by

user.user93.user
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Problem # 58/4 Solution Using C++

#include <iostream>
using namespace std;

struct stDate
{
short Year;
short Month;
short Day;
};

struct stPeriod
{
stDate StartDate;
stDate EndDate;
};

bool IsDate1BeforeDate2(stDate Date1, stDate Date2)


{
return ([Link] < [Link]) ? true : (([Link] ==
[Link]) ? ([Link] < [Link] ? true : ([Link] ==
[Link] ? [Link] < [Link] : false)) : false);
}

bool IsDate1EqualDate2(stDate Date1, stDate Date2)


{
return ([Link] == [Link]) ? (([Link] ==
[Link]) ? (([Link] == [Link]) ? true : false) : false)
: false;
}

bool IsDate1AfterDate2(stDate Date1, stDate Date2)


{
return (!IsDate1BeforeDate2(Date1, Date2) &&
!IsDate1EqualDate2(Date1, Date2));

enum enDateCompare { Before = -1, Equal = 0, After = 1 };

[Link]
© Copyright 2022
Problem # 58/4 Solution Using C++

enDateCompare CompareDates(stDate Date1, stDate Date2)


{

if (IsDate1BeforeDate2(Date1, Date2))
return enDateCompare::Before;

if (IsDate1EqualDate2(Date1, Date2))
return enDateCompare::Equal;

/* if (IsDate1AfterDate2(Date1,Date2))
return enDateCompare::After;*/

//this is faster
return enDateCompare::After;
}

bool IsOverlapPeriods(stPeriod Period1, stPeriod Period2)


{

if (
CompareDates([Link], [Link]) ==
enDateCompare::Before
||
CompareDates([Link], [Link]) ==
enDateCompare::After
)
return false;
else
return true;

short ReadDay()
{
short Day;
cout << "\nPlease enter a Day? ";
cin >> Day;
return Day;
}

short ReadMonth()
{
short Month;
cout << "Please enter a Month? ";
cin >> Month;
return Month;
}

[Link]
© Copyright 2022
Problem # 58/4 Solution Using C++

short ReadYear()
{
short Year;
cout << "Please enter a Year? ";
cin >> Year;
return Year;
}

stDate ReadFullDate()
{
stDate Date;

[Link] = ReadDay();
[Link] = ReadMonth();
[Link] = ReadYear();

return Date;
}

stPeriod ReadPeriod()
{
stPeriod Period;
cout << "\nEnter Start Date:\n";
[Link] = ReadFullDate();
cout << "\nEnter End Date:\n";
[Link] = ReadFullDate();
return Period;
}

int main()
{
cout << "\nEnter Period 1:";
stPeriod Period1 = ReadPeriod();

cout << "\nEnter Period 2:";


stPeriod Period2 = ReadPeriod();

if (IsOverlapPeriods(Period1, Period2))
cout << "\nYes, Periods Overlap\n";
else
cout << "\nNo, Periods do not Overlap\n";

system("pause>0");
return 0;
}

[Link]
© Copyright 2022

You might also like