I am writing a calendar in C. I'm still very new to the language. I think i wrote most of the functions/algorithms correctly (it would be nice if you guys could check for any errors too). Now I'm having a tough time printing the calendar out In a correct format. For example, if I enter this data: 12/x/2010 to getMonthYear(). I am trying to get my calendar to start in the correct day, so the 1st is on Wednesday, 2nd Thursday, 3rd Friday... I should be able to enter any month and year and get the day to correspond with the correct weekday. All of this is going to have to be in the prototype printCalendar(). Any help is much appreciated.
#include <stdio.h>
#define MAX 31
#define MID 30
#define MIN 28
#define FOUR 4
#define ONEHUN 100
#define FOHUN 400
#define BEGIN 1900
#define YEAR 365
//Prototypes:
void getMonthYear(int *month, int *year);
int toJulian(int month, int day, int year);
int dayInMonth(int month, int year);
int leapYear(int year);
long yearsToDays(int year);
void printCalendar(int startDay, int numDays);
void printHeader();
void main(void)
{
int day, month, year, startDay, numDays;
startDay = 1;
numDays = 0;
day = 0;
getMonthYear(&month, &year);
toJulian(month, day, year);
yearsToDays(year);
printCalendar(startDay, numDays);
}
//Ask user to imput data
void getMonthYear(int *month, int *year)
{
printf("Enter Month: ");
scanf_s("%d", month);
printf("Enter Year: ");
scanf_s("%d", year);
printf("\n\n");
}
//Takes a calendar date and calculates its julian day within that year
int toJulian(int month, int day, int year)
{
--month;
while(month != 0)
{
day += daysInMonth(month, year);
--month;
}
return day;
}
//Takes a month and year, calculates how many days are in this particular month
int daysInMonth(int month, int year)
{
switch (month)
{
case 1 : case 3 : case 5 : case 7 : case 8 : case 10 : case 12 :
return MAX;
break;
case 4 : case 6 : case 9 : case 11 :
return MID;
break;
case 2 :
return MIN + leapYear(year);
break;
}
}
//Takes a year, returns 1 if this is a leap year, 0 otherwise
int leapYear(int year)
{
if (year % FOUR == 0 && (year % ONEHUN != 0 || year % FOHUN == 0))
return 1;
else
return 0;
}
//Takes a year, returns the number of days from 1/1/1900 to the end of the previous year
long yearsToDays(int year)
{
long days = 0;
while(year != BEGIN)
{
--year;
days += YEAR + leapYear(year);
}
return days;
}
//Outputs the calendar to the screen.
void printCalendar(int startDay, int numDays)
{
printHeader();
}
void printHeader()
{
printf("%4s%4s%4s%4s%4s%4s%4s \n", "Su", "M", "Tu", "W", "Th", "F", "Sa");
}