Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

CHƯƠNG 6

1111.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
ofstream outfile;
string hoten;
int namsinh;
cout << "Nhap ho ten: ";
getline(cin, hoten);
cout << "Nhap nam sinh: ";
cin >> namsinh;
outfile.open("D:/BT1.txt", ios::out);
if (outfile.is_open())
{
outfile << hoten << '#' << namsinh << endl;
cout << "Ghi file thanh cong";
outfile.close();
}
else
cout << "Ghi file khong thanh cong";
cout << endl;
system("pause");
return 0;
}

2222.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
ifstream infile;
string hoten;
int namsinh;
infile.open("D:\\BT1.txt", ios::in);
if (infile.is_open())
{
getline(infile, hoten, '#');
infile >> namsinh;
cout << "Ho ten: " << hoten << endl;
cout << "Tuoi: " << 2023 - namsinh << endl;
infile.close();
}
else
cout << "Doc file khong thanh cong";
cout << endl;
system("pause");
return 0;
}

3333.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void ghifile(string& filename)
{
ofstream outfile;
string data;
cout << "Nhap du lieu: ";
getline(cin, data);
outfile.open(filename, ios::out);
if (outfile.is_open())
{
cout << "Ghi file thanh cong" << endl;
outfile << data;
outfile.close();
}
else
cout << "Ghi file khong thanh cong" << endl;
}
void docfile(string filename)
{
ifstream infile;
string data;
infile.open(filename, ios::in);
if (infile.is_open())
{
cout << "Mo file thanh cong: " << endl;
getline(infile, data);
cout << data;
infile.close();
}
else
cout << "Mo file khong thanh cong" << endl;
}
int main()
{
string filename;
cout << "Nhap ten file: ";
getline(cin, filename);
ghifile(filename);
docfile(filename);
system("pause");
return 0;
}
4444.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void ghifile(string& filename)
{
ofstream outfile;
string data;
cout << "Nhap du lieu: ";
getline(cin, data);
outfile.open(filename, ios::out);
if (outfile.is_open())
{
cout << "Ghi file thanh cong" << endl;
outfile << data;
outfile.close();
}
else
cout << "Ghi file khong thanh cong" << endl;
}
void docfile(string filename)
{
ifstream infile;
string data;
infile.open(filename, ios::in);
if (infile.is_open())
{
cout << "Mo file thanh cong" << endl;
getline(infile, data);
cout << data;
infile.close();
}
else
cout << "Mo file khong thanh cong" << endl;
}
void doc2file(string f1, string f2, string &strkq)
{
ifstream infile;
string data1, data2;
infile.open(f1, ios::in);
if (infile.is_open())
{
cout << "\nMo file: " << f1 << " thanh cong" << endl;
getline(infile, data1);
cout << data1 << endl;
infile.close();
}
else
cout << "Mo file khong thanh cong\n";
infile.open(f2, ios::in);
if (infile.is_open())
{
cout << "\nMo file: " << f2 << " thanh cong" << endl;
getline(infile, data2);
cout << data2 << endl;
infile.close();
}
else
cout << "Mo file khong thanh cong\n";
if (data1 == data2)
cout << "Hai chuoi bang nhau" << endl;
else
if (data1 < data2)
cout << "Chuoi 1 < chuoi 2\n";
else
cout << "Chuoi 1 > chuoi 2\n";
strkq = data1 + " " + data2;
}
int demnguyenam(string filename)
{
ifstream infile;
string data;
int kq = 0;
infile.open(filename, ios::in);
if (infile.is_open())
{
getline(infile, data);
for (int i = 0; i < data.length(); i++)
if (data.at(i) == 'a' || data.at(i) == 'u')
kq++;
infile.close();
}
else
cout << "Dooc file khong thanh cong\n";
return kq;
}
int main()
{
string f1, f2, s;
cout << "Nhap file 1: ";
getline(cin, f1);
ghifile(f1);
docfile(f1);
cout << "\nNhap file 2: ";
getline(cin, f2);
ghifile(f2);
docfile(f2);
doc2file(f1, f2, s);
cout << "Ket qua noi chuoi: " << s << endl;
cout << "So nguyen am file 1: " << demnguyenam(f1) << endl;
cout << "So nguyen am file 2: " << demnguyenam(f2) << endl;
system("pause");
return 0;
}

