剑指20:表示数值的字符串

xiaoxiao2021-02-28  11

输入一个字符串,判断是否是数值表示形式。

A . B e C

其中AC都可以是有符号数 B是无符号数 且前后存在ABC是否必须存在的问题

mark const char ** 的传参

class Solution { public: //有符号数识别处理 bool scanInt(const char** str){ //传入为二级指针,便于对一级指针中的指针进行修改 if(**str == '+'||**str =='-') (*str)++; return scanUnsignedInt(str); } //无符号数识别处理 bool scanUnsignedInt(const char** str){ const char* before = *str; while(**str!='\0'&& **str >='0' && **str <='9') (*str)++; return *str > before; //根据地址偏移看是否存在无符号数 } bool isNumeric(const char* str) //mark mistake 没有const 出错 { if(str == nullptr) return false; bool numberic = scanInt(&str); if(*str == '.') { str++; numberic = scanUnsignedInt(&str) || numberic; //mark mistake 顺序很重要 } if(*str =='e'||*str == 'E'){ str++; numberic = scanInt(&str) && numberic; //mistake 顺序顺序 } return numberic && *str=='\0'; //都合理且到达尾部 成立 其余情况都不是字符串 } };
转载请注明原文地址: https://www.6miu.com/read-1900280.html

最新回复(0)