C beginner programming help Calendar code update

2019-08-02 20:50发布

问题:

Implement a function that prints the calendar for a given month and year. First, prompt the user:

Enter the month and year:

Once the user enters a valid input (two integers separated by a space), print out the calendar in a format be similar to the output of the UNIX cal command. For example, if the user enters 03 2014, the output should be:

http://imgur.com/3LXleAr

An updated version of my code, sorry about indentation im new to stackflow, it wont let me simply just copy and paste my code says i have indentation problems:

#include<stdio.h>

int main(){
int year;
int month, day;

int days_in_month[]={0,31,28,31,30,31,30,31,31,30,31,30,31};

char *months[]=
{
" ",
"               January",
"               February",
"               March",
"               April",
"               May",
"               June",
"               July",
"               August",
"               September",
"               October",
"               November",
"               December"
};




printf("Please enter a month and year: ");
scanf("%d %d", &month, &year);


if(((year%4==0) && (year%100!=0)) || (year%400==0))
{
    days_in_month[2] = 29;

}
else
{
    days_in_month[2] = 28;

}




    printf("%s", months[month]);
    printf("\nSun  Mon  Tue  Wed  Thu  Fri  Sat\n" );


    for ( day = 1; day <= 1; day++ )
    {
        printf(" ");
    }


    for ( day = 1; day <= days_in_month[month]; day++ )
    {
        printf("%2d", day );


        if ( ( day ) % 7 > 0 ){
            printf("   " );
        }
        else{
            printf("\n " );
    }
    }
return 0;
}

What i need help with is that, i can print the correct year and month and the amount of days for each month, however im am completely stuck as to what to do to the code so that the progrma itself knows where to start printing the first day of the month i enter. For example if i put in 01 2014 the calendar should print january and put a 1 under wensday, 2 under thursday... and so on. Thanks for the help.

回答1:

What you want to determine will be, if mm-yyyy is the input then to determine the week day of 1-mm-yyyy so that you can start printing dates for that month from that week day. To determine the weekday there is formula. read this. there is code in C for determining the week day for a given date.



回答2:

try this

#include<stdio.h>

int main(){
int year;
int month, day;

int days_in_month[]={0,31,28,31,30,31,30,31,31,30,31,30,31};

char *months[]=
{
" ",
"               January",
"               February",
"               March",
"               April",
"               May",
"               June",
"               July",
"               August",
"               September",
"               October",
"               November",
"               December"
};




printf("Please enter a month and year: ");
scanf("%d %d", &month, &year);


if(((year%4==0) && (year%100!=0)) || (year%400==0))
{
    days_in_month[2] = 29;

}
else
{
    days_in_month[2] = 28;

}


    int weekday ; // WeekDay ( Sunday - 0,Monday- 1,.....)
    int day1 = 1;
    int i;

weekday  = (day1+=month<3?year--:year-2,23*month/9+day1+4+year/4-year/100+year/400)%7;

    printf("%d day is %d weekday\n", day1,weekday);

    printf("%s", months[month]);
    printf("\nSun  Mon  Tue  Wed  Thu  Fri  Sat\n" );

    if(weekday < 7)
    for(i=0; i<weekday; i++)
            printf("     " );

    for ( day = 1; day <= 1; day++ )
    {
        printf(" ");
    }

    for ( day = 1; day <= days_in_month[month]; day++ )
    {
        printf("%2d", day );


        if ( ( day + weekday) % 7 > 0 ){
            printf("   " );
        }
        else{
            printf("\n " );
    }
    }
return 0;
}