fork download
  1. using System;
  2.  
  3. class Program
  4. {
  5. static void Main(string[] args)
  6. {
  7. string[] data = new string[] {
  8. "a",
  9. "1",
  10. "0",
  11. "+0",
  12. "-0",
  13. "01",
  14. "+12345",
  15. "-12345",
  16. "1.2",
  17. "1.2.3",
  18. "12-3",
  19. "1a",
  20. "+1",
  21. "-0.123",
  22. "1.2e2",
  23. "1.2E2",
  24. "1.2e",
  25. "1.2e+3",
  26. "1.2e-3",
  27. "1.2e-3.5",
  28. ".5",
  29. ".5e2",
  30. "5.e2",
  31. };
  32.  
  33. Checker ic = new IntChecker();
  34. foreach (string str in data)
  35. {
  36. ic.Reset();
  37. Console.Write(str + " : ");
  38. foreach (char ch in str)
  39. {
  40. if (ic.IsError())
  41. {
  42. break;
  43. }
  44.  
  45. ic.Next(ch);
  46. }
  47.  
  48. if (ic.IsAcceptable())
  49. {
  50. Console.WriteLine("int({0})", int.Parse(str));
  51. }
  52. else
  53. {
  54. Console.WriteLine("NaN({0})", str);
  55. }
  56. }
  57. }
  58. }
  59.  
  60. interface Checker
  61. {
  62. void Reset();
  63. void Next(char ch);
  64. bool IsAcceptable();
  65. bool IsError();
  66. }
  67.  
  68. class IntChecker : Checker
  69. {
  70. enum IntParsingState
  71. {
  72. Error,
  73. State0,
  74. State1,
  75. State2,
  76. State3,
  77. }
  78.  
  79. IntParsingState _state;
  80.  
  81. public IntChecker()
  82. {
  83. Reset();
  84. }
  85.  
  86. public void Reset()
  87. {
  88. _state = IntParsingState.State0;
  89. }
  90.  
  91. public void Next(char ch)
  92. {
  93. switch (_state)
  94. {
  95. case IntParsingState.State0:
  96. if (ch == '-' ||
  97. ch == '+')
  98. {
  99. _state = IntParsingState.State1;
  100. }
  101. else if (ch == '1' ||
  102. ch == '2' ||
  103. ch == '3' ||
  104. ch == '4' ||
  105. ch == '5' ||
  106. ch == '6' ||
  107. ch == '7' ||
  108. ch == '8' ||
  109. ch == '9')
  110. {
  111. _state = IntParsingState.State2;
  112. }
  113. else if (ch == '0')
  114. {
  115. _state = IntParsingState.State3;
  116. }
  117. else
  118. {
  119. _state = IntParsingState.Error;
  120. }
  121. break;
  122. case IntParsingState.State1:
  123. if (ch == '1' ||
  124. ch == '2' ||
  125. ch == '3' ||
  126. ch == '4' ||
  127. ch == '5' ||
  128. ch == '6' ||
  129. ch == '7' ||
  130. ch == '8' ||
  131. ch == '9')
  132. {
  133. _state = IntParsingState.State2;
  134. }
  135. else if (ch == '0')
  136. {
  137. _state = IntParsingState.State3;
  138. }
  139. else
  140. {
  141. _state = IntParsingState.Error;
  142. }
  143. break;
  144. case IntParsingState.State2:
  145. if (ch == '0' ||
  146. ch == '1' ||
  147. ch == '2' ||
  148. ch == '3' ||
  149. ch == '4' ||
  150. ch == '5' ||
  151. ch == '6' ||
  152. ch == '7' ||
  153. ch == '8' ||
  154. ch == '9')
  155. {
  156. _state = IntParsingState.State2;
  157. }
  158. else
  159. {
  160. _state = IntParsingState.Error;
  161. }
  162. break;
  163. case IntParsingState.State3:
  164. case IntParsingState.Error:
  165. _state = IntParsingState.Error;
  166. break;
  167. }
  168. }
  169.  
  170. public bool IsAcceptable()
  171. {
  172. return
  173. _state == IntParsingState.State2 ||
  174. _state == IntParsingState.State3;
  175. }
  176.  
  177. public bool IsError()
  178. {
  179. return _state == IntParsingState.Error;
  180. }
  181. }
  182.  
Success #stdin #stdout 0.02s 131520KB
stdin
Standard input is empty
stdout
a : NaN(a)
1 : int(1)
0 : int(0)
+0 : int(0)
-0 : int(0)
01 : NaN(01)
+12345 : int(12345)
-12345 : int(-12345)
1.2 : NaN(1.2)
1.2.3 : NaN(1.2.3)
12-3 : NaN(12-3)
1a : NaN(1a)
+1 : int(1)
-0.123 : NaN(-0.123)
1.2e2 : NaN(1.2e2)
1.2E2 : NaN(1.2E2)
1.2e : NaN(1.2e)
1.2e+3 : NaN(1.2e+3)
1.2e-3 : NaN(1.2e-3)
1.2e-3.5 : NaN(1.2e-3.5)
.5 : NaN(.5)
.5e2 : NaN(.5e2)
5.e2 : NaN(5.e2)