C初学者编程帮助日历代码更新 [英] C beginner programming help Calendar code update

查看:164
本文介绍了C初学者编程帮助日历代码更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

执行打印给定月份和年份的日历的功能。首先提示用户:

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

输入月份和年份:

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

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

我的代码更新版本,抱歉关于缩进新的stackflow,它不会让我只是简单复制粘贴我的代码说我有缩进问题:

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

我需要帮助的是,我可以打印正确的年份和月份和每个月的天数,但是我完全不知道该怎么做的代码,以便程序本身知道从哪里开始印刷我进入的一个月的第一天。例如,如果我放在2014年1月,日历应该打印一月,在星期四之前放置1,在星期四下等2。
感谢您的帮助。

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.

推荐答案

尝试这个

#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初学者编程帮助日历代码更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