标题:程序有严重漏洞,找不出原因!!走过路过千万要帮忙改过!!
只看楼主
dsjdcy
Rank: 1
等 级:新手上路
帖 子:31
专家分:0
注 册:2007-12-2
得分:0 
终于搞定这个问题了!!!和大家分享一下
/****这是一个关于单位换算的程序,涉及到的单位很少,但我平时上班只用到这些单位!!各位如果有更好的算法或是觉得什么地方不太妥当,还望提出!!!****/
#include "stdio.h"
#include "string.h"
float everyone_um(float a,char b[]);
void out(float a,float b,char c[]);

main()
{
        float a,b;
        char c[4];
        int z;
        puts("==========wecome!!=============");
        for(;;)
        {
            z=scanf("%f%4s",&a,c);
            if(z!=2)
            {
                a=0;z=2;c[4]="0";
                puts("Number is error!!please agin!!!");
                while (getchar() !='\n');                     /*清空输入缓存区,.关键就在这里,我加了这个语句用来清空键盘缓存区,因为scanf从缓存区没读到与变量a匹配的数据,则会不断的从缓存区里读取,从而导致后来的循环当中不会等待用户输入.而加了这个语句,一旦输入不匹配,则将缓存清空,让下次循环中scanf能顺利等待输入.大家现在关心的可能是这个语句为什么会有清空缓存的功能,这个就要说到getchar函数的运行机制了.getchar永远只从缓存区拿走第一个字符如果缓存区没字符,则等待用户输入,直到输入回车(这些输入包括回车\n将被存放到缓存区)然后getchar取走第一个字符.而在这里用while循环就是让getchar将缓存里面的字符一个个清除,直到遇到'\n'结束,下个循环中,缓存区为空scanf就会等待输入了!!!!!个人拙见,还高各位补充说明!!!!*/
                continue;
            }
            b=everyone_um(a,c);
            if(b==-1)
            {
                puts("unit is error!!! please again!!!");
                continue;
            }
            out(a,b,c);
        }
}

void out(float a,float b,char c[])
{
        printf("---------------------------\n%7.2f %s=%10.4f mm\n",a,c,0.001*b);
        printf("%7.2f %s=%10.4f si\n",a,c,0.1*b);
        printf("%7.2f %s=%10.4f um\n",a,c,b);
        printf("%7.2f %s=%10.4f ft\n",a,c,b*1/25.4/1000/12);
        printf("%7.2f %s=%10.4f in\n",a,c,b*1/25.4/1000);
        printf("%7.2f %s=%10.4f mil\n",a,c,b*1/25.4);
        printf("%7.2f %s=%10.4f uin\n",a,c,b*1/0.0254);
        printf("%7.2f %s=%10.4f oz\n---------------------------\n\n",a,c,b*1/25.4/1.35);
}


float everyone_um(float every,char c[])
{
        if(!strcmp(c,"mm"))
                return(1000*every);
        if(!strcmp(c,"si"))
                return(10*every);
        if(!strcmp(c,"um"))
                return(every);
        if(!strcmp(c,"ft"))
                return(304800*every);
        if(!strcmp(c,"in"))
                return(25400*every);
        if(!strcmp(c,"mil"))
                return(25.4*every);
        if(!strcmp(c,"uin"))
                return(0.0254*every);
        if(!strcmp(c,"oz"))
                return(25.4*1.35*every);
        else
                return(-1);
}

有兴趣共同学习的加QQ287624635 !或E-main至dsjdcy@
2007-12-13 15:44



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-191732-1-1.html




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

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