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
|
#include <iostream>
#include <fstream>
#include <string>
#include "datefuns.cc"
using namespace std;
bool IsLeapYear(int year);
int DayOfYear(int MonthNum);
int OrdDate1(int DayOfMonth, int MonthNum, int year, int& Orddate);
// This function converts an int to a string
string intToString(int i);
int main()
{
// Declare Variables
ifstream DatesFile;
int DayNum, DayNum2;
int MonthNum, MonthNum2;
int YearNum, YearNum2;
int N;
int OrdDate1, OrdDate2;
int Lengthofcurrentyear;
int year;
string DateonLine;
Lengthofcurrentyear = IsLeapYear(year)? 366 : 365;
// Open the file
DatesFile.open("Dates.txt");
while (! DatesFile.eof() )
{
// For Each line of Input
getline (DatesFile, DateonLine);
// Read the Date and N
cin >> DayNum >> MonthNum >> YearNum >> N;
//assigns lengthofcurrent year to either of those based on isleapyear
IsLeapYear (YearNum);
// Convert Date to Ordinal Form, then add's N to it.
OrdDate1 ( DayNum, MonthNum, YearNum);
OrdDate2 = OrdDate1 + N;
// Checks to see if Date 2 goes past 365 ( end of the year )
if (OrdDate2 > Lengthofcurrentyear)
{
OrdDate2 = OrdDate2 - Lengthofcurrentyear;
YearNum = YearNum + 1;
}
// Convert back to mm dd yyyy form
dayToDate(OrdDate2, YearNum2/* inout*/ , MonthNum2, DayNum2 );
dow(YearNum2, MonthNum2, DayNum2);
// Convert both dates to long format
dayOfWeekToDayName();
monthNumToMonthName( MonthNum2 );
intToString( DayNum2 );
intToString( YearNum2);
//Display the first and second dates, and N.
cout << "Start date is " << dayOfWeekToDayName( dow(YearNum, MonthNum, DayNum); ) << "," << monthNumToMonthName(MonthNum) << DayNum << "," << YearNum << endl;
cout << N << "Days from that date is is " << dayOfWeekToDayName << "," << monthNumToMonthName << DayNum2 << "," << YearNum2 << endl;
}
return 0;
}
int OrdDate1(int DayNum, int MonthNum, int year)
//Pre: Needs day, month, year
//Post: Returns the date in ordinal format.
{
//Get dates
cin >> MonthNum;
cin >> DayNum;
cin >> year;
//Calculates the day as an ordinal int data type.
return DayOfYear( MonthNum ) + DayNum;
}
int DayOfYear(int MonthNum)
//This Function calculates the day of the year from the month
//from the beginning of the year. Called by the main function
{
if ( MonthNum == 1 )
return 0;
else if ( MonthNum == 2 )
return 31;
else if ( MonthNum == 3 )
return 31 + 28;
else if ( MonthNum == 4 )
return 31 + 28 + 31;
else if ( MonthNum == 5 )
return 31 + 28 + 31 + 30;
else if ( MonthNum == 6 )
return 31 + 28 + 31 + 30 + 31;
else if ( MonthNum == 7 )
return 31 + 28 + 31 + 30 + 31 + 30;
else if ( MonthNum == 8 )
return 31 + 28 + 31 + 30 + 31 + 30 + 31;
else if ( MonthNum == 9 )
return 31 + 28 + 31 + 30 + 31 + 30 + 31 + 30;
else if ( MonthNum == 10 )
return 31 + 28 + 31 + 30 + 31 + 30 + 31 + 30 + 30;
else if ( MonthNum == 11 )
return 31 + 28 + 31 + 30 + 31 + 30 + 31 + 30 + 30 + 31;
else if ( MonthNum == 12 )
return 31 + 28 + 31 + 30 + 31 + 30 + 31 + 30 + 30 + 330;
}
|