Advertisement
huyhung94

Định danh biểu thức hậu tố.

Sep 21st, 2013
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 2.70 KB | None | 0 0
  1. program Hau_to;
  2. uses crt;
  3. Type
  4.     mang=array[1..1000]of string[10];
  5.     mang1=array[1..1000] of real;
  6. var bthuc:mang;
  7.     s:mang1;
  8.     i,n,kt,T:integer;
  9.     x,y,z,w:real;
  10. procedure Push(var S:mang1;var T:integer; X:real);
  11.    begin
  12.         if (T>=n) then
  13.            writeln('Tran STACK.')
  14.         else
  15.             begin
  16.               inc(t);
  17.               s[T]:=x;
  18.             end;
  19.         textcolor(red);
  20.         writeln('Da day so ',x:2:2,' vao STACK.');
  21.         textcolor(white);
  22.    End;
  23. Function Pop(S:mang1;var T:integer):real;
  24.    begin
  25.         if T<=0 then
  26.            writeln('STACK het.')
  27.         else
  28.           begin
  29.                Pop:=s[t];
  30.                textcolor(blue);
  31.                writeln('  Lay ',s[t]:2:2,' ra khoi STACK.');
  32.                textcolor(white);
  33.                dec(t);
  34.           end;
  35.  
  36.    End;
  37. Procedure Nhap;
  38.    begin
  39.         i:=0;
  40.         textcolor(green);
  41.         writeln('Nhap tung phan tu cua bieu thuc.');
  42.         writeln('Nhap # de ket thuc bieu thuc.');
  43.         textcolor(white);
  44.         writeln;
  45.         repeat
  46.               inc(i);
  47.               write('Nhap phan tu: ');
  48.               readln(bthuc[i]);
  49.         until bthuc[i]='#';
  50.         n:=i;
  51.    end;
  52. Begin
  53.      clrscr;
  54.      Nhap;
  55.      for i:=1 to n-1  do
  56.          write(bthuc[i],' ');
  57.      i:=1;
  58.      writeln;
  59.      t:=0;
  60.      Repeat
  61.            val(bthuc[i],x,kt);
  62.            if kt=0 then
  63.                    push(s,t,x)
  64.  
  65.            else
  66.                begin
  67.                   y:=Pop(S,t);
  68.                   z:=Pop(S,t);
  69.                   textcolor(yellow);
  70.                   if bthuc[i]='+' then
  71.                      begin
  72.                         writeln('Thuc hien phep toan ',z:2:2,bthuc[i],y:2:2,'=',z+y:2:2);
  73.                         w:=y+z;
  74.                      end;
  75.                   if bthuc[i]='-' then
  76.                      begin
  77.                         writeln('Thuc hien phep toan ',z:2:2,bthuc[i],y:2:2,'=',z-y:2:2);
  78.                         w:=z-y;
  79.                      end;
  80.                   if bthuc[i]='*' then
  81.                      begin
  82.                         writeln('Thuc hien phep toan ',z:2:2,bthuc[i],y:2:2,'=',z*y:2:2);
  83.                         w:=z*y;
  84.                      end;
  85.                   if bthuc[i]='/' then
  86.                      begin
  87.                         writeln('Thuc hien phep toan ',z:2:2,bthuc[i],y:2:2,'=',z/y:2:2);
  88.                         w:=z/y;
  89.                      end;
  90.                   textcolor(white);
  91.                   delay(1250);
  92.                   Push(s,t,w);
  93.                end;
  94.            inc(i);
  95.      until bthuc[i]='#';
  96.      textcolor(green);
  97.      writeln;
  98.      write('Dinh gia cua bieu thuc = ',w:4:2);
  99.      readln
  100. End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement