Advertisement
regergr

Untitled

Jan 24th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.13 KB | None | 0 0
  1. program project1;
  2.  
  3. uses
  4. SysUtils;
  5.  
  6. var
  7. stack: array of char;
  8. size, code: integer;
  9.  
  10. procedure push_stack(Data: char);
  11. begin
  12. stack[size] := Data;
  13. size += 1;
  14. end;
  15.  
  16. function pop_stack(): char;
  17. begin
  18. Result := stack[size - 1];
  19. size -= 1;
  20. end;
  21.  
  22. function isDigit(c: char): boolean;
  23. begin
  24. if (c in ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', ',', '.']) then
  25. exit(True);
  26. exit(False);
  27. end;
  28.  
  29. function read_num(var buf: string; c: char): string;
  30.  
  31. begin
  32. buf += c;
  33. if (c = '.') or (c = ',') then
  34. begin
  35. if (pos(',', buf) > 0) or (pos('.', buf) > 0) then
  36. begin
  37. code := 4;
  38. exit;
  39. end;
  40. end;
  41. Result := buf;
  42. end;
  43. //n = read number
  44. //u = read un_minus
  45. function create_opz(s: string): string;
  46. type
  47. states = (expect_sym, expect_operation, expect_num);
  48. const
  49. matrix_of_states: array [states, '('..'/'] of char =
  50. (('(', '2', '3', '3', 'n', 'u', 'n', '3'),
  51. ('(', ')', '*', '+', '3', '-', '3', '/'),
  52. ('(', '2', '3', '3', 'n', 'u', 'n', '3'));
  53. var
  54. state: states;
  55. i: integer;
  56. buf: string;
  57. p: char;
  58. begin
  59. state := expect_sym;
  60. SetLength(stack, length(s));
  61. i := 1;
  62. buf := '';
  63. while (i <= length(s)) do
  64. begin
  65. case state of
  66. expect_sym:
  67. begin
  68. p := matrix_of_states[expect_sym, s[i]];
  69. if (s[i] = ' ') then
  70. i += 1
  71.  
  72. else if (p = 'n') or (isDigit(s[i]) or (p = 'u')) then
  73. begin
  74. if (p = 'u') then
  75. begin
  76. push_stack('_');
  77. i += 1;
  78. if not (isDigit(s[i])) then
  79. begin
  80. code := 3;
  81. exit('error');
  82. end;
  83. end;
  84. while (isDigit(s[i])) do
  85. begin
  86. buf += read_num(buf, s[i]);
  87. if (code = 4) then
  88. exit('error');
  89. i += 1;
  90. end;
  91. state := expect_operation;
  92. Result += buf;
  93. buf := '';
  94. end
  95. else if (p = '(') then
  96. begin
  97. push_stack('(');
  98. state := expect_num;
  99. end
  100. else if (p in ['2', '3']) then
  101. begin
  102. code := StrToInt(p);
  103. exit('error');
  104. end
  105. else
  106. begin
  107. code := 4;
  108. exit('error');
  109. end;
  110. end;
  111.  
  112.  
  113. expect_num:
  114. begin
  115. p := matrix_of_states[expect_num, s[i]];
  116. if (s[i] = ' ') then
  117. i += 1
  118. else if (p = 'n') of (isDigit(s[i]) of (p = 'u')) then
  119. begin
  120. if (p = 'u') then
  121. begin
  122. push_stack('_');
  123. i += 1;
  124. if not (isDigit(s[i])) then
  125. begin
  126. code := 3;
  127. exit('error');
  128. end;
  129. end;
  130. while (isDigit(s[i])) do
  131. begin
  132. buf += read_num(buf, s[i]);
  133. if (code = 4) then
  134. exit('error');
  135. i += 1;
  136. end;
  137. state := expect_operation;
  138. Result += buf;
  139. buf := '';
  140. end
  141. else if (p = '3') then
  142. begin
  143. code := 3;
  144. exit('error');
  145. end
  146. else if (p = '(') then
  147. begin
  148. push_stack('(');
  149. state := expect_num;
  150. end
  151. else if (p = ')') then
  152. begin
  153. while (stack[size-1] <> '(') do begin
  154. result += pop_stack() + ' ';
  155. if (size = 0) then begin
  156. code := 2;
  157. exit('error');
  158. end;
  159. end;
  160. pop();
  161. end;
  162. else if (p in ['+', '-', '*', '/'])
  163. end;
  164.  
  165.  
  166. expect_operation:
  167. begin
  168. p := matrix_of_states[state, s[i]];
  169. if (s[i] = ' ') then
  170. i += 1;
  171. end;
  172. end;
  173. i += 1;
  174. end;
  175. end;
  176.  
  177. function code_error(s: string): string;
  178. var
  179. opz: string;
  180. begin
  181. opz := create_opz(s);
  182. end;
  183.  
  184. var
  185. s: string;
  186. begin
  187.  
  188. Assign(input, 'input.txt');
  189. Assign(output, 'output.txt');
  190. reset(input);
  191. rewrite(output);
  192. Read(s);
  193. Write(code_error(s));
  194.  
  195. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement