Модератор: Модераторы
program lab8;
uses
crt;
const
n = 20;
type
pos = 0..n;
var
st,res : string;
critical : array[0..n div 2] of pos;
temp : array[0..n div 2 +1] of string;
sign : array[1..n] of char;
priority : array[0..n] of pos;
SignPos : array[1..n] of integer;
i,j,k,m,x,l,o : integer;
f : boolean;
pr,min,top : pos;
begin
clrscr;
writeln('enter the expression in the infix form :');
read(st);
j := 1;
for i := 1 to length(st) do
begin
f := true;
case st[i] of
'+' : sign[j] := '+';
'-' : sign[j] := '-';
'*' : sign[j] := '*';
'/' : sign[j] := '/';
else
begin
dec(j);
f := false;
end;
end;
if f = true then SignPos[j] := i;
inc(j);
end;
dec(j);
pr := 3;
priority[0] := pr; {priority[0] - max element in mass priority}
for i := j downto 1 do
begin
if st[signpos[i] + 2] = ')' then inc(pr,2);
priority[i] := pr;
if st[signpos[i] - 2] = '(' then dec(pr,2);
if (sign[i] = '*') or (sign[i] = '/') then inc(priority[i]);
if priority[i] > priority[0] then priority[0] := priority[i];
if pr > priority[0] then priority[0] := pr;
end;
m := priority[0];
writeln;
priority[0] := 0;
k := 0;
critical[0] := 0;
for i := 1 to j do
begin
if (priority[i-1] > priority[i]) and (priority[i+1] > priority[i]) then
begin
inc(k);
critical[k] := i; {finding critical points}
inc(critical[0]); {nubmer of critical points}
end;
end;
critical[k+1] := j+1;
k := 1;
x:=1;
{} for i := 1 to critical[0]+1 do
{} begin
for m := k to critical[i]-1 do
begin
if (priority[m] > priority[m+1]) and (priority[m] > priority[m-1]) then
begin
top := m;
temp[x] := sign[m] + st[signpos[m]-1] + st[signpos[m]+1];
pr := priority[m];
if priority[k] < priority[critical[i]-1] then min := priority[k]
else min := priority[critical[i]-1];
end;
end;
for l := pr - 1 downto min do
begin
for m := k to critical[i]-1 do
begin
if priority[m] = l then
begin
if m < top then
begin
o := signpos[m] - 1;
temp[x] := sign[m] + st[o] + temp[x];
end;
if m > top then
begin
o := signpos[m] + 1;
temp[x] := sign[m] + temp[x] + st[o];
end;
end;
end;
end;
k := critical[i]+1;
inc(x);
end;
m := 0;
min := n;
for i := 1 to critical[0] do
begin
if priority[critical[i]] > m then m := priority[critical[i]];
if priority[critical[i]] < min then min := priority[critical[i]];
end;
for i := m downto min do
begin
for k := 1 to critical[0] do
begin
if priority[critical[k]] = i then
begin
temp[k] := sign[critical[k]] + temp[k] + temp[k+1];
temp[k+1] := temp[k];
l := k+1;
while priority[critical[l]] > priority[critical[k]] do
begin
temp[l+1] := temp[k];
inc(l);
end;
l := k-1;
while priority[critical[l]] >= priority[critical[k]] do
begin
temp[l] := temp[k];
dec(l);
end;
end;
end;
end;
res := temp[1];
if top = 0 then
begin
for i := 1 to j do res := sign[i] + res;
for i := 0 to j do res := res + st[2*i +1];
end;
writeln(res);
readkey;
end.
Вернуться в Обучение Free Pascal
Сейчас этот форум просматривают: нет зарегистрированных пользователей и гости: 11