实现了打印日历给定月份和年份的函数。首先,提示用户:
输入月份和年份:
一旦用户输入一个有效的输入(两个整数用空格隔开),打印出的格式日历是类似的UNIX CAL
命令的输出。例如,如果用户输入 03 2014年
,输出应该是:
我需要能够询问用户特定的输入,这个问题正在寻求帮助。我还具有与创建code表示将能够打印基于所输入的不同月份,如每个月开始在不同的一天的麻烦。我不能使用任何过于复杂,因为我是在一个初级课程的规划。
在code我到目前为止只打印出月:
的#include< stdio.h中>
诠释的main()
{
INT K,RMD;
的printf(2014年3月的\ n);
的printf(苏周一周二周三周四周五周六\ N);
为(K = 1; K&所述; 32; ++ k)的{
如果(K == 1){
的printf(%2D \ N,K);
}
否则如果(K%7 == 1){
的printf(%2D \ N,K);
}
其他 {
的printf(%2d中,k)的;
}
}
返回0;
}
解决方案
的#include< stdio.h中>
INT isLeapYear(INT年); / *如果真闰年* /
INT leapYears(INT年); / *闰年数* /
INT todayOf(INT Y,INT男,INT D); / *天自今年年初数* /
长天(INT Y,INT男,INT D); / *总天数* /
无效日历(INT Y,INT M); / *显示日历在M Y * /
诠释的主要(无效){
INT年,月;
的printf(请输入月份和年份:);
scanf函数(%D,和放大器;当月,和放大器;一年);
日历(年,月);
返回0;
}
INT isLeapYear(INT Y)/ *如果真闰年* /
{
收益率(Y%400 == 0)|| ((Y%4 == 0)及和放大器;!(Y%100 = 0));
}
INT leapYears(INT Y)/ *闰年数* /
{
返回Y / 4 - Y / 100 + Y / 400;
}
INT todayOf(INT Y,INT男,INT D)/ *天自今年年初数* /
{
静态INT DAYOFMONTH [] =
{1 / *虚* /,0,31,59,90,120,151,181,212,243,273,304,334};
返回DAYOFMONTH [米] + D +((米→2&安培;&安培; isLeapYear(y))为1:0);
}
长天(INT Y,INT男,INT D)/ *总天数* /
{
INT lastYear;
lastYear = Y - 1;
返回365L * lastYear + leapYears(lastYear)+ todayOf(Y,M,D);
}
无效日历(INT Y,INT M)/ *显示日历我的* /
{
为const char * NameOfMonth [] = {NULL / * dummp * /,
月,月,月,月,五一,六一,
七五,八五,九五,十月,月,月
};
焦炭周[] =苏周一周二周三周四周五周六;
INT DAYOFMONTH [] =
{1 / *虚* /,31,28,31,30,31,30,31,31,30,31,30,31};
INT weekOfTopDay;
INT I,日;
weekOfTopDay =天(Y,M,1)%7;
如果(isLeapYear(y))为
DAYOFMONTH [2] = 29;
的printf(\ñ%S%D \ñ%S \ N,NameOfMonth [M],Y,周);
对于(i = 0; I< weekOfTopDay;我++)
的printf();
对于(I = weekOfTopDay,天= 1;天< = DAYOFMONTH [M];我++,一天++){
的printf(%2D,日);
如果(I%7 == 6)
的printf(\ N);
}
的printf(\ N);
}
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:
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.
The code I have so far for only printing out March:
#include <stdio.h>
int main()
{
int k, rmd;
printf(" March 2014\n");
printf(" Su Mo Tu We Th Fr Sa\n");
for(k = 1; k < 32; ++k) {
if(k == 1){
printf(" %2d\n", k);
}
else if(k % 7 == 1) {
printf(" %2d\n", 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("\n %s %d\n%s\n", 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("\n");
}
printf("\n");
}