Anda di halaman 1dari 24

Đa giác (bao)

#include<conio.h>
#include<iostream.h>
#include<math.h>
class Diem{
private:
float x,y;
public:
Diem();
Diem(Diem &d);
~Diem();
friend ostream &operator << (ostream &os,Diem &d);
friend istream &operator >> (istream &is,Diem &d);
float kc(Diem &d);
};

class DaGiac{
private:
Diem *P;
int n;
public:
DaGiac();
~DaGiac();
float chuvi();
friend ostream &operator << (ostream &os,DaGiac &d);
friend istream &operator >> (istream &is,DaGiac &d);
};

Diem::Diem(){
x = 0;
y = 0;
}

Diem::Diem(Diem &d){
x = d.x;
y = d.y;
}

Diem::~Diem(){
x = 0;
y = 0;
}

ostream &operator <<(ostream &os,Diem &d){

os << "(" << d.x << "," << d.y << ")";
return os;
}

cout << "x= ";
1
is >> d.x;
cout << "y= ";
is >> d.y;
return is;
}

float Diem::kc(Diem &d){

return sqrt((d.x-x)*(d.x-x)+(d.y-y)*(d.y-y));
}

DaGiac::DaGiac(){
n = 0;
P = NULL;
}

DaGiac::~DaGiac(){
int i;

delete P;
n = 0;
}

float DaGiac::chuvi(){
float tmp = 0;
int i;

for(i=0;i<n;i++)
tmp = tmp + P[i].kc(P[(i+1)%n]);

return tmp;
}

ostream &operator << (ostream &os, DaGiac &t){

int i;

for(i=0;i<t.n;i++)
os << " A" << i << t.P[i];

return os;
}

int i;

cout << "Nhap so diem cua da giac : ";

is >> t.n;
t.P = new Diem[t.n];

for(i=0;i<t.n;i++)
{
2
cout << "Toa do diem thu " << i << endl;
cin >> t.P[i];
}

return is;
}

int main(){
DaGiac dg;

clrscr();
cout << "Nhap da giac: " << endl;
cin >> dg;

cout << "Chu vi da giac " << dg.chuvi() << endl;

getch();
return 0;
}

Đa thức(lớp)
#include <conio.h>
#include <iostream.h>
class DaThuc{
private:
int n;
double *a;
public:
DaThuc();
float operator [](int b);
float operator ^(float x);
DaThuc operator +(const DaThuc &d);
DaThuc operator *(DaThuc &d);
DaThuc operator -();
friend ostream &operator <<(ostream &os,DaThuc d);
friend istream &operator >>(istream &is, DaThuc &d);
};

DaThuc::DaThuc(){
n = -1;
a = NULL;
}

float DaThuc::operator [](int b){

if(b<0)
return double(n);
else
3
return a[b];
}

DaThuc tmp;
int i;

if(d.n > n)
tmp.n = d.n;
else
tmp.n = n;

tmp.a[i] = 0;

for(i=0;i<=n;i++) //tmp = this

tmp.a[i] = tmp.a[i] + a[i];

for(i=0;i<=d.n;i++) // tmp = tmp + d

tmp.a[i] = tmp.a[i] + d.a[i];

tmp.n--;

return tmp;
}

DaThuc DaThuc::operator *(DaThuc &d)

{
DaThuc tmp;
int i,j;

tmp.n = n + d.n;
tmp.a = new double[tmp.n+1];

for(i=0;i<=tmp.n;i++)//zero fill
tmp.a[i] = 0;

for(i=0;i<=n;i++)
for(j=0;j<=d.n;j++)
tmp.a[i+j] = tmp.a[i+j] + a[i]*d.a[j];

return tmp;
}

DaThuc DaThuc::operator -()

{
DaThuc tmp;
4
int i;

tmp.n = n;
tmp.a = new double[tmp.n+1];

for(i=0;i<=n;i++)
tmp.a[i] = -a[i];

return tmp;
}

ostream &operator <<(ostream &os,DaThuc d)

{
int i;

for(i = d.n;i>=0;i--)
if(d.a[i] != 0)
cout << d.a[i] << "x^" << i << " ";

return os;
}

int i;

cin >> d.n;

d.a =new double[d.n+1];

for(i=d.n;i>=0;i--)
{
cout << "He so x^" << i << ": ";
cin >> d.a[i];
}

for(i=0;i<=n;i++)
for(j=0;j<=d.n;j++)
tmp.a[i+j] = tmp.a[i+j] + a[i]*d.a[j];

return tmp;
}

DaThuc DaThuc::operator -()

{
DaThuc tmp;
int i;

tmp.n = n;
tmp.a = new double[tmp.n+1];
5
for(i=0;i<=n;i++)
tmp.a[i] = -a[i];

return tmp;
}

ostream &operator <<(ostream &os,DaThuc d)

{
int i;

for(i = d.n;i>=0;i--)
if(d.a[i] != 0)
cout << d.a[i] << "x^" << i << " ";

return os;
}

int i;

cin >> d.n;

d.a =new double[d.n+1];

for(i=d.n;i>=0;i--)
{
cout << "He so x^" << i << ": ";
cin >> d.a[i];
}

return is;
}

float DaThuc::operator ^(float x)

{
float sum = 0, px = 1.0;
int i;
for(i=0;i<=n;i++)
{
sum = sum + a[i] * px;
px = px * x;
}
return sum;
}

int main(){
DaThuc a,b;
clrscr();
6
cout << "Nhap da thuc thu nhat: " << endl;
cin >> a;
cout << "Nhap da thuc thu hai: " << endl;
cin >> b;

cout << a << " + " << b << " = " << (a+b) << endl;
cout << a << " * " << b << " = " << (a*b) << endl;
cout << "-(" << a << ") = " << -a << endl;
cout << "(" << a << ")(1.0) =" << (a^1.0) << endl;
cout << "(" << b << ")(1.0) =" << (b^1.0) << endl;
cout << "(" << a << " + "<< b << ")(1.0) =" << ((a+b)^1.0) << endl;
cout << "(" << a << " * "<< b << ")(1.0) =" << ((a*b)^1.0) << endl;
getch();
return 0;
}
Hình tròn (bao)
#include<conio.h>
#include<iostream.h>
#include<math.h>
class Diem{
private:
float x,y;
public:
Diem();
Diem(Diem &d);
~Diem();
friend ostream &operator << (ostream &os,Diem &d);
friend istream &operator >> (istream &is,Diem &d);
float kc(Diem &d);
};

class HinhTron{
private:
Diem Tam;
float r;
public:
HinhTron();
float chuvi();
float dientich();
int chua(Diem &d);
friend ostream &operator << (ostream &os,HinhTron &t);
friend istream &operator >> (istream &is,HinhTron &t);
};

Diem::Diem(){
x = 0;
y = 0;
}

Diem::Diem(Diem &d){
7
x = d.x;
y = d.y;
}

Diem::~Diem(){
x = 0;
y = 0;
}

ostream &operator <<(ostream &os,Diem &d){

os << "(" << d.x << "," << d.y << ")";
return os;
}

cout << "x= ";
is >> d.x;
cout << "y= ";
is >> d.y;
return is;
}

float Diem::kc(Diem &d){

return sqrt((d.x-x)*(d.x-x)+(d.y-y)*(d.y-y));
}

HinhTron::HinhTron():Tam(){
r = 0;
}

float HinhTron::chuvi(){
return (2*3.14*r);
}

float HinhTron::dientich(){
return (3.14*r*r);
}

ostream &operator << (ostream &os, HinhTron &t){

cout << "["<< t.Tam << "; R= " << t.r << "]";
return os;
}

istream &operator >> (istream &is, HinhTron &t){

cout << "Nhap toa do tam: " << endl;
is >> t.Tam;
cout << "Nhap ban kinh: " << endl;
is >> t.r;
return is;
}
8
int HinhTron::chua(Diem &d){
if(d.kc(Tam) > r)
return -1;

if(d.kc(Tam) == r)
return 0;

return 1;
}

