打印与适当输入相对应的月份

我必须编写一个程序,用户使用两个整数a和b,其中a对应于一年中的月份(1 = jan,2 = feb等)。 该程序必须打印“a”和随后的“b”月份之后的月份。 这就是我到目前为止所做的,但是对于我输入的每两个整数,我总是得到相同的输出:“1月,2月”。 任何帮助表示赞赏。

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

你有这个主要的

  scanf("%d%d", &M, &N); for (M = jan; M <= N; ((int)M++)) { /* ... */ } 

因此......在scanf行中,您将M(和N)更改为用户提供的值
在此之后你将M设置为jan有效地失去了用户选择的内容。

你需要检查你的方式。

我对改进程序的建议是用一系列月份名替换你的switch语句。 编程和阅读会容易得多。

每当您可以用数据结构替换代码时,这通常是一个很大的改进。 无论何时进行编程,都需要记住和使用。

所以按照我的建议使用月份数组看起来有点像这样:

 #include  const char* months[12] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; int main(void) { int m; printf("Enter month: "); scanf("%d", &m); if( 1 <= m && m <= 12 ) { printf("%s\n", months[m-1]); } return 0; } 

return表示从当前函数返回,因此在for循环的第一次迭代结束时,从main函数返回,程序退出。

试试这段代码:

 int main(void) { int a, b, m; printf("Please enter two integers between 1-12:\n"); scanf("%d%d", &a, &b); for (m=a+1; b; b--, m++) { printf(" "); print_month(m); /*function call to print month*/ putchar('\n'); } } 

小心,Beco

PS。 编辑:

另外,将枚举行更改为:

  enum month {jan=1,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec}; 

并注意可能溢出a + b> 12

第2位。 版本:另一种解释可能有用:你不能在这样的循环中使用return并期望循环将运行,因为在计算机运行return它会退出程序。

查看“%”和宏的C / C ++文档 – “#define”。 对于这个问题,交换机是不必要的,效率低下并且使代码变长。

增量和模数可用于增加月份。 sting的枚举值可以使用宏来替换值代码和包含相同代码的字符串。

枚举到使用宏字符串:
MSDN链接 – 字符串化运算符(#)

有很多方法可以使用它将枚举转换为字符串( Google it ),包括:

 // Use correct number of parameters here (can use multiple macros) #define ENUM_MACRO(name, offset, v1, v2, v3, v3)\ enum name { v1 = offset, v2, v3, v4};\ const char name##Strings[] = { #v1, #v2, #v3 };\ const char* name##ToString(value) { return name##Strings[value - offset]; } // This way you do not have two different // lists of months to maintain in your code // (the preprocessor creates them) ENUM_MACRO(Month, 1, January, February, March); // // usage: // Month month = Month::Janurary; const char* st = MonthToString(month); // // Incrementing month taking offset (1) and max (12) into account // month = (month + 1) % 12 + Month::Janurary; 

使用这些方法可以大大减少代码的大小,使其更易于阅读和维护。 此外 – 你通过摆脱所有分支来提高性能。

免责声明 – 我没有编译这段代码,是从内存中编写的。

 include  const char* months[12] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; int main(void) { int m; printf("Enter month: "); scanf("%d", &m); if( 1 <= m && m <= 12 ) { printf("%s\n", months[m-1]); } return 0; } 

为我工作。 谢谢。