Mẩu tin – tập tin

Cho phép phối hợp các kiểu dữ liệu cơ bản trong cùng 1 tổ chức dữ liệu để thể hiện các đối tượng trong thực tế

Ví dụ

Sinh viên : họ tên, ngày sinh, điểm TB

Hóa đơn : mã số HĐ, ngày ghi, danh sách dòng hóa đơn, tổng tiền,.

 

ppt70 trang | Chia sẻ: Mr Hưng | Lượt xem: 870 | Lượt tải: 0download
Bạn đang xem trước 20 trang nội dung tài liệu Mẩu tin – tập tin, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
MẨU TIN – TẬP TIN 8/28/2016Nội Dung Trình BàyMẩu tinXử lý tập tinMột số công cụ xây dựng của Studio NETDateCấu trúc mẫu tinCho phép phối hợp các kiểu dữ liệu cơ bản trong cùng 1 tổ chức dữ liệu để thể hiện các đối tượng trong thực tếVí dụSinh viên : họ tên, ngày sinh, điểm TBHóa đơn : mã số HĐ, ngày ghi, danh sách dòng hóa đơn, tổng tiền,...DateTổ chức trong PascalKhai báo kiểuType = Record : ; . : ; End;Khai báo biếnVar : ; DateVí dụType Ngay = Record ngay, thang, nam:integer; End; HocSinh= Record hoten:String; ngaysinh : Ngay; End;Var a,b : Ngay ; x,y : HocSinh; list : Array[1..10] of HocSinh; DateTổ chức trong CKhai báo kiểustruct { ; . ; };Khai báo biến ;DateVí Dụstruct Ngay {int ngay, thang, nam;}; struct HocSinh { char hoten[30]; Ngay ngaysinh; };Khai báo biến Ngay a,b; HocSinh x,y, list[10] ;DateCần phải viết các thủ tục/hàm để xử lý các yêu cầuVí dụIn lý lịch học sinhTính tuổi hiện tại...DateCài đặtProcedure InLyLich (A:HocSinh);Begin Writeln(“Ho ten : ” + A.HoTen); Writeln(“Ngay Sinh : ” + A.Ngay.Ngay+”/”+A.Ngay.Thang+”/”+A.Ngay.Nam);End;Funtion TuoiHienTai(X:HocSinh):Integer;Begin TuoiHienTai:= 2006 - A.Ngay.Nam;End; DateSử dụngInLyLich(X);For i:=1 to 10 do InLyLich(List[i]);Writeln(“Nam nay duoc “+TuoiHienTai(X) +” tuoi “);For i:=1 to 10 do Writeln(List[i].Hoten, TuoiHienTai(list[i]));DateÝ tưởng của OOPCác xử lý gắn liền với dữ liệuMã giảVar X : HocSinh X.InLyLich X.TuoiHienTaiVar List : Array[1..10] of HocSinhFor i=1 to 10 do List[i].InLyLichDateKhai báo cấu trúc trong C#Cú pháppublic struct { public ; public ; ......... public ; }DateVí dụstruct HOCSINH { internal string ht; internal int ns; internal float[] diem; }Lưu ý :- Nên dùng public- Có thể dùng 1 trong các từ :public,private,internal,privateDateMinh họa : ViDu4_1a.csusing System;namespace ConsoleApplication1{ struct HOCSINH { internal string ht; internal int ns; internal float[ ] diem; }DateMinh họa : ViDu4_1a.cs public class Vidu4_1a { public static void Main() { HOCSINH x ; x.diem = new float[3]; Console.Write("Nhap ho ten : "); x.ht=Console.ReadLine(); Console.Write("Nhap nam sinh : "); x.ns=Int32.Parse(Console.ReadLine()); for (int i=0;i<x.diem.Length;i++) { Console.Write("Nhap diem mon thu {0} : ",i+1); x.diem[i]=Single.Parse(Console.ReadLine()); }Date Console.WriteLine("Ho Ten : {0}" ,x.ht); Console.WriteLine("Nam Sinh : {0}",x.ns); for (int i=0;i<x.diem.Length;i++) Console.Write("{0} ",x.diem[i]); } // Main } // class} //name spaceLưu ý : Với vùng tin kiểu mảng : cần khởi tạo trước khi sử dụngDateDateMinh họa : ViDu4_1.csTrong ví dụ này ta sử dụng các phương thức xem như các “hàm xử lý, các chương trình con” truyền thống trong các NNLT trước đâyusing System;namespace ConsoleApplication1{ struct HOCSINH { internal string ht; internal int ns; internal float[ ] diem; }Datepublic class Vidu4_1 { static void NhapDuLieu(ref HOCSINH hs) { hs.diem = new float[3]; Console.Write("Nhap ho ten : "); hs.ht=Console.ReadLine(); Console.Write("Nhap nam sinh : "); hs.ns=Int32.Parse(Console.ReadLine()); for (int i=0;i<hs.diem.Length;i++) { Console.Write("Nhap diem mon thu {0} : ",i+1); hs.diem[i]=Single.Parse(Console.ReadLine()); } } // NhapDuLieu Datestatic void InDuLieu(ref HOCSINH hs) { Console.WriteLine("Ho Ten : {0}" ,hs.ht); Console.WriteLine("Nam Sinh : {0}",hs.ns); for (int i=0;i<hs.diem.Length;i++) Console.Write("{0} ",hs.diem[i]); }public static void Main() { // Khởi tạo tương tự như 1 đối tượng HOCSINH x = new HOCSINH ( ) ; NhapDuLieu(ref x); InDuLieu(ref x); } }}DateMinh họa : ViDu4_2.cspublic struct HOCSINH { public string ht; public int ns; public float[ ] diem; } public struct GV { public string ht; public float bl; } public struct LOP { public GV gvcn; public HOCSINH[ ] ds; }Datepublic class ViDu4_2 { static LOP lop4a ; public static void Main() { lop4a.gvcn.ht="Le Thi Kim Dung"; lop4a.ds= new HOCSINH[10]; lop4a.ds[0].ht="Tran Huy Hoang"; Console.WriteLine("{0}",lop4a.gvcn.ht); Console.WriteLine("{0}",lop4a.ds[0].ht); } }}DateDateMinh họa : ViDu4_3.cspublic struct SACH { public string tuasach,tacgia; public int namxb; //tương tự như lớp : có hàm xây dựng public SACH(string s1, string s2,int i) { tuasach=s1; tacgia=s2; namxb=i; } }Datepublic class ViDu4_3{//Khai báo 1 mảng chứa sẵn dữ liệustatic SACH[ ] thuvien=new SACH[ ] { new SACH("Ba chang ngu lam","Dumas cha",2005), new SACH("Tra Hoa Nu","Dumas con",2004), new SACH("Cuon theo chieu gio","Mitchell",2003), new SACH("Tinh yeu cuoc song","London",2000), };public static void Main( ) { for(int i=0;i<thuvien.Length;i++) Console.WriteLine("{0} {1} {2}",thuvien[i].tuasach,thuvien[i].tacgia,thuvien[i].namxb); }}DateDatestatic void InDS(){for(int i=0;i<thuvien.Length;i++) Console.WriteLine("{0}, {1}, {2}",thuvien[i].tuasach,thuvien[i].tacgia,thuvi en[i].namxb); }static void SapXepTheoNXB(){for (int i=0;i<thuvien.Length-1;i++) for (int j=i+1;j<thuvien.Length;j++) if (thuvien[j].namxb<thuvien[i].namxb) { SACH s = thuvien[i]; thuvien[i]=thuvien[j]; thuvien[j]=s; }}Datepublic static void Main() { Console.WriteLine("Truoc khi sap xep ..."); InDS(); SapXepTheoNXB(); Console.WriteLine("Sau khi sap xep theo nam xuat ban ..."); InDS(); }DateMinh họa : ViDu4_4.cspublic class ViDu4_5 { SACH[ ] thuvien; void InDS() { for(int i=0;i<thuvien.Length;i++) Console.WriteLine("{0}, {1}, {2}",thuvien[i].tuasach,thuvien[i].tacgia,thuvien[i].namxb); } void SapXepTheoNXB() { for (int i=0;i<thuvien.Length-1;i++) for (int j=i+1;j<thuvien.Length;j++) if (thuvien[j].namxb<thuvien[i].namxb) { SACH s = thuvien[i]; thuvien[i]=thuvien[j]; thuvien[j]=s; } }Datepublic static void Main(){ViDu4_5 exam = new ViDu4_5( ); exam.thuvien = new SACH[] {new SACH("Ba chang ngu lam","Dumas cha",2005), new SACH("Tra Hoa Nu","Dumas con",2004), new SACH("Cuon theo chieu gio","Mitchell",2003), new SACH("Tinh yeu cuoc song","London",2000), };Console.WriteLine("Truoc khi sap xep ...");exam.InDS();exam.SapXepTheoNXB();Console.WriteLine("Sau khi sap xep theo nam xb ...");exam.InDS();}DateMinh họa : ViDu4_5.csusing System;using System.IO;using System.Text;class ViDu4_5{ public static void Main() { string path = "e:\\vidu4_1.cs"; //hay string path = @"e:\vidu4_1.cs"; StreamReader sr = File.OpenText(path); string s = ""; do { s = sr.ReadLine(); if (s!=null) Console.WriteLine(s); } while (s!=null); sr.Close(); }}DateDateDateDateDateTạo điều khiển làm thư viện lớpYêu cầu Khi xây dựng 1 chương trình cho các ứng dụng của 1 ngân hàng, thường có yêu cầu nhập và hiệu chỉnh 3 loại ngoại tê mạnh Usd/Euro/Yen. Thay vì cứ mỗi form lại phải thiết kê từ đầu  Nên xây dựng 1 điều khiển (control) tương tự như các điều khiển chuẩn (textbox,listbox,button,..) để sau đó có thể “nhúng” vào 1 form sau này DateDateDateDatepublic class BangTyGia : System.Windows.Forms.UserControl { private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label3; private System.Windows.Forms.Label label4; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.TextBox textBox2; private System.Windows.Forms.TextBox textBox3; private System.Windows.Forms.Button button1; private System.ComponentModel.Container components = null; public double Dollar, Yen, Euro ; public BangTyGia() {Dateprivate void button1_Click (object sender, System.EventArgs e) { this.Dollar =Double.Parse(this.textBox1.Text); this.Euro =Double.Parse(this.textBox2.Text); this.Yen =Double.Parse(this.textBox3.Text); }DateDateDateDateDateDateDateDatepublic class Form1 : System.Windows.Forms.Form { private BangTyGia.BangTyGia bangTyGia1; private System.Windows.Forms.Label label1; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.Label label2; private System.Windows.Forms.ComboBox comboBox1; private System.Windows.Forms.Label label3; private System.Windows.Forms.Label label4;Datestatic void Main() { Application.Run(new Form1()); }private void comboBox1_SelectedIndexChanged (object sender, System.EventArgs e) { double x = Double.Parse(this.textBox1.Text); double y=0; switch (this.comboBox1.SelectedIndex) { case 0:y=x/this.bangTyGia1.Dollar;break; case 1:y=x/this.bangTyGia1.Euro ;break; case 2:y=x/this.bangTyGia1.Yen ;break; } this.label4.Text=y.ToString(); }DateDateỨng dụng trên web pageYêu cầu Xây dựng 1 trang web để có thể duyệt bằng chương trình IE/FireFox/Opera/.Trang web này cho phép người dùng tính các phép toán cộng trừ nhân chia đơn giảnDateDateDateDateDateDateDateDateDateDateDateỨng dụng trên PocketPC-MobileViết chương trình trên một Pocket PC cho phép người sử dụng nhập vào ngày, tháng và năm bất kỳ.Sau đó in ra thứ trong tuần của ngày trênHướng dẫn Sử dụng lớp DateTime trong System.Globalization nhằm tính thứ trong tuần DateTime dt = new DateTime(năm,tháng,ngày);  dt.WeekDay cho biết thứ trong tuầnDateDateDateDateDateDateusing System;using System.Drawing;using System.Collections;using System.Windows.Forms;using System.Data;using System.Globalization ;namespace TinhThu{ public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label3; private System.Windows.Forms.Label label4; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.TextBox textBox2;Dateprivate System.Windows.Forms.TextBox textBox3;private System.Windows.Forms.Button button1;private System.Windows.Forms.Label label5;public Form1()protected override void Dispose( bool disposing )static void Main() {Application.Run(new Form1());}private void button1_Click(object sender, System.EventArgs e){ Int32 d = Int32.Parse(this.textBox1.Text); Int32 m = Int32.Parse(this.textBox2.Text); Int32 y = Int32.Parse(this.textBox3.Text); DateTime dt = new DateTime(y,m,d); this.label5.Text ="That date is "+dt.DayOfWeek.ToString(); }} }Date

Các file đính kèm theo tài liệu này:

  • pptcosolaptrinh_csharp_bai8_3982.ppt
Tài liệu liên quan