Tuesday, July 14, 2009

MFC - Find if a given string is numeric

Find if a given string is numeric





bool IsNumeric(const CString& str, bool bIsInt)
{
int nSize = str.GetLength();
int ReturnValue = 0;
int DotCounter = 0;
bool bContiue = true;



//-------Case of integer--------------------------
if ( bIsInt )
{
// Goes over the string characters
for ( int i=0; i < nSize; i++ )
{
ReturnValue = isdigit( str.GetAt(i) );
if ( !ReturnValue )
{
CString strChar = str.GetAt(i);
if ( (i == 0) && (strChar=="-") )
bContiue = true;
else
bContiue = false;
}

if ( !bContiue )
return false;
}

return true;
}

//-------Case of double----------------------
// Goes over the string characters
for ( int i=0; i < nSize; i++ )
{
ReturnValue = isdigit( str.GetAt(i) );
if ( !ReturnValue )
{
CString strChar = str.GetAt(i);
if ((i == 0) && (strChar=="-") )
bContiue = true;
else if ( (i!=0) && (strChar== ".") && (DotCounter==0) )
{
DotCounter = 1;
bContiue = true;
}
else
bContiue = false;
}

if ( !bContiue )
return false;
}

return true;
}

0 תגובות:

Post a Comment

 
Home | About | Link | Link
Simple Proff Blogger Template Created By Herro | Inspiring By Busy Bee Woo Themes