通用数据检查函数集






1.07/5 (13投票s)
2002年12月25日
2分钟阅读

54513

244
一套通用的数据校验函数,可用于任何需要校验的系统。
引言
在许多程序中,我们需要检查用户数据的范围。对于一个包含数千个参数的程序,验证所有数据将是一件痛苦的事情。最好的方法是自动进行检查。为了使其运行,关键在于如何以统一的方式编写校验例程。我找到了方法。这只是 C++ 中的一个简单技巧。我为所有人提供了源代码。在后续文章中,我将向您展示一种以通用方式使用这些例程的特殊控件。:)
校验理论
检查数据范围的基本理论非常简单但至关重要。在数学上,数据的范围是定义域。在 1D 条件下,一个定义域具有一个或两个限制,一个用于较大的边界,一个用于较小的边界。通常,我们将其写为 [min,max] 表示闭域,或 (min , max ) 表示开域。我们的校验例程应包含我们可能遇到的所有条件。
我列出以下条件
1) Let it be , no any check ! 2) given number must be less than the limit. number < min 3) given number must be great than the limit. number > max 4) given number must be within a range min < number < max 5) given number must be less or equal to limit number <= min 6) given number must be great or equal to limit number >= max 7) given number must be within a range with lower equal min<= number < max 8) given number must be within a range with greater equal min < number <= max 9) given number must be within a range equal to bound min <= number <= max 10) given number must be outside a range without equal number < min AND number > max 11) given number must be outside a range with lower equal number <= min AND number > max 12) given number must be outside a range with lower equal number < min AND number >= max 13) given number must be outside a range with lower equal number <= min AND number >= max The following condition is important for compatility. 14 ) given number must be less than the limit ( the limit is set as max ) number <max 15 ) given number must be great than the limit ( the limit is set as min ) number >min 16 ) given number must be less than or equal to the limit ( the limit is set as max ) number <= max 17 ) given number must be great than or equal to the limit ( the limit is set as min ) number >=min
处理数据类型
另一个难题是如何将这些规则应用于各种数据结构。C++ 提供了服务器方法,例如字符串、双精度浮点数、整数、长整数、日期时间等。每种方法都有不同的校验方式。关系符号的含义也不同。不安全的方法是提供一个宏。我认为更好的方法是使用 C++ 的模板,它具有强类型,可以在运行时使我们的代码更安全。
template<class T> BOOL internalCheckLess( T& value , T& minv, T& maxv) { return value < minv; } template<class T> BOOL internalCheckGreat( T& value , T& minv, T& maxv) { return value > maxv; } template<class T> BOOL internalCheckLessMax( T& value , T& minv, T& maxv) { return value < maxv; } template<class T> BOOL internalCheckGreatMin( T& value , T& minv, T& maxv) { return value > minv; } template<class T> BOOL internalCheckLessMaxEqu( T& value , T& minv, T& maxv) { return value <= maxv; } template<class T> BOOL internalCheckGreatMinEqu( T& value , T& minv, T& maxv) { return value >= minv; } template<class T> BOOL internalCheckBoth( T& value , T& minv, T& maxv) { return value > minv && value < maxv ; } template<class T> BOOL internalCheckLessEqu( T& value , T& minv, T& maxv) { return value <= minv; } template<class T> BOOL internalCheckGreatEqu( T& value , T& minv, T& maxv) { return value >= maxv; } template<class T> BOOL internalCheckBothEqu( T& value , T& minv, T& maxv) { return value >= minv && value <= maxv ; } template<class T> BOOL internalCheckBothLessEqu( T& value , T& minv, T& maxv) { return value >= minv && value < maxv ; } template<class T> BOOL internalCheckBothGreatEqu( T& value , T& minv, T& maxv) { return value > minv && value <= maxv ; } template<class T> BOOL internalCheckOutEqu( T& value , T& minv, T& maxv) { return value <= minv && value >= maxv ; } template<class T> BOOL internalCheckOutLessEqu( T& value , T& minv, T& maxv) { return value <= minv && value > maxv ; } template<class T> BOOL internalCheckOutGreatEqu( T& value , T& minv, T& maxv) { return value < minv && value >= maxv ; } template<class T> BOOL internalCheckOut( T& value , T& minv, T& maxv) { return value < minv && value > maxv ; }
提示
当校验例程发现用户输入的数据出现错误时,我们应该向用户发出消息,并告知他或她发生了什么类型的错误。通常,我们会显示带有原因的消息框。消息将根据用户指定的设置显示。我们将所有错误消息收集到一个头文件中,以便根据他们的语言或更合适的措辞进行轻松更改。我们可以为每种数据类型编写一个函数。
为什么不使用类
将这些函数编写为类可能是一个好主意。以函数的形式编写这些函数是一种轻量级的方法。我认为使用函数比编写类更方便。我将在下一篇文章中演示如何在控件组中使用这些函数,该文章即将发布。
就这样!尽情享受吧。
历史
Version Author Date Content =========================================================================== 0.9 Johnson Zhou 2002/08/01 Initial 1.0 Johnson Zhou 2002/12/07 Write internal function as template 1.01 Johnson Zhou 2002/12/25 Add function of 14)-17).