Reputation: 93
I am working on a calendar project that was due about 4 days ago but I have been working on it even though the score is going to be grossly diminished because I want to try and understand it. So far I have it to the point where a calendar prints out in debug along with the correct number of days in the calendar. The only things I want to fix are that I want to make the first day of each month start off in the correct position so maybe some sort of loop + " " so that it can loop 4 spaces to the first day of the month or something like that. Also I want it so that when I enter the month, year, and num months the month and year will show the calendar of a month, while nummonths will display the following months. This is a really hard concept for me to understand. Help is Greatly! Appreciated! I have been working on this for WAYYY to long.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int getdaycode(int month, int year);
void printheader(int month, int year);
int getndim(int month, int year);
int main(void) {
int day, month, year, nummonths;
printf("Enter Month, Year, and number of Months: ");
if (scanf("%d %d %d", &month, &year, &nummonths) != 3
|| year < 0 || month < 1 || month > 12) {
printf("invalid input\n");
return 1;
}
for (int i = 0; i < nummonths; i++) {
printheader(month, year);
int numdays = getndim(month, year);
int daycode = getdaycode(month, year);
printf("%*s", daycode * 4, ""); /* print 4 spaces for each skipped day */
for (day = 1; day <= numdays; day++) {
printf("%3d", day);
daycode = (daycode + 1) % 7;
if (daycode != 0)
printf(" ");
else
printf("\n");
}
if (daycode != 0)
printf("\n");
printf("\n");
month = month + 1;
if (month > 12) {
month -= 12;
year += 1;
}
}
return 0;
}
int getdaycode(int month, int year)
{
int numdays;
{
numdays = ((year - 1) * 365 + ((year - 1) / 4) - ((year - 1) / 100) + ((year - 1) / 400)); // how many days including exceptions
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) //check if leapyear
{
if (month == 1) // January
numdays = numdays;
if (month == 2) // February
numdays = numdays + 31;
if (month == 3) // March
numdays = numdays + 28 + 31 + 1;
if (month == 4) // April
numdays = numdays + 31 + 28 + 31 + 1;
if (month == 5) // May
numdays = numdays + 30 + 31 + 28 + 31 + 1;
if (month == 6) // June
numdays = numdays + 31 + 30 + 31 + 28 + 31 + 1;
if (month == 7) // July
numdays = numdays + 30 + 31 + 30 + 31 + 28 + 31 + 1;
if (month == 8) // August
numdays = numdays + 31 + 30 + 31 + 30 + 31 + 28 + 31 + 1;
if (month == 9) // September
numdays = numdays + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31 + 1;
if (month == 10) // October
numdays = numdays + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31 + 1;
if (month == 11) // November
numdays = numdays + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31 + 1;
if (month == 12) // December
numdays = numdays + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31 + 1;
}
else
{
if (month == 1) // January
numdays = numdays;
if (month == 2) // February
numdays = numdays + 31;
if (month == 3) // March
numdays = numdays + 28 + 31;
if (month == 4) // April
numdays = numdays + 31 + 28 + 31;
if (month == 5) // May
numdays = numdays + 30 + 31 + 28 + 31;
if (month == 6) // June
numdays = numdays + 31 + 30 + 31 + 28 + 31;
if (month == 7) // July
numdays = numdays + 30 + 31 + 30 + 31 + 28 + 31;
if (month == 8) // August
numdays = numdays + 31 + 30 + 31 + 30 + 31 + 28 + 31;
if (month == 9) // September
numdays = numdays + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31;
if (month == 10) // October
numdays = numdays + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31;
if (month == 11) // November
numdays = numdays + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31;
if (month == 12) // December
numdays = numdays + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31;
}
}
int daycode = (numdays + 1) % 7;
return daycode;
}
void printheader(int month, int year)
{
printf("%14d %1d\n", month, year);
printf("Sun ");
printf("Mon ");
printf("Tue ");
printf("Wed ");
printf("Thu ");
printf("Fri ");
printf("Sat\n");
}
int getndim(int month, int year)
{
int numdays;
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) //check if leapyear
{
if (month == 1) // January
numdays = 31;
if (month == 2) // February
numdays = 29;
if (month == 3) // March
numdays = 31;
if (month == 4) // April
numdays = 30;
if (month == 5) // May
numdays = 31;
if (month == 6) // June
numdays = 30;
if (month == 7) // July
numdays = 31;
if (month == 8) // August
numdays = 31;
if (month == 9) // September
numdays = 30;
if (month == 10) // October
numdays = 31;
if (month == 11) // November
numdays = 30;
if (month == 12) // December
numdays = 31;
}
else
{
if (month == 1) // January
numdays = 31;
if (month == 2) // February
numdays = 28;
if (month == 3) // March
numdays = 31;
if (month == 4) // April
numdays = 30;
if (month == 5) // May
numdays = 31;
if (month == 6) // June
numdays = 30;
if (month == 7) // July
numdays = 31;
if (month == 8) // August
numdays = 31;
if (month == 9) // September
numdays = 30;
if (month == 10) // October
numdays = 31;
if (month == 11) // November
numdays = 30;
if (month == 12) // December
numdays = 31;
}
return numdays;
}
Upvotes: 1
Views: 205
Reputation: 144951
Your code has several issues:
daycode
is off by one.daycode
, not nummonths
in main()
, resulting in all months appearing to start on a Sunday.Here is a suggestion:
int main(void) {
int day, month, year, nummonths;
printf("Enter Month, Year, and number of Months: ");
if (scanf("%d %d %d", &month, &year, &nummonths) != 3
|| year <= 0 || month < 1 || month > 12) {
printf("invalid input\n");
return 1;
}
for (int i = 0; i < nummonths; i++) {
printheader(month, year);
int numdays = getndim(month, year);
int daycode = getdaycode(month, year);
printf("%*s", daycode * 4, ""); /* print 4 spaces for each skipped day */
for (day = 1; day <= numdays; day++) {
printf("%3d", day);
daycode = (daycode + 1) % 7;
if (daycode != 0)
printf(" ");
else
printf("\n");
}
if (daycode != 0)
printf("\n");
printf("\n");
month = month + 1;
if (month > 12) {
month -= 12;
year += 1;
}
}
return 0;
}
You also need to fix getdaycode
by changing the formula to compute it from numdays
at the end of the function:
...
}
int daycode = (numdays + 1) % 7;
return daycode;
}
}
If you have learned arrays already, your code could be simplified a lot:
static int const days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
static int const leapdays[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int getdaycode(int month, int year) {
int numdays = 0;
if (year > 0) {
// how many days in previous years including exceptions
numdays = (year - 1) * 365 + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400;
}
if (month >= 1 && month <= 12) {
if (month > 2 && (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)))
numdays += 1; // count february 29
for (int i = 0; i < month - 1; i++)
numdays += days[i];
}
// need to offset by one so 1/1/1 falls on a Monday.
// dates before 1753 are computed according to the
// proleptic Gregorian calendar.
return (numdays + 1) % 7;
}
int getndim(int month, int year) {
if (month < 1 || month > 12)
return 0;
else
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
return leapdays[month - 1];
else
return days[month - 1];
}
Upvotes: 3
Reputation: 34839
I did something similar recently because I wanted to know the number of Tuesdays in March, April, and May. So I wrote this code
#include <stdio.h>
int main( void )
{
char *monthNames[] = { "March", "April", "May" };
printf( "Enter starting day (0-6): " );
int day;
if ( scanf( "%d", &day ) != 1 )
return 1;
day = -(day % 7);
for ( int m = 0; m < 3; m++ ) {
printf( "\n%s\n", monthNames[m] );
int maxday = (m == 1) ? 30 : 31;
while ( day < maxday ) {
for ( int col = 0; col < 7; col++ ) {
if ( day < 0 )
printf( " " );
else if ( day < maxday )
printf( " %2d", day+1 );
day++;
}
printf( "\n" );
}
if ( day > maxday )
day = day - maxday - 7;
else
day = 0;
}
}
The general idea is the variable day
starts as a number between -6
and 0
. As long as day
is negative, the code prints three spaces. When the day
is between 0 and the number of days in the month, the code prints a number. At the end of the month, the starting value for day
is computed for the next month, based on where the previous month ended.
Upvotes: 1