字符串是否为数字及有效性检查

在编写MFC窗体程序时,我们经常要判断用户输入的一个字符串是否合法有效,以此来增强程序的健壮性。

最近,在测试系统的对话框的输入时,发现存在大量这样的问题,甚至有一些特定变态的输入还导致系统异常退出。

为了解决这些问题,我编写下面这个头文件,来判断输入的字符串是否为数字,以及是否在有效范围之内。

希望以下代码对你有用!

 

#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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值