当前位置: 代码迷 >> C语言 >> 菜菜鸟 终于写出来了..大家帮我看看有没有改进的地方!谢谢自由人生
  详细解决方案

菜菜鸟 终于写出来了..大家帮我看看有没有改进的地方!谢谢自由人生

热度:442   发布时间:2008-05-03 03:04:26.0
菜菜鸟 终于写出来了..大家帮我看看有没有改进的地方!谢谢自由人生
代码如下:

/****该函数判断输入是否为闰年并且判断日期是否输入正确****/

#include <stdio.h>

void main()
{
    int year=0;
    int monch=0;
    int day=0;

    while(1)
    {
        printf("请输入生日(格式1988-06-18):");
        scanf("%d-%d-%d",&year,&monch,&day);
        fflush(stdin);

        if((year>=1900 && year<=2000))
        {
            if((year%4==0&&year%100!=0)||year%400==0)
            {
                printf("%d年是闰年\n",year);
                if(monch==1||monch==3||monch==5||monch==7||monch==8||monch==10||monch==12)
                {
                    printf("%d月有31天\n",monch);
                    if(day<=31&&day>=1)
                    {
                        printf("日期输入正确\n");
                        return;
                    }
                    else
                        printf("日期输入错误\n");
                }
                else
                    if(monch==4||monch==6||monch==9||monch==11)
                    {
                        printf("%d月有30天\n",monch);
                        if(day<=30&&day>=1)
                        {
                            printf("日期输入正确\n");
                            return;
                        }
                        else
                            printf("日期输入错误!\n");
                    }
                else
                    if(monch==2)
                    {
                        printf("%d月有29天\n",monch);
                        if(day<=29&&day>=1)
                        {
                            printf("日期输入正确\n");
                            return;
                        }
                        else
                            printf("日期输入错误!\n");
                    }

                else
                    if(monch>12||monch<0)
                        printf("月份输入错误!\n");
            }
            else
            {
                printf("%d年不是闰年\n",year);
                if(monch==1||monch==3||monch==5||monch==7||monch==8||monch==10||monch==12)
                {
                    printf("%d月有31天\n",monch);
                    if(day<=31&&day>=1)
                    {
                        printf("日期输入正确\n");
                        return;
                    }
                    else
                        printf("日期输入错误!\n");
                }
                else
                    if(monch==4||monch==6||monch==9||monch==11)
                    {
                        printf("%d月有30天\n",monch);
                        if(day<=30&&day>=1)
                        {
                            printf("日期输入正确\n");
                            return;
                        }
                        else
                            printf("日期输入错误\n");
                    }
                else
                    if(monch==2)
                    {
                        printf("%d月有28天\n",monch);
                        if(day<=28&&day>=1)
                        {
                            printf("日期输入正确\n");
                            return;
                        }
                        else
                            printf("日期输入错误!\n");
                    }
                else
                    if(monch>12||monch<0)
                        printf("月份输入错误!\n");
            }
        }
        else    
            printf("你输入的年份错误!\n");        
    }
}

[[it] 本帖最后由 剁吧剁吧 于 2008-5-3 03:05 编辑 [/it]]
搜索更多相关的解决方案: 菜菜  人生  自由  改进  

----------------解决方案--------------------------------------------------------
程序代码:
#include <stdio.h>

