本文出自:blog.csdn.net/svitter
依据上述转换图,写出直接转向代码:
//============================================================================
// Name : Comlier.cpp
// Author : vit
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include <string.h>
using namespace std;
bool confirm;
int status;
int no;
bool JudgeNum(char &a)
{
if(a >= '0' && a <= '9')
return true;
else
return false;
}
void analysis(string &a)
{
if(status == 0)
{
if(a[no] == '.')
{
status = 6;
no ++;
analysis(a);
}
else if(JudgeNum(a[no]))
{
status = 1;
no ++;
analysis(a);
}
else
{
confirm = false;
// cout << status;
return;
}
}
else if(status == 1)
{
if(JudgeNum(a[no]) || a[no] == '.')
{
status = 2;
no++;
analysis(a);
}
else if(a[no] == 'E'|| a[no] == 'e')
{
status = 5;
no++;
analysis(a);
}
else
{
confirm = false;
// cout << status;
return ;
}
}
else if(status == 2)
{
if(JudgeNum(a[no]))
{
status = 2;
no++;
analysis(a);
}
else if(a[no] == 'E'|| a[no] == 'e')
{
status = 3;
no++;
analysis(a);
}
else
{
confirm = true;
// cout << status;
return;
}
}
else if(status == 3)
{
if(a[no] == '+' || a[no] == '-')
{
status = 4;
no++;
analysis(a);
}
else
{
confirm = false;
// cout << status;
return;
}
}
else if(status == 4)
{
if(JudgeNum(a[no]))
{
status = 5;
no++;
analysis(a);
}
else
{
confirm = false;
// cout << status;
return;
}
}
else if(status == 5)
{
if(JudgeNum(a[no]))
{
status = 5;
no++;
analysis(a);
}
else
{
confirm = true;
// cout << status;
return;
}
}
else if(status == 6)
{
if(JudgeNum(a[no]))
{
status = 2;
no++;
analysis(a);
}
else
{
confirm = false;
// cout << status;
return;
}
}
else
cout << "status error." << endl;
}
int main()
{
string a;
while(cin >> a)
{
confirm = false;
status = 0;
no = 0;
analysis(a);
if(confirm)
cout << "yes" << endl;
else
cout << "no" << endl;
}
}