简体   繁体   中英

Implement the following function which converts a string representation of a floating-point number into a double:

Implement the following function which converts a string representation of a floating-point number into a double:

double strToDouble(char[] str)

Your implementation should be able to properly handle strings such as:

  • “123456”
  • “-123.456”
  • “123.456e13”
  • “123.456e-13”

Any improperly formatted input strings should result in the value 0.0 being returned.

Hints and Suggestions:

  • Use the function strToInt above as a starting point.
  • Use if statements after the first loop to check for things like decimal points, e's (for scientific notation exponents), and negative signs.
  • Use a separate loop to process things after a possible decimal point.
  • Use yet another loop to process possible exponent values after an e.

how i answered after i call function strToDouble

1)should be right output be like that :

123456
-123.456000
1234560000000000.000000
0.000000

2)Why we need to put [i + 2] and [i+1]?I put randomly, but i do not have explanation to that.

-

double strToDouble(const char str[]) {
    int i = 0, k = 10;
    double result = 0, doubleValue, expValue = 0, resultDecimal = 0;
    double exp = 1;

    if (str[0] == '-')
    {
        i++;
    }

    while (str[i] >= '0' && str[i] <= '9')
    {
        doubleValue = str[i] - '0';
        result = result * 10 + doubleValue;
        i++;
    }

    if (str[i] == '.')
    {
        while (str[i + 1] >= '0' && str[i + 1] <= '9')
        {
            doubleValue = (str[i + 1] - '0');
            resultDecimal = resultDecimal + doubleValue / k;
            k *= 10;
            i++;
        }
    }


    if (str[i + 1] == 'e')
    {
        if (str[i + 2] == '-')
        {
            while (str[i + 3] >= '0' && str[i + 3] <= '9')
            {
                doubleValue = str[i + 3] - '0';
                expValue = expValue * 10 + doubleValue;
                i++;
            }
            for (k = 0; k < expValue; k++)
            {
                exp /= 10;
            }
        }
        else
        {
            while (str[i + 2] >= '0' && str[i + 2] <= '9')
            {
                doubleValue = str[i + 2] - '0';
                expValue = expValue * 10 + doubleValue;
                i++;
            }
            for (k = 0; k < expValue; k++)
            {
                exp *= 10;
            }
        }
    }

    result = (result + resultDecimal) * exp;

Why we need to put [i + 2] and [i+1] ?I put randomly, but i do not have explanation to that.

These random offsets break your code. It works for the given examples, but it breaks if you don't specify a period but an exponent, for example '1e3`, because you carry over the offset from the code parsing the mantissa after the period even if there isn't a period:

if (str[i] == '.') {
    // parse mantissa, using i + 1 as index
}

if (str[i + 1] == 'e')     // i + 1 may be one beyond the character you
                           // should be looking at.

You should advance the current character after the period only if you really find one:

if (str[i] == '.') {
    i++;            // step over period
    // parse mantissa, using i as index
}

if (str[i] == 'e') {
    i++;           // step over 'e'
    // parse exponent, using i as index
}

The same goes for stepping past the signs + (which you don't seem to treat at all) and - (which you detect, but don't consider in the result).

In other words: The character you are currently examining should always be at the index i . You can look ahead, but only in context. When you look ahead, you should also make sure that the character you look at isn't after a possible null terminator.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM