Đây quả là một bài có ý nghĩa, vừa có các Excercises rất phù hợp với những người bắt đầu, và sẽ đi từ cơ bản nâng cao dần, vừa lại có đề bài bằng Tiếng Anh, giúp rèn luyện khả năng đọc TA sau này.
Mình đang bắt đầu học nên up lên đây để mọi người cùng tham khảo, mình sẽ liên tục Update những bài thực hành mới nhất
Đề bài:
EXERCISE 1
Write a program that asks the user to type 10 integers of an array. The program must compute and write the number of integers greater or equal to 10.
EXERCISE 2
Write a program that asks the user to type 10 integers of an array. The program must compute and write the maximum of array.
EXERCISE 3
Write a program that asks the user to type 10 integers of an array and an integer V. The program must search if V is in the array of 10 integers. The program writes "V is in the array" or "V is not in the array".
EXERCISE 4
Write a program that asks the user to type 10 integers of an array. The program must write the indice of the greater element of the array.
EXERCISE 5
Write a program that asks the user to type 10 integers and writes the smallest value.
EXERCISE 6
Write a program that display multiplication table of any number (BANG CUU CHUONG).
-----------------------------------------
Ex1:
Ex2:Code:#include <iostream.h> class son { private: int so[10]; public: void nhap() { for (int i=0;i<10;i++) { cout << "Number " << i+1 << ": "; cin>>so[i]; } } void xuat() { int dem=0; cout << "Cac so lon hon 10: "; for (int i=0;i<10;i++) { if (so[i]>=10) { cout << so[i] << ", "; ++dem; } } cout << "Cac so lon hon 10 la:" << dem; cout << "\n\n\n"; } }; void main() { son huong; huong.nhap(); huong.xuat(); }
Ex3:Code:#include <iostream.h> class son { private: int so[10]; public: void nhap() { for (int i=0;i<10;i++) { cout << "Number " << i+1 << ": "; cin>>so[i]; } } void xuat() { int lon=so[0]; for (int j=0;j<10;j++) { if (so[j]>lon) { lon=so[j]; } } cout<<"So lon nhat la: " << lon << endl; } }; void main() { son huong; huong.nhap(); huong.xuat(); }
Ex4:Code:#include <iostream.h> #include <stdlib.h> class son { private: int so[10]; int v; public: void nhap() { for (int i=0;i<10;i++) { cout << "Number " << i+1 << ": "; cin>>so[i]; } cout << "Nhap mot so nguyen V: "; cin >> v; } void xuat() { for (int j=0;j<10;j++) { if (so[j]==v) { cout << "V is in the array\n"; exit(1); } } cout << "V is not in the array\n"; exit(1); } }; void main() { son huong; cout <<"..:: CHUONG TRINH TIM SO V CO TRONG MANG KHONG? ::.." << endl; huong.nhap(); huong.xuat(); }
Ex5:gop vao bai 1
Ex6:Code:#include <iostream.h> class son { private: int so[10]; public: void nhap() { for (int i=0;i<10;i++) { cout << "Number " << i+1 << ": "; cin>>so[i]; } } void xuat() { int nho=so[0]; for (int j=0;j<10;j++) { if (so[j]<nho) { nho=so[j]; } } cout<<"So nho nhat la: " << nho << endl; } }; void main() { son huong; huong.nhap(); huong.xuat(); }
Code:#include <iostream.h> class son { private: int n; public: void nhan() { cout<<"Nhap bang cuu chuong can tinh: "; cin>>n; for (int i=1;i<=10;i++) { cout << n << " x " << i << " = " << n*i << endl; } } }; void main() { son huong; huong.nhan(); }
Đã được chỉnh sửa lần cuối bởi iamvtn : 01-08-2007 lúc 11:37 PM. Lý do: Thiếu thẻ code
Đây quả là một bài có ý nghĩa, vừa có các Excercises rất phù hợp với những người bắt đầu, và sẽ đi từ cơ bản nâng cao dần, vừa lại có đề bài bằng Tiếng Anh, giúp rèn luyện khả năng đọc TA sau này.
Viết chương trình nhập vào một dãy số bất kì, xét xem dãy số đó có tạo thành một cấp số cộng hay không? một cấp số nhân hay không?
Code:#include <iostream.h> #include <stdlib.h> class mang { private: int so[100]; int n; public: void nhap() { cout << "Nhap tong phan tu: "; cin>>n; for(int i=1;i<n;i++) { cout<<"Nhap so"<<i<<":"; cin>>so[i]; } } void xuat() { int dem=0; for (int i=3;i<n;i++) { if ((so[i]-(so[i-1]))==((so[i-1])-(so[i-2]))) { dem++; } } if ((n-3)==dem) { cout << "Day la cap so cong"<<endl; } else cout << "Khong phai cap so cong"<<endl; } void capnhan() { int dem=0; for (int i=2;i<n;i++) { if (so[i]==(so[i-1]*so[i-1])) { dem++; } } if ((n-2)==dem) { cout << "Day la cap so nhan"<<endl; } else cout << "Khong phai cap so nhan"<<endl; } }; void main() { mang ha; ha.nhap(); ha.capnhan(); }
Đã được chỉnh sửa lần cuối bởi iamvtn : 01-08-2007 lúc 11:35 PM. Lý do: Thiếu thẻ code
Cộng tuổi
Code:#include <iostream.h> class Sinhvien { char hoten[15]; int tuoi; char diachi[5]; public: void nhapgiatri() { cout << "Nhap gia tri HOTEN:"; cin >> hoten; cout << "Nhap gia tri tuoi:"; cin>> tuoi; cout << "Nhap gia tri dia chi:"; cin>> diachi; } void ingiatri() { cout << "Ho ten la: " << hoten << endl; cout << "Tuoi la: " << tuoi << endl; cout << "Dia Chi la: " << diachi << endl; } void intuoi() { congtuoi(); ingiatri(); } private: void congtuoi() { tuoi = tuoi + 5; } }; void main() { Sinhvien sinhvien; sinhvien.nhapgiatri(); sinhvien.ingiatri(); sinhvien.intuoi(); }
Ví dụ về sử dụng Class cơ bản
Code:#include <iostream.h> class huong { public: void ham1() { cout << "Cai nay la o ham 1" << endl; } void ham2() { cout << "Cai nay la o ham 2" << endl; } }; void main() { huong son; cout << "Cai nay o ham Main" << endl; son.ham1(); son.ham2(); }
Ví dụ về sử dụng 2 Class - Tính lương
Code:#include <iostream.h> class Toanhoc { public: int Conghaiso(int soa, int sob) { return soa + sob; } int Nhanhaiso(int soa, int sob) { return soa * sob; } }; class Tinhluong { int tongluong; char ten[15]; int luongcoban; int hh; public: void Nhapdulieu() { cout<<"Nhap Ten:"; cin>> ten; cout << "Nhap luong co ban:"; cin >> luongcoban; cout << "Nhap hoa hong:"; cin>> hh; } void Indulieu() { cout << "Ho ten:" << ten <<endl; cout << "Luong co ban: " << luongcoban << endl; cout << "Hoa hong: " << hh <<endl; cout << "Tong luong nhan duoc:" << tongluong << endl; } void TinhTongluong() { Tongnhanduoc(luongcoban,hh); } private: void Tongnhanduoc(int luong, int hoahong) { Toanhoc tinh; tongluong = tinh.Conghaiso(luong, hoahong); } }; void main() { Tinhluong tinh; tinh.Nhapdulieu(); cout << "Du lieu vua nhap vao tu ban phim." <<endl; tinh.Indulieu(); tinh.TinhTongluong(); cout << "Du lieu da duoc tinh toan:" << endl; tinh.Indulieu(); }
Nhân viên - lớp học
Code:#include <iostream.h> class Nhanvien { public: void Nhapdulieu() { cout << "Nhap ho ten:"; cin >> Hoten; cout << "Nhap tuoi: "; cin >> tuoi; cout << "Nhap dia chi:"; cin >> Diachi; } void Indulieu() { cout << "Ho ten la: " << Hoten << endl; cout << "Tuoi la:" << tuoi << endl; cout << "Dia chi la: " << Diachi << endl; } private: int tuoi; char Hoten[20]; char Diachi[50]; }; class Lophoc { public: void Nhaplophoc() { for (int i=0; i < 5; i++) { cout << endl << endl; cout << "NHAP DU LIEU HOC VIEN " << (i + 1) << ":" << endl; hocvien[i].Nhapdulieu(); } } void Inlophoc() { for (int i=0; i < 5; i++) { cout << endl << endl; cout << "HOC VIEN " << (i+1) << ":" << endl; hocvien[i].Indulieu(); } } void suadanhsach() { int vitri; cout << "Nhap vi tri cua hoc vien trong mang:"; cin >> vitri; hocvien[vitri].Nhapdulieu(); } private: Nhanvien hocvien[5]; }; void main() { Lophoc lop; lop.Nhaplophoc(); lop.Inlophoc(); lop.suadanhsach(); lop.Inlophoc(); /*Nhanvien hocvien[2]; hocvien[0].Nhapdulieu(); hocvien[1].Nhapdulieu(); cout << "Gia tri vua nhap vao la:" << endl; cout << "Hoc vien thu 2:" << endl; hocvien[1].Indulieu(); cout << "Hoc vien thu 1:" << endl; hocvien[0].Indulieu();*/ }
Tính chu vi diện tích hình chữ nhật
Code:#include <iostream.h> class Congthuc { public: int chuvi(int dai1, int rong1) { return (dai1+rong1)*2; } int dientich(int dai1, int rong1) { return dai1*rong1; } }; class Tinh { int dai,rong,cv,dt; public: void Nhap() { cout << "Nhap chieu dai: "; cin >> dai; cout << "Nhap chieu rong: "; cin >> rong; } void xuatra() { cout << "---------------------" << endl; cout << "Chu vi la: " << cv << endl; cout << "Dien tich la: " << dt << endl; cout << "---------------------" << endl; } void lay() { laycongthuc(dai,rong); } private: void laycongthuc(int so1,int so2) { Congthuc ct; cv=ct.chuvi(so1,so2); dt=ct.dientich(so1,so2); } }; void main() { Tinh tinh; tinh.Nhap(); tinh.lay(); tinh.xuatra(); }
Bán cà chua
Code:#include <iostream.h> class congthuc { public: int tinhtien(int dongia, int soluong, int thue) { int tienthue; tienthue=((dongia*soluong)*thue)/100; return (soluong*dongia)+tienthue; } }; class bancachua { int dongia,soluong,thue,tinhtiencachua; private: void laycongthuc(int dongia,int soluong,int thue) { congthuc thuccong; tinhtiencachua=thuccong.tinhtien(dongia,soluong,thue); } public: void nhap() { cout << "Nhap don gia: "; cin >> dongia; cout << "Nhap so luong: "; cin >> soluong; cout << "Nhap thue: "; cin >> thue; } void laycongthuctuthangprivatera() { laycongthuc(dongia,soluong,thue); } void xuatra() { cout << "------------------------\n"; cout << "Thanh tien la: " << tinhtiencachua<<endl; cout << "Chuc cac ban an ca chua ngon mieng"<<endl; } }; void main() { bancachua huong; huong.nhap(); huong.laycongthuctuthangprivatera(); huong.xuatra(); }
Sắp xếp thẳng hàng
Code:#include <iostream.h> #include <string.h> #include <conio.h> class Employee { private: char firtsName[10]; char lastName[10]; char city[10]; char phone[15]; public: void Nhapdulieu() { cout << "Nhap thong tin HO:"; cin >> firtsName; cout << "Nhap thong tin TEN:"; cin >> lastName; cout << "Nhap thong tin Thanh Pho:"; cin >> city; cout << "Nhap thong tin DIEN THOAI:"; cin >> phone; } void Indulieu() { int i = strlen(firtsName); cout << "Do dai cua First Name la:" << i << endl; cout << " First Name: " << firtsName ; for (int ispace=0; ispace < 10 - i; ispace++) { cout << " "; } cout << " Last Name: " << lastName << endl; cout << " City : " << city ; i = strlen(city); for (int jspace=0; jspace < 10 - i; jspace++) { cout << " "; } cout << " Phone: " << phone << endl; } }; void main() { clrscr(); Employee nhanvien; nhanvien.Nhapdulieu(); nhanvien.Indulieu(); }