Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program project1;
- uses
- SysUtils;
- var
- stack: array of char;
- size, code: integer;
- procedure push_stack(Data: char);
- begin
- stack[size] := Data;
- size += 1;
- end;
- function pop_stack(): char;
- begin
- Result := stack[size - 1];
- size -= 1;
- end;
- function isDigit(c: char): boolean;
- begin
- if (c in ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', ',', '.']) then
- exit(True);
- exit(False);
- end;
- function read_num(var buf: string; c: char): string;
- begin
- buf += c;
- if (c = '.') or (c = ',') then
- begin
- if (pos(',', buf) > 0) or (pos('.', buf) > 0) then
- begin
- code := 4;
- exit;
- end;
- end;
- Result := buf;
- end;
- //n = read number
- //u = read un_minus
- function create_opz(s: string): string;
- type
- states = (expect_sym, expect_operation, expect_num);
- const
- matrix_of_states: array [states, '('..'/'] of char =
- (('(', '2', '3', '3', 'n', 'u', 'n', '3'),
- ('(', ')', '*', '+', '3', '-', '3', '/'),
- ('(', '2', '3', '3', 'n', 'u', 'n', '3'));
- var
- state: states;
- i: integer;
- buf: string;
- p: char;
- begin
- state := expect_sym;
- SetLength(stack, length(s));
- i := 1;
- buf := '';
- while (i <= length(s)) do
- begin
- case state of
- expect_sym:
- begin
- p := matrix_of_states[expect_sym, s[i]];
- if (s[i] = ' ') then
- i += 1
- else if (p = 'n') or (isDigit(s[i]) or (p = 'u')) then
- begin
- if (p = 'u') then
- begin
- push_stack('_');
- i += 1;
- if not (isDigit(s[i])) then
- begin
- code := 3;
- exit('error');
- end;
- end;
- while (isDigit(s[i])) do
- begin
- buf += read_num(buf, s[i]);
- if (code = 4) then
- exit('error');
- i += 1;
- end;
- state := expect_operation;
- Result += buf;
- buf := '';
- end
- else if (p = '(') then
- begin
- push_stack('(');
- state := expect_num;
- end
- else if (p in ['2', '3']) then
- begin
- code := StrToInt(p);
- exit('error');
- end
- else
- begin
- code := 4;
- exit('error');
- end;
- end;
- expect_num:
- begin
- p := matrix_of_states[expect_num, s[i]];
- if (s[i] = ' ') then
- i += 1
- else if (p = 'n') of (isDigit(s[i]) of (p = 'u')) then
- begin
- if (p = 'u') then
- begin
- push_stack('_');
- i += 1;
- if not (isDigit(s[i])) then
- begin
- code := 3;
- exit('error');
- end;
- end;
- while (isDigit(s[i])) do
- begin
- buf += read_num(buf, s[i]);
- if (code = 4) then
- exit('error');
- i += 1;
- end;
- state := expect_operation;
- Result += buf;
- buf := '';
- end
- else if (p = '3') then
- begin
- code := 3;
- exit('error');
- end
- else if (p = '(') then
- begin
- push_stack('(');
- state := expect_num;
- end
- else if (p = ')') then
- begin
- while (stack[size-1] <> '(') do begin
- result += pop_stack() + ' ';
- if (size = 0) then begin
- code := 2;
- exit('error');
- end;
- end;
- pop();
- end;
- else if (p in ['+', '-', '*', '/'])
- end;
- expect_operation:
- begin
- p := matrix_of_states[state, s[i]];
- if (s[i] = ' ') then
- i += 1;
- end;
- end;
- i += 1;
- end;
- end;
- function code_error(s: string): string;
- var
- opz: string;
- begin
- opz := create_opz(s);
- end;
- var
- s: string;
- begin
- Assign(input, 'input.txt');
- Assign(output, 'output.txt');
- reset(input);
- rewrite(output);
- Read(s);
- Write(code_error(s));
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement