During the previous interview, an interview question is often given to the interviewer. Given a string, the value of the floating point indicated by this string is output. The requirements are as follows:
Write a conversion function. The input of this function is a string that represents a floating point number. Convert the string to a floating point number and output it. Condition: consider various situations and use as few cycles as possible in the code. You cannot call functions in the API or crt library. For example, if the input string is "345.7", the floating point number is 345.7. The interface can be: float StrToFloatA (TCHAR * pstrfloat );
I didn't expect that there were not many people doing this right. (I wrote the test on the computer and threw the function into the test machine to see if it was correct.) So I realized it in my free time, it is still a little difficult. The Code is as follows:
/*-------------------------------------------------------------------------
// File name: StringToFloat. h
// Creator: magictong
// Creation Time: 14:14:25
// Function description:
//
// $ Id: $
//-----------------------------------------------------------------------*/
# Ifndef _ STRINGTOFLOAT_H __
# Define _ STRINGTOFLOAT_H __
//-------------------------------------------------------------------------
Float StrToFloatW (wchar_t * pstrfloat );
Float StrToFloatA (char * pstrfloat );
# If defined (UNICODE) | defined (_ UNICODE)
# Define StrToFloat StrToFloatW
# Else
# Define StrToFloat StrToFloatA
# Endif //! UNICODE
//-------------------------------------------------------------------------
// $ Log: $
# Endif/* _ STRINGTOFLOAT_H __*/
/*-------------------------------------------------------------------------
// File name: StringToFloat. cpp
// Creator: magictong
// Creation Time: 14:14:02
// Function description:
//
// $ Id: $
//-----------------------------------------------------------------------*/
# Include "stdafx. h"
# Include "StringToFloat. h"
//-------------------------------------------------------------------------
# Pragma warning (disable: 4244)
//-------------------------------------------------------------------------
// Function: StrToFloatA
// Function: convert a string to a floating point number.
// Return value: float
// Parameter: char * pstrfloat
// Note:
//-------------------------------------------------------------------------
Float StrToFloatA (char * pstrfloat)
{
// Check
If (! Pstrfloat)
{
Return 0.0;
}
Bool bNegative = false;
Bool bDec = false;
Char * pSor = 0;
Char chByte = '0 ';
Float fInteger = 0.0;
Float fDecimal = 0.0;
Float fDecPower = 0.1f;
// First judge whether it is a negative number
If (pstrfloat [0] = '-')
{
BNegative = true;
PSor = pstrfloat + 1;
}
Else
{
BNegative = false;
PSor = pstrfloat;
}
While (* pSor! = '\ 0 ')
{
ChByte = * pSor;
If (bDec)
{
// Decimal
If (chByte> = '0' & chByte <= '9 ')
{
FDecimal + = (chByte-'0') * fDecPower;
FDecPower = fDecPower * 0.1;
}
Else
{
Return (bNegative? -(FInteger + fDecimal): fInteger + fDecimal );
}
}
Else
{
// Integer
If (chByte> = '0' & chByte <= '9 ')
{
FInteger = fInteger * 10.0 + chByte-'0 ';
}
Else if (chByte = '.')
{
BDec = true;
}
Else
{
Return (bNegative? -FInteger: fInteger );
}
}
PSor ++;
}
Return (bNegative? -(FInteger + fDecimal): fInteger + fDecimal );
}
//-------------------------------------------------------------------------
// Function: StrToFloatW
// Function: convert a string to a floating point number.
// Return value: float
// Parameter: char * pstrfloat
// Note:
//-------------------------------------------------------------------------
Float StrToFloatW (wchar_t * pstrfloat)
{
// Check
If (! Pstrfloat)
{
Return 0.0;
}
Bool bNegative = false;
Bool bDec = false;
Wchar_t * pSor = 0;
Wchar_t chByte = L '0 ';
Float fInteger = 0.0;
Float fDecimal = 0.0;
Float fDecPower = 0.1f;
// First judge whether it is a negative number
If (pstrfloat [0] = l '-')
{
BNegative = true;
PSor = pstrfloat + 1;
}
Else
{
BNegative = false;
PSor = pstrfloat;
}
While (* pSor! = L' \ 0 ')
{
ChByte = * pSor;
If (bDec)
{
// Decimal
If (chByte> = L '0' & chByte <= L '9 ')
{
FDecimal + = (chByte-L '0') * fDecPower;
FDecPower = fDecPower * 0.1;
}
Else
{
Return (bNegative? -(FInteger + fDecimal): fInteger + fDecimal );
}
}
Else
{
// Integer
If (chByte> = L '0' & chByte <= L '9 ')
{
FInteger = fInteger * 10.0 + chByte-L '0 ';
}
Else if (chByte = L '.')
{
BDec = true;
}
Else
{
Return (bNegative? -FInteger: fInteger );
}
}
PSor ++;
}
Return (bNegative? -(FInteger + fDecimal): fInteger + fDecimal );
}
//-------------------------------------------------------------------------
// $ Log: $
Test cases:
// StringToFloatShell. cpp: Defines the entry point for the console application.
//
# Include "stdafx. h"
# Include "StringToFloat. h"
Int _ tmain (int argc, _ TCHAR * argv [])
{
Float aaaa = 0;
Aaaa = StrToFloat (L "12.34 ");
Aaaa = StrToFloat (L "a23 ");
Aaaa = StrToFloat (L "1234 ");
Aaaa = StrToFloat (L "12.34 ");
Aaaa = StrToFloat (L "12.34.56 ");
Aaaa = StrToFloat (L ". 34 ");
Aaaa = StrToFloat (L "34a ");
Aaaa = StrToFloat (L "34a. 456 ");
Aaaa = StrToFloat (L "-34 ");
Aaaa = StrToFloat (L "-56.34 ");
Aaaa = StrToFloat (L "-3.45.67 ");
Aaaa = StrToFloat (L "-. 45.6a ");
Aaaa = StrToFloat (L "-.");
Aaaa = StrToFloat (L "-0 ");
Return 0;
}
You can look for bugs again.
[END]
From magictong's column