ĐỆ QUY
1. Viết hàm đệ quy tìm xem số nguyên X có thuộc mảng 1 chiều A hay không (biết các phần tử
mảng A là số nguyên và có thứ tự tăng dần).
#include <iostream>
using namespace std;
bool bt1(int x, int n, int arr[6])
{
if (n < 0)
{
return false;
}
if (arr[n] == x)
{
return true;
}
else
{
return bt1(x, n - 1, arr);
}
}
int main()
{
int x = 5; //so can xet
int n = 6; //so phan tu cua mang
int arr[6] = { 2,4,5,8,9,12 };
if (bt1(x, n, arr) == 1)
cout << "true";
else
cout << "false";
return 0;
}

2. Viết hàm đệ quy trả về tổng các phần tử trên hàng i của mảng 2 chiều A.
#include <iostream>
using namespace std;
int bt2(int x, int i, int A[3][4])
{
if (x < 0)
{
return 0;
}
return A[i][x] + bt2(x - 1, i, A);
}
int main()
{
int x = 3; //so cot tru 1
int i = 1; //vi tri hang can tinh tong
int A[3][4];
A[0][0] = 1; A[0][1] = 3; A[0][2] = 4; A[0][3] = 7;
A[1][0] = 2; A[1][1] = 4; A[1][2] = 5; A[1][3] = 8;
A[2][0] = 0; A[2][1] = 5; A[2][2] = 9; A[2][3] = 10;

cout << bt2(x, i, A);


return 0;
}

3. Viết hàm đệ quy trả về tổng các phần tử của mảng 2 chiều A.
#include <iostream>
using namespace std;
int bt2(int x, int i, int A[3][4])
{
if (x < 0)
{
return 0;
}
return A[i][x] + bt2(x - 1, i, A);
}
int bt3(int x, int y, int A[3][4])
{
if (y < 0)
{
return 0;
}
else
return bt2(x, y, A) + bt3(x, y - 1, A);
}
int main()
{
int x = 3; //so cot tru 1
int i = 1; //vi tri hang can tinh tong
int y = 2;
int A[3][4];
A[0][0] = 1; A[0][1] = 3; A[0][2] = 4; A[0][3] = 7;
A[1][0] = 2; A[1][1] = 4; A[1][2] = 5; A[1][3] = 8;
A[2][0] = 0; A[2][1] = 5; A[2][2] = 9; A[2][3] = 10;

cout << bt3(x, y, A);


return 0;
}

4. Viết hàm đệ quy để trả lời: số nguyên X có nằm trong mảng A? nếu có thì xuất hiện bao nhiêu
lần?
#include <iostream>
using namespace std;
int bt4(int x, int n, int A[10])
{
if (n < 0)
{
return 0;
}
if (A[n] == x)
{
return 1 + bt4(x, n - 1, A);
}
else
{
return bt4(x, n - 1, A);
}
}
int main()
{
int x = 6; //so can xet
int n = 10; //so phan tu cua mang
int A[10] = { 2,6,5,8,9,6,7,2,6,9 };
int z;
z = bt4(x, n, A);
if (z > 0)
{
cout << "so X nam trong mang A va xuat hien " << z << " lan";
}
else
cout << "so X khong nam trong mang A";
return 0;
}

BTH:
#include <iostream>
using namespace std;

//Giai thua
int giaiThua(int n)
{
if (n == 1)
return 1;
else
return giaiThua(n - 1) * n;

}
// S= 1! + 2 !.....
int tinhS(int n)
{
if (n == 1)
return 1;
else
return giaiThua(n) + tinhS(n - 1);
}
//In ra nhi phan cua n
void nhiPhan(int n)
{
if (n == 0)
return;
else
nhiPhan(n / 2);
cout << n % 2;
}
//nhi Phan cach 2
int NP(int n)
{
if (n == 0)
return 0;
else
return n % 2 + NP(n / 2) * 10;
}
// In ra so Dao Nguoc
void daoNguoc(int n)
{
if (n == 0)
return;
cout << n % 10;
daoNguoc(n / 10);
}
//Dao nguoc cach 2
int soDN(int n, int sdn)
{
if (n == 0)
return sdn;
else
{
sdn = sdn * 10 + n % 10;
return soDN(n / 10, sdn);
}
}

// dem chu so de qui


int demCS(int n)
{
if (n < 10)
return 1;
else
return 1 + demCS(n / 10);
}
//Chuyen he 2 sang he 10
int luyThua(int x, int y)
{
if (y == 0)
return 1;
else
return x * luyThua(x, y - 1);
}
int thapPhan(int n, int sm)
{
if (n == 0)
return 0;
else
return (n % 10) * luyThua(2, sm) + thapPhan(n / 10, sm + 1);
}
//Uoc chung lon nhat cua 2 so nguyen
int UC(int a, int b)
{
if (b == 0)
return a;
else
return UC(b, a % b);
}
int main()
{
int n, a, b;
cout << "Nhap so n: ";
do
{
cin >> n;
if (n < 0)
cout << "Ban nhap sai. Nhap lai!";
} while (n < 0);
cout << "Giai thua cua " << n << " la: " << giaiThua(n) << endl;
cout << "Nhi phan cua " << n << " la: ";
nhiPhan(n);
cout << endl;
daoNguoc(n);
cout << endl;
cout << n << "co so dao nguoc la: " << soDN(n, 0) << endl;
cout << "Nhap so a: ";
cin >> a;
cout << "Nhap so b: ";
cin >> b;
cout << "Uoc chung lon nhat cua 2 so" << a << " va " << b << " la: " << UC(a, b) << endl;
return 0;
}
ĐỆ QUY TRONG MẢNG 1 CHIỀU:
#include <iostream>
using namespace std;
void nhap(int a[], int n)
{
for (int i = 0; i < n; i++)
{
cout << "a[" << i << "]= ";
cin >> a[i];
}
}
void xuat(int a[], int n)
{
cout << "Mang dang luu tru: ";
for (int i = 0; i < n; i++)
{
cout << a[i] << " ";
}
cout << endl;
}

//Tinh tong trong mang bang de quy


int tinhTong(int a[], int n)
{
if (n == 1)
return a[0];
else
return tinhTong(a, n - 1) + a[n - 1];
}
//Tim so lon nhat trong mang
int Max(int a[], int n)
{
if (n == 1)
return a[0];
int m = Max(a, n - 1);
if (a[n - 1] > m)
return a[n - 1];
return m;
}
int main()
{
int n, a[MAXSIZE];
cout << "Nhap so luong phan tu: ";
do
{
cin >> n;
if (n < 0)
cout << "Ban nhap sai. Nhap lai!" << endl;

} while (n < 0);


nhap(a, n);
xuat(a, n);
cout << "Tong mang = " << tinhTong(a, n) << endl;
cout << "Phan tu max= " << Max(a, n) << endl;
return 0;

//Pascal
#include <iostream>
using namespace std;
int tinhPC(int n, int k)
{
if (k == 0 || k == n)
return 1;
else
return tinhPC(n - 1, k) + tinhPC(n - 1, k - 1);
}
void xuatTG(int n)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j <= i; j++)
{
cout << tinhPC(i, j) << " ";

}
cout << endl;
}
}
int main()
{
int n;
cout << "Nhap so n: ";
do {
cin >> n;
if (n < 0)
cout << "Ban nhap sai. Nhap lai!" << endl;
} while (n < 0);
xuatTG(n);
return 0;
}
CON TRỎ
1. Viết chương trình dùng cấp phát động của con trỏ để viết hàm nhập, xuất 1 mảng số
nguyên r hàng và c cột. Sau đó viết hàm dùng con trỏ để tính tổng các phần tử nào là số nguyên
tố.
#include <iostream>
using namespace std;
void nhap(int** p, int row, int col)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
p[i][j] = rand() % 50;
}
}
}
void xuat(int** p, int row, int col)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
cout << setw(3) << p[i][j];
}
cout << endl;
}
}
bool snt(int n)
{
if (n < 2)
return false;
else
{
for (int i = 2; i <= sqrt((double)n); i++)
{
if (n % i == 0)
return false;
}
}
return true;
}
int sumPrime(int** p, int row, int col)
{
int sumP = 0;
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
if (snt(p[i][j]))
sumP += p[i][j];
}
}
return sumP;
}

