#include<iostream>
class isnumeric
{
public:
isnumeric(const char* str);
bool scanInteger(const char** str);//需要将指针进行偏移
bool scanUnsignedInteger(const char** str);
};
//字符串数值模式:A[.[B]][e|EC]或者.B[e|EC]
isnumeric::isnumeric(const char* str){
if (str == nullptr)return;
bool numeric = scanInteger(&str);//判断是否有A部分
if (*str == '.')
{
++str;
//判断‘.’后面是否有整数
numeric = scanUnsignedInteger(&str) || numeric;
}
if (*str == 'e' || *str == 'E')
{
++str;
//判断‘e’‘E’后面是否有整数,且‘e’‘E’前后必须有数字
numeric = scanInteger(&str) && numeric;
}
if (*str == '\0' && numeric)//指针走向最后一个字符,且要符合规则
std::cout << "Yes";
else
std::cout << "No";
std::cout << std::endl;
}
bool isnumeric::scanInteger(const char** str)
{
while (**str == '+' || **str == '-')
++(*str);
return scanUnsignedInteger(str);
}
bool isnumeric::scanUnsignedInteger(const char** str)
{
const char* before = *str;
while (**str != '\0' && **str >= '0' && **str <= '9')
++(*str);
return *str > before;
}
int main(int argc, char* argv[])
{
const char* B = "5e30";
const char* V = "5e9.0";
isnumeric M(B);
isnumeric N(V);
std::cin.get();
return 0;
}
表示数值的字符串
最新推荐文章于 2021-12-21 07:00:05 发布