BÀI THỰC HÀNH SỐ 1
Viết chương trình nhập vào 3 giá trị dương a, b, c. Kiểm tra xem chúng có thể tạo thành
ba cạnh của một tam giác hay không, nếu đúng thì đó là tam giác vuông, cân, đều hay tam
giác thường và cho biết chu vi và diện tích của hình tam giác đó.
Hướng dẫn
Diện tích tam giác được tính:
))()(( cpbpappS −−−=
với
2
cba
p
+
+
=
Giao diện chương trình:
- Các đối tượng TextBox có thuộc tính Name là: Text1 -> Text7 (thứ tự từ trên xuống)
- Các đối tượng CommandButton (nút lệnh) có Name từ: Command1 -> Command3
Mã lệnh tham khảo
Dim a As Byte, b As Byte, c As Byte ‘ Các biến chứa ba cạnh tam giác
Dim S As Single, P As Single ‘ Chứa diện tích và chu vi của tam giác
‘ Sự kiện Click của nút Command2 (nút Tiep tuc)
Private Sub Command2_Click()
Text1.Text = "" ‘ Xóa rỗng các đối tượng TextBox
Text2.Text = "" : Text3.Text = ""
Text4.Text = "" : Text5.Text = ""
Text6.Text = "" : Text7.Text = ""
End Sub
‘Sự kiện Click của nút Command3 (nút Ket thuc)
Private Sub Command3_Click()
Text4.Text = "DIEU NAY SAI"
End If
End Sub
Các bài tập khác
1. Giải phương trình bậc hai ax
2
+ bx + c = 0 (a ≠ 0)
2. Giải hệ phương trình bậc nhất
=+
=+
222
111
cybxa
cybxa
Hướng dẫn:
D=
22
11
ba
ba
D
x
=
22
BÀI THỰC HÀNH SỐ 2
Viết chương trình mô phỏng một máy tính bỏ túi dạng đơn giản, có thể thực hiện được
các phép toán +, -, *, / và các hàm Sin(x), Cos(x), Tan(x), Cotang(x), n!,
Thiết kế giao diện
Mã lệnh tham khảo
Dim s1 As Double, s2 As Double, kq As Double ‘ Số thứ nhất, thứ hai và kết quả
Dim Pheptoan As Byte
‘ Sự kiện Click mảng đối tượng Command2 (0, 1, 2,…,9 và .)
Private Sub Command2_Click(Index As Integer)
Text1.Text = Text1.Text & Command2(Index).Caption
End Sub
‘ Sự kiện Click mảng đối tượng Command1 (Sin, Cos, Tan,…)
Private Sub Command1_Click(Index As Integer)
Dim a As Double, ra As Double
a = Val(Text1.Text) ‘ Nhận giá trị của a từ đối tượng Text1
ra = ToRadian(a) ‘ Gọi CTC đổi a sang đơn vị Radian
Select Case Index
Case 0: Text1.Text = Sin(ra) ‘ Gọi hàm tính Sin
Case 1: Text1.Text = Cos(ra) ‘ Gọi hàm tính Cos
Case 2: Text1.Text = Tan(ra) ‘ Gọi hàm tính Tan
Case 3: Text1.Text = 1/Tan(ra) ‘ Gọi hàm tính Cotang
Case 4: Text1.Text = Sqr(a) ‘ Gọi hàm tính căn bậc hai
Case 5: Text1.Text = Giaithua(CByte(a)) ‘ Gọi CTC tính N! (định nghĩa ở dưới)
Case 6: Text1.Text = Log(a) / Log(10) ‘ Tính Lôgarít cơ số 10 (thập phân)
Case 7: Text1.Text = Exp(a) ‘ Gọi hàm tính Exp(x) _ tính e mũ x
End Select
End Sub
‘ Sự kiện Click đối tượng Command3 (=)
: Clear
Text1.Text = kq ‘ Hiển thị kết quả
End Sub
‘ Sự kiện Click mảng đối tượng Command4 (=)
Private Sub Command4_Click(Index As Integer)
Pheptoan = Index ‘ Lưu phép toán
s1 = Val(Text1.Text) ‘ Lưu số thứ nhất vào biến s1
Text1.Text = "" ‘ Xoá nội dung màn hình
End Sub
‘ Sự kiện Click đối tượng Command5 (Exit)
Private Sub Command5_Click()
Unload Me
End Sub
‘ Sự kiện Click đối tượng Command6 (Clear)
Private Sub Command6_Click()
Text1.Text = ""
End Sub
‘ Ðịnh nghĩa hàm đổi từ Ðộ → Radian
Private Function ToRadian(a As Double) As Double
Dim kq As Double
kq = (3.141592654 / 180) * a
ToRadian = kq ‘ Gán kết quả lại cho tên hàm
End Function
‘ Ðịnh nghĩa hàm tính N!
Private Function Giaithua(N As Byte) As Long
Dim i As Byte, Dim kq As Long
kq = 1
For i = 1 To N
kq = kq * i
HScroll1.Max = Picture2.Width - Picture1.Width
End If
' Thanh cuộn dọc
If Picture2.Height <= Picture1.Height Then
VScroll1.Visible = False ' Ẩn thanh cuộn dọc
Else
VScroll1.Visible = True ' Hiển thị thanh cuộn dọc
VScroll1.Max = Picture2.Height - Picture1.Height
End If
End Sub
‘ Sự kiện Click của Check1, 2, 3, 4
Private Sub Check1_Click()
If Check1.Value = 0 Then
File1.Archive = False
Else
File1.Archive = True
End If
End Sub
Private Sub Check2_Click()
If Check2.Value = 0 Then
File1.System = False
Else
File1.System = True
End If
End Sub
Private Sub Check3_Click()
If Check3.Value = 0 Then
File1.Hiđen = False
Else
Dir1.Path = Drive1.Drive
End Sub
‘ Sự kiện nạp của FORM
Private Sub Form_Load()
Text1.Text = ""
Combo1.AddItem ("Picture Files_ (*.jpg;
*.gif; *.bmp)")
Combo1.AddItem ("All Files (*.*)")
Combo1.AddItem ("Text Files (*.txt)")
Combo1.ListIndex = 1
If File1.Archive = True Then
Check1.Value = 1
Else
Check1.Value = 0
End If
If File1.System = True Then
Check2.Value = 1
Else
Check2.Value = 0
End If
If File1.Hiđen = True Then
Check3.Value = 1
Else
Check3.Value = 0
End If
If File1.Normal = True Then
Check4.Value = 1
Else
Check4.Value = 0
End If
On Error Resume Next ' Bẫy lỗi
Db.Open
If Db.Errors.Count <> 0 Then
MsgBox "Loi : " & Err.Description, vbCritical
Else
frmLogin.Show ' Hiển thị form Login
Unload Me
End If
End Sub
Private Sub Command2_Click() ' Nút Thoát
Unload Me
End Sub
Form frmLogin (đăng nhập)
* Giao diện
Gồm các đối tượng để nhập dữ liệu Text1, Text2 và 2 nút lệnh Command1, Command2
* Mã lệnh tham khảo
Option Explicit
Private Sub Command1_Click()
Dim rs As New ADODB.Recordset
rs.Open "Select * from Users where Username='" + Text1.Text + "' and Password='" +
Text2.Text + "'", Db
If rs.EOF Then
MsgBox "Ten User khong ton tai hoac mat khau khong dung !", vbCritical
Else
frmMain.Show ' Hiển thị form chính
Unload Me ' Đóng form log in
End If
End Sub
rs.Open "select * from Sinhvien where MaL=" & MaL & " order by Hoten", Db
Do While Not rs.EOF
lstHoten.AddItem rs!Hoten
lstMaSV.AddItem rs!MaSV
rs.MoveNext
Loop
MaSV = 0
End Sub
' Chương trình con hiển thị thông tin sinh viên
Private Sub HienthiSV(MaSV As Integer)
Dim rs As New ADODB.Recordset
rs.Open "select * from Sinhvien where MaSV=" & MaSV & "", Db
If Not rs.EOF Then
txtHoten.Text = rs!Hoten
chkGioitinh.Value = IIf(rs!Gioitinh = True, 1, 0)
txtNgaysinh.Text = rs!Ngaysinh & ""
txtNoisinh.Text = rs!Noisinh & ""
txtMon1.Text = rs!Mon1 & "": txtMon2.Text = rs!Mon2 & ""
txtMon3.Text = rs!Mon3 & ""
txtDTB.Text = rs!dtb & ""
txtXeploai.Text = rs!Xeploai & ""
Else
ClearAll ‘ Gọi Chương trình con xoá các thông tin
End If
End Sub
' Chương trình con xoá các thông tin
Private Sub ClearAll()
txtHoten.Text = "": txtNgaysinh.Text = "": txtNoisinh.Text = ""
txtMon1.Text = "": txtMon2.Text = "": txtMon3.Text = ""
txtDTB.Text = "": txtXeploai.Text = ""
rs!Mon2 = Val(txtMon2.Text)
rs!Mon3 = Val(txtMon3.Text)
txtDTB.Text = Round((rs!Mon1 + rs!Mon2 + rs!Mon3) / 3, 2) ' Làm tròn 2 chữ số
Select Case Val(txtDTB.Text)
Case Is >= 8: txtXeploai.Text = "Giỏi"
Case Is >= 7: txtXeploai.Text = "Khá"
Case Is >= 5: txtXeploai.Text = "Trung bình"
Case Else: txtXeploai.Text = "Yếu"
End Select
rs!dtb = txtDTB.Text
rs!Xeploai = txtXeploai.Text
rs!MaL = MaL
rs.Update ' Lưu vào CSDL
If MaSV = 0 Then LietkeDSSV MaL
End If
End Sub
‘ Xoá thông tin về sinh viên
Private Sub cmdDelete_Click()
Dim traloi As Integer
If MaSV = 0 Then
MsgBox "Phai chon SV can xoa", vbInformation
Else
traloi = MsgBox("Ban muon xo SV nay hay khong?", vbQuestion + vbYesNo)
If traloi = vbYes Then
Db.Execute "delete from Sinhvien where MaSV=" & MaSV & ""
LietkeDSSV MaL
End If
End If
End Sub
Một số yêu cầu khác
Trong đó:
- ImageList1: Chứa các hình ảnh làm biểu tượng cho các nút lệnh trên Toolbar1
- ImageList2: Chứa các hình ảnh làm biểu tượng cho các nút lệnh trên TreeView1
- Nội dung Menu: Với các mục có tên (Name) như hình bên phải
Mã lệnh tham khảo
Option Explicit
Dim MaSV As Integer, MaL As Integer ' Chứa mã SV và mã lớp được chọn
‘ Định nghĩa CTC xoá các nội dung các TextBox (Vào Tools → Add Procedure → …)
Private Sub ClearAll()
txtHoten.Text = "": txtNgaysinh.Text = "": txtNoisinh.Text = ""
txtMon1.Text = "": txtMon2.Text = "": txtMon3.Text = "" : txtDTB.Text = "": txtXeploai.Text = ""
End Sub
TreeView1
ImageList2
ImageList1
ToolBar1
ProgressBar1
CommonDialog1
Dim nod As Node, rs As New ADODB.Recordset
' Thêm nút gốc
Set nod = TreeView1.Nodes.Add(, , "R", "Quản lý sinh viên", 1, 1)
nod.ForeColor = vbRed: nod.Bold = True: nod.Expanded = True
' Thêm các nút ngành học
rs.Open "select * from Nganh", Db
Do While Not rs.EOF
Set nod = TreeView1.Nodes.Add("R", tvwChild, "N" & rs!MaNG, rs!TenNG, 2, 3)
nod.Bold = True: nod.ForeColor = vbBlue
rs.MoveNext
Loop
' Thêm các nút lớp
rs.Close
rs.Open "select * from Lop", Db
Do While Not rs.EOF
Set nod = TreeView1.Nodes.Add("N" & rs!MaNG, tvwChild, "L" & rs!MaL, rs!TenL, 4, 5)
rs.MoveNext
Loop
End Sub
‘ Sự kiện xảy ra khi form hiển thị
Private Sub Form_Load()
ClearAll ‘ Gọi các CTC
LoadTree
AddColumns
End Sub
‘ Định nghĩa CTC liệt kê DS sinh viên của lớp có mã là MaL vào ListView1
Public Sub LietkeDSSVListView(MaL As Integer)
Dim rs As New ADODB.Recordset, Item As ListItem
ListView1.ListItems.Clear ' Xoá nội dung hiện tại
ClearAll ' Xoá các TextBox
Dim rs As New ADODB.Recordset
rs.Open "select * from Sinhvien where MaSV=" & MaSV & "", Db
If Not rs.EOF Then
txtHoten.Text = rs!Hoten
chkGioitinh.Value = IIf(rs!Gioitinh = True, 1, 0)
txtNgaysinh.Text = rs!Ngaysinh & "" : txtNoisinh.Text = rs!Noisinh & ""
txtMon1.Text = rs!Mon1 & "": txtMon2.Text = rs!Mon2 & "": txtMon3.Text = rs!Mon3 & ""
txtDTB.Text = rs!DTB & "" : txtXeploai.Text = rs!Xeploai & ""
Else
ClearAll ‘ Xoá nội dung
End If
End Sub
‘ Sự kiện xảy ra khi nhắp chọn một phần tử trong ListView1
Private Sub ListView1_ItemClick(ByVal Item As MSComctlLib.ListItem)
MaSV = Mid(Item.Key, 3) ' Mã SV được chọn
HienthiSV MaSV ‘ Gọi CTC hiển thị thông tin của SV
End Sub
‘ Nút Tạo mới một sinh viên
Private Sub cmdNew_Click()
MaSV = 0 : ClearAll ' Xoá nội dung
End Sub
‘ Nút Lưu thông tin sinh viên
Private Sub cmdSave_Click()
Dim rs As New ADODB.Recordset
If txtHoten.Text = "" Or txtNgaysinh.Text = "" Or MaL = 0 Then
MsgBox "Chua nhap Ho ten, Ngay sinh hoac chua chon Lop !", vbCritical
Else
rs.Open "select * from Sinhvien where MaSV=' " & MaSV & " ' ", Db, adOpenStatic, adLockOpt…
If rs.EOF Then rs.AddNew
rs!Hoten = txtHoten.Text
Dim rs As New ADODB.Recordset
rs.Open "select * from Sinhvien", Db, adOpenStatic, adLockOptimistic
' Thiết lập thanh tiến trình
ProgressBar1.Min = 0: ProgressBar1.Value = 0
If Not rs.EOF Then
rs.MoveLast: ProgressBar1.Max = rs.RecordCount: rs.MoveFirst
End If
' Tính ĐTB cho từng SV
Do While Not rs.EOF
rs!DTB = Round((rs!Mon1 + rs!Mon2 + rs!Mon3) / 3, 2)
rs.Update
ProgressBar1.Value = ProgressBar1.Value + 1
rs.MoveNext
Loop
ProgressBar1.Value = 0
End Sub
‘ Menu chọn hình nền cho form
Private Sub mnuHinhnen_Click()
CommonDialog1.Filter = "Picture file (*.jpg)|*.jpg|All files (*.*)|*.*"
CommonDialog1.FilterIndex = 1
CommonDialog1.ShowOpen
frmMain.Picture = LoadPicture(CommonDialog1.FileName)
End Sub
‘ Menu chọn màu nền cho form
Private Sub mnuMaunen_Click()
CommonDialog1.ShowColor
ListView1.BackColor = CommonDialog1.Color
End Sub
‘ Sự kiện xảy ra khi click chọn các nút lệnh của Toolbar
Private Sub Toolbar1_ButtonClick(ByVal Button As MSComctlLib.Button)
Mã lệnh tham khảo:
Option Explicit
‘ Thiết lập các thuộc tính cho ListView1
Private Sub Form_Load()
ListView1.View = lvwReport ' Chế độ xem dạng bảng
ListView1.FullRowSelect = True ‘ Cho phép chọn cả dòng
ListView1.GridLines = True
ListView1.ColumnHeaders.Add , , "Họ tên", 3000, 0
ListView1.ColumnHeaders.Add , , "Ngày sinh", 1500, 1
ListView1.ColumnHeaders.Add , , "Nơi sinh", 1500, 2
End Sub
‘ Nút Tìm kiếm
Private Sub Command1_Click()
Dim rs As New ADODB.Recordset, item As ListItem
If Text1.Text = "" Then
MsgBox "Phai nhap thong tin tim kiem !", vbCritical
Else
If Option1.Value = True Then ‘ Tìm kiếm theo tên
rs.Open "select * from Sinhvien where Hoten Like '%" + Text1.Text + "%' ", Db
End If
If Option2.Value = True Then ‘ Tìm kiếm theo ngày sinh
rs.Open "select * from Sinhvien where Ngaysinh=' " + Text1.Text + " ' ", Db
End If
If Option3.Value = True Then ‘ Tìm kiếm theo nơi sinh
rs.Open "select * from Sinhvien where Noisinh=' " + Text1.Text + " ' ", Db
End If
ListView1.ListItems.Clear
' Liệt kê các kết quả tìm kiếm được vào ListView1
Do While Not rs.EOF
Set item = ListView1.ListItems.Add
ListView1.FullRowSelect = True ‘ Cho phép chọn cả dòng
ListView1.ColumnHeaders.Add , , "Tên lớp", 3000, 0
ListView1.ColumnHeaders.Add , , "Thống kê", 1500, 1
End Sub
‘ Nút Thống kê
Private Sub Command1_Click()
Dim rs As New ADODB.Recordset, item As ListItem
Dim sql As String ‘ Biến sql chứa câu lệnh SQL để thống kê
If Option1.Value = True Then ‘ Thống kê theo số lượng mỗi lớp
sql = sql & "select TenL, Count(MaSV) as Thongke from Sinhvien as A, Lop as B "
sql = sql & "Where (A.MaL=B.MaL) Group by TenL order by TenL"
End If
If Option2.Value = True Then ‘ Thống kê theo ĐTB cao nhất mỗi lớp
sql = sql & "select TenL, Max(DTB) as Thongke from Sinhvien as A, Lop as B "
sql = sql & "Where (A.MaL=B.MaL) Group by TenL order by Max(DTB) DESC"
End If
If Option3.Value = True Then
sql = sql & "select TenL, Count(MaSV) as Thongke from Sinhvien as A, Lop as B "
sql = sql & "Where (A.MaL=B.MaL) and (DTB<5) Group by TenL order by Count(MaSV) DESC"
End If
rs.Open sql, Db ‘ => Thực hiện truy vấn với câu lệnh SQL nằm trong biến sql
ListView1.ListItems.Clear
' Liệt kê các kết quả tìm kiếm được vào ListView1
Do While Not rs.EOF
Set item = ListView1.ListItems.Add
item.Text = rs!TenL & ""
item.SubItems(1) = rs!Thongke & ""
rs.MoveNext
Loop
End Sub
* Mã lệnh:
Dim MaL As Integer ' Chứa mã lớp hiện đang được chọn
Public Sub LoadTree()
‘ Giống bài thực hành trước
End Sub
‘ Sự kiện xảy ra khi form được hiển thị
Private Sub Form_Load()
Set Db = New ADODB.Connection
Db.ConnectionString = Adodc1.ConnectionString
On Error Resume Next ' Bẫy lỗi
Db.Open
If Db.Errors.Count <> 0 Then ' Nếu bị lỗi
MsgBox "Loi xay ra: " & Err.Description, vbCritical
Unload Me
End If
DataGrid1
TreeView1
ToolBar1
Các đối tượng lần lượt có
tên là: txtHoten, chkGioitinh,
txtNgaysinh. txtNoisinh,
txtMon1, txtMon2, txtMon3,
txtDTB, txtXeploai
Các nút lần lượt có tên là:
End Select
.Update ' Lưu ĐTB và Xeploai vào CSDL
End With
End Sub
‘ Nút xoá sinh viên
Private Sub cmdDelete_Click()
Dim traloi As Integer
traloi = MsgBox("Ban muon xoa SV nay hay khong?", vbQuestion + vbYesNo)
If traloi = vbYes Then Adodc1.Recordset.Delete
End Sub
‘ Tính ĐTB cho tất cả các sinh viên
Private Sub cmdTinhDTB_Click()
Dim rs As New ADODB.Recordset
rs.Open "select * from Sinhvien", Db, adOpenStatic, adLockOptimistic
ProgressBar1.Min = 0: ProgressBar1.Value = 0
If Not rs.EOF Then
rs.MoveLast: ProgressBar1.Max = rs.RecordCount: rs.MoveFirst
End If
Do While Not rs.EOF
rs!DTB = Round((rs!Mon1 + rs!Mon2 + rs!Mon3) / 3, 2)
Select Case rs!DTB
Case Is >= 8: rs!Xeploai = "Gioi"
Case Is >= 7: rs!Xeploai = "Kha"
Case Is >= 5: rs!Xeploai = "Trung binh"
Case Else: rs!Xeploai = "Yeu"
End Select
rs.Update
ProgressBar1.Value = ProgressBar1.Value + 1 : rs.MoveNext
Loop
ProgressBar1.Value = 0