In KTLT PDF
In KTLT PDF
In KTLT PDF
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;
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;
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);
}
}
//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;
system("pause");
return 0;
}
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;
}
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];
delete[ ]p;
p = NULL;
system("pause");
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();