Last active
December 10, 2015 11:39
-
-
Save kiryl/4428902 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var | |
i : integer; | |
inf, post : string; | |
stack : array[1..255] of char; | |
top : integer; | |
procedure die(error: string); | |
begin | |
writeln(error); | |
halt(1); | |
end; | |
procedure push(ch : char); | |
begin | |
inc(top); | |
stack[top] := ch; | |
end; | |
procedure pop(); | |
begin | |
post := post + stack[top]; | |
dec(top); | |
end; | |
function prio(ch : char) : integer; | |
begin | |
case ch of | |
'*', '/': prio := 1; | |
'+', '-': prio := 0; | |
'(': prio := -1; { Not an operator, but can be found on stack } | |
else die('Unknown operator: ' + ch); | |
end; | |
end; | |
begin | |
write('Enter expression in infex form: '); | |
readln(inf); | |
for i := 1 to length(inf) do | |
begin | |
case inf[i] of | |
'0'..'9': post := post + inf[i]; | |
'+', '-', '*', '/': | |
begin | |
while (top > 0) and (prio(inf[i]) <= prio(stack[top])) do | |
pop(); | |
push(inf[i]); | |
end; | |
'(': push(inf[i]); | |
')': | |
begin | |
while (top > 0) and (stack[top] <> '(') do | |
pop(); | |
if (top > 0) then dec(top) | |
else die('Unbalanced parentheses'); | |
end; | |
else die('Unexpected character: ' + inf[i]); | |
end; | |
end; | |
while top > 0 do | |
case stack[top] of | |
'+', '-', '*', '/': pop(); | |
else die('Unbalanced parentheses'); | |
end; | |
writeln('Postfix form: ', post); | |
end. | |
var | |
i : integer; | |
inf, post : string; | |
stack : array[1..255] of char; | |
top : integer; | |
procedure die(error: string); | |
begin | |
writeln(error); | |
halt(1); | |
end; | |
procedure push(ch : char); | |
begin | |
inc(top); | |
stack[top] := ch; | |
end; | |
procedure pop(); | |
begin | |
post := post + stack[top]; | |
dec(top); | |
end; | |
function prio(ch : char) : integer; | |
begin | |
case ch of | |
'*', '/': prio := 1; | |
'+', '-': prio := 0; | |
'(': prio := -1; { Not an operator, but can be found on stack } | |
else die('Unknown operator: ' + ch); | |
end; | |
end; | |
begin | |
write('Enter expression in infex form: '); | |
readln(inf); | |
for i := 1 to length(inf) do | |
begin | |
case inf[i] of | |
'0'..'9': post := post + inf[i]; | |
'+', '-', '*', '/': | |
begin | |
while (top > 0) and (prio(inf[i]) <= prio(stack[top])) do | |
pop(); | |
push(inf[i]); | |
end; | |
'(': push(inf[i]); | |
')': | |
begin | |
while (top > 0) and (stack[top] <> '(') do | |
pop(); | |
if (top > 0) then dec(top) | |
else die('Unbalanced parentheses'); | |
end; | |
else die('Unexpected character: ' + inf[i]); | |
end; | |
end; | |
while top > 0 do | |
case stack[top] of | |
'+', '-', '*', '/': pop(); | |
else die('Unbalanced parentheses'); | |
end; | |
writeln('Postfix form: ', post); | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment