Advertisement
Guest User

Untitled

a guest
Feb 9th, 2017
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <cassert>
  3. using namespace std;
  4.  
  5. int julianDateCalc(int x, int y);
  6.  
  7. int daysPerMonth[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
  8.  
  9. int main()
  10. {
  11.     int firstDay, secondDay, firstMonth, secondMonth = 0;
  12.  
  13.     //Read in dates by the user.
  14.     cout << "Please enter two dates in number format (dd mm)" << endl;
  15.     cout << "Day: ";
  16.     cin >> firstDay;
  17.     cout << "Month: ";
  18.     cin >> firstMonth;
  19.     cout << endl;
  20.     cout << "Day: ";
  21.     cin >> secondDay;
  22.     cout << "Month: ";
  23.     cin >> secondMonth;
  24.     cout << endl;
  25.  
  26.     //Check validity of date entered.
  27.     assert(firstDay > 0 && firstDay < (daysPerMonth[firstMonth - 1] + 1));
  28.     assert(secondDay > 0 && secondDay < (daysPerMonth[secondMonth - 1] + 1));
  29.  
  30.     //Function calls for the Julian conversion.
  31.     julianDateCalc(firstDay, firstMonth);
  32.     julianDateCalc(secondDay, secondMonth);
  33.  
  34.     //Check to determine which date is lower/higher and calculate difference in days between the dates.
  35.     if (julianDateCalc(firstDay, firstMonth) > julianDateCalc(secondDay, secondMonth))
  36.     {
  37.         cout << "The first date converted is: " << julianDateCalc(firstDay, firstMonth) << endl;
  38.         cout << "The second date converted is: " << julianDateCalc(secondDay, secondMonth) << endl;
  39.         cout << endl;
  40.         cout << " The difference in days between the two dates is: " << julianDateCalc(firstDay, firstMonth) - julianDateCalc(secondDay, secondMonth) << " days" << endl;
  41.     }
  42.     else
  43.     {
  44.         cout << "The first date converted is: " << julianDateCalc(firstDay, firstMonth) << endl;
  45.         cout << "The second date converted is: " << julianDateCalc(secondDay, secondMonth) << endl;
  46.         cout << endl;
  47.         cout << "The difference in days between the two dates is: " << julianDateCalc(secondDay, secondMonth) - julianDateCalc(firstDay, firstMonth) << " days" << endl;
  48.     }
  49.  
  50.     return 0;
  51. }
  52.  
  53. //Function to calculate the Julian date of the user's entry.
  54. int julianDateCalc(int day, int month)
  55. {
  56.     int sum = 0;
  57.  
  58.     for (int i = 0; i < month - 1 ; i++)
  59.     {
  60.         sum += daysPerMonth[i];
  61.     }
  62.  
  63.     return sum + day;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement