| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1585 人关注过本帖
标题:习题代码求前辈指点
取消只看楼主 加入收藏
pytao1991
Rank: 1
等 级:新手上路
帖 子:55
专家分:4
注 册:2015-8-26
结帖率:62.5%
收藏
已结贴  问题点数:10 回复次数:5 
习题代码求前辈指点
习题要求输入3个整数值,分别代表月,日,年。例如输入了12,31,2003 程序就以 31st December 2003 的格式输出该日期。
必须在日期的后面加上th,nd,st和rd。例如1st,2nd
我的思路就是先用if语句检测月,日,年的数值范围,因为一年月份最多12个,每月天数最多31....
然后就是想用枚举。例如存储12个月份,然后根据输入输出12个月份中的一个....
下面是我的代码,错误很多。希望前辈指点。我后面用switch语句另写了一个答案,达到题目要求了。但还想试试其他写法,也能达到题目的要求
#include <stdio.h>

int main(void)
{
    unsigned int month = 0u;
    unsigned int data = 0u;
    unsigned int years = 0u;

    printf("输入三个整数: ");
    scanf("%u %u %u\n",&month,&data,&years);

    if(month >= 1 && month <= 12)
       {if(data <=31 && data >= 1)
            {if(years <= 1900 && years >= 2099)
                enum day {1st,2nd,3rd,4th,5th,6th,7th,8th,9th,10th,11th,12th,
                                   13th,14th,15th,16th,17th,18th,19th,20th,21st,22nd,23rd,
                                    24th,25th,26th,27th,28th,29th,30th,31st};
                enum day today = 1st+(data-1);
               
                enum a_month {January,FebMarch,MarApril,AprMay,MayJune,JunJuly,JulAugust,AugSeptember,
                                 SepOctober,OctNovember,NovDecember,December};
                enum a_month A_month= January+(month-1);

                printf("%u %u %u\n",today,A_month,years);
            else
                printf("请输入1900到2099之间的整数");
           }
         
        else
            printf("请输入1到31之间的整数");

       }
    else
        printf("请输入1到12之间的整数");
上面这个代码错误很多,求前辈指点。下面是我用switch语句写的另外一个答案代码
#include <stdio.h>

int main(void)
{
    unsigned int month = 0u;
    unsigned int day = 0u;
    unsigned int years = 0u;

    printf("输入三个整数:");
    scanf("%u %u %u",&month,&day,&years);

    switch(day)
    {
        case 1:
            printf("1st");
            break;
        case 2:
            printf("2nd");
            break;
        case 3:
            printf("3rd");
            break;
        case 4:
            printf("4th");
            break;
        case 5:
            printf("5th");
            break;
        case 6:
            printf("6th");
            break;
        case 7:
            printf("7th");
            break;
        case 8:
            printf("8th");
            break;
        case 9:
            printf("9th");
            break;
        case 10:
            printf("10th");
            break;
        case 11:
            printf("11th");
            break;
        case 12:
            printf("12th");
            break;
        case 13:
            printf("13th");
            break;
        case 14:
            printf("14th");
            break;
        case 15:
            printf("15th");
            break;
        case 16:
            printf("16th");
            break;
        case 17:
            printf("17th");
            break;
        case 18:
            printf("18th");
            break;
        case 19:
            printf("19th");
            break;
        case 20:
            printf("20th");
            break;
        case 21:
            printf("21st");
            break;
        case 22:
            printf("22nd");
            break;
        case 23:
            printf("23rd");
            break;
        case 24:
            printf("24th");
            break;
        case 25:
            printf("25th");
            break;
        case 26:
            printf("26th");
            break;
        case 27:
            printf("27th");
            break;
        case 28:
            printf("28th");
            break;
        case 29:
            printf("29th");
            break;
        case 30:
            printf("30th");
            break;
        case 31:
            printf("31st");
            break;
        default:
            printf("请输入1到31之间的整数");
            break;
    }
switch(month)
    {
        case 1:
            printf(" January");
            break;
        case 2:
            printf(" Februar");
            break;
        case 3:
            printf(" March");
            break;
        case 4:
            printf(" April");
            break;
        case 5:
            printf(" May");
            break;
        case 6:
            printf(" June");
            break;
        case 7:
            printf(" July");
            break;
        case 8:printf(" August");
            break;
        case 9:
            printf(" September");
            break;
        case 10:
            printf(" October");
            break;
        case 11:
            printf(" November");
            break;
        case 12:
            printf(" December");
            break;
        default:
            printf("请输入1到12之间的整数");
            break;
    }
    if(years <=2099 && years >= 1900)
        printf(" %u\n",years);
    else
        printf("请输入1900到2099之间的整数");
    return 0;
}
搜索更多相关主题的帖子: include 检测 
2015-12-24 22:47
pytao1991
Rank: 1
等 级:新手上路
帖 子:55
专家分:4
注 册:2015-8-26
收藏
得分:0 
是因为枚举类型的枚举器只能用英文还是因为枚举器只能代表数值而不是名称呢
2015-12-24 23:11
pytao1991
Rank: 1
等 级:新手上路
帖 子:55
专家分:4
注 册:2015-8-26
收藏
得分:0 
回复 3楼 TonyDeng
使用枚举本身就是不对的,或者说用枚举也写不出达到题目要求的代码对么?
2015-12-24 23:22
pytao1991
Rank: 1
等 级:新手上路
帖 子:55
专家分:4
注 册:2015-8-26
收藏
得分:0 
回复 5楼 TonyDeng
我是很低级的入门者,不过你说的意思大概也懂了。比如日期,如果是1.2.3结尾的,后面就分别以st,nd,rd,其他的就用th,例如1st,2nd,3re,4th一直到20th.
是这个意思么
2015-12-24 23:35
pytao1991
Rank: 1
等 级:新手上路
帖 子:55
专家分:4
注 册:2015-8-26
收藏
得分:0 
回复 7楼 TonyDeng
4元素的数组不懂什么意思
2015-12-24 23:36
pytao1991
Rank: 1
等 级:新手上路
帖 子:55
专家分:4
注 册:2015-8-26
收藏
得分:0 
回复 9楼 TonyDeng
这个应该还没学习到
2015-12-24 23:42
快速回复:习题代码求前辈指点
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.034776 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved