Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Visual C++ and MFC Fundamentals Chapter 21: Tree and List Controls


#endif // MFCEXT_H


  1. To add a new source file, on the main menu, click File -> New... In the Files
    property page of the New dialog box, click C++ Source File. In the File Name edit
    box, type mfcextent and click OK

  2. In the empty file, type the following:


#include "stdafx.h"
#include " mfcextent.h"

namespace MFCExtensions
{
BOOL IsNatural(const CString Str)
{
// Check each character of the string
// If a character at a certain position is not a digit,
// then the string is not a valid natural number
for(int i = 0; i < Str.GetLength(); i++)
{
if( Str[i] < '0' || Str[i] > '9' )
return FALSE;
}

return TRUE;
}

BOOL IsNumeric(const CString Str)
{
// Make a copy of the original string to test it
CString WithoutSeparator = Str;
// Prepare to test for a natural number
// First remove the decimal separator, if any
WithoutSeparator.Replace(".", "");

// If this number were natural, test it
// If it is not even a natural number, then it can't be valid
if( IsNatural(WithoutSeparator) == FALSE )
return FALSE; // Return Invalid Number

// Set a counter to 0 to counter the number of decimal separators
int NumberOfSeparators = 0;

// Check each charcter in the original string
for(int i = 0; i < Str.GetLength(); i++)
{
// If you find a decimal separator, count it
if( Str[i] == '.' )
NumberOfSeparators++;
}

// After checking the string and counting the decimal separators
// If there is more than one decimal separator,
// then this cannot be a valid number
if( NumberOfSeparators > 1 )
return FALSE; // Return Invalid Number
else // Otherwise, this appears to be a valid decimal number
return TRUE;
}
Free download pdf