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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
// Define function prototypes here.
unsigned int days_in_month(unsigned int, unsigned int);
unsigned int leap(unsigned int);
bool valiDATE(unsigned int, unsigned int, unsigned int);
unsigned int juliandate(unsigned int year, unsigned int month, unsigned int day);
void main(void)
{
unsigned int mm = 0, dd = 0, yyyy, day_of_year = 0;
unsigned int i = 0;
char answer = 'Y';
do {
day_of_year = 0;
cout << "Please enter the month, day and year like MM DD YYYY." << endl;
cin >> mm >> dd >> yyyy;
//
// The following WHILE LOOP is used to make sure that the
// data entered are correct.
//
while (!valiDATE(mm, dd, yyyy))
{
cout << "Please enter the month, day and year like MM DD YYYY." << endl;
cin >> mm >> dd >> yyyy;
};
cout << "The date has been validated." << endl << endl;
unsigned int juliandate(mm, dd, yyyy);
cout << "The Julian Date is: " << juliandate << endl;
cout << "Do you want to continue (Y/N)?" << endl;
cin >> answer;
} while (answer == 'Y' || answer == 'y');
cout << endl << endl << endl;
system("pause");
}
unsigned int days_in_month(unsigned int month, unsigned int year){
unsigned int days = 0;
if (month == 2) {
days = 28 + leap(year); // function leap returns 1 for leap years
}
else if (month == 4 || month == 6 || month == 9 || month == 11) {
days = 30;
}
else {
days = 31;
}
return days;
}
unsigned int leap(unsigned int year)
{
if (year % 400 == 0)
return 1;
else if (year % 100 != 0 && year % 4 == 0)
return 1;
else
return 0;
}
bool valiDATE(unsigned int month, unsigned int day, unsigned int year) {
unsigned int flag = 0;
unsigned int days = 0;
// Validate the month first
if (month < 1 || month > 12) {
cout << "Value for month (MM) is out of range." << endl;
return false;
}
// Validate the day of month
days = days_in_month(month, year);
if (day < 1 || day > days) {
cout << "Value for day (DD) is out of range." << endl;
return false;
}
// Validate the year
if (year < 1990 || year > 2020) {
cout << "Value for year (YYYY) is out of range." << endl;
return false;
}
return true;
}
//Function to take date of installation and convert it to a julian date
unsigned int juliandate(unsigned int year, unsigned int month, unsigned int day)
{//Passes in Installation date
//Declare variables to be used in converting date
int julianDate;
int I;
int J;
int K;
I = year;
J = month;
K = day;
//Formula to convert gregorian date to julian date
julianDate = K - 32075 + 1461 * (I + 4800 + (J - 14) / 12) / 4 + 367 *
(J - 2 - (J - 14) / 12 * 12) * 2 / 12 - 3 * ((I + 4900 + (J - 14) / 12) / 100) / 4;
return julianDate;
|