int main()
{
int** p, r, c;
cout << "Nhap so dong: "; cin >> r;
cout << "Nhap so cot: "; cin >> c;

p = new int* [r];


for (int i = 0; i < r; i++)
p[i] = new int[c];
nhap(p, r, c);
xuat(p, r, c);
cout << "Tong cac so nguyen to co trong mang la: " << sumPrime(p, r, c) << endl;

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


delete[]p[i];
delete[]p;
p = NULL;

system("pause");
return 0;
}

2. ước chung của 2 số nguyên (cấp phát động)


#include <iostream>
using namespace std;
int main()
{
int a, b, d;
int* pa = &a, * pb = &b;

cout << "Nhap 2 so nguyen: ";


cin >> a >> b;

while (*pb != 0)
{
d = *pa % *pb;
*pa = *pb;
*pb = d;
}
cout << "UCLN cua 2 so nguyen da nhap la: " << *pa << endl;

system("pause");
return 0;
}

//phan tu le, dia chi


int main()
{
int n, maxP = 0;
int* p;

do
{
cout << "Nhap do lon mang: ";
cin >> n;
if (n < 0 || n > MS)
cout << "Khong hop le. Nhap lai.\n";
} while (n < 0 || n > MS);
p = new int[n];

cout << "Mang duoc khoi tao la: \n";


for (int i = 0; i < n; ++i)
{
*(p + i) = rand() % 20;
cout << *(p + i) << " ";
if (*(p + i) > *(p + maxP))
maxP = i;
}

cout << "\nCac phan tu le trong mang la: \n";


for (int i = 0; i < n; ++i)
{
if (*(p + i) % 2 != 0)
cout << *(p + i) << " ";
}
cout << "\nDia chi cua phan tu lon nhat trong mang la: " << &maxP << endl;

delete[ ]p;
p = NULL;
system("pause");
return 0;
}

//Con trỏ trong tập tin