void main()
{
    int year=0;
    int monch=0;
    int day=0;

    while(1)
    {
        printf("请输入生日(格式1988-06-18):");
        scanf("%d-%d-%d",&year,&monch,&day);
        fflush(stdin);

        if((year>=1900 && year<=2000))
        {
            if((year%4==0&&year%100!=0)||year%400==0)
            {
                printf("%d年是闰年\n",year);
                if(monch==1||monch==3||monch==5||monch==7||monch==8||monch==10||monch==12)
                {
                    printf("%d月有31天\n",monch);
                    if(day<=31&&day>=1)
                    {
                        printf("日期输入正确\n");
                        return;
                    }
                    else
                        printf("日期输入错误\n");
                }
                else
                {
                    if(monch==4||monch==6||monch==9||monch==11)
                    {
                        printf("%d月有30天\n",monch);
                        if(day<=30&&day>=1)
                        {
                            printf("日期输入正确\n");
                            return;
                        }
                        else
                            printf("日期输入错误!\n");
                    }
                    else
                    {
                        if(monch==2)
                        {
                            printf("%d月有29天\n",monch);
                            if(day<=29&&day>=1)
                            {
                                printf("日期输入正确\n");
                                return;
                            }
                            else
                                printf("日期输入错误!\n");
                        }
                        else
                        {
                            if(monch>12||monch<0)
                                printf("月份输入错误!\n");
                        }
                    }
                }
                else
                {
                    printf("%d年不是闰年\n",year);
                    if(monch==1||monch==3||monch==5||monch==7||monch==8||monch==10||monch==12)
                    {
                        printf("%d月有31天\n",monch);
                        if(day<=31&&day>=1)
                        {
                            printf("日期输入正确\n");
                            return;
                        }
                        else
                            printf("日期输入错误!\n");
                    }
                    else
                    {
                        if(monch==4||monch==6||monch==9||monch==11)
                        {
                            printf("%d月有30天\n",monch);
                            if(day<=30&&day>=1)
                            {
                                printf("日期输入正确\n");
                                return;
                            }
                            else
                                printf("日期输入错误\n");
                        }
                        else
                        {
                            if(monch==2)
                            {
                                printf("%d月有28天\n",monch);
                                if(day<=28&&day>=1)
                                {
                                    printf("日期输入正确\n");
                                    return;
                                }
                                else
                                    printf("日期输入错误!\n");
                            }
                            else
                            {
                                if(monch>12||monch<0)
                                    printf("月份输入错误!\n");
                            }
                        }
                    }
            }
        }
        else
            printf("你输入的年份错误!\n");
    }
}


太宏伟壮观了 看的我眼晕
简单给你改了一下书写问题
程序算法你自己再看看 再想想
找找数学(数字)上的共性
----------------解决方案--------------------------------------------------------
#define _IS_LEAP_YEAR(y)        (((y % 4 == 0) && (y % 100 != 0)) || \
                                ((y + 1900) % 400 == 0))
上面是从VC的crt源文件中抽出来的一个宏,不好怎么评价,但既然是微软的东西,自然还是比较权威的(呵呵,最好不要用这个词了,最近都得罪广陵了,嘿嘿~~~),我还是倾向于改成inline函数的形式:
/********************************************************
** Highlight software by yzfy(雨中飞燕) http://yzfy.org *
*********************************************************/
#include <stdio.h>

bool IsLeap(int year)
{
    return (year%4==0 && year%100!=0) || year%400==0;
}

bool IsDateVaild(int year,int month,int day)
{
    if (month<=0 || day<=0 || day>31)
        return false;
    switch (month)
    {
    case 4:case 6:case 9:case 11:
        if (day>30)return false;
        break;
    case 2:
        if (day>28 && !IsLeap(year))
            return false;
        break;
    }
    return true;
}

int main()
{
    int year=0;
    int month=0;
    int day=0;

    for (;;)
    {
        printf("请输入生日(格式1988-06-18):");

        while (scanf("%d-%d-%d",&year,&month,&day)!=3)
        {
            printf("格式错误,请重新输入。\n");
            while (getchar()!='\n');
        }
        printf("%d年%s是闰年。\n",year,IsLeap(year)?"":"不");
        printf("日期输入%s。\n\n",IsDateVaild(year,month,day)?"正确":"错误");
    }
    return 0;
}


