Guest User

Untitled

a guest
Jun 23rd, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.77 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4. #include <fstream>
  5.  
  6. using namespace std;
  7.  
  8. //function prototypes
  9. void printmonth(const int, const int);
  10. void printyear(const int);
  11. bool isleap(const int);
  12. int dayofweek(const int, const int, const int);
  13.  
  14. //global variables
  15. fstream outFile;    //allows user to input/output data to a seperate file
  16. int year;
  17. int startday;
  18. const int DAY_WIDTH = 3;
  19.  
  20. // *********************************************************************
  21. // * Function Name: main                                               *
  22. // * Description:  This function opens the file for output and prompts *
  23. // *   user to input a year. It then calls function printyear, which   *
  24. // *   uses function printmonth to output the calendar.                *
  25. // *                                                                   *
  26. // * Parameter Description: none                                       *
  27. // *                                                                   *
  28. // * Author: Matthew Draper                                            *
  29. // * Date:  February 3, 2012                                           *
  30. // *********************************************************************
  31. int main()
  32. {
  33.     outFile.open("cal.txt"); // open file
  34.     outFile.seekg(0L, ios::beg); // move read pointer to beg
  35.  
  36.     cout << "Welcome to the Calendar printing program! \n\n";
  37.     cout << "Please enter a 4 digit year after 1581: ";
  38.     cin >> year;
  39.     cout << endl;
  40.    
  41.     while (year < 1582 || year > 9999)
  42.     {
  43.         cout << "Invalid. Enter a 4 digit year after 1581: ";
  44.         cin >> year;
  45.         cout << endl;
  46.     }
  47.     //calls printyear function
  48.     printyear(year);
  49.  
  50.     cout << endl << endl;
  51.    
  52.     cout << "The Calendar for " << year << " has been sent to and displayed in"
  53.         << " the newly created file." << endl << endl;
  54.    
  55.     //closes the file
  56.     outFile.close();
  57.  
  58.     return 0;
  59. }
  60.  
  61. // *********************************************************************
  62. // * Function Name: isleap                                             *
  63. // * Description:  The function determines if its a leap year          *
  64. // *                                                                   *
  65. // * Parameter Description: int year -> Numeric year                   *
  66. // *                                                                   *
  67. // * Author: Matthew Draper                                            *
  68. // * Date:  February 3, 2012                                           *
  69. // *********************************************************************
  70. bool isleap(const int year)
  71. {
  72.     if (year % 4 == 0)
  73.     {
  74.         if (year % 100 == 0 && year % 400 != 0)
  75.             return false;
  76.         else
  77.             return true;
  78.     }
  79.     else
  80.         return false;
  81. }
  82.  
  83. // *********************************************************************
  84. // * Function Name: dayofweek                                          *
  85. // * Description:  The function uses the values passed in for the      *
  86. // * starting date, the numeric month, and the user input year.        *
  87. // * year. The day of the week that month started on is then           *
  88. // * calculated and passed back to function 'printmonth'               *
  89. // *                                                                   *
  90. // * Parameter Description: int day -> numeric day of the month        *
  91. // *                        int month -> numeric month of the year     *
  92. // *                        int year ->     numeric year               *
  93. // *                                                                   *
  94. // * Author: Matthew Draper                                            *
  95. // * Date:  February 3, 2012                                           *
  96. // *********************************************************************
  97. int dayofweek(const int day, const int month, const int year)
  98. {
  99.     int a,y,m,d;
  100.        
  101.     a = (14 - month) / 12;
  102.     y = year - a;
  103.     m = month + (12 * a) - 2;
  104.     d = (day + y + (y / 4) - (y / 100) + (y / 400) + (31 * m / 12)) % 7;
  105.     startday = d;
  106.  
  107.     return startday;
  108. }
  109.  
  110. // *********************************************************************
  111. // * Function Name: printmonth                                         *
  112. // * Description:  The function uses the values passed in for the      *
  113. // * the numeric month, and user input year                            *
  114. // * it's a leap year. The month chosen is then printed out correctly. *
  115. // *                                                                   *
  116. // * Parameter Description: int month -> Numeric Month 1-12            *
  117. // *                        int year -> Numeric year                   *
  118. // *                                                                   *
  119. // * Author: Matthew Draper                                            *
  120. // * Date:  February 3, 2012                                           *
  121. // *********************************************************************
  122. void printmonth(const int month, const int year)
  123. {
  124.  
  125.     int DaysInMonth[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
  126.     //calls function isleap and returns true/false
  127.     isleap(year);
  128.     if (isleap(year) == true)
  129.         DaysInMonth[2] = 29;
  130.  
  131.     string MonthCalled[13]={" ","January", "February", "March", "April", "May",
  132.      "June", "July", "August", "September", "October", "November", "December"};
  133.  
  134.     string DaysHeader = "Su Mo Tu We Th Fr Sa";
  135.     string centered = MonthCalled[month];
  136.  
  137.     //this centers the month name over the days and dates
  138.     int chars = centered.length();
  139.     int ws = chars/2 + 10;
  140.  
  141.     outFile << setw(ws) << MonthCalled[month] << endl;
  142.     outFile << DaysHeader << endl;
  143.     outFile << " ";
  144.  
  145.     //calls function dayofweek and uses the returned 'startday' value
  146.     dayofweek(1, month, year);
  147.  
  148.     //loop keeps adding 3 spaces until startday is reached
  149.     for(int i=1; i <= startday; i++)
  150.     {
  151.         outFile << "   ";
  152.     }
  153.     //loop that prints out the date under the corresponding day of week.
  154.     int DayMarker;
  155.  
  156.     for (int day = 1; day <= DaysInMonth[month]; day++)
  157.     {
  158.         DayMarker = startday + day;
  159.  
  160.         if (DayMarker % 7 == 0)
  161.             outFile << day << endl << setw(DAY_WIDTH - 1);
  162.         else
  163.             outFile << day << setw(DAY_WIDTH); // correctly formats
  164.     }
  165.  
  166.     outFile << endl << endl; //creates space between each printed month
  167.  
  168.     return;
  169. }
  170.  
  171. // *********************************************************************
  172. // * Function Name: printyear                                          *
  173. // * Description:  This function prints the user requested year and    *
  174. // *     then calls function printmonth 12 times to print the calendar *
  175. // *     for every month of that year.
  176. // * Parameter Description: year - numeric year sent in by user input  *
  177. // *                                                                   *
  178. // * Author: Matthew Draper                                            *
  179. // * Date:  February 3, 2012                                           *
  180. // *********************************************************************
  181. void printyear(const int year)
  182. {
  183.     outFile << setw(12) << year << "\n\n";
  184.  
  185.     printmonth(1, year);
  186.     printmonth(2, year);
  187.     printmonth(3, year);
  188.     printmonth(4, year);
  189.     printmonth(5, year);
  190.     printmonth(6, year);
  191.     printmonth(7, year);
  192.     printmonth(8, year);
  193.     printmonth(9, year);
  194.     printmonth(10, year);
  195.     printmonth(11, year);
  196.     printmonth(12, year);
  197.  
  198.     return;
  199. }
Add Comment
Please, Sign In to add comment