Thực hành 1
Mã nguồn:
// Trải nghiệm thư viện chuẩn hiện đại của C++ và thư viện thuật toán
// Ví dụ này sử dụng các nội dung sau:
// 1. Chuỗi string, vector động và iterator
// 2. Thư viện thuật toán: đảo ngược thứ tự phần tử, xoay phần tử
// 3. Hàm mẫu và tham chiếu const
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
// Khai báo hàm mẫu để in ra các phần tử của container
template<typename T>
void hienThi(const T& ds);
// Các hàm thử nghiệm
void kiemTra1();
void kiemTra2();
void kiemTra3();
int main() {
cout << "Kiem tra 1:\n";
kiemTra1();
cout << "\nKiem tra 2:\n";
kiemTra2();
cout << "\nKiem tra 3:\n";
kiemTra3();
}
// Định nghĩa hàm mẫu để hiển thị phần tử
template<typename T>
void hienThi(const T& ds) {
for (auto pt : ds)
cout << pt << " ";
cout << endl;
}
// Kiem tra 1 - Dao nguoc chuoi
void kiemTra1() {
string banDau = "0123456789";
cout << "Chuoi goc: " << banDau << endl;
string daoNguoc = banDau;
reverse(daoNguoc.begin(), daoNguoc.end());
cout << "Sau khi dao nguoc: " << daoNguoc << endl;
string copyDao = banDau;
reverse_copy(banDau.begin(), banDau.end(), copyDao.begin());
cout << "Sau khi copy va dao nguoc: " << copyDao << endl;
}
// Kiem tra 2 - Dao nguoc mang dong vector
void kiemTra2() {
vector<int> dsGoc = {2, 0, 4, 9};
cout << "Danh sach goc: ";
hienThi(dsGoc);
vector<int> dsDao = dsGoc;
reverse(dsDao.begin(), dsDao.end());
cout << "Sau khi dao nguoc: ";
hienThi(dsDao);
vector<int> dsCopyDao = dsGoc;
reverse_copy(dsGoc.begin(), dsGoc.end(), dsCopyDao.begin());
cout << "Sau khi copy va dao nguoc: ";
hienThi(dsCopyDao);
}
// Kiem tra 3 - Xoay phan tu trong vector
void kiemTra3() {
vector<int> dsGoc = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
cout << "Danh sach goc: ";
hienThi(dsGoc);
vector<int> dsXoay1 = dsGoc;
rotate(dsXoay1.begin(), dsXoay1.begin() + 1, dsXoay1.end());
cout << "Sau khi xoay vi tri thu nhat: ";
hienThi(dsXoay1);
vector<int> dsXoay2 = dsGoc;
rotate(dsXoay2.begin(), dsXoay2.begin() + 2, dsXoay2.end());
cout << "Sau khi xoay vi tri thu hai: ";
hienThi(dsXoay2);
}
Thực hành 2
Mã nguồn:
// Sử dụng thư viện thuật toán cho việc sinh số ngẫu nhiên và tính toán
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <numeric>
#include <iomanip>
using namespace std;
// Hàm hiển thị danh sách
template<typename T>
void xuatDanhSach(const T& ds);
// Sinh số nguyên ngẫu nhiên từ 0 đến 100
int sinhSoNgauNhien();
// Hàm kiểm tra
void tinhToan1();
void tinhToan2();
int main() {
cout << "Kiem tra 1:\n";
tinhToan1();
cout << "\nKiem tra 2:\n";
tinhToan2();
}
// Hiển thị danh sách
template<typename T>
void xuatDanhSach(const T& ds) {
for (auto pt : ds)
cout << pt << " ";
cout << endl;
}
// Sinh số ngẫu nhiên
int sinhSoNgauNhien() {
return rand() % 101;
}
// Tính toán với danh sách số ngẫu nhiên
void tinhToan1() {
vector<int> dsSo(10);
generate(dsSo.begin(), dsSo.end(), sinhSoNgauNhien);
cout << "Danh sach so ngau nhien: ";
xuatDanhSach(dsSo);
vector<int> sapXep = dsSo;
sort(sapXep.begin(), sapXep.end());
cout << "Sau khi sap xep: ";
xuatDanhSach(sapXep);
}
// Tìm giá trị lớn nhất, nhỏ nhất và trung bình
void tinhToan2() {
vector<int> dsSo(10);
generate(dsSo.begin(), dsSo.end(), sinhSoNgauNhien);
cout << "Danh sach so ngau nhien: ";
xuatDanhSach(dsSo);
int giaTriNhoNhat = *min_element(dsSo.begin(), dsSo.end());
int giaTriLonNhat = *max_element(dsSo.begin(), dsSo.end());
double trungBinh = accumulate(dsSo.begin(), dsSo.end(), 0.0) / dsSo.size();
cout << "Gia tri nho nhat: " << giaTriNhoNhat << endl;
cout << "Gia tri lon nhat: " << giaTriLonNhat << endl;
cout << fixed << setprecision(2) << "Trung binh cong: " << trungBinh << endl;
}
Thực hành 3
Mã nguồn:
// Kiểm tra chuỗi có phải là chuỗi đối xứng không
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
bool laDoiXung(string s);
int main() {
string s;
while (cin >> s)
cout << boolalpha << laDoiXung(s) << endl;
}
bool laDoiXung(string a) {
int len = a.size();
string b = a;
for (int i = 0; i < len / 2; ++i)
swap(a[i], a[len - i - 1]);
return b == a;
}
Thực hành 4
Mã nguồn:
// Chuyen doi he so thap phan sang cac he so khac
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
string chuyenHe(int so, int coSo = 2);
int main() {
int so;
while (cin >> so) {
cout << "Thap phan: " << so << endl;
cout << "Nhi phan: " << chuyenHe(so) << endl;
cout << "Bat phan: " << chuyenHe(so, 8) << endl;
cout << "Luc thap phan: " << chuyenHe(so, 16) << endl << endl;
}
}
string chuyenHe(int so, int coSo) {
string ketQua;
string chuSo = "0123456789ABCDEF";
while (so > 0) {
ketQua = chuSo[so % coSo] + ketQua;
so /= coSo;
}
return ketQua.empty() ? "0" : ketQua;
}
Thực hành 5
Mã nguồn:
// In ra bảng chữ cái được xoay theo vị trí chỉ định
#include <iostream>
#include <string>
#include <algorithm>
#include <iomanip>
using namespace std;
template<typename T>
void hienThi(const T& ds);
void xoayBangChuCai();
int main() {
cout << " a b c d e f g h i j k l m n o p q r s t u v w x y z" << endl;
for (int i = 1; i <= 26; ++i)
xoayBangChuCai();
}
template<typename T>
void hienThi(const T& ds) {
for (auto pt : ds)
cout << pt << " ";
cout << endl;
}
int viTriXoay = 1;
void xoayBangChuCai() {
string bangChuCai = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
rotate(bangChuCai.begin(), bangChuCai.begin() + viTriXoay, bangChuCai.end());
cout << setw(2) << viTriXoay << " ";
hienThi(bangChuCai);
viTriXoay++;
}
Thực hành 6
Mã nguồn:
// Tạo bài toán tính toán ngẫu nhiên và đánh giá kết quả
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;
int sinhSoNgauNhien();
int main() {
srand(static_cast<unsigned int>(time(nullptr)));
int tongDiem = 0;
for (int i = 0; i < 10; ++i) {
int so1 = sinhSoNgauNhien() + 1;
int so2 = sinhSoNgauNhien() + 1;
int phepTinh = rand() % 4;
int dapAn;
string toanTu;
int ketQua;
switch (phepTinh) {
case 0:
toanTu = "*";
ketQua = so1 * so2;
break;
case 1:
if (so1 % so2 != 0) {
if (so1 < so2)
swap(so1, so2);
so1 = so1 % so2 == 0 ? so1 : so2 * (rand() % (so1 / so2) + 1);
}
toanTu = "/";
ketQua = so1 / so2;
break;
case 2:
toanTu = "+";
ketQua = so1 + so2;
break;
case 3:
if (so1 < so2)
swap(so1, so2);
toanTu = "-";
ketQua = so1 - so2;
break;
}
cout << so1 << " " << toanTu << " " << so2 << " = ";
cin >> dapAn;
if (dapAn == ketQua)
tongDiem++;
}
double tiLeDung = (double)tongDiem / 10 * 100;
cout << "Ti le dung: " << fixed << setprecision(2) << tiLeDung << "%" << endl;
}
int sinhSoNgauNhien() {
return rand() % 10;
}