Calendar Creator

Reply

Join Date: Mar 2005
Posts: 6
Reputation: eleet is an unknown quantity at this point 
Solved Threads: 0
eleet eleet is offline Offline
Newbie Poster

Calendar Creator

 
1
  #1
Mar 15th, 2005
I am trying towrite a C program which will print yearly calendars, given as input the year numbers. Input data will be taken from a text file, which will contain an unknown number of data lines, each containing a single integer giving a year,

I first created a function to find the first day of the year:

[PHP]int find_jan_first
(int year) /* the input year, must be >= MIN_YEAR */

/*

Calculates the day of the week on which January 1 falls for given year.
We start the formula at 5, because January 1, 1582 was a Friday.
Then the day of the week is advanced by one for each year, adding the
number of included leap years, because in leap years January 1st
advances by 2 days and in regular years it advances by 1 day.

*/

{ /* find_jan_first */

int firstDayInJan; /* 0 is Sunday, 1 is Monday, ..., 6 is Saturday */

/* starting on January 1, 1582, add days to find the first day */
/* in January of the year chosen */
firstDayInJan = ((5 + (year - MIN_YEAR) +
count_leap_years(year)) % DAYS_PER_WEEK);

return (firstDayInJan);

} /* find_jan_first */[/PHP]

and also a function to find leap year:
[PHP]int count_leap_years
(int year) /* user's choice of year, must be >= MIN_YEAR */

/*

Calculates the number of leap years since 1582 to the current year.
This function returns the numbers of leap years since the given year.
Count does not include the given year if it is a leap year.

*/

{ /* count_leap_years */

int leapYears; /* number of leap years to return */
int hundreds; /* number of years multiple of a hundred */
int fourHundreds; /* number of years multiple of four hundred */

/* determine number of years in interval that are a multiple of 4 */
leapYears = (year - (MIN_YEAR - 1)) / 4;

/* determine number of years in interval that are a multiple of 100 */
/* and subtract, because they are not leap years */
hundreds = (year - 1501) / 100;
leapYears -= hundreds;

/* determine number of years in interval that are a multiple of 400 */
/* and add these back in, since they are leap years */
fourHundreds = (year - 1201) / 400;
leapYears += fourHundreds;

return (leapYears);

} /* count_leap_years */[/PHP]

I know the MIN_Year is 1582

and I think that you would put in input by inFile = fopen (fileName, "r");.

What I am having trouble with is the actual loop to begin show the final product looks like this.

[PHP]1985

January

S M T W T F S

--------------------

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




February

S M T W T F S

--------------------

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[/PHP]
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 445
Reputation: 1o0oBhP is an unknown quantity at this point 
Solved Threads: 6
1o0oBhP's Avatar
1o0oBhP 1o0oBhP is offline Offline
Posting Pro in Training

Re: Calendar Creator

 
1
  #2
Mar 16th, 2005
Well looping would be a simple way to draw it! Does this have to be a console app? Many Win32 Functions are available for arranging text or characters at co-ordinates on a window....
http://sales.carina-e.com

no www
no nonsense

coming soon to a pc near you! :cool:
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 6
Reputation: eleet is an unknown quantity at this point 
Solved Threads: 0
eleet eleet is offline Offline
Newbie Poster

Re: Calendar Creator

 
0
  #3
Mar 16th, 2005
It does not have to be a looping funciton I just thought it was the best way to do it, what was your idea I would like to hear it, 1o0oBhP.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 445
Reputation: 1o0oBhP is an unknown quantity at this point 
Solved Threads: 6
1o0oBhP's Avatar
1o0oBhP 1o0oBhP is offline Offline
Posting Pro in Training

Re: Calendar Creator

 
0
  #4
Mar 20th, 2005
Sorry for the late reply Ive been away at uni working hard! As for drawing the calendar why not have a nested loop to draw the whole lot?

  1.  
  2. char days[] = { "S", "M", "T", "W", "T", "F", "S" }; // day abbrs
  3.  
  4. cout << "your month here" << "\n\n";
  5.  
  6. for(int i = 0; i < 7; i++)
  7. cout << days[i] << " ";
  8. cout << "\n";
  9.  
  10. for(int i = 0; i < 14; i++)
  11. cout << "-";
  12. cout << "\n";

This will print the header. Then you want to work out how many rows the calendar month will have. This will depend on what day it starts on and how long the month is. Have a go and see what you come up with. When you have the number of rows you can use a loop to put the correct numbers in the right places on the screen!
http://sales.carina-e.com

no www
no nonsense

coming soon to a pc near you! :cool:
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 6
Reputation: eleet is an unknown quantity at this point 
Solved Threads: 0
eleet eleet is offline Offline
Newbie Poster

Re: Calendar Creator

 
0
  #5