#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct NgayThangNam
{
int ngay, thang, nam;
};
struct SinhVien
{
int MaSV;
string HoSV, TenSV;
NgayThangNam ntn;
double DQT, DCK;
};
void Nhap1SV(SinhVien& sv)
{
cout << "Nhap MSSV: ";
cin >> sv.MaSV;
cin.ignore();
cout << "Nhap ho SV: ";
getline(cin, sv.HoSV);
cout << "Nhap ten SV: ";
getline(cin, sv.TenSV);
cout << "Nhap ngay thang nam sinh: ";
cin >> sv.ntn.ngay >> sv.ntn.thang >> sv.ntn.nam;
cout << "Nhap diem QT: ";
cin >> sv.DQT;
cout << "Nhap diem CK: ";
cin >> sv.DCK;
}
void NhapDSSV(SinhVien*& ds, int& n)
{
cout << "Nhap so luong sinh vien: ";
cin >> n;
while (n <= 0 || n > 50)
{
cout << "Nhap sai. Nhap lai: ";
cin >> n;
}
ds = new SinhVien[n];
for (int i = 0; i <= n - 1; i++)
{
cout << "Nhap thong tin cho sinh vien thu " << i + 1 << endl;
Nhap1SV(ds[i]);
}
}
void GhiTTXuongFile(SinhVien* ds, int n)
{
ofstream outF;
outF.open("D:\\Cau 3.txt");
if (outF.is_open())
{
for (int i = 0; i < n; i++)
{
outF << ds[i].MaSV << endl
<< ds[i].HoSV << " " << ds[i].TenSV << endl
<< ds[i].ntn.ngay << "/" << ds[i].ntn.thang << "/" <<
ds[i].ntn.nam << endl
<< ds[i].DCK << endl
<< ds[i].DQT;
}
cout << "Ghi file thanh cong\n";
outF.close();
}
else
cout << "Mo file khong thanh cong\n";
}
void DocTTTuFile(SinhVien* ds, int n)
{
int KTMaSV;
int diemTK, MaSV;
int ngay, thang, nam;
double diemCK, diemQT;
string Ho, Ten;
bool kiemTra = false;
cout << "Nhap vao Ma SV cua sinh vien can tim: ";
cin >> KTMaSV;
ifstream inF;
inF.open("Cau 3.txt");
if (inF.is_open())
{
while (!inF.eof())
{
inF >> MaSV;
inF.ignore();
getline(inF, Ho, ' ');
getline(inF, Ten);
inF >> ngay;
inF.ignore();
inF >> thang;
inF.ignore();
inF >> nam;
inF >> diemQT;
inF >> diemCK;
if (KTMaSV == MaSV)
{
kiemTra = true;
cout << MaSV << "|" << Ho << " " << Ten << "|" << ngay
<< "|" << (diemQT + diemCK) / 2;
break;
}
}
inF.close();
if (kiemTra == false)
cout << "Khong tim thay sinh vien\n";
}
else
cout << "Doc file that bai\n";
}
int main()
{
SinhVien* ds = 0;
int n = 0;
NhapDSSV(ds, n);
string path;
GhiTTXuongFile(ds, n);
/*DocTTTuFile(ds, n);*/
}

//Đọc tập tin


