Tải bản đầy đủ (.ppt) (23 trang)

Bai 16 Vi du lam viec voi tep

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 (303.22 KB, 23 trang )

<span class='text_page_counter'>(1)</span>

<span class='text_page_counter'>(2)</span> 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ụ? Một số hàm chuẩn thường dùng khi đọc/ghi tệp văn bản? Viết cú pháp đóng tệp? Lấy ví dụ?.

<span class='text_page_counter'>(3)</span> Đáp án Câu 1: - Khai báo biến tệp: Var <tên biến tệp> : text; Ví dụ: Var tep1,tep2: text; - Gắn tên tệp: assign (<biến tệp>,<tên tệp>); Ví dụ: assign(tep1, ‘dulieu.in’); assign(tep2, ‘ketqua.out’); - Mở tệp: + Mở tệp để đọc: reset(<biến tệp>); Ví dụ: reset(tep1); + Mở tệp để ghi: rewrite(<biến tệp>); Ví dụ: rewrite(tep2);.

<span class='text_page_counter'>(4)</span> Đáp án Câu 2: Đọc/ghi tệp văn bản: Đọc tệp: read(<biến tệp>,<danh sách biến>); hoặc readln(<biến tệp>,<danh sách biến>); Ví dụ: read(tep1,a,b); Ghi tệp: write(<biến tệp>,<danh sách kết quả>); hoặc writeln(<biến tệp>,<danh sách kết quả>); Ví dụ: write(tep2, ‘Tong la: ’, t);.

<span class='text_page_counter'>(5)</span> Đáp án Câu 2: Một số hàm chuẩn trong đọc/ghi tệp văn bản: + Hàm eof(<biến tệp>) trả về giá trị true khi con trỏ tệp đang chỉ tới cuối tệp. + Hàm eoln(<biến tệp>) trả về giá trị true khi con trỏ tệp đang chỉ tới cuối dòng. Đóng tệp: close(<biến tệp>); Ví dụ: close(tep1); close(tep2);.

<span class='text_page_counter'>(6)</span>

<span class='text_page_counter'>(7)</span> Ví dụ 1: Nhập vào một xâu, ghi xâu vừa nhập ra tệp ketqua.txt. Xác định bài toán * Input: + Xâu được nhập từ bàn phím * Output: + Tệp ketqua.txt chứa xâu vừa nhập.

<span class='text_page_counter'>(8)</span> Ví dụ 1: Nhập vào một xâu, ghi xâu vừa nhập ra tệp ketqua.txt. Xây dựng thuật toán ? B1. Nhập xâu B2. Ghi xâu ra tệp ketqua.txt.

<span class='text_page_counter'>(9)</span> Ví dụ 1: Nhập vào một xâu, ghi xâu vừa nhập ra tệp ketqua.txt Trình tự các bước thực hiện. - Khai báo biến biến xâu s - Khai báo biến tệp f để ghi dữ liệu ra tệp. -Gắn biến tệp f với tệp ‘ketqua.txt’ - Mở tệp để ghi dữ liệu. - Nhập xâu s - Ghi xâu vừa nhập ra tệp ketqua.txt - Đóng tệp..

<span class='text_page_counter'>(10)</span> program khoang_cach; var. Khai báo biến tệp. s:string; f:text;. begin. Gắn tên tệp. assign(f,‘ketqua.txt'); Mở tệp để ghi. rewrite(f); write(‘nhap xau: ’);. readln(s);. writeln(f,‘ Xau moi nhap la: ’,s); close(f); end.. Đóng tệp. Ghi tệp.

<span class='text_page_counter'>(11)</span>

<span class='text_page_counter'>(12)</span> program giaiptbac1; Câu 1: Chương trình bên có tên là gì? var f1, f2 : text; Chương trình có sử dụng kiểu dữ liệu a, b : integer; tệp không? x : real; begin assign(f1, ‘input.txt'); reset(f1); assign(f2, ‘output.txt'); rewrite(f2); while not eof(f1) do begin Tên chương trình là giaiptbac1 read(f1,a,b); Chương trình có sử x:=-b/a; dụng kiểu dữ liệu tệp writeln(f2,‘nghiem la: ',x:7:1); end; close(f1); close(f2); end..

<span class='text_page_counter'>(13)</span> program giaiptbac1; var f1, f2 : text; Câu 2: Chương trình sử dụng bao nhiêu tệp, biến tệp? Kể tên? a, b : integer; x : real; begin assign(f1, ‘input.txt'); reset(f1); assign(f2, ‘output.txt'); rewrite(f2); while not eof(f1) do begin 2 tệp và 2 biến tệp Tệp là input.txt và read(f1,a,b); output.txt x:=-b/a; Biến tệp là f1 và f2 writeln(f2,‘nghiem la: ',x:7:1); end; close(f1); close(f2); end..

<span class='text_page_counter'>(14)</span> program giaiptbac1; Câu 3: Tệp input.txt được gắn cho var f1, f2 : text; biến tệp nào? Tệp input.txt dùng để a, b : integer; đọc hay ghi dữ liệu? x : real; begin assign(f1, ‘input.txt'); reset(f1); assign(f2, ‘output.txt'); rewrite(f2); while not eof(f1) do begin Tệp input.txt được gắn cho biến tệp f1. read(f1,a,b); Tệp input.txt dùng để x:=-b/a; đọc dữ liệu. writeln(f2,‘nghiem la: ',x:7:1); end; close(f1); close(f2); end..

<span class='text_page_counter'>(15)</span> program giaiptbac1; Câu 4: Câu lệnh gắn tên tệp và mở tệp var f1, f2 : text; của tệp input.txt? a, b : integer; x : real; begin assign(f1, ‘input.txt'); reset(f1); assign(f2, ‘output.txt'); rewrite(f2); while not eof(f1) do begin assign(f1,‘input.txt'); read(f1,a,b); reset(f1); x:=-b/a; writeln(f2,‘nghiem la: ',x:7:1); end; close(f1); close(f2); end..

<span class='text_page_counter'>(16)</span> program giaiptbac1; Câu 5: Tệp output.txt được gắn cho var f1, f2 : text; biến tệp nào? Tệp output.txt dùng để a, b : integer; đọc hay ghi dữ liệu? x : real; begin assign(f1, ‘input.txt'); reset(f1); assign(f2, ‘output.txt'); rewrite(f2); while not eof(f1) do begin Tệp output.txt được gắn cho biến tệp f2. read(f1,a,b); Tệp output.txt dùng x:=-b/a; để ghi dữ liệu. writeln(f2,‘nghiem la: ',x:7:1); end; close(f1); close(f2); end..

<span class='text_page_counter'>(17)</span> program giaiptbac1; Câu 6: Câu lệnh gắn tên tệp và mở tệp var f1, f2 : text; của tệp output.txt? a, b : integer; x : real; begin assign(f1, ‘input.txt'); reset(f1); assign(f2, ‘output.txt'); rewrite(f2); while not eof(f1) do begin assign(f2,‘output.txt'); read(f1,a,b); rewrite(f2); x:=-b/a; writeln(f2,‘nghiem la: ',x:7:1); end; close(f1); close(f2); end..

<span class='text_page_counter'>(18)</span> program giaiptbac1; Câu 7: Câu lệnh đọc tệp? var f1, f2 : text; Câu lệnh ghi tệp a, b : integer; x : real; begin assign(f1, ‘input.txt'); reset(f1); assign(f2, ‘output.txt'); rewrite(f2); while not eof(f1) do Đọc tệp: begin Read(f1,a,b); read(f1,a,b); Ghi tệp: x:=-b/a; Writeln(f2,’nghiem la:’,x:7:1); writeln(f2,‘nghiem la: ',x:7:1); end; close(f1); close(f2); end..

<span class='text_page_counter'>(19)</span> program giaiptbac1; Câu 8: Tệp input.txt lưu trữ dữ liệu var f1, f2 : text; gì? a, b : integer; x : real; begin assign(f1, ‘input.txt'); reset(f1); assign(f2, ‘output.txt'); rewrite(f2); while not eof(f1) do begin Tệp input.txt chứa liên tiếp các cặp số read(f1,a,b); nguyên x:=-b/a; writeln(f2,‘nghiem la: ',x:7:1); end; close(f1); close(f2); end..

<span class='text_page_counter'>(20)</span> program giaiptbac1; Câu 9: Điều kiện để tham chiếu đến var f1, f2 : text; tất cả các cặp số nguyên trong tệp a, b : integer; input.txt? x : real; begin assign(f1, ‘input.txt'); reset(f1); assign(f2, ‘output.txt'); rewrite(f2); while not eof(f1) do begin not eof(f1) read(f1,a,b); x:=-b/a; writeln(f2,‘nghiem la: ',x:7:1); end; close(f1); close(f2); end..

<span class='text_page_counter'>(21)</span> program giaiptbac1; Câu 10: Chương trình sử dụng bao var f1, f2 : text; nhiêu câu lệnh đóng tệp? a, b : integer; Không đóng tệp được không? Vì sao? x : real; begin assign(f1, ‘input.txt'); reset(f1); assign(f2, ‘output.txt'); rewrite(f2); while not eof(f1) do 2 câu lệnh đóng tệp begin Không! Vì sau khi read(f1,a,b); đóng tệp hệ thống mới x:=-b/a; hoàn tất việc ghi dữ liệu ra tệp. writeln(f2,‘nghiem la: ',x:7:1); end; close(f1); close(f2); end..

<span class='text_page_counter'>(22)</span> program giaiptbac1; Câu 11: Chương trình bên giải quyết var f1, f2 : text; bài toán gì? Giải quyết như thế nào? a, b : integer; x : real; begin assign(f1, ‘input.txt'); reset(f1); assign(f2, ‘output.txt'); rewrite(f2); while not eof(f1) do Giải phương trình bậc nhất begin Đọc các cặp cơ số từ tệp read(f1,a,b); input.txt, tính nghiệm và x:=-b/a; ghi kết quả ra tệp writeln(f2,‘nghiem la: ',x:7:1); output.txt end; close(f1); close(f2); end..

<span class='text_page_counter'>(23)</span>

<span class='text_page_counter'>(24)</span>

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×