点评一下你的代码:
1 格式很好,值得赞赏。
2 要争取自己的代码中。没有任何两行是一模一样的。如果发现会出现重复代码,使用更清晰的逻辑避免,或者写成子函数调用。
3 int main,这个是小毛病,无足挂齿,记住就可以了。
4 fflush只用于可写的文件句柄(输出流),用处是将还存在缓冲区的数据写入到文件中,用在可读的文件句柄(输入流)上无效。如果要完成类似的功能,参考我的代码。
5 只能输入一个世纪的时间么……太没扩展性了吧?嘿嘿,玩笑而已,不过也要注意代码扩展性哦~~
6 多种结构都可以使用的时候,考虑用最优的结构,比如for(;;)比while(1)好(前者直接跳转,后者需要判断,当然这个判断现代编译器一般都优化掉了的……)一连串的if可以换成switch。反正可以多学别的语法嘛,何乐而不为呢~~~
7 有人建议,如果一个函数超过40行,优化你的程序。所以呢,如果发现代码过长了,就是考验你优化能力的时候了!

就这么多,加油哦~~~
----------------解决方案--------------------------------------------------------
[bo]以下是引用 [un]starwing[/un] 在 2008-5-3 06:37 的发言:[/bo]

#define _IS_LEAP_YEAR(y)        (((y % 4 == 0) && (y % 100 != 0)) || \
                                ((y + 1900) % 400 == 0))
上面是从VC的crt源文件中抽出来的一个宏,不好怎么评价,但既然是微软的东西,自然 ...



复制你的代码出错 用VC++6.0 编译出现6个错误

错误1:未定义的 IsLeap和IsDateVaild  前面你用 bool 我不明白是什么意思 请指教?
错误2:为定义的 false和true   返回真和假的意义在哪里我有点不明白
----------------解决方案--------------------------------------------------------
while (getchar()!='\n'); 这个语句我也没有学 虽然知道 getchar()!='\n' 是个条件 但是不明白是什么意思..刚刚百度了下 不是很明白
----------------解决方案--------------------------------------------------------
那个……不好意思……我的代码是使用C++编译器编译的……除非你保存成.c的扩展名,要不然不会出错的啊……晕……我改改吧……
----------------解决方案--------------------------------------------------------
/********************************************************
** Highlight software by yzfy(雨中飞燕) http://yzfy.org *
*********************************************************/
#include <stdio.h>

typedef enum {FALSE,TRUE} BOOL;

BOOL IsLeap(int year)
{
    return ((year%4==0 && year%100!=0) || year%400==0)? TRUE : FALSE;
}

BOOL IsDateVaild(int year,int month,int day)
{
    if (month<=0 || day<=0 || day>31)
        return FALSE;
    switch (month)
    {
    case 4:case 6:case 9:case 11:
        if (day>30)return FALSE;
        break;
    case 2:
        if (day>28 && !IsLeap(year))
            return FALSE;
        break;
    }
    return FALSE;
}

int main()
{
    int year=0;
    int month=0;
    int day=0;

    for (;;)
    {
        printf("请输入生日(格式1988-06-18):");

        while (scanf("%d-%d-%d",&year,&month,&day)!=3)
        {
            printf("格式错误,请重新输入。\n");
            while (getchar()!='\n');
        }
        printf("%d年%s是闰年。\n",year,IsLeap(year)?"":"不");
        printf("日期输入%s。\n\n",IsDateVaild(year,month,day)?"正确":"错误");
    }
    return 0;
}


刚刚剔除了C++的东西,这份代码C和C++都可以编译运行了……

[[it] 本帖最后由 StarWing83 于 2008-5-3 14:45 编辑 [/it]]
----------------解决方案--------------------------------------------------------
注意到函数的名字了吗?IsXXX,中文应该就是是某某某,比如IsLeap的意思就是是闰年吗?所以,这样的函数返回真或假是很自然的。比如IsLeap(2000):2000年是闰年吗?返回“真”。
while(getchar()!='\n');的意思是,读取当前行的字符,直到遇到回车。你知道getchar只有用户输入一个回车才会开始读取。如果scanf有误(这里错误的表现是返回值不等于3),那么证明现在缓冲区的数据格式有误。于是读取(并抛弃掉)当前行的字符,然后要求用户重新输入。
----------------解决方案--------------------------------------------------------
大家都在show 码 -,-
俺写不出来 可惜...
----------------解决方案--------------------------------------------------------
  相关解决方案