《剑指offer》第二十题(表示数值的字符串)
2024-08-27 09:54:24
// 面试题20:表示数值的字符串
// 题目:请实现一个函数用来判断字符串是否表示数值(包括整数和小数)。例如,
// 字符串“+100”、“5e2”、“-123”、“3.1416”及“-1E-16”都表示数值,但“12e”、
// “1a3.14”、“1.2.3”、“+-5”及“12e+5.4”都不是 #include <iostream> bool scanUnsignedInteger(const char** str);
bool scanInteger(const char** str); // 数字的格式可以用A[.[B]][e|EC]或者.B[e|EC]表示,其中A和C都是
// 整数(可以有正负号,也可以没有),而B是一个无符号整数
// 当B存在时候,A可以不存在
bool isNumeric(const char* str)
{
if (str == nullptr)
return false; bool numeric = scanInteger(&str); // 如果出现'.',接下来是数字的小数部分
if (*str == '.')
{
++str; // 下面一行代码用||的原因:
// 1. 小数可以没有整数部分,例如.123等于0.123;
// 2. 小数点后面可以没有数字,例如233.等于233.0;
// 3. 当然小数点前面和后面可以有数字,例如233.666
numeric = scanUnsignedInteger(&str) || numeric;
} // 如果出现'e'或者'E',接下来跟着的是数字的指数部分
if (*str == 'e' || *str == 'E')
{
++str; // 下面一行代码用&&的原因:
// 1. 当e或E前面没有数字时,整个字符串不能表示数字,例如.e1、e1;
// 2. 当e或E后面没有数字时,整个字符串不能表示数字,例如12e
numeric = numeric && scanInteger(&str);
} // 下面一行代码用&&的原因:
// 当e或E后面没有整数时,整个字符串不能表示数字,例如12e + 5.4
return numeric && *str == '\0';
} bool scanUnsignedInteger(const char** str)
{
const char* before = *str;
while (**str != '\0' && **str >= '' && **str <= '')
++(*str); // 当str中存在若干0-9的数字时,返回true
return *str > before;
} // 整数的格式可以用[+|-]B表示, 其中B为无符号整数
bool scanInteger(const char** str)
{
if (**str == '+' || **str == '-')
++(*str);
return scanUnsignedInteger(str);
} // ====================测试代码====================
void Test(const char* testName, const char* str, bool expected)
{
if (testName != nullptr)
printf("%s begins: ", testName); if (isNumeric(str) == expected)
printf("Passed.\n");
else
printf("FAILED.\n");
} int main(int argc, char* argv[])
{
Test("Test1", "", true);
Test("Test2", "123.45e+6", true);
Test("Test3", "+500", true);
Test("Test4", "5e2", true);
Test("Test5", "3.1416", true);
Test("Test6", "600.", true);
Test("Test7", "-.123", true);
Test("Test8", "-1E-16", true);
Test("Test9", "1.79769313486232E+308", true); printf("\n\n"); Test("Test10", "12e", false);
Test("Test11", "1a3.14", false);
Test("Test12", "1+23", false);
Test("Test13", "1.2.3", false);
Test("Test14", "+-5", false);
Test("Test15", "12e+5.4", false);
Test("Test16", ".", false);
Test("Test17", ".e1", false);
Test("Test18", "e1", false);
Test("Test19", "+.", false);
Test("Test20", "", false);
Test("Test21", nullptr, false);
system("pause");
return ;
}
最新文章
- MySQL 数据库主从复制架构
- Android中<;meta-data>;的使用
- 使用druid连接池的超时回收机制排查连接泄露问题
- 洛谷P1984 SDOI2008烧水问题
- Linux下 执行程序
- Lantern免费使用教程【转】
- pxe网络安装操作系统 原理与详细过程
- HDU 4513 哥几个系列故事——形成完善II manacher求最长回文
- Android AES加密算法,现在实际上
- Python 多线程进程高级指南(二)
- P2P技术概要
- 全文检索-Elasticsearch (四) elasticsearch.net 客户端
- SQL Server中将多行数据拼接为一行数据(一个字符串)
- Intel的CPU漏洞:Spectre
- Shell脚本备份数据库(多库)
- [Objective-C] Copy 和 MutableCopy
- c函数指针
- 两种 AuthorizationSchemes 在 ASP.NET Core 2
- js间隔一段时间打印数据库中的值
- Typescript 学习笔记四:回忆ES5 中的类
热门文章
- jquery 的each函数
- mysql外键使用和事物使用
- outlook还原初始设置
- Windows10 64位下安装TensorFlow谷歌人工智能系统已官方原生支持
- 什么是公网IP、内网IP和NAT转换?
- [分享] 采用opencv_cascadetrain进行训练的步骤及注意事项 [复制链接]
- Eclipse启动Tomcat时,45秒超时解决方式
- bzoj 1179 [APIO 2009]Atm(APIO水题) - Tarjan - spfa
- sql逻辑查询语句的执行顺序
- ExtJS使用入门