有效数判定 Valid Number
来源:互联网 发布:怎样开淘宝网店卖衣服 编辑:程序博客网 时间:2024/05/18 03:58
问题:给出一个字符串,判断它是否是一个有效的数字。
什么叫有效的数字呢?
整数 ,小数 "2.5",正数 "+25",负数"-25",科学计数法"-2e1+0"。
特殊用例:
“.5”, “5.” 认为是有效数字。
无效用例:
“.", "e25", "25e", "2e5e5", "2.5.5", "2e2.5"
输入检查:左右两端出现空格是允许的,内部不能有空格。
方法1:依据上面所能想到的无效用例,处理零碎的细节。
方法2:将零碎的细节用有限状态自动机来表示。
方法3:偷懒直接用strtod函数。
方法1:处理零碎的细节。
class Solution {public: bool isNumber(const char *s) { if(s == NULL) return false; int ex, dot; ex = dot = 0; // only once while(*s == ' ') // space s++; if(*s == '+' || *s == '-') // sign s++; if(isNum(*s)) // first char is numeric s++; else if(*s == '.') // if first char is '.' { s++; dot = 1; if(isNum(*s)) // then second char has to be numeric s++; else return false; } else return false; while(*s != '\0') { if(*s == '.') { if(ex == 1 || dot == 1) return false; dot = 1; } else if(*s == 'e') { if(ex == 1) return false; ex = 1; if(*(s+1) == '+' || *(s+1) == '-') //sign for exponent s++; if(!isNum(*(s+1))) // numeric after 'e' return false; } else if(*s == ' ') // space has to be on the right side { break; } else if(!isNum(*s)) //other letter { return false; } s++; } while(*s == ' ') // space s++; if(*s == '\0') return true; else return false; } bool isNum(const char c) { return (c >= '0' && c <= '9'); // 0<=c<=9 }};
方法2:有限状态自动机。
{
-1, 0, 1, 3, 9, -1, //状态0:""
-1, -1,-1, 3, 9, -1, //状态1:"+"
-1, 8, -1, 4, -1, 5, //状态2:"2."
-1, 8, -1, 3, 2, 5, //状态3:"2"
-1, 8, -1, 4, -1, 5, //状态4:"2.3"
-1, -1, 6, 7, -1, -1, //状态5:"2.3e"
-1, -1, -1, 7, -1, -1, //状态6:"2.3e+"
-1, 8, -1, 7, -1, -1, //状态7:"2.3e+4"
-1, 8, -1, -1, -1, -1, //状态8:"2.3e+4 "
-1, -1, -1, 4, -1, -1 //状态9:"."
};
#include <iostream>#include <stdio.h>using namespace std;class Solution {public: enum InputType { INVALID, //0 SPACE, //1 SIGN, //2 DIGIT, //3 DOT, //4 EXPONENT, //5 }; bool isNumber(const char *s) { if(s == NULL) return false; const int transitionTable[][6] = { -1, 0, 1, 3, 9, -1, //状态0:"" -1, -1,-1, 3, 9, -1, //状态1:"+" -1, 8, -1, 4, -1, 5, //状态2:"2." -1, 8, -1, 3, 2, 5, //状态3:"2" -1, 8, -1, 4, -1, 5, //状态4:"2.3" -1, -1, 6, 7, -1, -1, //状态5:"2.3e" -1, -1, -1, 7, -1, -1, //状态6:"2.3e+" -1, 8, -1, 7, -1, -1, //状态7:"2.3e+4" -1, 8, -1, -1, -1, -1, //状态8:"2.3e+4 " -1, -1, -1, 4, -1, -1 //状态9:"." }; int state = 0; InputType input; while(*s != 0) { if(*s == ' ') input = SPACE; else if(*s == '+' || *s == '-') input = SIGN; else if(*s == '.') input = DOT; else if(*s == 'e' || *s == 'E') input = EXPONENT; else if(*s >= '0' && *s <= '9') input = DIGIT; else input = INVALID; state = transitionTable[state][input]; if(state == -1) return false; ++s; //cout<<state<<" "; } //cout<<endl; return (state == 2 || state == 3 || state == 4 || state == 7 || state == 8); }};int main(){ Solution s; char str[100]; bool r; while(scanf("%s", str) != EOF) { r = s.isNumber(str); cout<<"result :"<<r<<endl; } return 1;}
方法3:偷懒。
double strtod (const char* str, char** endptr);
将字符串形式的浮点数转化为double类型。
参数一,str为输入字符串。
参数二,二重指针,函数返回后,endptr会指向str中第一个浮点数之后的位置。
返回值,转化成的double浮点数。
class Solution {public: bool isNumber(const char *s) { char *left; strtod(s, &left); if(left == s) return false; while(*left != 0) { if(*left != ' ') return false; ++left; } return true; }};
- 有效数判定 Valid Number
- 有效Valid Number .
- [LeetCode]Valid Number有效数字
- [Lintcode]Valid Number有效数字
- 有效数独 Valid Sudoku
- 判定一个数独是否有效。
- Valid Number 判断一个数字是否有效
- 【LeetCode】- Valid Number(有效数字)
- leetCode 65.Valid Number (有效数字)
- Valid Sudoku 有效的数独
- LeetCode----36. Valid Sudoku(有效数读)
- leetcode 36. Valid Sudoku 数独有效
- LeetCode OJ 之 Valid Number (有效数字的判断)
- Valid Sudoku 检查数独是否有效 @LeetCode
- [leetcode]Valid Sudoku(判断有效数独 C语言实现)
- LeetCode 36 Valid Sudoku(有效数独)
- leetcode 36. Valid Sudoku-有效数独|哈希表
- valid-sudoku判断是否是有效数独
- pitch yaw roll是什么
- 对于hibernate的一级缓存和二级缓存理解
- 【图形打印】菱形
- 【Xamarin4月培训季开课啦】C#开发iOS、Android应用,快速上手!
- 【数据结构】递归求解迷宫问题
- 有效数判定 Valid Number
- C#实现透明WinForm控件
- android 多线程Thread,Runnable,Handler,AsyncTask等之间的关系
- 调用webservice,出现错误:Uncaught SoapFault exception: [HTTP] Error Fetching http headers
- 深入理解JavaScript系列(32):设计模式之观察者模式
- 我的博客开通啦!
- 矩阵的乘法
- In-Memory Database Cache 入门配置
- 生产者-消费者问题