Bài giảng lập trình nâng cao

62 1 0
Bài giảng lập trình nâng cao

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

LẬP TRÌNH NÂNG CAO KHOA CNTT - 2022 PHAN HỒ DUY PHƯƠNG 3/29/2022 PHAN HỒ DUY PHƯƠNG NỘI DUNG Đệ qui Chuỗi Tập tin Đa luồng Phân tích thuật tốn 3/29/2022 PHAN HỒ DUY PHƯƠNG ĐỆ QUI LẬP TRÌNH NÂNG CAO 3/29/2022 Phan Hồ Duy Phương http://duyphuong.top PHAN HỒ DUY PHƯƠNG ĐỆ QUI Định nghĩa: phương pháp lập trình cho phép hàm gọi lại VD: void Test(){ Test(); } => phải có điểm dừng 3/29/2022 PHAN HỒ DUY PHƯƠNG ĐỆ QUI Ưu điểm: oThuận lợi cho việc biểu diễn toán oLập trình tinh gọn Khuyết điểm: oKhơng tối ưu thời gian oGây tốn nhớ 3/29/2022 PHAN HỒ DUY PHƯƠNG ĐỆ QUI Chương trình đệ qui chia phần: Phần sở: Điểm dừng hay điều kiện thoát khởi chương trình Phần đệ qui: Gọi lại 3/29/2022 PHAN HỒ DUY PHƯƠNG ĐỆ QUI oĐệ qui tuyến tính (Linear Recusion) oĐệ qui nhị phân (Binary Recursion) oĐệ qui lồng (Nested Recursion) oĐệ qui hỗ tương (Mutual Recursion) 3/29/2022 PHAN HỒ DUY PHƯƠNG ĐỆ QUI Đệ qui tuyến tính (Linear Recusion): Mỗi lần thực gọi đệ qui lần VD: int tinhGiaiThua(int n){ if(n==1) return 1; else return n*tinhGiaiThua(n-1); } 3/29/2022 PHAN HỒ DUY PHƯƠNG ĐỆ QUI Đệ qui nhị phân (Binary Recusion): Mỗi lần thực gọi đệ qui hai lần VD: int Combine(int n, int k){ if(k==0||k==n) return 1; else return Combine(n-1,k)+ Combine(n-1,k-1); } 3/29/2022 PHAN HỒ DUY PHƯƠNG ĐỆ QUI Đệ qui lồng (Nested Recusion) VD: int Ackerman(int n, int n){ if(m==0) return n+1; else if(n==0) return Ackerman(m-1,1); else return Ackerman(m-1, Ackerman(m,n-1)); } 3/29/2022 PHAN HỒ DUY PHƯƠNG TẬP TIN oXử lý tập tin oLớp File sử dụng lớp System.IO.File System.IO.FileInfo oLớp File chứa tất phương thức tĩnh, thêm vào lớp File tự động kiểm tra permission tập tin oLớp FileInfo phải tạo thể lớp dùng 3/29/2022 PHAN HỒ DUY PHƯƠNG TẬP TIN oSystem.IO.File oGồm phương thức tĩnh để thực thao tác file Mô tả Thành viên Copy() Sao chép file tới đường dẫn nguồn Create() Tạo file Delete() Xóa file Exists() Trả giá trị Boolean để xác định file có tồn khơng GetAttributes() Trả đối tượng kiểu System.IO.FileAttributes – file ẩn – không 3/29/2022 PHAN HỒ DUY PHƯƠNG TẬP TIN oSystem.IO.File Thành viên Mô tả GetLastAccessTime() Trả đối tượng kiểu DateTime – thời gian truy cập lần sau file GetLastWriteTime() Trả đối tượng kiểu DateTime – thời gian lần đưa nội dung vào file Move() Chuyển file tới đường dẫn xác định Open() Mở file trả đối tượng System.IO.FileStream cho file OpenRead() Mở file để đọc liệu trả đối tượng System.IO.FileStream đọc cho file 3/29/2022 PHAN HỒ DUY PHƯƠNG TẬP TIN oSystem.IO.File Thành viên Mô tả OpenWrite() Mở file để viết nội dung trả đối tượng System.IO.FileStream đọc/viết cho file SetAttributes() Chấp nhận đối tượng System.IO.FileAttributes mà chứa thông tin thuộc tính file GetCreationTime() Trả đối tượng kiểu DateTime – ngày tháng tạo file 3/29/2022 PHAN HỒ DUY PHƯƠNG TẬP TIN oSystem.IO.FileInfo oFileInfo cung cấp số chi tiết tập tin có ổ cứng bạn (kích thước, thuộc tính, thời gian tạo ra) trợ giúp việc tạo xóa bỏ tập tin Thành viên Mơ tả CreationTime Nhận thiết lập thời gian tạo file Directory Trả đối tượng DirectoryInfo – thể thư mục cha file DirectoryName Trả tên thư mục cha file Exists Trả giá trị Boolean – file tồn chưa 3/29/2022 PHAN HỒ DUY PHƯƠNG TẬP TIN oSystem.IO.FileInfo Thành viên Mô tả LastAccessTime Trả đối tượng DataTime – thể thời gian lần viết nội dung sau vào file Length Trả kích thước file Name Trả tên file CopyTo() Sao chép file tới nơi khác Create() Tạo file Delete() Xóa file MoveTo() Chuyển file 3/29/2022 PHAN HỒ DUY PHƯƠNG TẬP TIN oSystem.IO.FileInfo Thành viên Mô tả Open() Mở file với chế độ đọc/viết chia sẻ quyền OpenRead() Mở file để đọc trả đối tượng System.IO.FileStream đọc OpenWrite() Mở file để viết trả đối tượng System.IO.FileStream OpenText() Mở file trả đối tượng System.IO.StreamReader với mã UTF8 từ file tồn Extension Trả phần mở rộng file FullName Trả đường dẫn đầy đủ file 3/29/2022 PHAN HỒ DUY PHƯƠNG TẬP TIN oĐọc ghi file text StreamReader/Writer oLớp StreamReader StreamWriter sử dụng để đọc ghi liệu vào file text (tập tin văn bản) 3/29/2022 PHAN HỒ DUY PHƯƠNG TẬP TIN Đọc file text StreamReader oLớp StreamReader sử dụng để đọc nội dung file text (tập tin văn bản) oĐược kế thừa từ lớp TextReader Các phương thức: oClose() oRead()/ReadLine() oPeek() 3/29/2022 PHAN HỒ DUY PHƯƠNG TẬP TIN Đọc file text StreamReader oVD: StreamReader strReader = new StreamReader(filePath); while (!strReader.EndOfStream){ int c = strReader.Read(); " Console.WriteLine(Convert.ToChar(c)+ "+ char.IsDigit(Convert.ToChar(c))); } 3/29/2022 PHAN HỒ DUY PHƯƠNG TẬP TIN Đọc file text StreamReader oVD: StreamReader strReader = new StreamReader(filePath); string line; while ((line = str.ReadLine())!=null){ Console.WriteLine(line); } 3/29/2022 PHAN HỒ DUY PHƯƠNG TẬP TIN Ghi file text StreamWriter oLớp StreamWriter sử dụng để ghi nội dung file text (tập tin văn bản) oĐược kế thừa từ lớp TextWriter Các phương thức: oClose() oWrite()/WriteLine() 3/29/2022 PHAN HỒ DUY PHƯƠNG TẬP TIN oGhi file text StreamWriter oVD: StreamWriter str = new StreamWriter(filePath,true,Encoding.Unicode); while (true){ char c = Console.ReadKey().KeyChar; if (c == 'q') break; str.Write(c); } str.Close(); 3/29/2022 PHAN HỒ DUY PHƯƠNG TẬP TIN oGhi file text StreamWriter oVD: StreamWriter str = new StreamWriter(filePath,true,Encoding.Unicod e); string s = “Xin chào Mekong Uni"; str.WriteLine(s); str.Close(); 3/29/2022 PHAN HỒ DUY PHƯƠNG TẬP TIN oGhi file text StreamWriter oVD: StreamWriter str = new StreamWriter(filePath,true,Encoding.Unicod e); string s = “Xin chào Mekong Uni"; str.WriteLine(s); str.Close(); 3/29/2022 PHAN HỒ DUY PHƯƠNG ... 3/29/2022 PHAN HỒ DUY PHƯƠNG ĐỆ QUI LẬP TRÌNH NÂNG CAO 3/29/2022 Phan Hồ Duy Phương http://duyphuong.top PHAN HỒ DUY PHƯƠNG ĐỆ QUI Định nghĩa: phương pháp lập trình cho phép hàm gọi lại VD: void... tốn oLập trình tinh gọn Khuyết điểm: oKhơng tối ưu thời gian oGây tốn nhớ 3/29/2022 PHAN HỒ DUY PHƯƠNG ĐỆ QUI Chương trình đệ qui chia phần: Phần sở: Điểm dừng hay điều kiện khởi chương trình. .. luồng Phân tích thuật tốn 3/29/2022 PHAN HỒ DUY PHƯƠNG CHUỖI VÀ CÁC THAO TÁC TRÊN CHUỖI LẬP TRÌNH NÂNG CAO 3/29/2022 Phan Hồ Duy Phương http://duyphuong.top PHAN HỒ DUY PHƯƠNG CHUỖI KHÁI NIỆM

Ngày đăng: 29/12/2022, 16:00