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

时间:2011-03-25 23:29:00

标签: c

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

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

6 个答案:

答案 0 :(得分:6)

你在主

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

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

您需要检查自己的行为方式。

答案 1 :(得分:6)

我对改进程序的建议是将switch语句替换为月份名称数组。编程和阅读会容易得多。

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

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

#include <stdio.h>

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

答案 2 :(得分:5)

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

答案 3 :(得分:2)

试试这段代码:

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

保重, 贝乔

PS。编辑:

另外,将枚举行更改为:

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

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

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

答案 4 :(得分:0)

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

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

使用宏来枚举字符串:
MSDN link - Stringizing Operator (#)

有很多方法可以使用它将枚举转换为字符串(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;

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

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

答案 5 :(得分:0)

include <stdio.h>

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

为我工作。谢谢。