UVA_325_Identifying Legal Pascal Real Constants

#include<iostream>    
#include<sstream>    
#include<string>    
#include<vector>    
#include<list>    
#include<set>    
#include<map>    
#include<stack>    
#include<queue>    
#include<algorithm>  
#include<bitset>
#pragma warning(disable:4996)    
using std::cin;
using std::cout;
using std::endl;
using std::stringstream;
using std::string;
using std::vector;
using std::list;
using std::pair;
using std::set;
using std::multiset;
using std::map;
using std::multimap;
using std::stack;
using std::queue;
using std::priority_queue;
using std::bitset;
bool check(const string &str)
{
	int state = 0;//当前状态
	for (size_t i = 0; i < str.size(); i++)
	{
		switch (state)
		{
		case 0: 
		{
			if (str[i] == '+' || str[i] == '-' || isdigit(str[i]))
			{
				state = 1;
			}
			else
			{
				return false;
			}
		}break;
		case 1:
		{
			if (isdigit(str[i]))
			{
				;
			}
			else if (str[i] == '.')
			{
				state = 2;
			}
			else if (str[i] == 'E' || str[i] == 'e')
			{
				state = 4;
			}
			else
			{
				return false;
			}
		}break;
		case 2:
		{
			if (isdigit(str[i]))
			{
				state = 3;
			}
			else
			{
				return false;
			}
		}break;
		case 3:
		{
			if (isdigit(str[i]))
			{
				;
			}
			else if (str[i] == 'E' || str[i] == 'e')
			{
				state = 4;
			}
			else
			{
				return false;
			}
		}break;
		case 4:
		{
			if (str[i] == '+' || str[i] == '-' || isdigit(str[i]))
			{
				state = 5;
			}
			else
			{
				return false;
			}
		}break;
		case 5:
		{
			if (isdigit(str[i]))
			{
				;
			}
			else
			{
				return false;
			}
		}break;
		default:return false;
		}
	}
	if (state == 3 || state == 5)
	{
		return true;
	}
	return false;
}
int main()
{
	//freopen("input.txt", "r", stdin);    
	//freopen("output.txt", "w", stdout);
	string str;
	while (cin >> str)
	{
		if (str[0] == '*')
		{
			break;
		}
		cout << str;
		if (check(str))
		{
			cout << " is legal." << endl;
		}
		else
		{
			cout << " is illegal." << endl;
		}
	}
	return 0;
}

你可能感兴趣的:(uva,FSA)