Chinaunix首页 | 论坛 | 博客
  • 博客访问: 708027
  • 博文数量: 118
  • 博客积分: 1437
  • 博客等级: 上尉
  • 技术积分: 1155
  • 用 户 组: 普通用户
  • 注册时间: 2009-02-22 20:23
文章分类

全部博文(118)

文章存档

2022年(32)

2017年(3)

2014年(4)

2013年(1)

2011年(2)

2010年(16)

2009年(60)

我的朋友

分类: C/C++

2009-03-25 09:20:30

怎么有没搞明白,下面的程序,在程序中间定义的变量为什么编译器不报错?
/*

**  Written by Dann Corbit as K&R 2, Exercise 4-2 (Page 73).

**  Keep in mind that this is *JUST* a student exercise, and is

**  light years away from being robust.

**

**  Actually, it's kind of embarassing, but I'm too lazy to fix it.

**

**  Caveat Emptor, not my fault if demons fly out of your nose,

**  and all of that.

*/

#include

#include

#include

#include

#include

 

int my_atof(char *string, double *pnumber)

{

    /* Convert char string to double data type. */

    double          retval;

    double          one_tenth = 0.1;

    double          ten = 10.0;

    double          zero = 0.0;

    int             found_digits = 0;

    int             is_negative = 0;

    char           *num;

 

    /* Check pointers. */

    if (pnumber == 0) {

        return 0;

    }

    if (string == 0) {

        *pnumber = zero;

        return 0;

    }

    retval = zero;

 

    num = string;

 

    /* Advance past white space. */

    while (isspace(*num))

        num++;

 

    /* Check for sign. */

    if (*num == '+')

        num++;

    else if (*num == '-') {

        is_negative = 1;

        num++;

    }

    /* Calculate the integer part. */

    while (isdigit(*num)) {

        found_digits = 1;

        retval *= ten;

        retval += *num - '0';

        num++;

    }

 

    /* Calculate the fractional part. */

    if (*num == '.') {

        double          scale = one_tenth;

        num++;

        while (isdigit(*num)) {

            found_digits = 1;

            retval += scale * (*num - '0');

            num++;

            scale *= one_tenth;

        }

    }

    /* If this is not a number, return error condition. */

    if (!found_digits) {

        *pnumber = zero;

        return 0;

    }

    /* If all digits of integer & fractional part are 0, return 0.0 */

    if (retval == zero) {

        *pnumber = zero;

        return 1;               /* Not an error condition, and no need to

                                 * continue. */

    }

    /* Process the exponent (if any) */

    if ((*num == 'e') || (*num == 'E')) {

        int             neg_exponent = 0;

        int             get_out = 0;

        long            index;

        long            exponent = 0;

        double          getting_too_big = DBL_MAX * one_tenth;

        double          getting_too_small = DBL_MIN * ten;

 

        num++;

        if (*num == '+')

            num++;

        else if (*num == '-') {

            num++;

            neg_exponent = 1;

        }

        /* What if the exponent is empty?  Return the current result. */

        if (!isdigit(*num)) {

            if (is_negative)

                retval = -retval;

 

            *pnumber = retval;

 

            return (1);

        }

        /* Convert char exponent to number <= 2 billion. */

        while (isdigit(*num) && (exponent < LONG_MAX / 10)) {

            exponent *= 10;

            exponent += *num - '0';

            num++;

        }

 

        /* Compensate for the exponent. */

        if (neg_exponent) {

            for (index = 1; index <= exponent && !get_out; index++)

                if (retval < getting_too_small) {

                    get_out = 1;

                    retval = DBL_MIN;

                } else

                    retval *= one_tenth;

        } else

            for (index = 1; index <= exponent && !get_out; index++) {

                if (retval > getting_too_big) {

                    get_out = 1;

                    retval = DBL_MAX;

                } else

                    retval *= ten;

            }

    }

    if (is_negative)

        retval = -retval;

 

    *pnumber = retval;

 

    return (1);

}

/*

** Lame and evil wrapper function to give the exercise the requested

** interface.  Dann Corbit will plead innocent to the end.

** It's very existence means that the code is not conforming.

** Pretend you are a C library implementer, OK?  But you would fix

** all those bleeding gaps, I am sure.

*/

double atof(char *s)

{

    double          d = 0.0;

    if (!my_atof(s, &d))

    {

#ifdef DEBUG

        fputs("Error converting string in [sic] atof()", stderr);

#endif

        raise(SIGFPE);

    }

    return d;

}

 


char  *strings[] = {

    "1.0e43",

    "999.999",

    "123.456e-9",

    "-1.2e-3",

    "1.2e-3",

    "-1.2E3",

    "-1.2e0",

    "cat",

    "",

    0

};

int  main(void)

{

    int             i = 0;

    for (; *strings[i]; i++)

        printf("atof(%s) = %g\n", strings[i], atof(strings[i]));

    return 0;

}
阅读(1411) | 评论(1) | 转发(0) |
0

上一篇:GRUB下键盘失灵

下一篇:class_device_create

给主人留下些什么吧!~~

chinaunix网友2009-03-30 00:27:37

你可能理解错了。 C的变量定义可以出现在任意block的可执行语句之前。 包括if, while 的block 开头都可以。不过一般习惯是放在function block的开头。 参考C++ the Complete Reference 任意版本