int main(){
HinhTron tr;
Diem d;

clrscr();
cout << "Nhap hinh tron: " << endl;
cin >> tr;

cin >> d;

cout << "Hinh tron " << tr << endl;

cout << "Chu vi hinh tron " << tr.chuvi() << endl;
cout << "Dien tich hinh tron " << tr.dientich() << endl;

if(tr.chua(d) == 1)
cout << d << " nam trong " << tr;
else if(tr.chua(d) == 0)
cout << d << " nam tren " << tr;
else
cout << d << " nam ngoai " << tr;

getch();
return 0;
}

Hình tròn (dẫn xuất)

#include<conio.h>
#include<iostream.h>
#include<math.h>
class Diem{
private:
float x,y;
public:
Diem();
Diem(Diem &d);
9
~Diem();
friend ostream &operator << (ostream &os,Diem &d);
friend istream &operator >> (istream &is,Diem &d);
float kc(Diem &d);
Diem *diachi();
};

class HinhTron: public Diem

{
private:
float r;
public:
HinhTron();
float chuvi();
float dientich();
int chua(Diem &d);
friend ostream &operator << (ostream &os,HinhTron &t);
friend istream &operator >> (istream &is,HinhTron &t);
};

Diem::Diem(){
x = 0;
y = 0;
}

Diem::Diem(Diem &d){
x = d.x;
y = d.y;
}

Diem::~Diem(){
x = 0;
y = 0;
}

Diem *Diem::diachi(){
return this;
};

ostream &operator <<(ostream &os,Diem &d){

os << "(" << d.x << "," << d.y << ")";
return os;
}

cout << "x= ";
is >> d.x;
cout << "y= ";
is >> d.y;
return is;
10
}

float Diem::kc(Diem &d){

return sqrt((d.x-x)*(d.x-x)+(d.y-y)*(d.y-y));
}

HinhTron::HinhTron():Diem(){
r = 0;
}

ostream &operator << (ostream &os, HinhTron &h){

Diem *tam;
tam = h.diachi();
cout << "[" << *tam << " R=" << h.r << "]";
return os;
}

istream &operator >> (istream &is, HinhTron &h){

Diem *tam;
tam = h.diachi();
cout << "Nhap toa do tam " << endl;
cin >> *tam;
cout << "Nhap ban kinh " << endl;
cin >> h.r;
return is;
}

int HinhTron::chua(Diem &d){

Diem *tam;
tam = diachi();

if(d.kc(*tam) > r)
return -1;

if(d.kc(*tam) == r)
return 0;

return 1;
}

float HinhTron::chuvi(){
return (2*3.14*r);
}

float HinhTron::dientich(){
return (3.14*r*r);
}

int main(){
HinhTron tr;
11
Diem d;

clrscr();
cout << "Nhap hinh tron: " << endl;
cin >> tr;

cin >> d;

cout << "Hinh tron " << tr << endl;

cout << "Chu vi hinh tron " << tr.chuvi() << endl;
cout << "Dien tich hinh tron " << tr.dientich() << endl;

if(tr.chua(d) == 1)
cout << d << " nam trong " << tr;
else if(tr.chua(d) == 0)
cout << d << " nam tren " << tr;
else
cout << d << " nam ngoai " << tr;

getch();
return 0;
}

Phân số (lớp)
#include <conio.h>
#include <iostream.h>

class PS{
private:
int tu, mau;
public:
PS();//ham tao khong doi
PS(int ts,int ms);
PS(PS &ps); //ham tao sao chep
~PS();
friend ostream &operator << (ostream &os,PS &ps);
friend istream &operator >> (istream &is,PS &ps);
PS operator -();// toan tu doi dau
PS &operator ++();
PS operator +(PS &ps);
PS operator *(PS &ps);
PS operator =(PS &ps);
int operator ==(PS &ps);
PS rutgon(); //toan tu rut gon
operator float();//ep kieu 1/2=0.5
};

12
PS::PS(){
tu = 0;
mau = 1;
}

tu = ts;
mau = ms;
}

PS::PS(PS &ps){
tu = ps.tu;
mau = ps.mau;
}

PS::~PS(){
tu = 0;
mau = 0;
}

ostream &operator << (ostream &os,PS &ps){

os << ps.tu << "/" << ps.mau;
return os;
}

