编译原理
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
bool DFA(string s){//识别无符号数
int len = s.length();//串长
// 判断最后一位是否合法
if(s[len - 1] < '0' || s[len - 1] > '9') return false;
if(s[len -1] == 'e' || s[len- 1] == 'E') return false;
if(s[len- 1] == '.' || s[len - 1] == '+' || s[len - 1] == '-') return false;
int i = 0;
if(s[0] < '0' || s[0] > '9') return false;//判断首位是否为数字
for(; i < len; i ++){
if(s[i] <= '9' && s[i] >= '0'){
i ++;
}
else if(s[i] == '.'){//当前位为'.'
char c = s[i + 1];
if(c >= '0' || c <= '9') i ++;
else return false;
}
else if(s[i] == 'E' || s[i] == 'e'){//当前位为'E'或'e'
char c = s[i + 1];//预判下一位
if(c >= '0' || c <= '9') i ++;
else if (c == '+' || c == '-') i ++;
else return false;
}
else if(s[i] =='+' || s[i] == '-') i ++;//当前位为'+'或'-'
else return false;
}
if(i == len) return true;
}
int main(){
cout << "input '*' to stop ! " << endl;
while(1){
string str;
cin >> str;
if(str == "*") return 0;
if(DFA(str)) cout << "YES" << endl;
else cout << "NO" << endl;
}
return 0;
}