using System;
class Program
{
static void Main(string[] args)
{
string[] data = new string[] {
"a",
"1",
"0",
"+0",
"-0",
"01",
"+12345",
"-12345",
"1.2",
"1.2.3",
"12-3",
"1a",
"+1",
"-0.123",
"1.2e2",
"1.2E2",
"1.2e",
"1.2e+3",
"1.2e-3",
"1.2e-3.5",
".5",
".5e2",
"5.e2",
};
Checker ic = new IntChecker();
foreach (string str in data)
{
ic.Reset();
Console.Write(str + " : ");
foreach (char ch in str)
{
if (ic.IsError())
{
break;
}
ic.Next(ch);
}
if (ic.IsAcceptable())
{
Console.WriteLine("int({0})", int.Parse(str));
}
else
{
Console.WriteLine("NaN({0})", str);
}
}
}
}
interface Checker
{
void Reset();
void Next(char ch);
bool IsAcceptable();
bool IsError();
}
class IntChecker : Checker
{
enum IntParsingState
{
Error,
State0,
State1,
State2,
State3,
}
IntParsingState _state;
public IntChecker()
{
Reset();
}
public void Reset()
{
_state = IntParsingState.State0;
}
public void Next(char ch)
{
switch (_state)
{
case IntParsingState.State0:
if (ch == '-' ||
ch == '+')
{
_state = IntParsingState.State1;
}
else if (ch == '1' ||
ch == '2' ||
ch == '3' ||
ch == '4' ||
ch == '5' ||
ch == '6' ||
ch == '7' ||
ch == '8' ||
ch == '9')
{
_state = IntParsingState.State2;
}
else if (ch == '0')
{
_state = IntParsingState.State3;
}
else
{
_state = IntParsingState.Error;
}
break;
case IntParsingState.State1:
if (ch == '1' ||
ch == '2' ||
ch == '3' ||
ch == '4' ||
ch == '5' ||
ch == '6' ||
ch == '7' ||
ch == '8' ||
ch == '9')
{
_state = IntParsingState.State2;
}
else if (ch == '0')
{
_state = IntParsingState.State3;
}
else
{
_state = IntParsingState.Error;
}
break;
case IntParsingState.State2:
if (ch == '0' ||
ch == '1' ||
ch == '2' ||
ch == '3' ||
ch == '4' ||
ch == '5' ||
ch == '6' ||
ch == '7' ||
ch == '8' ||
ch == '9')
{
_state = IntParsingState.State2;
}
else
{
_state = IntParsingState.Error;
}
break;
case IntParsingState.State3:
case IntParsingState.Error:
_state = IntParsingState.Error;
break;
}
}
public bool IsAcceptable()
{
return
_state == IntParsingState.State2 ||
_state == IntParsingState.State3;
}
public bool IsError()
{
return _state == IntParsingState.Error;
}
}
dXNpbmcgU3lzdGVtOwoKY2xhc3MgUHJvZ3JhbQp7CiAgc3RhdGljIHZvaWQgTWFpbihzdHJpbmdbXSBhcmdzKQogIHsKICAgIHN0cmluZ1tdIGRhdGEgPSBuZXcgc3RyaW5nW10gewogICAgICAiYSIsCiAgICAgICIxIiwKICAgICAgIjAiLAogICAgICAiKzAiLAogICAgICAiLTAiLAogICAgICAiMDEiLAogICAgICAiKzEyMzQ1IiwKICAgICAgIi0xMjM0NSIsCiAgICAgICIxLjIiLAogICAgICAiMS4yLjMiLAogICAgICAiMTItMyIsCiAgICAgICIxYSIsCiAgICAgICIrMSIsCiAgICAgICItMC4xMjMiLAogICAgICAiMS4yZTIiLAogICAgICAiMS4yRTIiLAogICAgICAiMS4yZSIsCiAgICAgICIxLjJlKzMiLAogICAgICAiMS4yZS0zIiwKICAgICAgIjEuMmUtMy41IiwKICAgICAgIi41IiwKICAgICAgIi41ZTIiLAogICAgICAiNS5lMiIsCiAgICB9OwogICAgCiAgICBDaGVja2VyIGljID0gbmV3IEludENoZWNrZXIoKTsKICAgIGZvcmVhY2ggKHN0cmluZyBzdHIgaW4gZGF0YSkKICAgIHsKICAgICAgaWMuUmVzZXQoKTsKICAgICAgQ29uc29sZS5Xcml0ZShzdHIgKyAiIDogIik7CiAgICAgIGZvcmVhY2ggKGNoYXIgY2ggaW4gc3RyKQogICAgICB7CiAgICAgICAgaWYgKGljLklzRXJyb3IoKSkKICAgICAgICB7CiAgICAgICAgICBicmVhazsKICAgICAgICB9CgogICAgICAgIGljLk5leHQoY2gpOwogICAgICB9CiAgICAgIAogICAgICBpZiAoaWMuSXNBY2NlcHRhYmxlKCkpCiAgICAgIHsKICAgICAgICBDb25zb2xlLldyaXRlTGluZSgiaW50KHswfSkiLCBpbnQuUGFyc2Uoc3RyKSk7CiAgICAgIH0KICAgICAgZWxzZQogICAgICB7CiAgICAgICAgQ29uc29sZS5Xcml0ZUxpbmUoIk5hTih7MH0pIiwgc3RyKTsKICAgICAgfQogICAgfQogIH0KfQoKaW50ZXJmYWNlIENoZWNrZXIKewogIHZvaWQgUmVzZXQoKTsKICB2b2lkIE5leHQoY2hhciBjaCk7CiAgYm9vbCBJc0FjY2VwdGFibGUoKTsKICBib29sIElzRXJyb3IoKTsKfQoKY2xhc3MgSW50Q2hlY2tlciA6IENoZWNrZXIKewogIGVudW0gSW50UGFyc2luZ1N0YXRlCiAgewogICAgRXJyb3IsCiAgICBTdGF0ZTAsCiAgICBTdGF0ZTEsCiAgICBTdGF0ZTIsCiAgICBTdGF0ZTMsCiAgfQoKICBJbnRQYXJzaW5nU3RhdGUgX3N0YXRlOwoKICBwdWJsaWMgSW50Q2hlY2tlcigpCiAgewogICAgUmVzZXQoKTsKICB9CgogIHB1YmxpYyB2b2lkIFJlc2V0KCkKICB7CiAgICBfc3RhdGUgPSBJbnRQYXJzaW5nU3RhdGUuU3RhdGUwOwogIH0KCiAgcHVibGljIHZvaWQgTmV4dChjaGFyIGNoKQogIHsKICAgIHN3aXRjaCAoX3N0YXRlKQogICAgewogICAgICBjYXNlIEludFBhcnNpbmdTdGF0ZS5TdGF0ZTA6CiAgICAgICAgaWYgKGNoID09ICctJyB8fAogICAgICAgICAgY2ggPT0gJysnKQogICAgICAgIHsKICAgICAgICAgIF9zdGF0ZSA9IEludFBhcnNpbmdTdGF0ZS5TdGF0ZTE7CiAgICAgICAgfQogICAgICAgIGVsc2UgaWYgKGNoID09ICcxJyB8fAogICAgICAgICAgY2ggPT0gJzInIHx8CiAgICAgICAgICBjaCA9PSAnMycgfHwKICAgICAgICAgIGNoID09ICc0JyB8fAogICAgICAgICAgY2ggPT0gJzUnIHx8CiAgICAgICAgICBjaCA9PSAnNicgfHwKICAgICAgICAgIGNoID09ICc3JyB8fAogICAgICAgICAgY2ggPT0gJzgnIHx8CiAgICAgICAgICBjaCA9PSAnOScpCiAgICAgICAgewogICAgICAgICAgX3N0YXRlID0gSW50UGFyc2luZ1N0YXRlLlN0YXRlMjsKICAgICAgICB9CiAgICAgICAgZWxzZSBpZiAoY2ggPT0gJzAnKQogICAgICAgIHsKICAgICAgICAgIF9zdGF0ZSA9IEludFBhcnNpbmdTdGF0ZS5TdGF0ZTM7CiAgICAgICAgfQogICAgICAgIGVsc2UKICAgICAgICB7CiAgICAgICAgICBfc3RhdGUgPSBJbnRQYXJzaW5nU3RhdGUuRXJyb3I7CiAgICAgICAgfQogICAgICAgIGJyZWFrOwogICAgICBjYXNlIEludFBhcnNpbmdTdGF0ZS5TdGF0ZTE6CiAgICAgICAgaWYgKGNoID09ICcxJyB8fAogICAgICAgICAgY2ggPT0gJzInIHx8CiAgICAgICAgICBjaCA9PSAnMycgfHwKICAgICAgICAgIGNoID09ICc0JyB8fAogICAgICAgICAgY2ggPT0gJzUnIHx8CiAgICAgICAgICBjaCA9PSAnNicgfHwKICAgICAgICAgIGNoID09ICc3JyB8fAogICAgICAgICAgY2ggPT0gJzgnIHx8CiAgICAgICAgICBjaCA9PSAnOScpCiAgICAgICAgewogICAgICAgICAgX3N0YXRlID0gSW50UGFyc2luZ1N0YXRlLlN0YXRlMjsKICAgICAgICB9CiAgICAgICAgZWxzZSBpZiAoY2ggPT0gJzAnKQogICAgICAgIHsKICAgICAgICAgIF9zdGF0ZSA9IEludFBhcnNpbmdTdGF0ZS5TdGF0ZTM7CiAgICAgICAgfQogICAgICAgIGVsc2UKICAgICAgICB7CiAgICAgICAgICBfc3RhdGUgPSBJbnRQYXJzaW5nU3RhdGUuRXJyb3I7CiAgICAgICAgfQogICAgICAgIGJyZWFrOwogICAgICBjYXNlIEludFBhcnNpbmdTdGF0ZS5TdGF0ZTI6CiAgICAgICAgaWYgKGNoID09ICcwJyB8fAogICAgICAgICAgY2ggPT0gJzEnIHx8CiAgICAgICAgICBjaCA9PSAnMicgfHwKICAgICAgICAgIGNoID09ICczJyB8fAogICAgICAgICAgY2ggPT0gJzQnIHx8CiAgICAgICAgICBjaCA9PSAnNScgfHwKICAgICAgICAgIGNoID09ICc2JyB8fAogICAgICAgICAgY2ggPT0gJzcnIHx8CiAgICAgICAgICBjaCA9PSAnOCcgfHwKICAgICAgICAgIGNoID09ICc5JykKICAgICAgICB7CiAgICAgICAgICBfc3RhdGUgPSBJbnRQYXJzaW5nU3RhdGUuU3RhdGUyOwogICAgICAgIH0KICAgICAgICBlbHNlCiAgICAgICAgewogICAgICAgICAgX3N0YXRlID0gSW50UGFyc2luZ1N0YXRlLkVycm9yOwogICAgICAgIH0KICAgICAgICBicmVhazsKICAgICAgY2FzZSBJbnRQYXJzaW5nU3RhdGUuU3RhdGUzOgogICAgICBjYXNlIEludFBhcnNpbmdTdGF0ZS5FcnJvcjoKICAgICAgICBfc3RhdGUgPSBJbnRQYXJzaW5nU3RhdGUuRXJyb3I7CiAgICAgICAgYnJlYWs7CiAgICB9CiAgfQoKICBwdWJsaWMgYm9vbCBJc0FjY2VwdGFibGUoKQogIHsKICAgIHJldHVybgogICAgICBfc3RhdGUgPT0gSW50UGFyc2luZ1N0YXRlLlN0YXRlMiB8fAogICAgICBfc3RhdGUgPT0gSW50UGFyc2luZ1N0YXRlLlN0YXRlMzsKICB9CgogIHB1YmxpYyBib29sIElzRXJyb3IoKQogIHsKICAgIHJldHVybiBfc3RhdGUgPT0gSW50UGFyc2luZ1N0YXRlLkVycm9yOwogIH0KfQo=