istream &operator >> (istream &is,PS &ps){

cout << "Tu so:";
is >> ps.tu;
cout << "Mau so:";
is >> ps.mau;
return is;
}

PS PS::operator -(){
PS ps;
ps.tu = -tu;
ps.mau = mau;
return ps;
}

PS &PS::operator ++(){
tu = tu + mau;
return rutgon();
}

int uscln(int a,int b){

int tmp;

if(a < b)
13
{
tmp = a;
a = b;
b = tmp;
}

while(b != 0)
{
tmp = b;
b = a % b;
a = tmp;
}
return a;
}

PS PS::rutgon(){
int tmp = uscln(tu, mau);
tu = tu / tmp;
mau = mau /tmp;
return (*this);
}

PS PS::operator +(PS &ps){

PS pr;
pr.tu = tu * ps.mau + mau * ps.tu;
pr.mau = mau * ps.mau;
return pr.rutgon();
}

PS PS::operator *(PS &ps){

PS pr;
pr.tu = tu * ps.tu;
pr.mau = mau * ps.mau;
return pr.rutgon();
}
PS PS::operator =(PS &ps){
tu = ps.tu;
mau = ps.mau;
return ps;
}

int PS::operator ==(PS &ps){

if(tu * 1.0/mau == ps.tu * 1.0/ps.mau)
return 1;
return 0;
}

PS::operator float(){
return (tu*1.0/mau);
}
14
int main(){
PS ps1,ps2;
clrscr();
cout << "Nhap phan so 1 " << endl;
cin >> ps1;
cout << "Nhap phan so 2 " << endl;
cin >> ps2;

cout << "- (" << ps2 << ") = " << (-ps2) << endl;
cout << "float(" << ps1 << ") = " << float(ps1) << endl;

cout << ps1 << " + " << ps2 << " = " << (ps1+ps2) << endl;
cout << ps1 << " * " << ps2 << " = " << (ps1*ps2) << endl;

if(ps1==ps2)
cout << ps1 << " == " << ps2 << " is true " << endl;
else
cout << ps1 << " == " << ps2 << " is false " << endl;

cout << "Execute: ps1++" << endl;

ps1++;
cout << "Result: ps1= " << ps1 << endl;

cout << "Execute: ps1 = ps2" << endl;

ps1 = ps2;
cout << "Result: ps1= "<< ps1;

getch();
return 0;
}
Phương trình bậc 2
#include <conio.h>
#include <iostream.h>
#include <math.h>
class PTB2{
private:
float a,b,c;
public:
PTB2();
PTB2(float a,float b,float c);
PTB2(PTB2 &p);
~PTB2();
float delta();
float x1();
float x2();
friend ostream &operator << (ostream &os,PTB2 &p);
friend istream &operator >> (istream &is,PTB2 &p);
};

15
PTB2::PTB2(){
a = b = c = 0;
}

PTB2::PTB2(float a,float b,float c)

{
this->a = a;
this->b = b;
this->c = c;
}

PTB2::PTB2(PTB2 &p)
{
a = p.a;
b = p.b;
c = p.c;
}

PTB2::~PTB2(){
a = b = c = 0;
}

float PTB2::delta(){
return (b*b - 4*a*c);
}

float PTB2::x1(){
return (-b + sqrt(delta()))/(2*a);
}

float PTB2::x2(){
return (-b - sqrt(delta()))/(2*a);
}

ostream &operator <<(ostream &os,PTB2 &p){

os << p.a << "x^2 + " << p.b << "x + " << p.c << " = 0";
return os;
}

istream &operator >>(istream &is,PTB2 &p){

cout << "a= ";
cin >> p.a;
cout << "b= ";
cin >> p.b;
cout << "c= ";
cin >> p.c;
return is;
}

int main(){
16
PTB2 p;

clrscr();
cout << "Nhap phuong trinh bac hai " << endl;
cin >> p;

if( p.delta() < 0 )

{
cout << "Phuong trinh vo nghiem " ;
}
else if( p.delta() == 0)
{
cout << "Phuong trinh co nghiem kep: x1 = x2 = " << p.x1();
}
else
{
cout << "Phuong trinh co hai nghiem: x1 = " << p.x1() << ", x2 =
" << p.x2();
}

getch();
return 0;
}

Sinh viên (dẫn xuất)

#include<conio.h>
#include<iostream.h>
class Nguoi{
private:
char *cmt;
char *hoten;
int tuoi;
public:
Nguoi();
~Nguoi();
friend ostream &operator <<(ostream &os,Nguoi &ng);
friend istream &operator >>(istream &is,Nguoi &ng);
Nguoi *diachi();
};
class SinhVien: public Nguoi
{
private:
char *lop;
char *masv;
float dtb;
public:
SinhVien();
~SinhVien();
17
friend ostream &operator <<(ostream &os,SinhVien &sv);
friend istream &operator >>(istream &is,SinhVien &sv);
operator float();
};

Nguoi::Nguoi(){
cmt = new char[10];
hoten = new char[25];
tuoi =0;
}

Nguoi::~Nguoi(){
delete cmt;
delete hoten;
tuoi =0;
}

Nguoi *Nguoi::diachi(){
return this;
}
ostream &operator <<(ostream &os,Nguoi &ng){
cout << "CMT: " << ng.cmt << ".Ho ten: " << ng.hoten << ".Tuoi: " <<
ng.tuoi;
return os;
}

istream &operator >>(istream &is,Nguoi &ng){

cout << "CMT: ";
is.ignore(1);
is.get(ng.cmt,10);

cout << "Ho va ten: ";

is.ignore(1);
is.get(ng.hoten,25);

cout << "Tuoi: ";

is >> ng.tuoi;

return is;

SinhVien::SinhVien():Nguoi(){
lop = new char[10];
masv = new char[7];
dtb = 0;
}

SinhVien::~SinhVien(){
delete lop;
18
delete masv;
dtb = 0;
}

ostream &operator <<(ostream &os,SinhVien &sv){

Nguoi *ng;
ng = sv.diachi();
os << *ng << ".Lop: " << sv.lop << ".MaSV: " << sv.masv << ".DTB: "
<< sv.dtb;
return os;
}

istream &operator >>(istream &is,SinhVien &sv){

Nguoi *ng;
ng = sv.diachi();
is >> *ng;
cout << "Nhap MaSV: ";
is.ignore(1);
is.get(sv.masv,10);

cout << "Nhap Lop: ";

is.ignore(1);
is.get(sv.lop,7);

cout << "DTB: ";

is >> sv.dtb;
return is;
}
SinhVien::operator float(){
return dtb;
}
int main(){
SinhVien sv[100];
int n,i,c;
float max;
clrscr();

cout << "Nhap so sinh vien can thu: ";

cin >> n;

for(i=0;i<n;i++)
{
cout << "Nhap sinh vien thu " << i <<": " << endl;
cin >> sv[i];
}

max=float(sv[0]);
for(i=1;i<n;i++)
if(max<float(sv[i]))
max = float(sv[i]);
19
c = 0;
cout << "Danh sach sinh vien dat diem cao nhat (" << max << " diem):
" << endl;
for(i=0;i<n;i++)
if(max == float(sv[i]))
{
cout << sv[i] << endl;
c++;
}
cout << "Co " << c << " nguoi trong danh sach.";

getch();
return 0;
}

Số phức (lớp)
#include <conio.h>
#include <iostream.h>
class SoPhuc{
private:
double thuc, ao;
public:
SoPhuc();
SoPhuc(double t,double a);
SoPhuc(SoPhuc &s);
~SoPhuc();
SoPhuc operator +(SoPhuc &s);
SoPhuc operator *(SoPhuc &s);
SoPhuc operator ++();
SoPhuc const &operator =(const SoPhuc &s);
int operator ==(SoPhuc &s);
friend ostream &operator << (ostream &os,SoPhuc &s);
friend istream &operator >> (istream &is,SoPhuc &s);
};

SoPhuc::SoPhuc(){
thuc = 0;
ao = 0;
}

SoPhuc::SoPhuc(double t,double a){

thuc = t;
ao = a;
}

SoPhuc::SoPhuc(SoPhuc &s){
thuc = s.thuc;
20
ao = s.ao;
}

SoPhuc::~SoPhuc(){
thuc = 0;
ao = 0;
}

SoPhuc SoPhuc::operator +(SoPhuc &s){

SoPhuc sr;
sr.thuc = thuc + s.thuc;
sr.ao = ao + s.ao;
return sr;
}

SoPhuc SoPhuc::operator *(SoPhuc &s){

SoPhuc sr;
sr.thuc = thuc * s.thuc - ao * s.ao;
sr.ao = thuc * s.ao + s.thuc * ao;
return sr;
}

thuc = s.thuc;
ao = s.ao;
return s;
}

