2 qq 34015982 qq_34015982 于 2016.02.25 09:09 提问

按如下函数原型用函数编程解决如下的日期转换问题(要求考虑闰年的问题)

图片

3个回答

caozhy
caozhy   Ds   Rxr 2016.02.25 21:29
 #include <stdio.h>

int mt[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

void set2(int y)
{
    if (y % 4 != 0)
    {
        mt[2] = 28;
    }
    else
    {
        if (y % 100 == 0 && y % 400 != 0)
            mt[2] = 28;
        else
            mt[2] = 29;
    }
}

void showmenu()
{
    printf("1. y/n/d -> yd\n");
    printf("2. yd -> y/n/d\n");
    printf("3. exit\n");    
}

int ymd2yd(int y, int m, int d)
{
    set2(y);
    int r = 0;
    for (int i = 0; i < m; i++)
        r += mt[i];
    r += d;
    return r;
}

void yd2ymd(int yd, int * r)
{
    set2(r[0]);
    int m = 0;
    while (yd > mt[m])
    {
        yd -= mt[m++];
    }
    r[1] = m;
    r[2] = yd;
}

int main()
{   
    char c;
    showmenu();
    while (scanf("%c", &c), 1)
    {
        switch (c)
        {
        case '1':
            int y, int m, int d;
            scanf("%d", &y);
            scanf("%d", &m);
            scanf("%d", &d);
            printf("%d\n", ymd2yd(y, m, d));
            showmenu();
            break;
        case '2':
            int yd;
            int r[3];
            scanf("%d", &r[0]);
            scanf("%d", &yd);
            yd2ymd(yd, r);
            printf("%d %d %d\n", r[0], r[1], r[2]);
            showmenu();
            break;
        case '\n':
            break;
        default:
            return 0;
        }
    }
    return 0;
}

caozhy
caozhy   Ds   Rxr 2016.02.25 21:33
  1. y/n/d -> yd
  2. yd -> y/n/d
  3. exit 1 2016 3 1 61
  4. y/n/d -> yd
  5. yd -> y/n/d
  6. exit 1 2016 7 18 200
  7. y/n/d -> yd
  8. yd -> y/n/d
  9. exit 1 2015 12 31 365
  10. y/n/d -> yd
  11. yd -> y/n/d
  12. exit 2 2016 61 2016 3 1
  13. y/n/d -> yd
  14. yd -> y/n/d
  15. exit 2 2015 61 2015 3 2
  16. y/n/d -> yd
  17. yd -> y/n/d
  18. exit 2 2015 225 2015 8 13
  19. y/n/d -> yd
  20. yd -> y/n/d
  21. exit 3 Press any key to continue
caozhy
caozhy   Ds   Rxr 2016.02.25 21:33
 1. y/n/d -> yd
2. yd -> y/n/d
3. exit 1
2016 3 1
61
1. y/n/d -> yd
2. yd -> y/n/d
3. exit 1
2016 7 18
200
1. y/n/d -> yd
2. yd -> y/n/d
3. exit 1
2015 12 31
365
1. y/n/d -> yd
2. yd -> y/n/d
3. exit 2
2016 61
2016 3 1
1. y/n/d -> yd
2. yd -> y/n/d
3. exit 2
2015 61
2015 3 2
1. y/n/d -> yd
2. yd -> y/n/d
3. exit 2
2015 225
2015 8 13
1. y/n/d -> yd
2. yd -> y/n/d
3. exit 3
Press any key to continue
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!