Program Tap Tin So Nguyen; CÁC BÀI TẬP VỀ TỆP 1/ Program Tap Tin So Nguyen; Uses Crt; Var f File Of Integer; So Integer; a Array[1 10] Of Integer; Spt,i,j Integer; Filename String[11]; Begin ClrScr; W[.]
1/ Program Tap_Tin_So_Nguyen; Uses Crt; Var f : File Of Integer; So : Integer; a : Array[1 10] Of Integer; Spt,i,j : Integer; Filename : String[11]; Begin ClrScr; Writeln('NHAP SO LIEU CHO TAP TIN SONGUYEN.DAT'); Writeln(' -') ; Writeln; CÁC BÀI TẬP VỀ TỆP Assign(f,'songuyen.dat'); ReWrite(f); For i := To 10 Do Begin Write('-So thu: ',i:2,' = '); Readln(So); Write(f,so); End; Close(f); Spt := 0; Reset(f); While NOT EOF(f) Do Begin Spt := Spt + 1; Read(f,so); a[Spt] := so; End; Close(f); For i := To Spt -1 Do For j := Spt Downto i + Do If a[j] < a[j-1] Then Begin So := a[j]; a[j] := a[j-1]; a[j-1] := So; End; Writeln; Writeln('Sau sap xep: '); For i := To 10 Do Write(a[i]:6); Writeln; Write(' Bam '); Readln End 2/ Program Ghep_Tap_Tin; Uses Crt; Var f1,f2,f3 : File Of Real; i : Integer; So : Real; Ch : Char; Begin ClrScr; Writeln('NHAP SO LIEU CHOAP1.DAT'); Writeln(' -'); Writeln; Assign(f1,'TAP1.DAT'); Rewrite(f1); i := 0; Repeat i := i + 1; Write('-So thu: ',i:2,' = '); Readln(So); Write(f1,so); Write(' Nhap nua ? (c/k) '); Readln(Ch); Until Upcase(Ch) ='K'; Close(f1); ClrScr; Writeln('NHAP SO LIEU CHO TAP2.DAT'); Writeln(' -'); Writeln; Assign(f2,'TAP2.DAT'); Rewrite(f2); i := 0; Repeat i := i + 1; Write('-So thu: ',i:2,' = '); Readln(So); Write(f2,so); Write(' Nhap nua ? (c/k) '); Readln(Ch); Until Upcase(Ch) ='K'; Close(f2); ClrScr; Writeln('GHEP TAP1 va TAP2 TAP3'); Writeln(' '); Writeln; Assign(f3,'TAP3.DAT'); Rewrite(f3); Reset(f1); Reset(f2); While NOT EOF(f1) Do Begin Read(f1,So); Write(f3,So); End; While NOT EOF(f2) Do Begin Read(f2,so); Write(f3,so); End; Reset(f3); While NOT EOF(f3) Do Begin Read(f3,So); Write(So :8:1); End; Close(f1); Close(f2); Close(f3); Writeln; Writeln; Write(‘Da ghep xong, Bam '); Readln; End 3/ Program Doc_Tung_Chu; Uses Crt; Var Filename : String; f : Text; Line : String[251]; k : Integer; Begin ClrScr; Writeln('DOC CHAM TAP TIN THEO TUNG CHU'); Writeln(' '); Writeln; Write('-Cho biet ten tap tin: '); Readln(Filename); Assign(f,Filename); Reset(f); While Not EOF(f) Do Begin Writeln; Write('-Cho biet ten tap tin: '); Readln(Filename); Assign(f1,filename); Reset(f1); Assign(f2,'Newfile.txt'); Rewrite(f2); i := 0; While Not EOF(f1) Do Begin Readln(f,line); For k := To Length(line) Do Begin Write(line[k]); Delay(150); End; Writeln; End; Close(f); End i := i + 1; Readln(f1,st); Writeln(f2,i,' ',St); End; Close(f1); Close(f2); Writeln; Writeln('Da danh so dong va ghi vao tap tin NEWFILE.TXT'); Writeln; 4/ Program Doc_Ghi_Tap_Tin; Var f1,f2 : Text; Filename : String; i : Integer; St : String; Begin Writeln('DANH SO DONG TAP TIN'); Writeln(' '); Write(' Bam de xem tap tin NEWFILE.TXT '); Readln; Assign(f2,'Newfile.txt'); Reset(f2); While Not EOF(f2) Do Begin Readln(f2,st); Writeln(St); End; Close(f2); Writeln; Write('Xem xong, ban '); Readln End 5/ Program Tron_Tap_tin; Uses Crt; Var f1,f2,f3 : File Of Integer; So1,So2 : Integer; i : Integer; Ch : Char; ok1,ok2 : Boolean; { } Function Layso1(Var So1 : Integer): Boolean; Begin If Not EOF(f1) Then Begin Read(f1,So1); LaySo1 := True; End Else layso1 := False; End; { } Function Layso2(Var So2 : Integer): Boolean; Begin If Not EOF(f2) Then Begin Read(f2,So2); LaySo2 := True; End Else layso2 := False; End; { } BEGIN Assign(f1,'so1.dat'); Rewrite(f1); ClrScr; Writeln('NHAP TAP TIN SO1'); Writeln(' '); Writeln; i := 0; Repeat i := i + 1; Write('-So thu: ',i:2,' = '); Readln(So1); Write(f1,so1); Write('-Nhap nua ? (c/k) '); Readln(Ch); Until Upcase(Ch) = 'K'; Close(f1); Assign(f2,'so2.dat'); Rewrite(f2); ClrScr; Writeln('NHAP TAP TIN SO2'); Writeln(' '); Writeln; i := 0; Repeat i := i + 1; Write('-So thu: ',i:2,' = '); Readln(So2); Write(f2,so2); Write('-Nhap nua ? (c/k) '); Readln(Ch); Until Upcase(Ch) = 'K'; Close(f2); Assign(f3,'so3.dat'); Rewrite(f3); Reset(f1); Reset(f2); ok1 := Layso1(So1); ok2 := Layso2(so2); While ok1 Or ok2 Do Begin If ok1 And ok2 Then {co ca tap tin} Begin If So1 < So2 Then Begin Write(f3,so1); ok1 := layso1(so1); End Else Begin Write(f3,so2); ok2 := Layso2(so2); End; End Else If ok1 Then {chi tap tin so1.dat} Begin Write(f3,so1); ok1 := layso1(so1); End Else If ok2 Then {chi tap tin so2.dat} Begin Write(f3,so2); ok2 := Layso2(so2); End; End; Writeln; Reset(f3); While not EOF(f3) Do Begin Read(f3,i); Write(i:6); End; Close(f1); Close(f2); Close(f3); Writeln; Write('Da tron xong, bam '); Readln END 6/ Program Tao_Tap_Tin_Co_Kieu; Type HocSinh = RECORD Ten : String[7]; Diem : 10; End; Var f : File Of Hocsinh; { -} Procedure TaoTapTin; Var Tam : HocSinh; Filename : String; Begin Write('-Cho biet ten tap tin: '); Readln(Filename); Assign(f,Filename); {$I-} Rewrite(f); {$I+} If IOResult Then Begin Writeln('Khong mo duoc tap tin: ',Filename); Halt; End; Repeat Write('Ten (bam de cham dut) : '); Readln(Tam.Ten); If Tam.Ten '' Then Begin Write('-Diem : '); Readln(Tam.Diem); Write(f,Tam); End; Until Tam.Ten = ''; Close(f); End; { -} Procedure XemLaiBanGhi; Var RecNo : Word; Tam : HocSinh; Begin Write('-Xem lai ban ghi thu may: '); Readln(RecNo); Reset(f); Seek(f,RecNo-1); Read(f,Tam); Writeln('-Ten : ',Tam.Ten); Writeln('-Diem : ',Tam.Diem); End; { -} BEGIN TaoTapTin; Writeln; XemLaiBanGhi; Writeln; Write(' Bam '); Readln; END 7/ Program Cat_Tap_tin; End; Var { -} f,g1,g2 : File; Procedure TinhTrungDiem; Buf : Array[1 63000] Of Byte; Begin Trungdiem : LongInt; TrungDiem := (Filesize(f) Div 2); { -} End; Procedure BaoLoi; { -} Begin Procedure ChepNuaDau; Writeln('Khong mo duoc tap tin'); Var Halt; S : LongInt; End; Num,SoDoc,SoGhi : Word; { -} Begin Procedure MoTapTin; S :=TrungDiem; Var Repeat TenTT,TenTT1,TenTT2: String; If Sizeof(Buf) spt); If TimThay Then Begin With ds[vitri] Do Writeln(Holot,' ',Ten,' Diem: ',Diem:0:1); Repeat Writeln('Co xoa khong ? (c/k) '); Ch := Readkey; Until Ch in['c','C','k','K']; If Upcase(Ch) = 'C' Then Begin spt := spt - 1; For i := vitri To spt Do ds[i] := ds[i+1]; 10/ Program DemChu; Uses Crt; Type Integer; Var MangChu = Array[Char] Of End; End Else Writeln('Khong tim thay'); Repeat Writeln('Tim nua khong ? (c/k) '); Ch := Readkey; Until Ch in['c','C','k','K']; Until Upcase(Ch) = 'K'; Rewrite(f); For i := To spt Do Write(f,ds[i]); Close(f); End; {Chuong trinh chiinh cua cap nhat} Begin Repeat Repeat ClrScr; Writeln(' MENU CAP NHAT '); Writeln('1-Sua'); Writeln('2-Them'); Writeln('3-Xoa'); Writeln('4-Thoat'); Ch := Readkey; Until Ch in['1' '4']; Case Ch Of '1' : Sua(f); '2' : Them(f); '3' : Xoa(f); End; Until Ch = '4' End; {************ CHUONG TRINH CHINH ***********} BEGIN ClrScr; Write('-Ten tap tin : '); Readln(Filename); Assign(f,Filename); Repeat Repeat ClrScr; Writeln(' MENU CHINH'); Writeln(' 1-Nhap'); Writeln(' 2-Sap xep'); Writeln(' 3-Xem'); Writeln(' 4-Cap nhat'); Writeln(' 5-Ket thuc'); Writeln; Ch := Readkey; Until ch in['1' '5']; Case Ch Of '1' : Nhap(f); '2' : SapXep(f); '3' : Xem(f); '4' : CapNhat(f); End; Until Ch = '5' END f : Text; Filename : String; Line : String[25]; Chu : Char; Letters,Lines,k : Integer; Dem : MangChu; Begin ClrScr; For Chu := Chr(0) To Chr(127) Do Dem[chu] := 0; Letters := 0; Write('-Cho biet ten tap tin: '); Readln(Filename); Assign(f,Filename); Reset(f); While Not EOF(f) Do Begin Readln(f,Line); For k := To Length(line) Do Begin If Line[k] In ['a' 'z'] Then Letters := Letters + 1; Dem[Line[k]] := Dem[Line[k]] + 1; End; End; Lines := 1; Close(f); Writeln('Tap tin: ',Filename,' co tat ca: ',Letters,' chu khong viet hoa'); Writeln; Writeln('Phan phoi tan suat cua cac chu nhu sau:'); Writeln; For Chu :='a' To 'z' Do Read(f,Ch); Begin If ch = St[1] Then Write('-Chu: ',Chu,' = '); Begin Write((Dem[chu]/Letters * 100):6:2,' Ok := True; % '); i := 1; If (Lines Mod 4) = Then While Not OK And ( i < Writeln; length(St)) Do Lines := Lines + 1; Begin End; Read(f,Ch); Readln If (Ch Chr(10)) And (Ch End Chr(13)) Then If Ch = St[1] Then Program Tim_Chuoi_Ky_Tu; i := Var Else Filename : String[12]; Begin f : Text; i := i + 1; St : String; If (Ch St[i]) Then Ch : Char; Ok := False; Ok : Boolean; End; i,solan:Integer; End; Begin If Ok Then Write('-Ten tap tin: '); Solan := Solan + 1; Readln(Filename); End; Write('-Nhap chuoi ky tu: '); End; Readln(St); Write('-Chuoi: ',St,' xuat hien : ',solan,' Assign(f,Filename); lan tap tin'); Reset(f); Readln; Solan := 0; Close(f); While NOt EOF(f) Do End Begin 11/ Program Xu_ly_dong; Var f1,f2 : Text; Filename : String[12]; lmax : Integer; Tam,st,dong : String; { } Procedure Catdong(Var st,dong:String;lmax :Integer); Var i : Integer; Begin i := lmax; While st[i] ' ' Do i:= i-1; Dong := copy(st,1,i-1); Delete(St,1,i); End; { } Procedure Lamday(Var dong: String;lmax : Integer); Var i,j : Integer; Begin i := lmax - length(dong); While i Do Begin j := Length(dong); While (j > 1) And (i 0) Do If (dong[j]=' ') And (dong[j-1] ' ') Then Begin Insert(' ',dong,j); j :=j-1; i := i-1; End Else j := j-1; End; End; { } BEGIN Write('-Ten tap tin: '); Readln(Filename); Write('-Chieu dai cua dong: '); Readln(lmax); Assign(f1,filename); Reset(f1); Assign(f2,'new.txt'); Rewrite(f2); St:=' '; While NOt EOF(f1) Do Begin Readln(f1,tam); St := St + Tam + ' '; While length(St) >= lmax Do Begin Catdong(St,dong,lmax); Lamday(dong,lmax); Writeln(f2,dong); End; End; Writeln(f2,St); Writeln; Writeln('Da thuc hien xong, bam '); Readln; reset(f2); While Not EOF(f2) Do Begin Readln(f2,dong); Writeln(dong); End; Writeln; Write(' Xem xong bam '); Readln; Close(f1); Close(f2); END ... Readln(f2,st); Writeln(St); End; Close(f2); Writeln; Write(''Xem xong, ban ''); Readln End 5/ Program Tron_Tap_tin; Uses Crt; Var f1,f2,f3 : File Of Integer; So1,So2 : Integer; i : Integer;... End; Close(f1); Close(f2); Close(f3); Writeln; Write(''Da tron xong, bam ''); Readln END 6/ Program Tao_Tap_Tin_Co_Kieu; Type HocSinh = RECORD Ten : String[7]; Diem : 10; End; Var f : File... -} BEGIN TaoTapTin; Writeln; XemLaiBanGhi; Writeln; Write('' Bam ''); Readln; END 7/ Program Cat_Tap_tin; End; Var { -} f,g1,g2 : File; Procedure TinhTrungDiem; Buf