while(!Myself->is_full_knowledge()) Myself->learn(Myself->enjoy);

Wednesday, November 22, 2006

Inverse Polish notation

5 * ( ( ( 9 + 8 ) * ( 4 * 6 ) + 7)
push(5)
push(9)
push(8)
push(pop + pop)
push(4)
push(6)
push(pop*pop)
push(pop*pop)
push(7)
push(pop+pop)
push(pop*pop)


----------------

stackinit();
repeat
repeat read(c) until c<>' ' ;

if(c == ')') then write(chr(pop));
if(c == '+') then push(ord(pop));
if(c == '*') then push(ord(pop));
if(c == '/') then push(orf(pop));

while(c>='0' and c<= '9') do
begin
write(c);
read(c);
end;

if(c<>'(') then write(' ');
untl eofn;



repeat
c = 0;
repeat read(c) until c<> ' ';
if c == '*' then x = pop*pop;
if c == '+' then x = pop + pop;
while c>= '0' and c <= '9' do
begin
x = 10*x + ord(c) - ord('0');
read(c);
end;
push(x);
until eoln;

No comments: