ç初学编程帮助日历代码更新(C beginner programming help Calendar

2019-10-19 15:25发布

实现一个可打印给定月份和年份日历的功能。 首先,提示用户:

输入月份和年份:

一旦用户输入一个有效输入(由空格分隔两个整数),打印出的格式的日历类似于UNIX cal命令的输出。 例如,如果用户输入03 2014,输出应该是:

http://imgur.com/3LXleAr

一个更新版本的我的代码,大约缩进进出口新的stackflow对不起,它不会让我只是简单地复制和粘贴我的代码说我有压痕问题:

#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;
}

我需要帮助的是,我可以打印天的正确的年份和月份和金额每月,但IM是完全被卡住,以什么做的代码,使自己知道progrma从哪里开始打印第一本月我输入的一天。 例如,如果我把01 2014年日历应打印一月,把一个1下wensday,2下周四...等等。 谢谢您的帮助。

Answer 1:

如果MM-yyyy是你要确定会,那么,什么输入确定1毫米YYYY的工作日,这样就可以开始打印了从本周日当月的日期。 为了确定平日有式。 阅读此 。 有代码下确定工作日给定日期。



Answer 2:

尝试这个

#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;
}


文章来源: C beginner programming help Calendar code update