【发布时间】:2011-03-25 23:28:59
【问题描述】:
我必须编写一个程序,由用户输入两个整数 a 和 b,其中 a 对应于一年中的月份(1 = jan,2 = feb 等)。该程序必须打印“a”之后的月份和接下来的“b”个月。这是我到目前为止所拥有的,但是对于我输入的每两个整数,我总是得到相同的输出:“一月,二月”。任何帮助表示赞赏。
#include<stdio.h>
enum month {jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec}; /*This
allows yoou to name a finite set and to declare identifiers*/
typedef enum month month;
month next_month(month M) /*this is a function definition*/
{
switch (M) /* like an if-else statement, if this month is true goto the M=month you chose*/
{
case jan:
M=feb;break;
case feb:
M=mar;break;
case mar:
M=apr;break;
case apr:
M=may;break;
case may:
M=jun;break;
case jun:
M=jul;break;
case jul:
M=aug;break;
case aug:
M=sep;break;
case sep:
M=oct;break;
case oct:
M=nov;break;
case nov:
M=dec;break;
case dec:
M=jan;break;
}
return M;
}
void print_month (month M) /*this is a function definition*/
{
switch (M) /* like an if-else statement, if this month is true goto the M=month you chose*/
{
case jan:
printf("January");break;
case feb:
printf("February");break;
case mar:
printf("March");break;
case apr:
printf("April");break;
case may:
printf("May");break;
case jun:
printf("June");break;
case jul:
printf("July");break;
case aug:
printf("August");break;
case sep:
printf("September");break;
case oct:
printf("October");break;
case nov:
printf("November");break;
case dec:
printf("December");break;
}
}
int main(void)
{
month M, N, sat;
printf("Please enter two integers:\n");
scanf("%d%d", &M, &N);
for (M = jan; M <= N; ((int)M++))
{
printf(" ");
print_month(M); /*function call to print month*/
printf(" ");
print_month(next_month(M)); /*function call to print previous month*/
putchar('\n');
return;
}
}
【问题讨论】:
-
如果这是一个家庭作业问题,你应该这样标记它。
-
你有调试器吗?使用调试器并逐行检查代码将对您的情况有很大帮助...您将能够看到哪些代码的行为与您的预期不同。
标签: c