int SoPhuc::operator ==(SoPhuc &s){

if(thuc == s.thuc && ao == s.ao)
return 1;
return 0;
}

thuc ++;
return (*this);
}

ostream &operator << (ostream &os,SoPhuc &s){

cout << "(" << s.thuc << "+" << s.ao << "i)";
return os;
}

istream &operator >> (istream &is,SoPhuc &s){

cout << "Nhap phan thuc: ";
cin >> s.thuc;
cout << "Nhap phan ao: ";
cin >> s.ao;
return is;
21
}
int main(){
SoPhuc a,b;

clrscr();
cout << "Nhap so phuc thu nhat: " << endl;
cin >> a;
cout << "Nhap so phuc thu hai: " << endl;
cin >> b;

cout << a << " + " << b << " = " << (a+b) << endl;
cout << a << " * " << b << " = " << (a*b) << endl;

if(a == b)
cout << a << " == " << b << " is true" << endl;
else
cout << a << " == " << b << " is false" << endl;

cout << "Execute: a++" << endl;

a++;
cout << "Result: a=" << a << endl;

cout << "Execute: a=b" << endl;

a = b;
cout << "Result: a=" << a << endl;

getch();
return 0;
}

Tam giác (bao)

#include<conio.h>
#include<iostream.h>
#include<math.h>
class Diem{
private:
float x,y;
public:
Diem();
Diem(Diem &d);
~Diem();
friend ostream &operator << (ostream &os,Diem &d);
friend istream &operator >> (istream &is,Diem &d);
float kc(Diem &d);
};

class TamGiac{
private:
Diem A,B,C;

22
public:
TamGiac();
float chuvi();
float dientich();
friend ostream &operator << (ostream &os,TamGiac &t);
friend istream &operator >> (istream &is,TamGiac &t);
};

Diem::Diem(){
x = 0;
y = 0;
}

Diem::Diem(Diem &d){
x = d.x;
y = d.y;
}

Diem::~Diem(){
x = 0;
y = 0;
}

ostream &operator <<(ostream &os,Diem &d){

os << "(" << d.x << "," << d.y << ")";
return os;
}

cout << "x= ";
is >> d.x;
cout << "y= ";
is >> d.y;
return is;
}

float Diem::kc(Diem &d){

return sqrt((d.x-x)*(d.x-x)+(d.y-y)*(d.y-y));
}

TamGiac::TamGiac():A(),B(),C(){
}

float TamGiac::chuvi(){
return (A.kc(B)+B.kc(C)+C.kc(A));
}

float TamGiac::dientich(){
float p,a,b,c;
a = B.kc(C);
23
b = A.kc(C);
c = A.kc(B);
p = (a+b+c)/2;
return sqrt((p-a)*(p-b)*(p-c)*p);
}

ostream &operator << (ostream &os, TamGiac &t){

cout << "A" << t.A << "; B" << t.B << "; C" << t.C;
return os;
}

istream &operator >> (istream &is, TamGiac &t){

cout << "Nhap toa do diem A: " << endl;
is >> t.A;
cout << "Nhap toa do diem B: " << endl;
is >> t.B;
cout << "Nhap toa do diem C: " << endl;
is >> t.C;
return is;
}

int main(){
TamGiac tg;

clrscr();
cout << "Nhap tam giac: " << endl;
cin >> tg;

cout << "Tam giac " << tg << endl;

cout << "Chu vi tam giac " << tg.chuvi() << endl;
cout << "Dien tich tam giac " << tg.dientich() << endl;
getch();
return 0;
}

24