Bài 16. Ví dụ làm việc với tệp
Chia sẻ bởi Nguyễn Ngọc Phú |
Ngày 10/05/2019 |
69
Chia sẻ tài liệu: Bài 16. Ví dụ làm việc với tệp thuộc Tin học 11
Nội dung tài liệu:
Tin học 11 - Chương 4 - Bài 11:Kiểu mảng
3/21/2018
Slide 1
WELCOME TO MY CLASS!
Ly Thuong Kiet High School
Informatics 11
________________________
teacher: Nguy?n Ng?c Ph
mail: [email protected]
cđu h?i: http://link.fpo.vn/giaosinh
uebsite: https://tinhocnhe.gnomio.com/
KHÔNG ĐIỆN THOẠI
KHÔNG TÁM CHUYỆN
KHÔNG NÓI LEO
Kiểm tra bài cũ
Câu 1:
Viết cú pháp khai báo biến tệp? Lấy ví dụ?
Viết cú pháp gắn tên tệp và mở tệp? Lấy ví dụ?
Câu 2:
Viết cú pháp đọc/ghi tệp văn bản? Lấy ví dụ?
Viết cú pháp đóng tệp? Lấy ví dụ?
Đáp án
Câu 1:
- Khai báo biến tệp: Var : text;
Ví dụ: Var tep1,tep2: text;
- Gắn tên tệp: assign (,);
Ví dụ: assign(tep1, ‘dulieu.in’);
assign(tep2, ‘ketqua.out’);
- Mở tệp:
+ Mở tệp để đọc: reset();
Ví dụ: reset(tep1);
+ Mở tệp để ghi: rewrite();
Ví dụ: rewrite(tep2);
Đáp án
Câu 1:
- Khai báo biến tệp: Var : text;
Ví dụ: Var tep1,tep2: text;
- Ví dụ gắn tên tệp và mở tệp:
assign(tep1, ‘dulieu.in’);
reset(tep1);
assign(tep2, ‘ketqua.out’);
rewrite(tep2);
Đáp án
Câu 2:
Đọc/ghi tệp văn bản:
Đọc tệp: read(,);
hoặc readln(,);
Ví dụ: read(tep1,a,b);
Ghi tệp: write(,);
hoặc writeln(,);
Ví dụ: write(tep2, ‘Tong la: ’, t);
Đáp án
Câu 2:
Đóng tệp: close();
Ví dụ: close(tep1);
close(tep2);
Đọc
Ghi
Ôn tập và ghi nhớ
assign(,);
rewrite();
reset();
read(,);
write(,);
close();
Bài 16 VÍ DỤ VỀ TỆP
Bài 1: Đọc vào một dãy n số nguyên (n<=100), ghi dãy số đó vào tệp có tên BAITAP.INP
- Khai báo;
Program bai1;
Uses crt;
Var
tep2: TEXT;
A:ARRAY[1 .. 100] of Integer;
i,n:Integer;
- Gán tên tệp;
Begin
Clrscr;
ASSIGN(tep2, `C:\BAITAP.INP’);
- Mở tệp ra để ghi;
REWRITE (tep2);
Các bưU?C:
Thể hiện bằng pascal
- Nhập số lUượng phần tử trong mảng;
For i:=1 to n do
Begin
Write(`Doc so thu `,i,`=`);
Readln(A[i]);
Write(tep2,A[i]);
End;
Write(‘Nhap n = `);Readln(n);
- Đọc vào mảng;
- Ghi ra tệp BAITAP.INP;
- Đóng tệp.
Close(tep2);
Readln;
END.
Nhap vao bao nhieu so :
6
Nhap so thu 1 =
5
Nhap so thu 2 =
7
Nhap so thu 3 =
20
Nhap so thu 4 =
15
Nhap so thu 5 =
1
Nhap so thu 6 =
8
-
- Khai báo;
Program Bai2;
Uses crt;
Var
tep: TEXT;
A:ARRAY[1 .. 100] of Integer;
S,i,n:Integer;
- Gán tên tệp;
Begin
Clrscr;
ASSIGN(tep, `D:\TP\BAITAP.INP’);
- Mở tệp ra để đọc;
RESET(tep);
Bài 2: Mở tệp BAITAP.INP, in dãy số trong tệp ra màn hình. Tính tổng của dãy số đó.
Các bước:
Thể hiện bằng pascal
- Gán tổng của dãy = 0;
Write(A[i]:5);
S:=S+A[i];
End;
S:=0;
- Dãy số nguyên không biết bao nhiêu số, nhưUng vẫn đọc ra được bằng cách dùng hàm EOF(tên biến tệp);
- Đóng tệp.
Close(tep);
Readln;
END.
- Đọc dữ liệu từ file BAITAP.INP;
- In ra màn hình dãy số;
- Tính tổng;
Writeln(`Tong cua day so la : `,S);
WHILE NOT(EOF(tep)) DO
Begin
Readln(tep,A[i]);
Writeln;
BÀI TẬP 3
program hinhchunhat;
var f1, f2 : text;
a, b : integer;
s : real;
begin
assign(f1, ‘input.txt`); reset(f1);
assign(f2, ‘output.txt`); rewrite(f2);
while not eof(f1) do
begin
read(f1,a,b);
s:=(a+b)*2;
writeln(f2,‘dien tich la: `,s:7:1);
end;
close(f1); close(f2);
end.
Câu 1: Chương trình bên có tên là gì?
Chương trình có sử dụng kiểu dữ liệu tệp không?
Tên chương trình là hinhchunhat
Chương trình có sử dụng kiểu dữ liệu tệp
Câu 2: Chương trình sử dụng bao nhiêu tệp, biến tệp? Kể tên?
2 tệp và 2 biến tệp
Tệp là input.txt và output.txt
Biến tệp là f1 và f2
program hinhchunhat;
var f1, f2 : text;
a, b : integer;
s : real;
begin
assign(f1, ‘input.txt`); reset(f1);
assign(f2, ‘output.txt`); rewrite(f2);
while not eof(f1) do
begin
read(f1,a,b);
s:=(a+b)*2;
writeln(f2,‘dien tich la: `,s:7:1);
end;
close(f1); close(f2);
end.
Câu 3: Tệp input.txt được gắn cho biến tệp nào? Tệp input.txt dùng để đọc hay ghi dữ liệu?
Tệp input.txt được gắn cho biến tệp f1.
Tệp input.txt dùng để đọc dữ liệu.
program hinhchunhat;
var f1, f2 : text;
a, b : integer;
s : real;
begin
assign(f1, ‘input.txt`); reset(f1);
assign(f2, ‘output.txt`); rewrite(f2);
while not eof(f1) do
begin
read(f1,a,b);
s:=(a+b)*2;
writeln(f2,‘dien tich la: `,s:7:1);
end;
close(f1); close(f2);
end.
Câu 4: Câu lệnh gắn tên tệp và mở tệp của tệp input.txt?
assign(f1,‘input.txt`);
reset(f1);
program hinhchunhat;
var f1, f2 : text;
a, b : integer;
s : real;
begin
assign(f1, ‘input.txt`); reset(f1);
assign(f2, ‘output.txt`); rewrite(f2);
while not eof(f1) do
begin
read(f1,a,b);
s:=(a+b)*2;
writeln(f2,‘dien tich la: `,s:7:1);
end;
close(f1); close(f2);
end.
Câu 5: Tệp output.txt được gắn cho biến tệp nào? Tệp output.txt dùng để đọc hay ghi dữ liệu?
Tệp output.txt được gắn cho biến tệp f2.
Tệp output.txt dùng để ghi dữ liệu.
program hinhchunhat;
var f1, f2 : text;
a, b : integer;
s : real;
begin
assign(f1, ‘input.txt`); reset(f1);
assign(f2, ‘output.txt`); rewrite(f2);
while not eof(f1) do
begin
read(f1,a,b);
s:=(a+b)*2;
writeln(f2,‘dien tich la: `,s:7:1);
end;
close(f1); close(f2);
end.
Câu 6: Câu lệnh gắn tên tệp và mở tệp của tệp output.txt?
assign(f2,‘output.txt`);
rewrite(f2);
program hinhchunhat;
var f1, f2 : text;
a, b : integer;
s : real;
begin
assign(f1, ‘input.txt`); reset(f1);
assign(f2, ‘output.txt`); rewrite(f2);
while not eof(f1) do
begin
read(f1,a,b);
s:=(a+b)*2;
writeln(f2,‘dien tich la: `,s:7:1);
end;
close(f1); close(f2);
end.
Câu 7: Câu lệnh đọc tệp?
Câu lệnh ghi tệp
Đọc tệp:
Read(f1,a,b);
Ghi tệp:
Writeln(f2,’dien tich la:’,s:7:1);
program hinhchunhat;
var f1, f2 : text;
a, b : integer;
s : real;
begin
assign(f1, ‘input.txt`); reset(f1);
assign(f2, ‘output.txt`); rewrite(f2);
while not eof(f1) do
begin
read(f1,a,b);
s:=(a+b)*2;
writeln(f2,‘dien tich la: `,s:7:1);
end;
close(f1); close(f2);
end.
Câu 8: Tệp input.txt lưu trữ dữ liệu gì?
Tệp input.txt chứa liên tiếp các cặp số nguyên
program hinhchunhat;
var f1, f2 : text;
a, b : integer;
s : real;
begin
assign(f1, ‘input.txt`); reset(f1);
assign(f2, ‘output.txt`); rewrite(f2);
while not eof(f1) do
begin
read(f1,a,b);
s:=(a+b)*2;
writeln(f2,‘dien tich la: `,s:7:1);
end;
close(f1); close(f2);
end.
Câu 9: Điều kiện để tham chiếu đến tất cả các cặp số nguyên trong tệp input.txt?
not eof(f1)
program hinhchunhat;
var f1, f2 : text;
a, b : integer;
s : real;
begin
assign(f1, ‘input.txt`); reset(f1);
assign(f2, ‘output.txt`); rewrite(f2);
while not eof(f1) do
begin
read(f1,a,b);
s:=(a+b)*2;
writeln(f2,‘dien tich la: `,s:7:1);
end;
close(f1); close(f2);
end.
Câu 10: Chương trình sử dụng bao nhiêu câu lệnh đóng tệp?
Không đóng tệp được không? Vì sao?
2 câu lệnh đóng tệp
Không! Vì sau khi đóng tệp hệ thống mới hoàn tất việc ghi dữ liệu ra tệp.
program hinhchunhat;
var f1, f2 : text;
a, b : integer;
s : real;
begin
assign(f1, ‘input.txt`); reset(f1);
assign(f2, ‘output.txt`); rewrite(f2);
while not eof(f1) do
begin
read(f1,a,b);
s:=(a+b)*2;
writeln(f2,‘dien tich la: `,s:7:1);
end;
close(f1); close(f2);
end.
Câu 11: Chương trình bên giải quyết bài toán gì? Bằng cách nào?
Tính diện tích hình chữ nhật
Bằng cách đọc các cặp số nguyên (là độ dài 2 cạnh) từ tệp input.txt, tính diện tích và ghi kết quả ra tệp output.txt
program hinhchunhat;
var f1, f2 : text;
a, b : integer;
s : real;
begin
assign(f1, ‘input.txt`); reset(f1);
assign(f2, ‘output.txt`); rewrite(f2);
while not eof(f1) do
begin
read(f1,a,b);
s:=(a+b)*2;
writeln(f2,‘dien tich la: `,s:7:1);
end;
close(f1); close(f2);
end.
GV: Lê Thị Xuân Huyền
3/21/2018
Slide 1
WELCOME TO MY CLASS!
Ly Thuong Kiet High School
Informatics 11
________________________
teacher: Nguy?n Ng?c Ph
mail: [email protected]
cđu h?i: http://link.fpo.vn/giaosinh
uebsite: https://tinhocnhe.gnomio.com/
KHÔNG ĐIỆN THOẠI
KHÔNG TÁM CHUYỆN
KHÔNG NÓI LEO
Kiểm tra bài cũ
Câu 1:
Viết cú pháp khai báo biến tệp? Lấy ví dụ?
Viết cú pháp gắn tên tệp và mở tệp? Lấy ví dụ?
Câu 2:
Viết cú pháp đọc/ghi tệp văn bản? Lấy ví dụ?
Viết cú pháp đóng tệp? Lấy ví dụ?
Đáp án
Câu 1:
- Khai báo biến tệp: Var
Ví dụ: Var tep1,tep2: text;
- Gắn tên tệp: assign (
Ví dụ: assign(tep1, ‘dulieu.in’);
assign(tep2, ‘ketqua.out’);
- Mở tệp:
+ Mở tệp để đọc: reset(
Ví dụ: reset(tep1);
+ Mở tệp để ghi: rewrite(
Ví dụ: rewrite(tep2);
Đáp án
Câu 1:
- Khai báo biến tệp: Var
Ví dụ: Var tep1,tep2: text;
- Ví dụ gắn tên tệp và mở tệp:
assign(tep1, ‘dulieu.in’);
reset(tep1);
assign(tep2, ‘ketqua.out’);
rewrite(tep2);
Đáp án
Câu 2:
Đọc/ghi tệp văn bản:
Đọc tệp: read(
hoặc readln(
Ví dụ: read(tep1,a,b);
Ghi tệp: write(
hoặc writeln(
Ví dụ: write(tep2, ‘Tong la: ’, t);
Đáp án
Câu 2:
Đóng tệp: close(
Ví dụ: close(tep1);
close(tep2);
Đọc
Ghi
Ôn tập và ghi nhớ
assign(
rewrite(
reset(
read(
write(
close(
Bài 16 VÍ DỤ VỀ TỆP
Bài 1: Đọc vào một dãy n số nguyên (n<=100), ghi dãy số đó vào tệp có tên BAITAP.INP
- Khai báo;
Program bai1;
Uses crt;
Var
tep2: TEXT;
A:ARRAY[1 .. 100] of Integer;
i,n:Integer;
- Gán tên tệp;
Begin
Clrscr;
ASSIGN(tep2, `C:\BAITAP.INP’);
- Mở tệp ra để ghi;
REWRITE (tep2);
Các bưU?C:
Thể hiện bằng pascal
- Nhập số lUượng phần tử trong mảng;
For i:=1 to n do
Begin
Write(`Doc so thu `,i,`=`);
Readln(A[i]);
Write(tep2,A[i]);
End;
Write(‘Nhap n = `);Readln(n);
- Đọc vào mảng;
- Ghi ra tệp BAITAP.INP;
- Đóng tệp.
Close(tep2);
Readln;
END.
Nhap vao bao nhieu so :
6
Nhap so thu 1 =
5
Nhap so thu 2 =
7
Nhap so thu 3 =
20
Nhap so thu 4 =
15
Nhap so thu 5 =
1
Nhap so thu 6 =
8
-
- Khai báo;
Program Bai2;
Uses crt;
Var
tep: TEXT;
A:ARRAY[1 .. 100] of Integer;
S,i,n:Integer;
- Gán tên tệp;
Begin
Clrscr;
ASSIGN(tep, `D:\TP\BAITAP.INP’);
- Mở tệp ra để đọc;
RESET(tep);
Bài 2: Mở tệp BAITAP.INP, in dãy số trong tệp ra màn hình. Tính tổng của dãy số đó.
Các bước:
Thể hiện bằng pascal
- Gán tổng của dãy = 0;
Write(A[i]:5);
S:=S+A[i];
End;
S:=0;
- Dãy số nguyên không biết bao nhiêu số, nhưUng vẫn đọc ra được bằng cách dùng hàm EOF(tên biến tệp);
- Đóng tệp.
Close(tep);
Readln;
END.
- Đọc dữ liệu từ file BAITAP.INP;
- In ra màn hình dãy số;
- Tính tổng;
Writeln(`Tong cua day so la : `,S);
WHILE NOT(EOF(tep)) DO
Begin
Readln(tep,A[i]);
Writeln;
BÀI TẬP 3
program hinhchunhat;
var f1, f2 : text;
a, b : integer;
s : real;
begin
assign(f1, ‘input.txt`); reset(f1);
assign(f2, ‘output.txt`); rewrite(f2);
while not eof(f1) do
begin
read(f1,a,b);
s:=(a+b)*2;
writeln(f2,‘dien tich la: `,s:7:1);
end;
close(f1); close(f2);
end.
Câu 1: Chương trình bên có tên là gì?
Chương trình có sử dụng kiểu dữ liệu tệp không?
Tên chương trình là hinhchunhat
Chương trình có sử dụng kiểu dữ liệu tệp
Câu 2: Chương trình sử dụng bao nhiêu tệp, biến tệp? Kể tên?
2 tệp và 2 biến tệp
Tệp là input.txt và output.txt
Biến tệp là f1 và f2
program hinhchunhat;
var f1, f2 : text;
a, b : integer;
s : real;
begin
assign(f1, ‘input.txt`); reset(f1);
assign(f2, ‘output.txt`); rewrite(f2);
while not eof(f1) do
begin
read(f1,a,b);
s:=(a+b)*2;
writeln(f2,‘dien tich la: `,s:7:1);
end;
close(f1); close(f2);
end.
Câu 3: Tệp input.txt được gắn cho biến tệp nào? Tệp input.txt dùng để đọc hay ghi dữ liệu?
Tệp input.txt được gắn cho biến tệp f1.
Tệp input.txt dùng để đọc dữ liệu.
program hinhchunhat;
var f1, f2 : text;
a, b : integer;
s : real;
begin
assign(f1, ‘input.txt`); reset(f1);
assign(f2, ‘output.txt`); rewrite(f2);
while not eof(f1) do
begin
read(f1,a,b);
s:=(a+b)*2;
writeln(f2,‘dien tich la: `,s:7:1);
end;
close(f1); close(f2);
end.
Câu 4: Câu lệnh gắn tên tệp và mở tệp của tệp input.txt?
assign(f1,‘input.txt`);
reset(f1);
program hinhchunhat;
var f1, f2 : text;
a, b : integer;
s : real;
begin
assign(f1, ‘input.txt`); reset(f1);
assign(f2, ‘output.txt`); rewrite(f2);
while not eof(f1) do
begin
read(f1,a,b);
s:=(a+b)*2;
writeln(f2,‘dien tich la: `,s:7:1);
end;
close(f1); close(f2);
end.
Câu 5: Tệp output.txt được gắn cho biến tệp nào? Tệp output.txt dùng để đọc hay ghi dữ liệu?
Tệp output.txt được gắn cho biến tệp f2.
Tệp output.txt dùng để ghi dữ liệu.
program hinhchunhat;
var f1, f2 : text;
a, b : integer;
s : real;
begin
assign(f1, ‘input.txt`); reset(f1);
assign(f2, ‘output.txt`); rewrite(f2);
while not eof(f1) do
begin
read(f1,a,b);
s:=(a+b)*2;
writeln(f2,‘dien tich la: `,s:7:1);
end;
close(f1); close(f2);
end.
Câu 6: Câu lệnh gắn tên tệp và mở tệp của tệp output.txt?
assign(f2,‘output.txt`);
rewrite(f2);
program hinhchunhat;
var f1, f2 : text;
a, b : integer;
s : real;
begin
assign(f1, ‘input.txt`); reset(f1);
assign(f2, ‘output.txt`); rewrite(f2);
while not eof(f1) do
begin
read(f1,a,b);
s:=(a+b)*2;
writeln(f2,‘dien tich la: `,s:7:1);
end;
close(f1); close(f2);
end.
Câu 7: Câu lệnh đọc tệp?
Câu lệnh ghi tệp
Đọc tệp:
Read(f1,a,b);
Ghi tệp:
Writeln(f2,’dien tich la:’,s:7:1);
program hinhchunhat;
var f1, f2 : text;
a, b : integer;
s : real;
begin
assign(f1, ‘input.txt`); reset(f1);
assign(f2, ‘output.txt`); rewrite(f2);
while not eof(f1) do
begin
read(f1,a,b);
s:=(a+b)*2;
writeln(f2,‘dien tich la: `,s:7:1);
end;
close(f1); close(f2);
end.
Câu 8: Tệp input.txt lưu trữ dữ liệu gì?
Tệp input.txt chứa liên tiếp các cặp số nguyên
program hinhchunhat;
var f1, f2 : text;
a, b : integer;
s : real;
begin
assign(f1, ‘input.txt`); reset(f1);
assign(f2, ‘output.txt`); rewrite(f2);
while not eof(f1) do
begin
read(f1,a,b);
s:=(a+b)*2;
writeln(f2,‘dien tich la: `,s:7:1);
end;
close(f1); close(f2);
end.
Câu 9: Điều kiện để tham chiếu đến tất cả các cặp số nguyên trong tệp input.txt?
not eof(f1)
program hinhchunhat;
var f1, f2 : text;
a, b : integer;
s : real;
begin
assign(f1, ‘input.txt`); reset(f1);
assign(f2, ‘output.txt`); rewrite(f2);
while not eof(f1) do
begin
read(f1,a,b);
s:=(a+b)*2;
writeln(f2,‘dien tich la: `,s:7:1);
end;
close(f1); close(f2);
end.
Câu 10: Chương trình sử dụng bao nhiêu câu lệnh đóng tệp?
Không đóng tệp được không? Vì sao?
2 câu lệnh đóng tệp
Không! Vì sau khi đóng tệp hệ thống mới hoàn tất việc ghi dữ liệu ra tệp.
program hinhchunhat;
var f1, f2 : text;
a, b : integer;
s : real;
begin
assign(f1, ‘input.txt`); reset(f1);
assign(f2, ‘output.txt`); rewrite(f2);
while not eof(f1) do
begin
read(f1,a,b);
s:=(a+b)*2;
writeln(f2,‘dien tich la: `,s:7:1);
end;
close(f1); close(f2);
end.
Câu 11: Chương trình bên giải quyết bài toán gì? Bằng cách nào?
Tính diện tích hình chữ nhật
Bằng cách đọc các cặp số nguyên (là độ dài 2 cạnh) từ tệp input.txt, tính diện tích và ghi kết quả ra tệp output.txt
program hinhchunhat;
var f1, f2 : text;
a, b : integer;
s : real;
begin
assign(f1, ‘input.txt`); reset(f1);
assign(f2, ‘output.txt`); rewrite(f2);
while not eof(f1) do
begin
read(f1,a,b);
s:=(a+b)*2;
writeln(f2,‘dien tich la: `,s:7:1);
end;
close(f1); close(f2);
end.
GV: Lê Thị Xuân Huyền
* Một số tài liệu cũ có thể bị lỗi font khi hiển thị do dùng bộ mã không phải Unikey ...
Người chia sẻ: Nguyễn Ngọc Phú
Dung lượng: |
Lượt tài: 0
Loại file:
Nguồn : Chưa rõ
(Tài liệu chưa được thẩm định)