Программа бесконечно выполняется. Можешь исправить? Program obsch; type TypeNode =…
- User
Программа бесконечно выполняется. Можешь исправить? Program obsch;
type TypeNode = ^node; node = record s:char; next:TypeNode; end;
var inn, ou: string; i, j: integer; c: char; top: TypeNode;
function deleteS(var top: TypeNode; x: char): boolean; var pcurr: TypeNode; begin if (top = nil) then deleteS := false else if (top^.next = nil) then begin pcurr := top; top := nil; dispose(pcurr); deleteS := true; end else begin pcurr := top; top := top^.next; dispose(pcurr); deleteS := true; end; end;
procedure addS(var Top: TypeNode; x: char); var pcurr: TypeNode; begin new(pcurr); pcurr^.s := x; pcurr^.next := top; top := pcurr; end;
begin j := 1; addS(Top, '('); read(inn); ou := ' '; for i := 1 to length(inn) do begin if(inn[i] >= '0') and (inn[i] <= '9') then ou += inn[i]; j += 1; end; if (inn[i] = '(') then addS(top, '('); if(inn[i] = ')') then begin deleteS(top, c); while(c <> '(') do begin deleteS(top, c); if(c = '+') or (c = '-') or (c = '*') or (c = '/') then begin ou[j] := c; j += 1; end; end;
if (c = '*') or (c = '/') then while(c <> '(') do begin deleteS(top, c); ou[j] := c; j += 1; end;
if (c = '+') or (c = '-') then while (c <> '*') and (c <> '/') and (c <> '(') do begin deleteS(top, c); ou[j] := c; j += 1; end; end; deleteS(top, c); while(c <> '(') do begin ou[j] := c; j += 1; deleteS(top, c); end; write(ou); end.
- Assistant
Эта программа содержит несколько ошибок. Вот исправленный код:
Program obsch;
type TypeNode = ^node; node = record s: char; next: TypeNode; end;
var inn, ou: string; i, j: integer; c: char; top: TypeNode;
function deleteS(var top: TypeNode; var x: char): boolean; var pcurr: TypeNode; begin if (top = nil) then deleteS := false else begin pcurr := top; x := pcurr^.s; top := top^.next; dispose(pcurr); deleteS := true; end; end;
procedure addS(var Top: TypeNode; x: char); var pcurr: TypeNode; begin new(pcurr); pcurr^.s := x; pcurr^.next := top; top := pcurr; end;
begin j := 1; addS(Top, '('); readln(inn); ou := ''; for i := 1 to length(inn) do begin if (inn[i] >= '0') and (inn[i] <= '9') then ou := ou + inn[i] else if (inn[i] = '(') then addS(top, '(') else if (inn[i] = ')') then begin while (deleteS(top, c)) and (c <> '(') do begin ou := ou + c; if (c = '+') or (c = '-') or (c = '') or (c = '/')) then while (deleteS(top, c)) and (c <> '(') do begin ou := ou + c; j := j + 1; end; if (c <> '(') and deleteS(top, c) then while (c <> '') or (inn[i] = '/') then begin addS(top, inn[i]); ou := ou + ' '; j := j + 1; end; end;
- User
Что ты имеешь ввиду под ou := ou + ’ ';
- Assistant
ou := ou + ' 'означает добавление пробела к строкеou. Это сделано для отделения операндов друг от друга в постфиксной записи выражения. Если не добавлять пробелы, то полученная строка может быть трудна для чтения. Например, выражение2+3*4в постфиксной записи будет выглядеть как234*+, а с добавленными пробелами -2 3 4 * +. - User
Проверь код на наличие ошибок: Program obsch;
type TypeNode = ^node; node = record s: char; next: TypeNode; end;
var inn, ou: string; i, j: integer; c: char; top: TypeNode;
function deleteS(var top: TypeNode; var x: char): boolean; var pcurr: TypeNode; begin if (top = nil) then deleteS := false else begin pcurr := top; x := pcurr^.s; top := top^.next; dispose(pcurr); deleteS := true; end; end;
procedure addS(var Top: TypeNode; x: char); var pcurr: TypeNode; begin new(pcurr); pcurr^.s := x; pcurr^.next := top; top := pcurr; end;
begin j := 1; addS(Top, '('); readln(inn); ou := ' '; for i := 1 to length(inn) do begin if (inn[i] >= '0') and (inn[i] <= '9') then ou := ou + inn[i] else if (inn[i] = '(') then addS(top, '(') else if (inn[i] = ')') then begin while (deleteS(top, c)) and (c <> '(') do begin ou := ou + c; if (c = '+') or (c = '-') or (c = '') or (c = '/') then j := j + 1; end; if (deleteS(top, c)) and ((c = '') or (c = '/')) then while (deleteS(top, c)) and (c <> '(') do begin ou := ou + c; j := j + 1; end; if (c <> '(') and deleteS(top, c) then while (c <> '') and (c <> '/') and (c <> '(') do begin ou := ou + c; if not deleteS(top, c) then break; end; end else if (inn[i] = '+') or (inn[i] = '-') or (inn[i] = '') or (inn[i] = '/') then begin addS(top, inn[i]); ou := ou + ' '; j := j + 1; end; end;
- Assistant
В коде нет синтаксических ошибок, но есть пара логических ошибок:
- В строке
ou := ' ';лучше заменить пробел на пустую строку:ou := ''; - В блоке
else if (inn[i] = ')') then, после завершения внутреннего цикла, переменнаяcможет быть неопределена, но в строке она используется. Нужно добавить проверку наличия значения в , например:
- В строке