Thực hành kỹ thuật lập trình - 26



Viết mã lệnh thực hiện các công việc

- Viết mã lệnh khai báo cấu trúc sinh viên

public struct SV

{

string _masv; string _tensv; int _namsinh;

public string masv

{

get { return _masv; } set { _masv = value; }

Có thể bạn quan tâm!

Xem toàn bộ 312 trang tài liệu này.

}

public string tensv

Thực hành kỹ thuật lập trình - 26

{

get { return _tensv; } set { _tensv = value; }

}

public int namsinh

{

get { return _namsinh; } set { _namsinh = value; }

}

public SV(string ma, string ten, int nam)

{

_masv = ma;

_tensv = ten;

_namsinh = nam;

}

public SV(SV a)

{

this._masv = a._masv; this._tensv = a._tensv; this._namsinh = a._namsinh;

}

public ListViewItem Xuat1()

{

ListViewItem item = new ListViewItem(); item.Text = _masv; item.SubItems.Add(_tensv); item.SubItems.Add(_namsinh.ToString()); return item;

}

}

- Viết mã lệnh khai báo cấu trúc sinh viên khoa cơ khí

public struct SV_CK

{


SV _sv;

double _diem_VKT, _diem_CNC; public SV sv

{

get { return _sv; } set { _sv = value; }

}

public double diem_VKT

{

get { return _diem_VKT; } set { _diem_VKT = value; }

}

public double diem_CNC

{

get { return _diem_CNC; } set { _diem_CNC = value; }

}

public SV_CK(SV sv1,double d1, double d2)

{ _sv= sv1;

_diem_VKT = d1;

_diem_CNC = d2;

}

public SV_CK(SV_CK a)

{

this._sv = a.sv;

this._diem_VKT = a._diem_VKT; this._diem_CNC = a._diem_CNC;

}

public ListViewItem Xuat2(SV_CK x)

{

ListViewItem item = x.sv.Xuat1(); item.SubItems.Add(_diem_VKT.ToString("F2")); item.SubItems.Add(_diem_CNC.ToString("F2")); return item;

}

public double DiemTB()

{

return (_diem_VKT + _diem_CNC) / 2;

}

public void GhiDuLieu(BinaryWriter writer)

{

writer.Write(_sv.masv); writer.Write(_sv.tensv); writer.Write(_sv.namsinh); writer.Write(_diem_VKT); writer.Write(_diem_CNC);

}

public SV_CK DocDulieu(BinaryReader read)



{

SV_CK X = new SV_CK();

try

{

X._sv.masv = read.ReadString(); X._sv.tensv = read.ReadString();


}

catch (IOException)

{

Console.WriteLine("Lỗi đọc tệp");

}

X._sv.namsinh = read.ReadInt32(); X.diem_VKT = read.ReadDouble(); X.diem_CNC = read.ReadDouble(); return X;

}

}

- Viết mã lệnh khai báo mảng cấu trúc sinh viên khoa cơ khí, biến nguyên n

SV_CK[] A = new SV_CK[100]; int n = 0;

- Viết mã lệnh cho hàm mở tệp để đọc

void Motep_doc(ref SV_CK[] A, ref int n)

{

FileStream fs = new FileStream(@"C:SVCK.data", FileMode.Open, FileAccess.Read);

BinaryReader reader = new BinaryReader(fs); n = reader.ReadInt32(); int cs = 0;

for (int i = 0; i < n; i++)

{

A[cs] = A[cs].DocDulieu(reader); cs++;

}

reader.Close(); fs.Close();

}

- Viết mã lệnh cho hàm mở tệp để ghi

void Motep_ghi(SV_CK[] A, int n)

{

FileStream fs = new FileStream(@"C:SVCK.data", FileMode.Create,

FileAccess.Write);

BinaryWriter bw = new BinaryWriter(fs); bw.Write(n);

try

{

for (int i = 0; i < n; i++) A[i].GhiDuLieu(bw);

}



catch (IOException ex)

{

Console.WriteLine("Loi" + ex);

}

bw.Close();

fs.Close();

}

- Viết mã lệnh cho hàm kiểm tra chuỗi rỗng

