Câu 1: Xây dựng lớp số phức
//Xay dung lop so phuc
#include<iostream>
using namespace std;
class SP {
//khai bao tp du lieu
private:
int pt;
int pa;
//khai bao ham phuong thuc
public:
void nhap();
void in();
};
void SP::nhap() {
cout<<"\n Nhap phan thuc:"; cin>>pt;
cout<<"\n Nhap phan ao: "; cin>>pa;
}
void SP::in() {
cout<}
int main() {
SP a,b,c[5];
a.nhap();
a.in();
}
Câu 2: Lớp phân số và các thao tác với phân số
1
#include<iostream>
#include<math.h>
using namespace std;
/* chuong trinh phan so
du lieu: tu so, mau so
phuong thuc: nhap, toi gian, in tinh tong
*/
class PS {
//khai bao thanh phan du lieu
private: //khai bao quyen truy xuat du lieu cua bien luon la private
int ts, ms;
//khai bao phuong thuc thanh phan:
public:
void nhap();
void toigian(PS &pso);
void in(){
cout<
};
};
//ham nhap
void PS::nhap() { // neu ko co PS:: thi ham nhap() o day la ham nhap tu do
cout<<"\n Nhap tu so:"; cin>>ts;
cout<<"\n Nhap mau so:"; cin>>ms;
/* kiem tra xem mau so co khac 0 hay ko
*/
};
//ham toi gian phan so
2
void PS::toigian(PS &pso) {
int a=pso.ts;
int b=pso.ms;
while (a != b){
if (a > b) a -= b;
else b -= a;
}
pso.ts = pso.ts / a;
pso.ms = pso.ms / a;
};
int main() {
PS p; //p la doi tuong cua lop phan so, p co kieu dl la phan so
//cin>>p.ts; // neu khong khai bao quyen truy xuat, buoc nay se bi loi not accessible
p.nhap(); //neu phuong thuc ko de public thi se ko co quyen xuat buoc nay
p.toigian(p);
cout<<"Phan so vua nhap la: ";
p.in();
return 0;
}
Câu 4: Xây dựng mảng 1 chiều
//nhap vao mang 1 chieu co n phan tu
#include<iostream>
using namespace std;
class mang {
private:
int n;
3
int a[100];
public:
mang()
{
n = 5;
for(int i=1; i
}
void nhap()
{
cout<<"Nhap so phan tu: "; cin>>n;
for (int i=1; i<=n; i++)
{
cout<<"\na["<
}
}
void in()
{
for(int i=1; i<=n; i++)
cout<
}
mang operator+(mang m)
{
mang kq;
if (n!=m.n)
{
cout<<"Khong cong duoc";
} else
4
{
kq.n=m.n;
for(int i=1; i<=n; i++)
kq.a[i] = a[i]+m.a[i];
}
return kq;
}
friend int layspt(mang x);
friend ostream &operator<<(ostream &os, mang x);
friend istream &operator>>(istream &is, mang &x);
};
ostream &operator<<(ostream &os, mang x)
{
os<
for(int i=1; i<= x.n; i++)
os<
return os;
}
int layspt(mang x)
{
int kq;
kq=x.n;
return kq;
}
int main()
{
mang a, b;
5
a.nhap();
b.nhap();
(a+b).in();
return 0;
}
Câu 5: Nhập và in ra sinh vien có sử dụng is-ostream
#include <iostream>
#include <string>
using namespace std;
// Xay dung lop nguoi
// DL: hoten, nam sinh, gioi tinh
class Nguoi {
private:
char hoTen[30], gioiTinh[5];
int namSinh;
public:
void nhap();
/* {cout<<"Nhap ho ten:"; cin.getline(ht,30);
cout<<*/
void in() {
cout<
}
friend ostream& operator<<(ostream& os, const Nguoi &a);
friend istream& operator<<(istream& is, Nguoi &a);
};
6
//ham nhap
istream& operator>>(istream& is, Nguoi &a) {
cout<
fflush(stdin);
cin>>a.namSinh;
cout<
fflush(stdin);
}
int main {
Nguoi a;
void nhap();
void in();
}
Câu 6: Quản lý sinh viên (masv, hoten, diem,..)
/* Lop sinh vien
DL: masv, hoten, masv, diem
PT: nhap, in,.. */
#include<iostream>
using namespace std;
class SV {
char masv[10];
char hoten[30];
int ns;
char lop[8];
float diem;
friend float xemdiem(SV x); //ham ban cua lop sv
7
public:
void nhap();
void in();
float getdiem();
};
void SV::nhap() {
cout<<"Nhap ma sinh vien: "; cin>>masv;
cout<<"Nhap ho ten: "; cin.getline(hoten,30);
cin.ignore();
cout<<"Nhap diem thi: "; cin>>diem;
}
void SV::in(){
cout<
}
float xemdiem(SV x){
return x.diem;
}
int main(){
int n,i;
SV ds[100];
cout << "Nhap so luong sinh vien: ";cin>>n;
for(i = 1;i <= n;i++){
cout<< "SV thu " << i << endl;
ds[i].nhap();
}
for(i = 1;i <= n;i++){
cout<< "SV thu " << i << endl;
8
ds[i].in();
}
cout<
return 0;
}
Câu 7: Quản lý lớp sinh viên kế thừa từ lớp NGUOI
/*Xay dung lop nguoi
DL: hoten, nam sinh, gioi tinh
PT: nhap, in, >>, <<*/
#include <iostream.h>
class NGUOI
{ char ht[30];
int ns;
char gt[5];
public:
void nhap()
{ cout<<"\nNhap ho ten:";cin.getline(ht,30);
cout<<"\nNhap nam sinh:";cin>>ns;
cout<<"\n Nhap gioi tinh:";cin>>gt;
}
void in()
{ cout<
}
};
class SV:NGUOI
{ char lop[8];
float dt;
9
public:
void nhap()
{ NGUOI::nhap();
cout<<"\nNhap lop: ";cin>>lop;
cout<<"\nNhap diem: ";cin>>dt;
}
void in()
{ NGUOI::in();
cout<
}
};
void main()
{
// NGUOI p;
// p.nhap();
// p.in();
SV s;
s.nhap();
s.in();
}
Câu 8: Tính lương nhân viên theo biên chế và hợp đồng:
#include <iostream>
using namespace std;
class Nhanvien{
protected:
string ht,ns,gt;
public:
10
void nhapNV(){
cin.ignore();
cout << "\nHo va ten nhan vien: ";
getline(cin, ht);
cout << "\nNgay sinh: "; cin >> ns;
cout << "\nGioi tinh: "; cin >> gt;
}
void inNV(){
cout << "\n Ho ten: " << ht << "\n Ngay sinh: " << ns << "\n Gioi tinh: " << gt;
}
};
class Bienche:public Nhanvien{
protected:
string hsl;
float tien;
public:
void nhap(){
nhapNV();
cout << "\nHe so luong: "; cin >> hsl;
cout << "\nTien phu cap chuc vu: "; cin >> tien;
}
void in(){
inNV();
cout << "\n He so luong: " << hsl;
cout << "\n Tien phu cap chuc vu: " << tien << endl;
}
};
11
class Hopdong:public Nhanvien{
protected:
string hsvg;
float tc,snlv;
public:
void nhap(){
nhapNV();
cout << "\nTien cong lao dong: "; cin >> tc;
cout << "\nSo ngay lam viec: "; cin >> snlv;
cout << "\nHe so vuot gio: "; cin >> hsvg;
}
void in(){
inNV();
cout << "\n Tien cong lao dong: " << tc;
cout << "\n So ngay lam viec: " << snlv;
cout << "\n He so vuot gio: " << hsvg << endl;
}
};
int main()
{
Bienche a;
a.nhap();
a.in();
Hopdong b;
b.nhap();
b.in();
}
12
Câu 9: Nhập và in ra danh sách thông tin sinh viên và giáo viên kế thừa từ lớp người:
#include <iostream>
#include<string>
#include<string.h>
#include<stdio.h>
using namespace std;
class NGUOI{
protected:
string ht,gt;
int tuoi;
public:
void Nhap()
{
cout <<"\nHo va ten: ";
fflush(stdin);
getline(cin,ht);
cout << "Gioi tinh: ";
getline(cin,gt);
cout<<"Nhap tuoi : ";
cin>>tuoi;
}
void In()
{
cout<
cout<
cout<
}
13
};
class SINHVIEN:public NGUOI {
protected:
string lop;
float diem;
public:
void Nhap(){
NGUOI::Nhap();
cout << "Nhap lop : ";
fflush(stdin);
getline(cin,lop);
cout << "Nhap diem : ";
cin >> diem;
}
void In()
{
NGUOI::In();
cout<
cout<
}
};
class GIAOVIEN: public NGUOI{
protected:
string boMon;
float hsLuong,phuCap;
public:
void Nhap()
14
{
NGUOI::Nhap();
cout <<"Nhap ten bo mon : " ;
fflush(stdin);
getline(cin,boMon);
cout << "Nhap he so luong : "; cin >> hsLuong;
cout << "Nhap phu cap : "; cin >>phuCap;
}
void In()
{
NGUOI::In();
cout<
cout<
cout<
cout<
}
float TinhLuong()
{
float luong;
luong = 540*hsLuong + phuCap;
return luong;
}
};
int main()
{
SINHVIEN sv[100];
15
GIAOVIEN gv[100];
int n;
cout<<"Nhap so luong sinh vien va giao vien: " ;cin >> n;
for(int i = 0; i < n; i++){
cout<
sv[i].Nhap();
}
for(int i = 0; i < n; i++)
{
cout<
//sv[i].TTnguoi();
gv[i].Nhap();
}
cout << "Danh sach thong tin sinh vien ";
for(int i = 0; i < n; i++)
{
cout <
sv[i].In();
}
cout <
for(int i = 0; i < n; i++)
{
cout <
gv[i].In();
}
return 0;
16
}
Câu 10: Hóa đơn vật tư:
#include <iostream>
using namespace std;
class vl {
private:
char tenvt[30];
char loaiphieu[30];
int mavt, date;
float dg,kl;
public:
void nhap();
float tt();
void in();
};
void vl::nhap() {
cout<<"\n nhap loai phieu: "; cin.getline(loaiphieu,20);
cout<<"\n nhap ma vat tu: "; cin>>mavt;
cout<<"\n nhap ten vat tu: "; cin.getline(tenvt,20);
cout<<"\n nhap ngay lap: "; cin>>date;
cout<<"\n nhap khoi luong: "; cin>>kl;
cout<<"\n nhap don gia: "; cin>>dg;
}
void vl::in(){
cout<<(dg*kl);
}
int main()
17
{
vl s;
s.nhap();
cout<<"\nThanh tien: ";
s.in();
}
Câu 11: Nhập thông tin và điểm sinh viên, in ra điểm trung bình
#include<iostream>
using namespace std;
/* Du lieu: ho ten, gioi tinh, ngay sinh, diem tin, diem toan,
tinh diem trung binh, in
*/
class SV {
//nhap thong tin sinh vien
private:
char hoten[30];
char gioitinh[5];
char ns[15];
float dTin, dToan, dNN;
//khai bao ham phuong thuc
public:
void nhap();
void in();
};
//nhap vao thong tin sinh vien
void SV::nhap() {
cout<<"\n Nhap ho ten sinh vien: "; cin.getline(hoten,30);
18
cout<<"\n Gioi tinh: "; cin.getline(gioitinh,5);
cout<<"\nNgay sinh: "; cin.getline(ns,15);
cout<<"\n Nhap vao diem Tin: "; cin>>dTin;
cout<<"\n Nhap vao diem Toan: "; cin>>dToan;
cout<<"\n Nhap vao diem Ngoai Ngu: "; cin>>dNN;
}
void SV::in() {
cout<<(dTin+dToan+dNN)/3;
}
int main() {
SV a,b,c,d;
a.nhap();
cout<<"\n Diem trung binh:";
a.in();
return 0;
}
12. Tìm min max của dãy số thực
#include <iostream>
using namespace std;
int main()
{
int a[100],n;
cout << "nhap n = ";cin >> n;
for(int i = 0; i < n; i++){
cout << "a" <<"["<< i <<"]" << "=";
cin >> a[i];
}
int max = a[0], min = a[0];
for (int i = 0; i < n; i++) {
if (max < a[i]){
max = a[i];
19
}
if (min > a[i]){
min = a[i];
}
}
cout << "gia tri min trong day :"<< min << endl;
cout << "gia tri max rong day :"<< max << endl;
return 0;
}
13. Chuong trinh tinh tong S = 1 + 1/2 + 1/3 +...+ 1/n
#include<iostream>
using namespace std;
int main(){
int n;
float s = 0;
cout << "nhap n = ";cin >>n;
for(int i = 1; i <= n; i++){
s += 1.0f*1/i;
}
cout << "tong s = "<< s << endl;
return 0;
}
14. Chuong trinh xay dung thuat toan sx selectionsort
#include<iostream>
using namespace std;
void nhap(int a[],int &n){
cout << "nhap so phan tu mang:";cin >> n;
for(int i = 0; i < n; i++){
cout <<"a[" << i << "]" <<" = ";cin >> a[i];
}
}
void xuat(int a[],int &n){
for(int i = 0; i < n; i++){
cout << a[i] << "\t";
}
}
void Selectionsort(int a[],int n){
20
int i,j,min;
for(i = 0; i < n - 1; i++){
min = i;
for(j = i + 1; j < n; j++){
if(a[j] < a[min]){
min = j;
int t = a[min];
a[min]= a[i];
a[i] = t;
}
}
}
}
int main(){
int a[100],n;
nhap(a,n);
Selectionsort(a,n);
cout <<"mang sau khi sx la : ";
xuat(a,n);
cout << endl;
return 0;
}
15. Chuong trinh xay dung thuat toan sx insert
#include<iostream>
using namespace std;
void nhap(int a[],int &n){
cout << "nhap so phan tu mang:";cin >> n;
for(int i = 0; i < n; i++){
cout <<"a[" << i << "]" <<" = ";
cin >> a[i];
}
}
void xuat(int a[],int &n){
for(int i = 0; i < n; i++){
cout << a[i] << "\t";
}
}
void InsertSort(int a[], int n)
21
{
int t, j;
for (int i = 1; i
j = i - 1;
t = a[i];
while (t<a[j] && j >= 0){
a[j + 1] = a[j];
j--;
}
a[j + 1] = t;
}
}
int main(){
int a[100],n;
nhap(a,n);
InsertSort(a,n);
cout <<"mang sau khi sx la : ";
xuat(a,n);
cout << endl;
return 0;
}
16. Chuong trinh xay dung thuat toan sx bublesort
#include<iostream>
using namespace std;
void nhap(int a[],int &n){
cout << "nhap so phan tu mang:";cin >> n;
for(int i = 0; i < n; i++){
cout <<"a[" << i << "]" <<" = ";
cin >> a[i];
}
}
void xuat(int a[],int &n){
for(int i = 0; i < n; i++){
cout << a[i] << "\t";
}
}
void BubbleSort(int a[],int n){
for(int i = 0; i < n -2; i++){
22
for(int j = n -1; j > i; j++){
if(a[j] < a[j - 1]){
int t = a[j];
a[j] = a[j - 1];
a[j - 1] = t;
}
}
}
}
int main(){
int a[100],n;
nhap(a,n);
BubbleSort(a,n);
cout <<"mang sau khi sx la : ";
xuat(a,n);
cout << endl;
return 0;
}
15. xay dung lop danh sach sv : hten,ngay,gt,dtb...
#include<iostream>
#include<string>
using namespace std;
class sv{
private :
float dtoan,dtin,dnn,dtb;
string ht,gt;
int ngay,thang,nam;
public :
void nhap();
void xuat();
};
void sv::nhap(){
cout << "nhap ho ten : ";cin.ignore();getline(cin,ht);
cout << "nhap ngay sinh/thang/nam sinh: ";cin >> ngay >> thang >> nam;
cout << "nhap gioi tinh: ";cin.ignore();getline(cin,gt);
cout << "nhap diem toan: ";cin >> dtoan;
cout << "nhap diem tin: ";cin >> dtin;
cout << "nhap diem ngoai ngu: ";cin >> dnn;
dtb = (dtoan + dtin + dnn)/3;
}
void sv::xuat(){
23
cout << "ho ten: "<< ht << endl;
cout << "ngay/thang/nam sinh: "<< ngay <<"/"<< thang<<"/"<< nam << endl;
cout << "gioi tinh: "<< gt << endl;
cout << "diem toan: "<< dtoan << endl;
cout << "diem tin : "<< dtin << endl;
cout << "diem ngoai ngu: "<< dnn << endl;
cout << "diem trung binh: "<< dtb << endl;
}
int main(){
sv a;
int n,i;
cout << "nhap so sinh vien: ";cin >> n;
for(int i = 0; i < n; i++){
cout << "STT: " << i+1 << endl;
a.nhap();
a.xuat();
}
return 0;
}
16. /*xay dung ctdl mo ta cac hoa don
du lieu:mavt,tenvt,loaipieu,ngay lap,kg,dgia,thanh tien
phuong thuc : nhap,tinh thanh tien,in */
#include<iostream>
#include<string>
using namespace std;
class VTu{
private :
float kg,dgia,tien;
string tenvt;
int mavt,ngay,thang,nam;
public :
void nhap();
void xuat();
};
void VTu::nhap(){
cout << "ten vat tu: ";cin.ignore();getline(cin,tenvt);
cout << "ma vat tu: ";cin >> mavt;
cout << "ngay/thang/nam lap: ";cin >> ngay >> thang >> nam;
cout << "so luong: ";cin >> kg;
cout << "don gia: ";cin >> dgia;
24
}
void VTu::xuat(){
cout << "ten vat tu: "<< tenvt << endl;
cout << "ma vat tu: "<< mavt << endl;
cout << "ngay/thang/nam lap: "<< ngay<<"/"<
cout << "so luong: "<< kg << endl;
cout << "don gia: "<< dgia << endl;
tien = kg * dgia;
cout << "tong tien :"<< tien <
}
int main(){
int n,i;
VTu b;
cout << "nhap so luong vat tu: ";cin>>n;
for(i = 1;i <= n;i++){
cout<< "VT thu " << i << endl;
b.nhap();
b.xuat();
}
return 0;
}
17. /*chung trinh xay dung lop ma tran
phuong thuc: nhap,inkq
input : so hang, so cot
#include<iostream>
using namespace std;
class MTran{
private:
int m,n;
int a[50][50];
public:
void nhap();
void xuat();
};
void MTran :: nhap(){
cout << "nhap so cot m: ";cin >> m;
cout << "nhap so hang n: ";cin >>n;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
cout << "a["<< i <<"]["<
25