Professional Documents
Culture Documents
Ma trận:
#include <iostream>
#define max 10
int n;
class matran
public:
int mt[max][max];
matran()
mt[i][j] = 0;
void nhapsl()
cout << "Ma tran thu [" << i << "][" << j << "]: ";
void insl()
};
matran c;
return c;
matran c;
return c;
matran c;
return c;
int main()
matran a,b,c,d;
cin >> n;
a.nhapsl();
b.nhapsl();
system("cls");
a.insl();
cout << "\n Ma tran B:\n";
b.insl();
c = a + b;
c.insl();
c = a - b;
c.insl();
c = a * b;
c.insl();
system("pause");
2. Date time
#include<iostream.h>
#include <conio.h>
using namespace std;
class date1
{
private:
int day;
int month;
int year;
static const int days[];
void helpIncrement();
public:
date1(int d=1,int m=1,int y=1900);
void setdate(int d,int m,int y);
date1 &operator++();
date1 &operator++(int);
date1 &operator+=(int addday);
bool leapyear(int y);//kiem tra nam nhuan
bool endofmonth(int d);//kiem tra ngay cuoi thang
friend ostream & operator<<(ostream & out,date1 &date);
};
3. Stack
#include <conio.h>
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include<fstream>
#include<cstring>
class Node
{
public:
int Data;
Node*Next;
public:
P->Next = NULL;
P->Data = x;
return P;
};
{ private:
Node * top;
public:
top = 0;
{}
if(top==0)
return 1;
else
return 0;
{
Node *P = top;
int i=0;
while (P != NULL) //trong khi chua het Stack thi van duyet
i++;
P = P->Next;
return i;
Node *P = MakeNode(a);
P->Next = top;
top = P;
return 1;
return i;
if (!isemty())
return x;
void print ()
{
Node *P = top;
while (P != NULL)
cout<<""<<P->Data<<" , ";
P = P->Next;
printf("\n");
};
main ()
int i = 0;
Cstack s;
int luachon;
char filename[30];
int kt =0 ;
do
cin>>luachon;
switch(luachon)
case 1:
system("cls");
if(s.isemty() ==1 )
cout<<"\nStack rong";
}
else
cout<<"\n---------------------------------\n";
break;
case 2:
system("cls");
cout<<"\n---------------------------------\n";
break;
case 3:
system("cls");
int them;
if (s.push(them) == 1 )
else
cout<<"\n---------------------------------\n";
break;
case 4:
system("cls");
if(s.isemty() ==1 )
{
cout<<"\nStack rong ! Khong co phan tu de lay !";
else
cout<<"\n---------------------------------\n";
break;
case 5:
system("cls");
cout<<"\n---------------------------------\n";
break;
case 6:
system("cls");
s.print();
cout<<"\n---------------------------------\n";
break;
case 9:
break;
default:
system("cls");
cout<<"\n---------------------------------\n";
}while(luachon!=9);
return 0;
public:
sinhvien(){};
sinhvien(char ht[50], int msv1, float d1,float d2, float d3)
{
strcpy(hoten,ht);
msv=msv1;
diemtoan=d1;
diemanh=d2;
diemvan=d3;
}
friend istream & operator >>(istream &is, sinhvien &r)
{
fflush(stdin);
cout<<"nhap ho ten: "<<endl;
is.getline(r.hoten,50);
}
friend ostream & operator <<(ostream &os, sinhvien &r)
{
cout<<"hoten "<<endl;
os<<r.hoten;
cout<<endl<<"msv ";
os<<r.msv;
cout<<endl<<"diem toan ";
os<<r.diemtoan;
cout<<endl<<"diem anh ";
os<<r.diemanh;
cout<<endl<<"diem van ";
os<<r.diemvan;
}
float dtb()const;
};
void swap(float *num1, float *num2)
{
float temp;
temp = *num1;
*num1 = *num2;
*num2 = temp;
}
float sinhvien::dtb()const
{
return ((diemtoan+diemanh+diemvan)/3);
}
int main()
{
int n;
sinhvien *sv;
sv=new sinhvien[n];
cout<<"nhap so luong sv";
cin>>n;
for (int i=0;i<n;i++)
{
cout<<""<<i+1<<endl;
cin>>sv[i];
}
for (int i=0;i<n;i++)
{
cout<<sv[i]<<endl;
}
cout<<endl<<"sau khi sap xep";
for (int i=0;i<n;i++)
for (int j=i+1;j<n;j++)
{
if (sv[i].dtb()<sv[j].dtb())
{
swap(sv[i],sv[j]);
}
}
#include <iostream>
#include<cstring>
using namespace std;
class dssv;
class sinhvien
{
char hoten[50];
int msv;
float diemtoan;
float diemanh;
float diemvan;
public:
sinhvien(){};
sinhvien(char ht[50], int msv1, float d1,float d2, float d3)
{
strcpy(hoten,ht);
msv=msv1;
diemtoan=d1;
diemanh=d2;
diemvan=d3;
}
void nhap()
{
fflush(stdin);
cout<<"nhap ho ten: "<<endl;
cin>>hoten;
cout<<"nhap ma sinh vien:"<<endl;
cin>>msv;
cout<<"nhap diem toan:"<<endl;
cin>>diemtoan;
cout<<"nhap diem anh:"<<endl;
cin>>diemanh;
cout<<"nhap diem van:"<<endl;
cin>>diemvan;
}
void in()
{
cout<<"hoten "<<endl;
cout<<hoten;
cout<<endl<<"msv ";
cout<<msv;
cout<<endl<<"diem toan ";
cout<<diemtoan;
cout<<endl<<"diem anh ";
cout<<diemanh;
cout<<endl<<"diem van ";
cout<<diemvan<<endl;
}
float dtb()const
{
return ((diemtoan+diemanh+diemvan)/3);
}
friend class dssv;
};
class dssv
{
sinhvien *a;
int n;
public:
dssv(){
}
~dssv(){
}
void nhap()
{
cout<<"nhap so luong sv:";
cin>>n;
a=new sinhvien[n];
for (int i=0;i<n;i++)
{
cout<<"nhap sv thu "<<i+1<<endl;
a[i].sinhvien::nhap();
}
}
void in()
{
for (int i=0;i<n;i++)
{
a[i].sinhvien::in();
}
}
void sapxep()
{
sinhvien temp;
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
if(a[i].dtb()>a[j].dtb()){
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
}
};
int main()
{
dssv d;
d.nhap();
d.in();
cout<<"sau khi sap xep: "<<endl;
d.sapxep();
d.in();
return 0;
6. Bài Phân Số
#include <iostream>
#include <cmath>
class phanso{
int tu,mau;
public:
tu=a;
mau=b;
~phanso()
friend istream & operator >> (istream & is, phanso & r)
{
cout<<"Nhap vao tu so: ";
is>> r.tu;
is>> r.mau;
return is;
friend ostream & operator << (ostream & os, phanso &r);
return phanso(a1,b1);
return phanso(a1,b1);
int a1 = tu * r.mau;
return phanso(a1,b1);
int a1 = tu * r.tu;
return phanso(a1,b1);
}
int a1 = tu * r.mau;
int a1 = tu * r.mau;
int rutgon();
};
int phanso::rutgon()
int a = tu;
int b = mau;
while ( a != b)
if (a > b)
a = a - b;
else
b = b - a;
}
}
return a;
return os;
int main()
phanso ps[100];
int n,i,choose;
cin >> n;
for(i=0;i<n;i++)
cout<< "Nhap thong tin phan so thu " << i+1 << ":"<< endl;
cout<<"(0) Thoat...!"<<endl;
while (choose != 0)
if(choose == 1)
{
cout<<"Mang vua nhap:"<<endl;
for(i=0;i<n;i++)
cout<<ps[i];
if(choose == 2)
phanso s(0,1);
for(i=0;i<n;i++)
s=s+ps[i];
if(choose == 3)
phanso s1 = ps[0];
for(i=1;i<n;i++)
s1=s1-ps[i];
if(choose == 4)
phanso s2(1,1);
for(i=0;i<n;i++)
s2=s2*ps[i];
if(choose == 5)
phanso s3=ps[0];
for(i=1;i<n;i++)
s3=s3/ps[i];
return 0;
7 . Hàng đợi
#include <iostream>
class queue
private:
int nodes[Max];
public:
front = 0;
tail = -1;
void nhap()
cin>>y;}
void in()
cout<<y<<" ";
void insert(int x); // them mot nut vao cuoi hang doi
};
int queue::empty()
int queue::full()
void queue::insert(int x)
if(full())
else
nodes[++tail] = x;
int queue::remove()
if(empty())
else
return(nodes[front++]);
int queue::queuesize()
}
int main()
queue *q1;
int i,n,x,y;
cin>>n;
q1=new queue[n];
for (i=0;i<n;i++)
q1->nhap();
cout<<"queue: ";
for (i=0;i<n;i++)
q1->in();
cin>>x;
q1->insert(x);
return 0;
8. Class Time
#include<iostream.h>
#include<conio.h>
#include<math.h>
using namespace std;
class Time
{
int h,m,s;
public:
Time(){h=m=s=0;}
Time(int h1,int m1, int s1)
{
h=h1;
m=m1;
s=s1;
}
friend istream &operator>>(istream &is,Time &a);
friend ostream &operator<<(ostream &os,Time &a);
friend Time operator+(Time a,Time b);
Time operator+(int ss);
friend Time operator+(int ss,Time a);
Time operator-(int ss);
int operator-(Time b);
friend int operator==(Time a,Time b)
{
return (int)(doi(a)==doi(b));
}
friend int doi(Time a);
friend Time doi(int n);
};
int doi(Time a)
{
return (int)a.h*3600+a.m*60+a.s;
}
Time doi(int n)
{
Time a;
a.h=n/3600;
a.m=n%3600/60;
a.s=n%60;
return a;
}
istream &operator>>(istream &is,Time &a)
{
cout<<”\n\aNhap thoi gian:”;
cout<<”\nNhap gio: “;
is>>a.h;
cout<<”\nNhap phut: “;
is>>a.m;
cout<<”\nNhap giay: “;
is>>a.s;
return is;
}
ostream &operator<<(ostream &os,Time &a)
{
os<<a.h<<” : “<<a.m<<” : “<<a.s<<endl;
return os;
}
Time operator+(Time a,Time b)
{
Time c;
c.h=a.h+b.h;
c.m=a.m+b.m;
c.s=a.s+b.s;
return doi(doi(c));
}
Time Time::operator+(int ss)
{
Time b;
b.h=h;
b.m=m;
b.s=s+ss;
return doi(doi(b));
}
Time operator+(int ss,Time a)
{
Time b;
b.h=a.h;
b.m=a.m;
b.s=a.s+ss;
return doi(doi(b));
}
Time Time::operator-(int ss)
{
Time b;
b.h=h;
b.m=m;
b.s=s-ss;
return doi(doi(b));
}
int Time::operator-(Time b)
{
Time c;
c.h=h-b.h;
c.m=m-b.m;
c.s=s-b.s;
return doi(c);
}
int main()
{
Time a,b,c;
int x;
cout<<”Nhap Thoi gian a:”<<endl;
cin>>a;
cout<<”\nNhap Thoi gian b:”<<endl;
cin>>b;
//Cong
c=a+b;
cout<<”\nCong Thoi gian: a+b= “<<c;
cout<<”\nNhap so giay muon cong them : “;
cin>>x;
c=a+x;
cout<<”\nCong Thoi gian voi giay: a+x= “<<c;
c=x+a;
cout<<”\nCong giay voi thoi gian: x+a= “<<c;
//Tru
int kq=a-b;
cout<<”\nTru Thoi gian: a-b= “<<kq<<” (s)”<<endl;
cout<<”\nNhap thoi gian de tru : “;
cin>>x;
c=a-x;
cout<<”\nKet qua : “<<c<<endl;
cout<<”\nSo sanh a va b: “;
if (a==b) cout<<” a==b “;
else cout<<” a!=b “;
system(“pause”);
return 0;
}
9. Thừa kế, đa thể.
Sử dụng kỹ thuật thừa kế, đa thể cài đặt các lớp CKH (cho khách hàng gửi có
kỳ hạn), KKH (cho khách hàng gửi không kỳ hạn) với các thông tin và các chức
năng bổ sung, sửa đổi (nếu có). Trong cả 2 lớp CKH, KKH phải đưa ra số tiền cuối
cùng khách hàng được nhận về (cả gốc và lãi).
Viết hàm nhập, xuất một danh sách n khách hàng của ngân hàng trên. Với n
là một số nguyên dương n<=100.
main(){
KH *kh[100];
int i, n;
cout<<"Nhap so khach hang: ";
cin>>n;
for(i=0; i<n; i++)
{
int choose;
cout<<"Khong ky han (1), Co Ky han (2): ";
cin>>choose;
if(choose==1)
kh[i] = new KKH();
if(choose==2)
kh[i] = new CKH();
kh[i]->Nhap();
}