bool kiemtra(string s)

{

if (string.IsNullOrWhiteSpace(s)) return true; else return false;

}

- Viết mã lệnh cho hàm kiểm tra dữ liệu nhập vào

bool kiemtra_dulieu()

{

int so; double x;

if (kiemtra(txt_masv.Text)|| kiemtra(txt_tensv.Text)|| kiemtra(txt_namsinh.Text)|| kiemtra(txt_diemVKT.Text)|| kiemtra( txt_diemCNC.Text)) return false;

if (txt_namsinh.Text.Length < 4 || txt_namsinh.Text.Length > 4) return

false;

if (!int.TryParse(txt_namsinh.Text, out so)) return false;

if (!double.TryParse(txt_diemVKT.Text, out x) || x<0) return false; if (!double.TryParse(txt_diemCNC.Text, out x)||x<0) return false; if (double.Parse(txt_diemVKT.Text) < 0 ||

double.Parse(txt_diemVKT.Text) > 10) return false; if (double.Parse(txt_diemCNC.Text) < 0 ||

double.Parse(txt_diemCNC.Text) > 10) return false; return true;

}

- Viết mã lệnh cho hàm reset các textbox

void reset()

{

txt_masv.ResetText(); txt_tensv.ResetText(); txt_namsinh.ResetText(); txt_diemVKT.ResetText();

txt_diemCNC.ResetText(); txt_masv.Focus();

}

- Viết mã lệnh cho sự kiện kích chuột vào nút “Thêm”

private void bt_them_Click(object sender, EventArgs e)

{

Motep_doc(ref A, ref n);

if (string.Compare(bt_them.Text, "Thêm") == 0)

{

vohieuhoa(true); txt_masv.Focus(); bt_them.Text = "Lưu";



}

else

{

if (kiemtra_dulieu())

{

int i;

string ma, ten; int ns; double diem_VKT, diem_CNC; ma = txt_masv.Text;

for (i = 0; i < n; i++)

if (string.Compare(A[i].sv.masv, ma) == 0) { MessageBox.Show("Trùng mã nhân viên", "Thông báo"); txt_masv.Focus(); return; }

ten = txt_tensv.Text;

ns = int.Parse(txt_namsinh.Text);

diem_VKT = double.Parse(txt_diemVKT.Text); diem_CNC = double.Parse(txt_diemCNC.Text); SV x = new SV(ma, ten, ns);

SV_CK sv = new SV_CK(x, diem_VKT, diem_CNC); if (i == n) { A[n] = sv; n++; bt_them.Text = "Thêm";

vohieuhoa(false); reset(); }

Motep_ghi(A, n); Motep_doc(ref A, ref n); Dua_DL_listview(A, n);

}

else { MessageBox.Show("Du lieu khong hop le", "Thong bao");

return; }

}

}

- Viết mã lệnh cho hàm hiển thị sinh viên khoa cơ khí lên Listview

void hienthi(SV_CK x)

{

ListViewItem item; double diem_TB; item = x.Xuat2(x);

diem_TB = x.DiemTB(); item.SubItems.Add(diem_TB.ToString("F2")); listview_SVCK.Items.Add(item);

}

- Viết mã lệnh cho sự kiện kích chuột vào nút đọc tệp

private void bt_doctepClick(object sender, EventArgs e)


{


Motep_doc(ref A, ref n);



Dua_DL_listview(A, n);


}

- Viết mã lệnh cho hàm đưa danh sách sinh viên vào ListView

private void Dua_DL_listview(SV_CK[] A, int n)

{

listview_SVCK.Items.Clear(); for (int i = 0; i < n; i++)

{

ListViewItem item = new ListViewItem(); item.Text = A[i].sv.masv; item.SubItems.Add(A[i].sv.tensv); item.SubItems.Add(A[i].sv.namsinh.ToString()); item.SubItems.Add(A[i].diem_VKT.ToString()); item.SubItems.Add(A[i].diem_CNC.ToString()); item.SubItems.Add(A[i].DiemTB().ToString("F2")); listview_SVCK.Items.Add(item);

}

}

- Viết mã lệnh cho sự kiện kích chuột vào nút sinh viên có điểm trung bình hai