Mar 21st, 2005
This is how I ended up writing the program.

  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4.  
  5. #define MIN_YEAR 1582
  6. #define MAX_YEAR 9999
  7. #define DAYS_PER_WEEK 7
  8. #define MAX_FILE_NAME_SIZE 25
  9. #define MAX_MONTH_NAME_SIZE 10
  10. #define PREVIOUSDAYSPACE 3
  11.  
  12. //function declarations
  13. int count_leap_years(int);
  14. int find_jan_first(int);
  15. int print_month(int, int, int);
  16.  
  17. void print_calendar(int);
  18. void print_dayNames();
  19.  
  20.  
  21. int main()
  22. {
  23.  
  24. int year;
  25.  
  26. char fileName[MAX_FILE_NAME_SIZE];
  27. FILE * inFile; //file pointer
  28. printf("Please enter a date file: "); // user enters text file
  29. scanf("%s",fileName);
  30. inFile = fopen (fileName,"r");
  31. while(!inFile) //error check if not valid file
  32. {
  33. printf("Try Again ");
  34. printf("Please enter a date file: ");
  35. scanf("%s",fileName);
  36. inFile = fopen (fileName,"r");
  37. }
  38. while ( fscanf ( inFile, "%d", &year )!= EOF ) //error if date not in Gregorian calendar
  39. {
  40. if (year < MIN_YEAR || year > MAX_YEAR)
  41. {
  42. printf("\nCalendar may be incorrect,\n");
  43. printf("the year may not exist in\n");
  44. printf("the Gregorian Calendar range.\n");
  45. }
  46. print_calendar (year);
  47. }
  48.  
  49. fclose (inFile);
  50.  
  51. }
  52.  
  53. void print_calendar(int year) //calling first day and year
  54. {
  55. int firstday; //first day of month
  56. int month; // month of the year
  57.  
  58. printf("\n %d\n\n", year);
  59. firstday = find_jan_first(year);
  60.  
  61. for(month= 1; month <= 12; month++)
  62. {
  63. firstday= print_month (month, firstday, year);
  64. }
  65.  
  66. }
  67.  
  68. int print_month (int month, int day, int year) //length of month and name of month
  69. {
  70.  
  71. int days, numspacefirst, spaces, datetoprint;
  72.  
  73. char monthName[MAX_MONTH_NAME_SIZE];
  74. switch(month)
  75. {
  76. case 1:
  77. days = 31;
  78. strcpy(monthName,"January");
  79. break;
  80. case 3:
  81. days = 31;
  82. strcpy(monthName,"March");
  83. break;
  84. case 5:
  85. days = 31;
  86. strcpy(monthName,"May");
  87. break;
  88. case 7:
  89. days = 31;
  90. strcpy(monthName,"July");
  91. break;
  92. case 8:
  93. days = 31;
  94. strcpy(monthName,"August");
  95. break;
  96. case 10:
  97. days = 31;
  98. strcpy(monthName,"October");
  99. break;
  100. case 12:
  101. days = 31;
  102. strcpy(monthName,"December");
  103. break;
  104. case 4:
  105. days = 30;
  106. strcpy(monthName,"April");
  107. break;
  108. case 6:
  109. days = 30;
  110. strcpy(monthName,"June");
  111. break;
  112. case 9:
  113. days = 30;
  114. strcpy(monthName,"September");
  115. break;
  116. case 11:
  117. days = 30;
  118. strcpy(monthName,"November");
  119. break;
  120. case 2:
  121. days = 28 +
  122. 1 * (count_leap_years(year+1) -count_leap_years(year));
  123. strcpy(monthName,"February");
  124. }
  125.  
  126. printf(" %s\n", monthName);
  127. print_dayNames();
  128. numspacefirst = PREVIOUSDAYSPACE * day; //spacing of date numbers
  129.  
  130. for (spaces=0;spaces<numspacefirst;spaces++)
  131. {
  132. printf("%c",' ');
  133. }
  134. for (datetoprint=1;datetoprint<=days;datetoprint++)
  135. {
  136. printf("%2d",datetoprint);
  137. printf("%c", ' ');
  138. day++;
  139. if (day == DAYS_PER_WEEK)
  140. {
  141. day = 0;
  142. printf("\n");
  143. }
  144. }
  145. printf("\n\n");
  146.  
  147. return day;
  148.  
  149. }// find the day
  150.  
  151. void print_dayNames() // print day identations
  152. {
  153. printf("S M T W T F S \n");
  154. printf("--------------------\n");
  155. }
  156.  
  157. int find_jan_first
  158. (int year) // the input year, must be >= MIN_YEAR
  159.  
  160. { // find_jan_first
  161.  
  162. int firstDayInJan; // 0 is Sunday, 1 is Monday, ..., 6 is Saturday
  163.  
  164. // starting on January 1, 1582, add days to find the first day
  165. // in January of the year chosen
  166. firstDayInJan = ((5 + (year - MIN_YEAR) +
  167. count_leap_years (year)) % DAYS_PER_WEEK);
  168.  
  169. return (firstDayInJan);
  170.  
  171. } // find_jan_first
  172.  
  173. int count_leap_years
  174. (int year) // user's choice of year, must be >= MIN_YEAR
  175.  
  176. { // count_leap_years
  177.  
  178. int leapYears; // number of leap years to return
  179. int hundreds; // number of years multiple of a hundred
  180. int fourHundreds; // number of years multiple of four hundred
  181.  
  182. // determine number of years in interval that are a multiple of 4
  183. leapYears = (year - (MIN_YEAR - 1)) / 4;
  184.  
  185. // determine number of years in interval that are a multiple of 100
  186. // and subtract, because they are not leap years
  187. hundreds = (year - 1501) / 100;
  188. leapYears -= hundreds;
  189.  
  190. // determine number of years in interval that are a multiple of 400
  191. // and add these back in, since they are leap years
  192. fourHundreds = (year - 1201) / 400;
  193. leapYears += fourHundreds;
  194.  
  195. return (leapYears);
  196.  
  197. } // count_leap_years
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC