#include<iostream>
#include<ctype.h>
#include<math.h>
#include<string>
using namespace std;
#define LETTER 0
#define DIGIT 1
#define POINT 2
#define OTHER 3
#define POWER 4
#define PLUS 5
#define MINUS 6
#define EndState -1
//12.34567e-123
int w;//1234567
int p;//123
int n;//5
int e;//-1
int d;//记录数字
double result;//记录最终结果
char* all[] = {
"12","34.567","89.",".345",".","12E34","12e34","12E+34",
"12e+34","12e-34","12.3E4","12.3e4","12.3E+4","12.3e+4",
"12.3E-4","12.3e-4",".38E4",".3e45",".38E+4",".3e+45",
".38E-4",".3e-45","3.E45","38.e4","3.E+45","38.e+4",
"3.E-45","38.e-4"
};
int isNumber(char *w);//判断字符串w是不是无符号数
int getChar(int c);//判断当前字符是数字,点,Ee,+,-
void excute(int& CurrentState,int ch);//根据当前状态和当前字符类型来进入下一个状态
int main(){
int i = -1;
for(;++i<sizeof(all)/sizeof(char*);){
cout<<all[i]<<" : ";
if(isNumber(all[i])){
cout<<"YES ";
cout<<result;
}
else{
cout<<"NO";
}
cout<<endl;
}
return 0;
}
//判断字符串w是不是无符号数
int isNumber(char *w){
string s(w);
int i = -1;
int ch;
int currentState = 0;
::w = 0;
p = 0;//123
n = 0;//5
e = 1;//-1
result = 0.0;
for(;++i<s.size();){
ch = getChar(s[i]);
excute(currentState,ch);
if(currentState == EndState){
return 0;
}
}
switch(currentState){
case 1:
case 2:
case 6:
result = ::w * pow(10,e*p-n);
return 1;
default:return 0;
};
}
/**
* 1. c是数字,返回1
* 2. c是点,返回2
* 3. c是E,返回4
* 4. c是+号,返回5
* 5. c是-号,返回6
*/
//判断当前字符是数字,点,Ee,+,-
int getChar(int c){
switch(c){
case '.':return POINT;
case 'E':
case 'e':return POWER;
case '+':return PLUS;
case '-':return MINUS;
default:
if(isdigit(c)){
d = c - 48;
return DIGIT;
}
break;
}
return OTHER;
}
/**
* 状态有
* -1: EndState
* 0 :
* 读取 数字 -> 1
* 读取 点 -> 3
*
* 1 :
* 读取 数字 -> 1
* 读取 '.' -> 2
* 读取 'E' -> 4
* 2 :
* 读取 数字 -> 2
* 读取 E -> 4
* 3:
* 读取 数字 -> 2
* 4:
* 读取 +/- -> 5
* 读取 数字 -> 6
* 5:
* 读取 数字 -> 6
* 6:
* 读取 数字 -> 6
*/
//根据当前状态和当前字符类型来进入下一个状态
void excute(int& currentState,int ch){
switch(currentState){
case 0:
switch(ch){
case DIGIT:
w = w * 10 + d;
currentState = 1;
break;
case POINT:
currentState = 3;
break;
default:
currentState = EndState;
break;
};
break;
case 1:
switch(ch){
case DIGIT:
w = w * 10 + d;
currentState = 1;
break;
case POINT:
currentState = 2;
break;
case POWER:
currentState = 4;
break;
default:
currentState = EndState;
break;
};
break;
case 2:
switch(ch){
case DIGIT:
w = w * 10 + d;
++n;
currentState = 2;
break;
case POWER:
currentState = 4;
break;
default:
currentState = EndState;
break;
};
break;
case 3:
switch(ch){
case DIGIT:
w = w * 10 + d;
++n;
currentState = 2;
break;
default:
currentState = EndState;
break;
};
break;
case 4:
switch(ch){
case DIGIT:
p = p * 10 + d;
currentState = 6;
break;
case PLUS:
currentState = 5;
break;
case MINUS:
e = -1;
currentState = 5;
break;
default:
currentState = EndState;
break;
};
break;
case 5:
switch(ch){
case DIGIT:
p = p * 10 + d;
currentState = 6;
break;
default:
currentState = EndState;
break;
};
break;
case 6:
switch(ch){
case DIGIT:
p = p * 10 + d;
currentState = 6;
break;
default:
currentState = EndState;
break;
};
break;
};
}
第一列 待判定的数字字符串
第二列 是否为无符号数
第三列 输出double FCON = w * pow( 10 , e * p – n); 即将字符串转换为double