môn nhỏ hơn 5

private void bt_SV5_Click(object sender, EventArgs e)

{

SV_CK [] B= new SV_CK[100]; int cs=0; listview_SVCK.Items.Clear();

for(int i=0;i<n;i++)

if (A[i].DiemTB() < 5)

{

B[cs]=A[i];

cs++;

}

if(cs==0) MessageBox.Show("Không có sinh viên nào có điểm TB nhỏ hơn 5", "Thông báo");

else Dua_DL_listview(B,cs);

}

- Viết mã lệnh cho sự kiện Load Form

private void Form1_Load(object sender, EventArgs e)

{

vohieuhoa(false);

}

- Viết mã lệnh cho hàm vô hiệu hóa các textbox

void vohieuhoa(bool gt)

{

txt_diemCNC.Enabled = gt; txt_diemVKT.Enabled = gt;



txt_masv.Enabled = gt; txt_namsinh.Enabled = gt; txt_tensv.Enabled = gt;

}

- Viết mã lệnh cho sự kiện kích chuột vào nút sửa

private void bt_sua_Click(object sender, EventArgs e)

{

Motep_doc(ref A, ref n);

if (string.Compare(bt_sua.Text, "Sửa") == 0)

{

vohieuhoa(true); txt_masv.Enabled = false; txt_tensv.Focus(); bt_sua.Text = "Cập nhật";

}

else if (kiemtra_dulieu())

{

for (int i = 0; i < n; i++)

if (string.Compare(A[i].sv.masv, txt_masv.Text) == 0)

{

SV x1 = new SV(txt_masv.Text, txt_tensv.Text,int.Parse(txt_namsinh.Text));

SV_CK X = new SV_CK(x1, double.Parse(txt_diemVKT.Text),double.Parse(txt_diemCNC.Text));

A[i] = X;

break;

}

Motep_ghi(A, n); Motep_doc(ref A, ref n);

Dua_DL_listview(A, n); bt_sua.Text = "Sửa"; vohieuhoa(false);

reset();

}

else { MessageBox.Show("Dữ liệu sửa không hợp lệ"); bt_sua.Text = "Cập nhật"; }

}


private void listview_SVCK_SelectedIndexChanged(object sender, EventArgs e)

{

try

{

for (int i = 0; i < listview_SVCK.SelectedItems.Count; i++)

{

txt_masv.Text

=listview_SVCK.SelectedItems[i].SubItems[0].Text; txt_tensv.Text =

listview_SVCK.SelectedItems[i].SubItems[1].Text; txt_namsinh.Text =

listview_SVCK.SelectedItems[i].SubItems[2].Text;



txt_diemVKT.Text = listview_SVCK.SelectedItems[i].SubItems[3].Text;

txt_diemCNC.Text = listview_SVCK.SelectedItems[i].SubItems[4].Text;

}

}

catch { MessageBox.Show("Bạn chưa chọn phần tử để xóa"); }

}

- Viết mã lệnh cho sự kiện kích chuột vào nút xóa

private void bt_xoa_Click(object sender, EventArgs e)

{

if (MessageBox.Show("Bạn có muốn xóa không?", "Thông báo", MessageBoxButtons.YesNo) == DialogResult.Yes)

{

Motep_doc(ref A, ref n);

for (int i = 0; i < listview_SVCK.SelectedItems.Count; i++) for (int j = 0; j < n; j++)

if (string.Compare(A[j].sv.masv, listview_SVCK.SelectedItems[i].SubItems[0].Text) == 0)

{

for (int k = j; k < n - 1; k++) A[k] = A[k + 1]; n--;

break;

}

Motep_ghi(A, n); Motep_doc(ref A, ref n); listview_SVCK.Items.Clear(); Dua_DL_listview(A, n); reset();

}

}

- Viết mã lệnh cho sự kiện kích chuột vào nút thoát

private void bt_Thoat_Click(object sender, EventArgs e)

{

this.Close();

}

Bước 6. Kết quả chạy chương trình

Xem tất cả 312 trang.

Ngày đăng: 16/07/2022
Trang chủ Tài liệu miễn phí