打印一个日历月 [英] Printing a calendar month
问题描述
实现一个打印给定月份和年份的日历的函数.首先,提示用户:
Implement a function that prints the calendar for a given month and year. First, prompt the user:
Enter the month and year:
一旦用户输入了一个有效的输入(两个用空格分隔的整数),以类似于 UNIX cal
命令输出的格式打印出日历.例如,如果用户输入 03 2014
,输出应该是:
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:
我需要帮助才能向用户询问此问题所要求的特定输入.我在创建能够根据输入打印不同月份的代码时也遇到了麻烦,因为每个月都从不同的一天开始.因为我正在学习编程初学者课程,所以我不能使用任何太复杂的东西.
I need help with being able to ask the user for the specific input that this question is asking for. I am also having trouble with creating code that will be able to print different months based on the input, as each month starts on a different day. I cannot use anything too complex as I'm taking a beginner course in programming.
到目前为止我只打印出 March 的代码:
The code I have so far for only printing out March:
#include <stdio.h>
int main()
{
int k, rmd;
printf(" March 2014
");
printf(" Su Mo Tu We Th Fr Sa
");
for(k = 1; k < 32; ++k) {
if(k == 1){
printf(" %2d
", k);
}
else if(k % 7 == 1) {
printf(" %2d
", k);
}
else {
printf(" %2d", k);
}
}
return 0;
}
推荐答案
#include <stdio.h>
int isLeapYear( int year ); /* True if leap year */
int leapYears( int year ); /* The number of leap year */
int todayOf( int y, int m, int d); /* The number of days since the beginning of the year */
long days( int y, int m, int d); /* Total number of days */
void calendar(int y, int m); /* display calendar at m y */
int main(void){
int year,month;
printf("Enter the month and year: ");
scanf("%d %d", &month, &year);
calendar(year, month);
return 0;
}
int isLeapYear( int y ) /* True if leap year */
{
return(y % 400 == 0) || ((y % 4 == 0) && (y % 100 != 0));
}
int leapYears( int y ) /* The number of leap year */
{
return y/4 - y/100 + y/400;
}
int todayOf( int y, int m, int d) /* The number of days since the beginning of the year */
{
static int DayOfMonth[] =
{ -1/*dummy*/,0,31,59,90,120,151,181,212,243,273,304,334};
return DayOfMonth[m] + d + ((m>2 && isLeapYear(y))? 1 : 0);
}
long days( int y, int m, int d) /* Total number of days */
{
int lastYear;
lastYear = y - 1;
return 365L * lastYear + leapYears(lastYear) + todayOf(y,m,d);
}
void calendar(int y, int m) /* display calendar at m y */
{
const char *NameOfMonth[] = { NULL/*dummp*/,
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
};
char Week[] = "Su Mo Tu We Th Fr Sa";
int DayOfMonth[] =
{ -1/*dummy*/,31,28,31,30,31,30,31,31,30,31,30,31 };
int weekOfTopDay;
int i,day;
weekOfTopDay = days(y, m, 1) % 7;
if(isLeapYear(y))
DayOfMonth[2] = 29;
printf("
%s %d
%s
", NameOfMonth[m], y, Week);
for(i=0;i<weekOfTopDay;i++)
printf(" ");
for(i=weekOfTopDay,day=1;day <= DayOfMonth[m];i++,day++){
printf("%2d ",day);
if(i % 7 == 6)
printf("
");
}
printf("
");
}
这篇关于打印一个日历月的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!