BÀI TẬP CHƯƠNG 8: KIỂU FILE

Một phần của tài liệu Bài tập Pascal (hay) có lời giải (Trang 50 - 56)

Bài 1 :

Hãy lập chương trình tạo một tệp số nguyên chứa các số nguyên tố nhỏ hơn 10000 theo thứ tự tăng dần .

(* Tạo file số nguyên tố nhỏ hơn 10000 *) Uses Crt; Const N=10000; Var i , j : Integer; f: File of Integer; a: Array[2..N] of boolean; BEGIN

For i:=2 to N do a[i]:=true; i:=2;

Repeat

For j:=2 to (N div i) do a[i*j]:=false; Repeat Inc(i) Until a[i] or (i>N); Until i>N;

Assign(F,'C:\SoNT.dat'); Rewrite(F); For i:=1 to N do If a[i] then Write(F,i); Close(F);

clrscr;

Write(' Viet ra file "C:\SoNT" cac so nguyen to nho hon 10000 '); Readln;

END .

Bài 2 :

Cho f là tệp văn bản chứa các xâu 10 kí tự . Hãy lập chương trình nhập và hiển thị nội dung file đó lên màn hình , mỗi xâu một dòng , đầy trang màn hình thì dừng lại đợi gõ Enter mới hiển thị trang tiếp theo cho tới hết.

(* Ghi và đọc file of String *) Uses Crt;

Const INP='FoString.dat'; Type String10 = String[10]; Procedure Ghi;

Var f: file of String10; S: String10; Begin

Assign(f,INP); Rewrite(f);

