kh1132000
31-03-2011, 11:16
Bài Tập: Cài đặt bài toán tính biểu thức số học bằng giải thuật tính giá trị biểu thức bằng kí pháp nghịch đảo. bằng ngôn ngữ Pacsal.
Bác nào biết giúp em với. Nó bao lỗi hoài
Program kyphapnghichdao;
type kieu_stack =array[1..80] of 0..1;
var BT:string[30];
S: kieu_stack;
Top,i,so1,so2,n,x:integer;
procedure PUSH(var s:kieu_stack;var Top:integer;x:integer);
begin
if Top=n then
writeln('s khong con cho trong');
else
begin
Top:=Top+1;
S[Top]:=x;
end;
end;
function POP(var s:kieu_stack;var Top:integer;x:integer):integer;
begin
if Top>0 then
begin
POP:=S[Top];
Top:=Top-1;
end;
else
writeln('stack day');
end;
function tinh_BT(BT:string):real;
begin
for i:=1 to length(BT) do
begin
case BT[i] of
'+':begin
so1:= POP(s, Top);
so2:= POP(s, Top);
PUSH(s,Top, so1+so2);
end;
'-':begin
so1:=POP(s, Top);
so2:=POP(s, Top);
PUSH(s,Top, so1-so2);
end;
'*':begin
so1:=POP(s, Top);
so2:=POP(s, Top);
PUSH(s,Top, so1*so2);
end;
'/':begin
so1:=POP(s, Top);
so2:=POP(s, Top);
PUSH(s,Top, so1/so2);
end;
slse
begin
val(BT[i],n,e);
PUSH(s,Top,n);
end;
end;
Tinh_BT:=POP(s,Top);
end;
BEGIN
write('nhap BT hau to:');
readln(BT);
writeln('tinh BT=',Tinh_BT(BT));
END.
Bác nào biết giúp em với. Nó bao lỗi hoài
Program kyphapnghichdao;
type kieu_stack =array[1..80] of 0..1;
var BT:string[30];
S: kieu_stack;
Top,i,so1,so2,n,x:integer;
procedure PUSH(var s:kieu_stack;var Top:integer;x:integer);
begin
if Top=n then
writeln('s khong con cho trong');
else
begin
Top:=Top+1;
S[Top]:=x;
end;
end;
function POP(var s:kieu_stack;var Top:integer;x:integer):integer;
begin
if Top>0 then
begin
POP:=S[Top];
Top:=Top-1;
end;
else
writeln('stack day');
end;
function tinh_BT(BT:string):real;
begin
for i:=1 to length(BT) do
begin
case BT[i] of
'+':begin
so1:= POP(s, Top);
so2:= POP(s, Top);
PUSH(s,Top, so1+so2);
end;
'-':begin
so1:=POP(s, Top);
so2:=POP(s, Top);
PUSH(s,Top, so1-so2);
end;
'*':begin
so1:=POP(s, Top);
so2:=POP(s, Top);
PUSH(s,Top, so1*so2);
end;
'/':begin
so1:=POP(s, Top);
so2:=POP(s, Top);
PUSH(s,Top, so1/so2);
end;
slse
begin
val(BT[i],n,e);
PUSH(s,Top,n);
end;
end;
Tinh_BT:=POP(s,Top);
end;
BEGIN
write('nhap BT hau to:');
readln(BT);
writeln('tinh BT=',Tinh_BT(BT));
END.