Professional Documents
Culture Documents
#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;
}
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;
}
for(i=0;i<t.n;i++)
os << " A" << i << t.P[i];
return os;
}
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;
Đ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;
}
if(d.n > n)
tmp.n = d.n;
else
tmp.n = n;
return tmp;
}
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;
}
tmp.n = n;
tmp.a = new double[tmp.n+1];
for(i=0;i<=n;i++)
tmp.a[i] = -a[i];
return tmp;
}
for(i = d.n;i>=0;i--)
if(d.a[i] != 0)
cout << d.a[i] << "x^" << i << " ";
return os;
}
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;
}
tmp.n = n;
tmp.a = new double[tmp.n+1];
5
for(i=0;i<=n;i++)
tmp.a[i] = -a[i];
return tmp;
}
for(i = d.n;i>=0;i--)
if(d.a[i] != 0)
cout << d.a[i] << "x^" << i << " ";
return os;
}
for(i=d.n;i>=0;i--)
{
cout << "He so x^" << i << ": ";
cin >> d.a[i];
}
return is;
}
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;
}
HinhTron::HinhTron():Tam(){
r = 0;
}
float HinhTron::chuvi(){
return (2*3.14*r);
}
float HinhTron::dientich(){
return (3.14*r*r);
}
if(d.kc(Tam) == r)
return 0;
return 1;
}
int main(){
HinhTron tr;
Diem d;
clrscr();
cout << "Nhap hinh tron: " << endl;
cin >> tr;
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;
}
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;
};
HinhTron::HinhTron():Diem(){
r = 0;
}
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;
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;
}
PS::PS(PS &ps){
tu = ps.tu;
mau = ps.mau;
}
PS::~PS(){
tu = 0;
mau = 0;
}
PS PS::operator -(){
PS ps;
ps.tu = -tu;
ps.mau = mau;
return ps;
}
PS &PS::operator ++(){
tu = tu + mau;
return rutgon();
}
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::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;
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(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);
}
int main(){
16
PTB2 p;
clrscr();
cout << "Nhap phuong trinh bac hai " << endl;
cin >> p;
getch();
return 0;
}
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;
}
return is;
SinhVien::SinhVien():Nguoi(){
lop = new char[10];
masv = new char[7];
dtb = 0;
}
SinhVien::~SinhVien(){
delete lop;
18
delete masv;
dtb = 0;
}
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(SoPhuc &s){
thuc = s.thuc;
20
ao = s.ao;
}
SoPhuc::~SoPhuc(){
thuc = 0;
ao = 0;
}
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;
getch();
return 0;
}
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;
}
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);
}
int main(){
TamGiac tg;
clrscr();
cout << "Nhap tam giac: " << endl;
cin >> tg;
cout << "Chu vi tam giac " << tg.chuvi() << endl;
cout << "Dien tich tam giac " << tg.dientich() << endl;
getch();
return 0;
}
24