You are on page 1of 4

uses crt;

type
danhsach= ^phantu;
phantu = record
tu: string[15];
tiep: danhsach;
end;
result = -1..1;
var
tudien: array['A'..'Z'] of danhsach; ptr: pointer;
{ Th? t?c kh?i t?o khoá t? A .. Z c?a t? di?n }
procedure CreatTD;
var
p: danhsach; ch: char;
begin
for ch:='A' to 'Z' do
begin
new(p);
p^.tu[0] := chr(1);
p^.tu := ch; p^.tiep := NIL;
tudien[ch] := p;
end;
end;
{ Hàm so sánh 2 t? }
function Cmp(tu1, tu2: string): result;
var
i: byte;
begin
for i:=1 to length(tu1) do tu1[i] := upcase(tu1[i]);
for i:=1 to length(tu2) do tu2[i] := upcase(tu2[i]);
if tu1 > tu2 then cmp := 1
else
if tu1 = tu2 then cmp := 0
else cmp := -1;
end;
{ Th? t?c b? sung t? vào danh sách theo khoá c?a nó }
procedure InputTD(tu: string; key: char);
var
p, q, r: danhsach;
begin
p := tudien[key]; r := p;
new(q); q^.tu := tu; q^.tiep := NIL;
while (p <> NIL) and (cmp(tu,p^.tu) = 1) do
begin
r := p; p := p^.tiep;
end;
if p = nil then r^.tiep := q
else
if cmp(tu,p^.tu) = -1 then
begin
q^.tiep := r^.tiep; r^.tiep := q;
end;
end;

{ Th? t?c nh?p thêm t? b?ng cách d?c n?i dung các t?p van b?n b? sung vào danh sách }
procedure AddListTD;
var
f: text; xau: string[255]; tu: string[15]; i, j: byte;
begin
write('Nhap tep van ban de bo sung vao tu dien: '); readln(xau);
assign(f,xau); reset(f);
while (not eof(f)) do
begin
i := 1; readln(f,xau);
while i<=length(xau) do
begin
j := 1;
while (xau[i]<>' ') and (xau[i]<>',') and (xau[i
]<>'.') and (xau[i]<>';') and (xau[i]<>':') and (xau[i]<>'!') and (xau[i]<>'?')
and (i<=length(xau)) do
begin
tu[j] := xau[i]; i := i+1; j := j+1;
end;
if j > 1 then
begin
tu[0] := chr(j-1);
InputTD(tu,upcase(tu[1]));
end;
i := i+1;
end;
end;
close(f);
end;
{ Th? t?c tìm ki?m t?. N?u có thì thông báo Ðã có trong t? di?n , n?u không thì chèn vào v
c?a nó trong t? di?n }
procedure FindWord;
var
p, q, r: danhsach; tu: string[15]; ans: char;
begin
write('Nhap tu can tim: '); readln(tu);
p := tudien[upcase(tu[1])]; r := p;
new(q); q^.tu := tu; q^.tiep := NIL;
while (p<>NIL) and (cmp(tu,p^.tu) = 1) do
begin
r := p; p := p^.tiep
end;
if (p<>NIL) and (cmp(tu,p^.tu) = 0) then
begin
writeln('Da co trong tu dien'); readln;
end
else
begin
writeln('Tu vua nhap chua co trong tu dien');
repeat
write('Ban muon them vao (C\K): '); rea
dln(ans);
until ((upcase(ans) = 'C') or (upcase(ans) = 'K'));
if upcase(ans)='C' then
begin
if p = nil then r^.tiep := q
else
if cmp(tu,p^.tu) = -1 then
begin
q^.tiep := r^.tiep; r^.tiep :=
q;
end;
end;
end;
end;
{ Th? t?c hi?n th? các t? ra màn hình theo th? t? tang theo khoá }
procedure ViewTD;
var
p: danhsach; ch: char; num: integer;count:integer;
begin
num := 0;count:=0;
for ch := 'A' to 'Z' do
begin
p := tudien[ch];
writeln(p^.tu); p := p^.tiep;
while p <> Nil do
begin
write(' ',p^.tu); p := p^.tiep;
count := count+1;
end;
num:=num+1;
if num=4 then
begin
writeln;writeln;
write('******* An phim Enter de tiep tuc...');
readln;
num:=0;
clrscr;
end;
writeln;
end;
write(' ********** Tu dien co ',count,' tu **********');
readln;
end;
{ Th? t?c ghi d? li?u t? danh sách vào t?p van b?n }
procedure WriteTD;
var
ch: char; p: danhsach; f: text;Tentep:string[100];
begin
write('Nhap ten tep:');readln(Tentep);
assign(f,Tentep); rewrite(f);
for ch:='A' to 'Z' do
begin
p := tudien[ch];
writeln(f,p^.tu); p := p^.tiep;
while p<>NIL do
begin
write(f,' ',p^.tu); p := p^.tiep;
end;
writeln(f);
end;
close(f);
end;
{ Th? t?c t?o menu chuong trình }
procedure Menu;
var
ans: byte;
begin
repeat
repeat
clrscr;
writeln(' +*+*+*+*+* MENNU +*+*+*+*+*');
writeln;
writeln(' 1 : Xem tu dien');
writeln(' 2 : Tim tu trong tu dien');
writeln(' 3 : Bo sung tu vao tu dien tu tep
van ban');
writeln(' 4 : Ghi tu dien ra tep van ban');
writeln(' 5 : Ket thuc chuong trinh');
writeln;
writeln(' +*+*+*+*+*+*+*+*+*+*+*+*+*+');
writeln;
write(' Lua chon (1..5): '); readln(ans);
clrscr;
until ((0 < ans) and (ans < 6));
case ans of
1: ViewTD;
2: FindWord;
3: AddListTD;
4: WriteTD;
end;
until ans = 5;
end;
{ Thân chuong trình }
BEGIN
mark(ptr);
CreatTD;
Menu;
release(ptr);
END.

You might also like