int main()
{
string s;
ofstream KT;
KT.open("BT4.txt");
cout << "Nhap mot chuoi: ";
getline(cin, s);
if (KT.is_open())
{
KT << s << endl;
cout << "Ghi file thanh cong !" << endl;
KT.close();
}
else
cout << "Ghi file khong thanh cong !" << endl;
return 0;

}
//Đề mẫu
#include <iostream>
#include <string>
#include <fstream>
#include <conio.h>
using namespace std;
const int MAX = 100;
struct DSQG
{
int hcv, hcb, hcd;
string quocG;
};
struct TT
{
string ten, thuDo;
int ds, dt;
};
int tongQG;
string tam;
DSQG dsqg[MAX];
TT ttin[MAX];
void cau1()
{
ifstream DS;
DS.open("D:\\Top20Tokyo.txt");
if (DS.is_open())
{

while (!DS.eof())
{
DS >> tongQG;
DS.ignore(1);
for (int i = 0; i < tongQG; i++)
{
if (i == 19)
getline(DS, dsqg[i].quocG);
else
getline(DS, dsqg[i].quocG, ',');
}
for (int i = 0; i < tongQG; i++)
{
DS >> dsqg[i].hcv;
DS.ignore(1);
DS >> dsqg[i].hcb;
DS.ignore(1);
DS >> dsqg[i].hcd;
DS.ignore(1);
}
}
DS.close();
}
else
cout << "Khong ghi duoc tap tin" << endl;
}
void cau2()
{
int i = 0;
ifstream TTin;
TTin.open("D:\\Top20TokyoInfo.txt");
if (TTin.is_open())
{
getline(TTin, tam);
while (!TTin.eof())
{
getline(TTin, ttin[i].ten, ',');
getline(TTin, ttin[i].thuDo, ',');
TTin >> ttin[i].ds;
TTin.ignore(1);
TTin >> ttin[i].dt;
TTin.ignore(1);
i++;
}
TTin.close();
}
else
cout << "Khong ghi duoc tap tin" << endl;
}
void cau3a()
{
int d, dem = 0;
cout << "Nhap dien tich D: ";
cin >> d;
for (int i = 0; i < tongQG; i++)
{
if (ttin[i].dt <= d)
{
dem++;
if (dem == 1)
cout << "Thong tin cac quoc gia co dien tich <= " << d << " la: " <<
endl;
cout << "Ten nuoc: " << ttin[i].ten << endl;
cout << "Thu do: " << ttin[i].thuDo << endl;
cout << "Dien tich: " << ttin[i].dt << endl;
cout << "Dan so: " << ttin[i].ds << endl;
}

}
if (dem == 0)
cout << "Khong co nuoc nao co dien tich <= " << d << endl;
}
void cau3b()
{
int v, dem = 0;
cout << "Nhap so huy chuong vang V: ";
cin >> v;
for (int i = 0; i < tongQG; i++)
{
if (dsqg[i].hcv <= v)
{
for (int j = 0; j < tongQG; j++)
{
if (dsqg[i].quocG == ttin[j].ten)
{
dem++;
if (dem == 1)
cout << "Thong tin cac quoc gia co huy chuong vang
<= " << v << " la: " << endl;
cout << "Ten nuoc: " << ttin[j].ten << endl;
cout << "Thu do: " << ttin[j].thuDo << endl;
cout << "Dien tich: " << ttin[j].dt << endl;
cout << "Dan so: " << ttin[j].ds << endl;
}
}

}
if (dem == 0)
cout << "Khong co quoc gia nao co huy chuong vang <=" << v << endl;
}
void cau3c()
{
string c, n;
int dem = 0;
cin.ignore();
cout << "Nhap thu do C: ";
getline(cin, c);
for (int i = 0; i < tongQG; i++)
{
if (c == ttin[i].thuDo)
{
n = ttin[i].ten;
for (int j = 0; j < tongQG; j++)
{
if (n == dsqg[j].quocG)
{
dem++;
if (dem == 1)
cout << "Huy chuong cua thu do " << c << " la: " <<
endl;
cout << "Huy chuong vang: " << dsqg[j].hcv << endl;
cout << "Huy chuong bac: " << dsqg[j].hcb << endl;
cout << "Huy chuong dong: " << dsqg[j].hcd << endl;
}
}
}
}
if (dem == 0)
cout << "Khong co thu do" << c << endl;
}
int Min(TT ttin[], int tongQG)
{
if (tongQG == 1)
return ttin[0].dt;
int m = Min(ttin, tongQG - 1);
if (ttin[tongQG - 1].dt > m)
return m;
return ttin[tongQG - 1].dt;
}

int main()
{
int chon;
cau1();
cau2();
do
{
system("cls");
cout << "-----------------MENU----------------" << endl;
cout << "1.Nhap so d, In ra thong tin cac nuoc co dien tich <= D" << endl;
cout << "2.Nhap v. In ra thong tin cac nuoc co huy chuong vang <= V" << endl;
cout << "3.Nhap c. In ra huy chuong cua nuoc co thu do C" << endl;
cout << "4.Tim dien tich nho nhat trong cac nuoc" << endl;
cout << "5.Ket thuc" << endl;
cout << "Ban chon: ";
cin >> chon;
switch (chon)
{
case 1:
cau3a();
break;
case 2:
cau3b();
break;
case 3:
cau3c();
break;
case 4:
for (int i = 0; i < tongQG; i++)
{
if (Min(ttin, tongQG) == ttin[i].dt)
{
cout << "Ten nuoc: " << ttin[i].ten
<< "\nThu do: " << ttin[i].thuDo
<< "\nDan so: " << ttin[i].ds
<< "\nDien tich: " << ttin[i].dt << endl;
}
}
break;
default:
cout << "Ban chon thoat!" << endl;
}
_getch();

} while (chon >= 1 && chon <= 4);


return 0;
}

You might also like