字符串是否为数字及有效性检查
在编写MFC窗体程序时,我们经常要判断用户输入的一个字符串是否合法有效,以此来增强程序的健壮性。
最近,在测试系统的对话框的输入时,发现存在大量这样的问题,甚至有一些特定变态的输入还导致系统异常退出。
为了解决这些问题,我编写下面这个头文件,来判断输入的字符串是否为数字,以及是否在有效范围之内。
希望以下代码对你有用!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | #ifndef __CT3D__CHECKVALIDVAL__H__ #define __CT3D__CHECKVALIDVAL__H__ class CStringEx { private : CString m_str; int m_dot_count; public : CStringEx() { m_dot_count = 0; } CStringEx( const CString& s) { m_str = s; } virtual ~CStringEx() { } public : bool isFloat() { if (m_str.IsEmpty()) return false ; m_dot_count = 0; int start_pos = 0; char c = m_str.GetAt(0); if (c== '+' || c== '-' ) start_pos = 1; int length = m_str.GetLength(); for ( int i=start_pos; i<length; i++) { c = m_str.GetAt(i); if (c== '.' ) { m_dot_count++; continue ; } if (c<0x30 || c>0x39) return false ; } if (m_dot_count>1) // ..23. return false ; if (length==1 && (start_pos==1 || m_dot_count==1)) // . or + return false ; if (length==2 && start_pos==1 && m_dot_count==1) // +. return false ; return true ; } bool isInterger() { if (m_str.IsEmpty()) return false ; int start_pos = 0; char c = m_str.GetAt(0); if (c== '+' || c== '-' ) start_pos = 1; int length = m_str.GetLength(); for ( int i=start_pos; i<length; i++) { c = m_str.GetAt(i); if (c<0x30 || c>0x39) return false ; } if (length==1 && start_pos==1) // + return false ; return true ; } bool isValidFloat( double minVal, double maxVal, bool isHasMin= true , bool isHasMax= true ) { if (maxVal<minVal) { double a = minVal; minVal = maxVal; maxVal = a; } if (!isFloat()) return false ; double d = atof ( LPCTSTR (m_str)); if (isHasMin && d<minVal) return false ; if (isHasMax && d>maxVal) return false ; return true ; } bool isValidInterger( int minVal, int maxVal, bool isHasMin= true , bool isHasMax= true ) { if (maxVal<minVal) { int a = minVal; minVal = maxVal; maxVal = a; } if (!isInterger()) return false ; int d = atoi ( LPCTSTR (m_str)); if (isHasMin && d<minVal) return false ; if (isHasMax && d>maxVal) return false ; return true ; } }; // 全局函数 class CStringValid { public : static bool CStringValid::strIsFloat( const CString& s) { CStringEx sx = s; return sx.isFloat(); } static bool CStringValid::strIsInterger( const CString& s) { CStringEx sx = s; return sx.isInterger(); } static bool CStringValid::strIsFloatValid( const CString& s, double minVal, double maxVal, bool isHasMin= true , bool isHasMax= true ) { CStringEx sx = s; return sx.isValidFloat(minVal, maxVal, isHasMin, isHasMax); } static bool CStringValid::strIsIntergerValid( const CString& s, int minVal, int maxVal, bool isHasMin= true , bool isHasMax= true ) { CStringEx sx = s; return sx.isValidInterger(minVal, maxVal, isHasMin, isHasMax); } }; #endif |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)