Writeln('Nhap vao f. Thoi nhap khi S='''' (go Enter)!'); Readln(S); While (S<>'') do Begin Write(f,S); Readln(S); End; Close(f); End; Procedure Doc;

Var f: file of String10; S: String10; Begin

Clrscr;

Assign(f,INP); Reset(f); While Not Eof(f) Do Begin

Read(f,S); Writeln(S);

If WhereY=25 then Begin

Write('Press Enter to continue..'); Readln; Clrscr; End; End; Close(f); Readln; End; BEGIN Ghi; Doc; END. Bài 3 :

Bạn hãy viết chương trình cho phép đọc dữ liệu từ bàn phím và ghi thêm vào cuối một tệp các bản ghi .

(* Doc và ghi vào cuối tệp các bản ghi *) Uses Crt;

Const inp='Hocsinh.dat'; Type Hocsinh=Record Ten : String[30]; Tuoi: Byte;

End;

Var F : file of Hocsinh; Hs: Hocsinh; BEGIN

Assign(f,inp); Reset(f);

Write('Ho va ten: '); Readln(Hs.Ten); Write('Tuoi : '); Readln(Hs.Tuoi); Seek(f,Filesize(F)); Write(f,Hs); Close(f); END . Bài 4 :

Cho một văn bản chứa trong một text file f . Trong văn bản , tính từ trái sang phải , từ trên xuống dưới , kí tự # là kí hiệu xoá đi một từ đứng ngay trước nó nếu có . Ví dụ ‘#Ta#oi di ngu#h###hoc’ có nghĩa là ‘Toi di hoc’ . Bạn hãy viết chương trình sửa lại file f theo quy ước trên . (* Sửa văn bản *) Uses Crt; Const fi='vanban.txt'; Var f: text; s: string; ch: char; (adsbygoogle = window.adsbygoogle || []).push({});

Procedure docfile ( fi : String ); Var f : text ;

Begin

Assign(f,fi); Reset(f); while not eof(f) do Begin Read(f,ch); Write(ch); End; close(f) ; writeln ; End ; BEGIN

Writeln(' Van ban ban dau doc tu file "vanban.txt" :') ; docfile(fi) ;

assign(f,fi) ; reset(f) ; s:='';

Read(f,ch);

If (ch='#')then Delete(s,length(s),1) Else s:=s+ch; Until Eof(f);

Close(f);

Assign(f,fi); Rewrite(f); Writeln(f,s);

Close(f);

Writeln(' Van ban sau khi sua chua :') ; docfile(fi) ;

Readln; END .

Bài 5 :

Cho 2 file f và g cùng kiểu ( nhưng không rõ kiểu nào ) . Bạn hãy lập thủ tục gán nội dung của file g cho file f.

(* Gan hai file *) Uses Crt; Const

f1='calc.ex'; f2='C:\calc.exe';

Procedure Copyfile(fi1,fi2: string); Var nread,nbuf: word;

buf: array [1..1024] of byte; f1,f2: file; Begin Assign(f1,fi1); Reset(f1,1); Assign(f2,fi2); Rewrite(f2,1); nbuf:=1024; Repeat Blockread(f1,buf,nbuf,nread); Blockwrite(f2,buf,nread); Until nread<>nbuf; Close(f1); Close(f2); End; BEGIN Copyfile(f1,f2); END. Bài 6 :

Cho một file text ghi lại một chương trình Pascal của một học sinh . Hãy viết chương trình kiểm tra lỗi của chương trình Pascal trên theo các cách sau đây :

Cách 1 : Kiểm tra xem số lượng các dấu ‘ ( dấu mở và dấu đóng ) có bằng nhau không ? Cách 2 : Kiểm tra xem số lượng các từ Begin và End có bằng nhau không ?

(* Dem (') và 'Begin' , 'End' *) Uses Crt;

Const fi='C8_6.txt';

Function Dem(c: string): integer; Var n,l: integer; f: text; S: string; Begin

l:=Length(c); n:=0; Assign(f,fi); Reset(f); While not Eof(f) do Begin Readln(f,S); While pos(c,s)<>0 do Begin Inc(n); Delete(s,pos(c,s),l); End; End; Close(f); Dem:=n; End; BEGIN Clrscr;

Write(' So luong cac dau ( va ) ');

If Dem('(')<>Dem(')') then Writeln('khong bang nhau.') Else Writeln('bang nhau.');

Write(' So luong cac tu Begin va End ');

If Dem('End')<>Dem('Begin') then Writeln('khong bang nhau.') Else Writeln('bang nhau.');

Readln; END .

Bài 7 : (adsbygoogle = window.adsbygoogle || []).push({});

Cho một file text . Hãy viết chương trình đếm xem file text trên chứa bao nhiêu từ . ( Chú ý : theo quy định , các từ cách nhau bởi một hay nhiều dấu cách ) .

(* Đếm từ *) Uses Crt; Const fi = 'hoten.txt'; Var f: text; s: string; dem: word;

BEGIN Clrscr; Clrscr; dem:=0;

Assign(f,fi); Reset(f); While not Eof(f) do Begin Readln(f,s); While s[1]=' ' do Delete(s,1,1); While length(s)>0 do Begin Case s[1] of

' ': While (s[1]=' ')and(length(s)>0) do Delete(s,1,1); Else

Begin inc(dem);

While (s[1]<>' ')and(length(s)>0) do Delete(s,1,1); End;

End; End; End; Close(f);

Write(' So tu co trong file hoten.txt la: ',dem); Readln;

END.

Bài 8 :

Cho một file text . Viết chương trình loại bỏ các khoảng trống thừa bên trong file text này .

(* Cat khoang trong thua *) Uses Crt;

Const fi = 'file.inp'; fo = 'C:\file.out'; Var inp,out: text; s: string; BEGIN

Assign(inp,fi); Reset(inp); Assign(out,fo); Rewrite(out); While not Eof(inp) do Begin

Readln(inp,s);

While (s[1]=' ')and(Length(s)>0) do Delete(s,1,1);

While (s[Length(s)]=' ')and(Length(s)>0) do Delete(s,Length(s),1); While (Length(s)>0)and(pos(' ',s)<>0) do Delete(s,pos(' ',s),1); Writeln(out,s);

End;

Close(out); Close(inp); END.

Một phần của tài liệu Bài tập Pascal (hay) có lời giải (Trang 50 - 56)