GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 16-Mar-2005, 17:30
eleet's Avatar
eleet eleet is offline
New Member
 
Join Date: Mar 2005
Posts: 8
eleet is an unknown quantity at this point

Trying to create a calendar


I have to write 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. For each data line you will first read in the year number.

I was able to write a function for finding the first day of the year:


CPP / C++ / C Code:
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 */

and another for leap year,

CPP / C++ / C Code:
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 */

I am having trouble getting it started if anyone could help, thanks
Last edited by LuciWiz : 16-Mar-2005 at 23:59. Reason: Please insert your C code between [c] & [/c] tags
  #2  
Old 17-Mar-2005, 00:03
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,373
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by eleet
I have to write 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. For each data line you will first read in the year number.

I was able to write a function for finding the first day of the year:


and another for leap year,

I am having trouble getting it started if anyone could help, thanks
Use code tags (read the sticky) not quote tags to display code.

Do your functions work? Did you write a main function to test them? Start by accepting the year from the keyboard and verify you get to the correct day for the year entered. Then ask specific questions about what you are having trouble with.
__________________

The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
  #3  
Old 17-Mar-2005, 18:44
eleet's Avatar
eleet eleet is offline
New Member
 
Join Date: Mar 2005
Posts: 8
eleet is an unknown quantity at this point
I worked almost all night these last couple of days but I think I finally got it. If anyone has any comments on it please respond.

ELEET

CPP / C++ / C Code:
 bbcode[/b]
#include <stdio.h>
#include <string.h>


#define MIN_YEAR 1582
#define MAX_YEAR 9999
#define DAYS_PER_WEEK 7
#define MAX_FILE_NAME_SIZE 25
#define MAX_MONTH_NAME_SIZE 10
#define PREVIOUSDAYSPACE 3

//function declarations
int count_leap_years(int);
int find_jan_first(int);
int print_month(int, int, int);

void print_calendar(int);
void print_dayNames();


int main()
{

	int year;
  
	char fileName[MAX_FILE_NAME_SIZE];
    FILE * inFile;                         //file pointer
	printf("Please enter a date file: "); // user enters text file
	scanf("%s",fileName);
	inFile = fopen (fileName,"r");
	while(!inFile)                   //error check if not valid file
	{
		printf("Try Again ");
		printf("Please enter a date file: ");
		scanf("%s",fileName);
		inFile = fopen (fileName,"r");
	}
	while ( fscanf ( inFile, "%d", &year )!= EOF ) //error if date not in Gregorian calendar
	{
		if (year < MIN_YEAR || year > MAX_YEAR)
		{
			printf("\nCalendar may be incorrect,\n"); 
			printf("the year may not exist in\n");
			printf("the Gregorian Calendar range.\n");
		}
		print_calendar (year);
	}
	
	fclose (inFile);
	
}

void print_calendar(int year)  //calling first day and year
{
	int firstday; //first day of month	
    int month;    // month of the year

	printf("\n       %d\n\n", year);
	firstday = find_jan_first(year);

	for(month= 1; month <= 12; month++)
	{
		firstday= print_month (month, firstday, year);
	}

}

int print_month (int month, int day, int year) //length of month and name of month
{
	int days, numspacefirst, spaces, datetoprint;

	char monthName[MAX_MONTH_NAME_SIZE];
	switch(month)
	{
	case 1: 
        days = 31;
		strcpy(monthName,"January");
        break;
    case 3: 
        days = 31;
		strcpy(monthName,"March");
        break;
    case 5: 
        days = 31;
		strcpy(monthName,"May");
        break;
    case 7: 
        days = 31;
		strcpy(monthName,"July");
        break;
    case 8: 
        days = 31;
		strcpy(monthName,"August");
        break;
    case 10: 
        days = 31;
		strcpy(monthName,"October");
        break;
    case 12: 
        days = 31;
		strcpy(monthName,"December");
        break;
    case 4: 
        days = 30;
		strcpy(monthName,"April");
        break;
    case 6: 
        days = 30;
		strcpy(monthName,"June");
        break;
    case 9: 
        days = 30;
		strcpy(monthName,"September");
        break;
    case 11:
        days = 30;
		strcpy(monthName,"November");
        break;
    case 2: 
        days = 28 +
			   1 * (count_leap_years(year+1) -count_leap_years(year));
        strcpy(monthName,"February");
	}

	printf("     %s\n", monthName);
	print_dayNames();
	numspacefirst = PREVIOUSDAYSPACE * day;   //spacing of date numbers
	
	for (spaces=0;spaces<numspacefirst;spaces++)
	{
		printf("%c",' ');
	}
	for (datetoprint=1;datetoprint<=days;datetoprint++)
	{
		printf("%2d",datetoprint);
		printf("%c", ' ');
		day++;
		if (day == DAYS_PER_WEEK)
		{
			day = 0;
			printf("\n");
		}
	}
	printf("\n\n");

	return day;

}// find the day

void print_dayNames() // print day identations
    {
    printf("S  M  T  W  T  F  S \n");
	printf("--------------------\n");
    }

int find_jan_first
(int year)              // the input year, must be >= MIN_YEAR 

{  // 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 

int count_leap_years
(int year)               // user's choice of year, must be >= MIN_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
  #4  
Old 17-Mar-2005, 21:21
dabigmooish's Avatar
dabigmooish dabigmooish is offline
Member
 
Join Date: May 2004
Location: Baltimore (middle of Canton)
Posts: 167
dabigmooish will become famous soon enough
You need a return in your main function. Other then that everything seems to work to me
__________________
"To argue with a person who has renounced the use of reason is like administering medicine to the dead."
-Thomas Paine
www.sullivan-county.com/deism.htm
  #5  
Old 17-Mar-2005, 22:34
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,373
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
In print_month(), rather than the large switch statement with hard coded values, you can set up two arrays:
CPP / C++ / C Code:
int days[] = {0, 31, 28, 31, 30, ...
char *monthName[MAX_MONTH_NAME_SIZE] = {""; "January", "February", "March",...
Then instead of the assignments you can simply reference:
CPP / C++ / C Code:
printf("     %s\n", monthName[month]);
...
for (datetoprint = 1; datetoprint <= days[month]; datetoprint++)
{
...
__________________

The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
 
 

Recent GIDBlogVista ?Widgets? on Windows XP by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Interested in helping create an RPG? crystalattice Member Announcements, Advertisements & Offers 16 06-May-2005 22:18
How to create integer variables OETN FLTK Forum 4 26-Dec-2004 10:04
create forum bearky MySQL / PHP Forum 19 23-Oct-2004 17:29
Can't seem to create db Tigress7 MySQL / PHP Forum 3 19-Aug-2003 09:19
How do I create JavaScript Links? JdS Web Design Forum 8 29-Jan-2003 15:02

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 15:57.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.