Tải bản đầy đủ (.pdf) (11 trang)

CHUỖI VÀ CÁC BÀI TOÁN TRÊN CHUỖI part 3 ppt

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (25.95 KB, 11 trang )


Vâ Minh Phæ – Bæ m«n Khoa häc m¸y tÝnh 23
if stack.index=0 then pops:=false
else
begin
pops:=true;
dt:=stack.data[stack.index];
dec(stack.index);
end;
end;

{ }

Procedure WriteStack(stack:stackc);
var i:integer;
Begin
For i:=0 to stack.index do
write(stack.data[i]);
End;
{ }
procedure WriteState(ch:char;P:string; stack:stackc);

Begin
inc(step);
write(step:3,' ');
write(ch:5);
write(p:20);{Hien thi tung buoc}
write(' ');
writestack(stack);{Hien thi tung buoc}
writeln;
if step mod 23=0 then



Vâ Minh Phæ – Bæ m«n Khoa häc m¸y tÝnh 24
Begin
write('Press enter to continue ');
readln;
end;
End;
{ }
Function Priority(Token:char):integer;
Begin
case Token of
'(',')': Priority:=0;
'-','+': Priority:=1;
'*','/': Priority:=2;
'^': Priority:=3;
End;
End;
{ }
Procedure Init(var stack:stackc);
Begin
stack.index:=0;
End;
{ }
Procedure InitR(var stack:stackR);
Begin
stack.index:=0;
End;
{ }
function TheTop(var stack:stackc):char;
Begin

if stack.index=0 then

Vâ Minh Phæ – Bæ m«n Khoa häc m¸y tÝnh 25
TheTop:=#0
else
TheTop:=stack.data[stack.index];
End;
{ }
function Empty(var stack:stackc):boolean;
Begin
if stack.index=0 then
Empty:=true
else
Empty:=false;
End;
{ }
function EmptyR(var stack:stackR):boolean;
Begin
if stack.index=0 then
EmptyR:=true
else
EmptyR:=false;
End;
{ }
function Push(var stack:stackc;dt:char):boolean;
Begin
if stack.index=max+1 then
push:=false
else
Begin

inc(stack.index);
push:=true;

Vâ Minh Phæ – Bæ m«n Khoa häc m¸y tÝnh 26
stack.data[stack.index]:=dt;
end;
End;
{ }
function PushR(var stack:stackR;dt:Real):boolean;
Begin
if stack.index=max+1 then
pushR:=false
else
Begin
inc(stack.index);
pushR:=true;
stack.data[stack.index]:=dt;
end;
End;
{ }
function Pop(var stack:stackc;var dt:char):boolean;
Begin
if stack.index=0 then
Pop:=false
else
Begin
Pop:=True;
dt:=stack.data[stack.index];
dec(stack.index);
End;

End;
{ }
function PopR(var stack:stackR;var dt:Real):boolean;

Vâ Minh Phæ – Bæ m«n Khoa häc m¸y tÝnh 27
Begin
if stack.index=0 then
PopR:=false
else
Begin
PopR:=True;
dt:=stack.data[stack.index];
dec(stack.index);
End;
End;
{ }
function DeleteBlank(s:string):string;
var i,j:integer;
Begin
j:=0;
i:=1;
while i<=length(s) do
Begin
if s[i]<>' ' then
Begin
inc(j);
s[j]:=s[i];
End;
inc(i);
End;

delete(s,j+1,length(s)-j);
DeleteBlank:=s;
End;
{ }

Vâ Minh Phæ – Bæ m«n Khoa häc m¸y tÝnh 28
function Polish(s:string; var p:string) :boolean;
var i,j:integer;
stack:stackc;
ch:char;
Begin

Polish:=true;
s:=DeleteBlank(s);
s:=s+')';
init(stack);
push(stack,'(');
i:=1;
p:='';
while not Empty(stack) do
Begin
if upcase(s[i]) in ['A' 'Z'] then
Begin
p:=p+upcase(s[i]);
WriteState(s[i],p,stack);
end
else
Begin
if s[i]='(' then
Begin

Push(stack,s[i]);
WriteState(s[i],p,stack);
End
else
if s[i] =')' then

Vâ Minh Phæ – Bæ m«n Khoa häc m¸y tÝnh 29
Begin
pop(stack,ch);
while ch<>'(' do
Begin
p:=p+ch;
pop(stack,ch);
End;
WriteState(s[i],p,stack);
End
else
if s[i] in ['-','+','/','*','^'] then
Begin{Coi nhu la phep tinh}
while (Priority(TheTop(stack))>=Priority(s[i])) do
Begin
pop(stack,ch);
p:=p+ch;
End;
push(stack,s[i]);
WriteState(s[i],p,stack);
End
else
Begin
Polish:=false;

exit;
End;
End;
inc(i);
End;
End;

Vâ Minh Phæ – Bæ m«n Khoa häc m¸y tÝnh 30
{ }
function Value(s:string;var outValue:real):boolean;
{ }
function Add(a,b:real):real;
Begin
Add:=a+b;
End;
{ }
Function Sub(a,b:real):real;
Begin
Sub:=a-b;
End;
{ }
Function Mul(a,b:real):real;
Begin
Mul:=a*b;
End;
{ }
Function Divi(a,b:real; var c:real):boolean;
Begin
if b=0 then
Divi:=false

else
Begin
Divi:=true;
c:=a/b;
End;
End;
{ }

Vâ Minh Phæ – Bæ m«n Khoa häc m¸y tÝnh 31
Function expl(a,b:real;Var c:real):boolean;
var temp:real;
Begin
if a<=0 then
expl:=false
else
Begin
expl:=true;
c:=exp(b*ln(a));
End;
End;
{ }
type values=record
exist:boolean;
value:real;
end;
var temp,temp2,t:real;
a:array['A' 'Z'] of values;
i:integer;
ch:char;
stack:stackR;

Check:boolean;
{Main of value}
Begin
For ch:='A' to 'Z' do
a[ch].exist:=false;
for i:=1 to length(s) do
if s[i] in ['A' 'Z'] then
a[s[i]].exist:=true;

Vâ Minh Phæ – Bæ m«n Khoa häc m¸y tÝnh 32
for Ch:= 'A' to 'Z' do
if a[ch].exist= true then
Begin
write('Nhap gia tri cho bien ',ch,':');
readln(a[ch].value);
End;
s:=s+')';
i:=1;
initR(stack);
while(s[i]<>')') do
Begin
if upcase(s[i]) in ['A' 'Z'] then
Begin
pushR(stack,a[s[i]].Value);
End;
if s[i] in ['-','+','/','*','^'] then
Begin
popR(stack,temp);
popR(stack,temp2);
Check:=true;

case s[i] of
'-':t:=Sub(temp,temp2);
'+':t:=Add(temp,temp2);
'*':t:=mul(temp,temp2);
'/':check:=divi(temp,temp2,t);
'^':check:=expl(temp,temp2,t);
end;
if check=false then
Begin

Vâ Minh Phæ – Bæ m«n Khoa häc m¸y tÝnh 33
value:=false;
exit;
end
else
Begin
pushR(stack,t);
End;
End;
inc(i);
End;
Value:=true;
popR(stack,outValue);
End;

{ }
Procedure mahoa( var s:string; dd:integer);
var
i:integer;
begin

for i:=1 to dd-length(s) do
s:=s+' ';
end;
{ }
Procedure taonen; {tao nen}
var i:integer;
begin
textbackground(15);
for i:=1 to 50 do

×