专栏主页:计算机专业基础知识总结(适用于期末复习考研刷题求职面试)系列文章https://blog.csdn.net/seeker1994/category_12585732.html
题目描述
请实现一个函数用来判断字符串是否表示数值(包括整数和小数)。例如,字符串"+100","5e2","-123","3.1416"和"-1E-16"都表示数值。 但是"12e","1a3.14","1.2.3","+-5"和"12e+4.3"都不是。
题解报告
编译原理状态机
# include <iostream>
# include <string>
using namespace std;
bool IsNum(char ch){
if(ch<'0'||ch>'9')
return false;
else
return true;
}
bool isNumeric(char* str){
int i = 0;
if(!(str[i] == '+' || str[i] == '-' || IsNum(str[i])) )
return false;
else{
i++;
while(IsNum(str[i]) && str[i] != '\0')
i++; //如果是数字一直往后找
if(str[i] == '\0'){
return true;
}
else if(str[i] == '.'){
i++;
if(str[i] == '\0')
return true;
else if(IsNum(str[i])){
while(str[++i]!='\0' && IsNum(str[i]));
if(str[i]=='e'||str[i]=='E'){
i++;
if(str[i]=='+' || str[i]=='-' || IsNum(str[i])){
while(str[++i]!='\0' && IsNum(str[i]));
if(str[i]=='\0')
return true;
else
return false;
}else
return false;
}else if(str[i]=='\0'){
return true;
}else{
return false;
}
}else{
return false;
}
}else if(str[i]=='e'||str[i]=='E'){
i++;
if(str[i]=='+' || str[i]=='-' || IsNum(str[i])){
while(str[++i]!='\0' && IsNum(str[i]));
if(str[i]=='\0') return true;
else return false;
}else return false;
}
else{
return false;
}
}
}
int main(){
char str[] = "1.25";
if(isNumeric(str)){
cout<<"YES"<<endl;
}else{
cout<<"NO"<<endl;
}
return 0;
}
优化后的代码,使用状态表:
# include <iostream>
# include <string>
using namespace std;
char arr[10] = "+-n.ne+-n";
int turn[10][9] = {
//+ - n . n e + - n
{1, 1, 1, 0, 0, 0, 0, 0, 0}, // # start
{0, 0, 1, 1, 0, 0, 0, 0, 0}, // +
{0, 0, 1, 1, 0, 0, 0, 0, 0}, // -
{0, 0, 1, 1, 0, 1, 0, 0, 0}, // n
{0, 0, 0, 0, 1, 0, 0, 0, 0}, // .
{0, 0, 0, 0, 1, 1, 0, 0, 0}, // n
{0, 0, 0, 0, 0, 0, 1, 1, 1}, // e
{0, 0, 0, 0, 0, 0, 0, 0, 1}, // +
{0, 0, 0, 0, 0, 0, 0, 0, 1}, // -
{0, 0, 0, 0, 0, 0, 0, 0, 1} // n
};
bool isNumeric(char* string) {
int cur = 0,i,j;
for(i = 0; string[i]; i++) {
for(j = 0; j < 9; j++) {
if(turn[cur][j]) {//状态转移
//状态转移
if(('0' <= string[i] && string[i] <= '9' && arr[j] == 'n') ||
(string[i] == 'E' && arr[j] == 'e') ||
string[i] == arr[j]) {
cur = j + 1;
break;
}
}
}
if(j == 9)
return false;
}
if(cur == 3 || cur == 4 || cur == 5 || cur == 9)
return true;
return false;
}
int main(){
char str[] = "1.25";
if(isNumeric(str)){
cout<<"YES"<<endl;
}else{
cout<<"NO"<<endl;
}
return 0;
}
#1正则
public boolean isNumeric(char[] str) {
String string = String.valueOf(str);
return string.matches("[\\+-]?[0-9]*(\\.[0-9]*)?([eE][\\+-]?[0-9]+)?");
}
//标记法
bool isNumeric(char* str) {
// 标记符号、小数点、e是否出现过
bool sign = false, decimal = false, hasE = false;
for (int i = 0; i < strlen(str); i++) {
if (str[i] == 'e' || str[i] == 'E') {
if (i == strlen(str)-1) return false; // e后面一定要接数字
if (hasE) return false; // 不能同时存在两个e
hasE = true;
} else if (str[i] == '+' || str[i] == '-') {
// 第二次出现+-符号,则必须紧接在e之后
if (sign && str[i-1] != 'e' && str[i-1] != 'E') return false;
// 第一次出现+-符号,且不是在字符串开头,则也必须紧接在e之后
if (!sign && i > 0 && str[i-1] != 'e' && str[i-1] != 'E') return false;
sign = true;
} else if (str[i] == '.') {
// e后面不能接小数点,小数点不能出现两次
if (hasE || decimal) return false;
decimal = true;
} else if (str[i] < '0' || str[i] > '9') //不合法字符
return false;
}
return true;
}