#include <iostream.h>
#include <graphics.h>
#include <stdlib.h>
#include <conio.h>
#include <math.h>
#include <dos.h>
class Diem
{
double x, y;
public:
Diem (double xx = 0, double yy = 0):x(xx), y(yy){}
void Set(double xx, double yy) {x = xx; y = yy;}
double GetX() const {return x;}
double GetY() const {return y;}
void Nhap();
void Xuat() const;
Diem Cong(Diem b){return Diem(x+b.x,y+b.y);}
Diem Tru(Diem b) {return Diem(x-b.x,y-b.y);}
void TinhTien(double dx, double dy) { x += dx; y += dy; }
void MoveTo() const {moveto(x,y);}
void LineTo() const {lineto(x,y);}
void Line(Diem b) const {MoveTo(); b.LineTo();}
void ViTu(Diem Tam, double TiLe);
void Quay(Diem Tam, double Goc);
Diem LayViTu(Diem Tam, double TiLe);
void Ve(int color = WHITE) const {putpixel(x,y,color);}
void Ve2(int color = WHITE, int size = 1) const;
double KhoangCach(Diem b);
};
inline void Diem::Nhap()
{
cin >> x >> y;
}
inline void Diem::Xuat() const
{
cout <<"(" << x << "," << y << ")";
}
void Diem::ViTu(Diem Tam, double TiLe)
{
x = (x-Tam.x)*TiLe + Tam.x;
y = (y-Tam.y)*TiLe + Tam.y;
}
Diem Diem::LayViTu(Diem Tam, double TiLe)
{
Diem d = *this;
d.ViTu(Tam, TiLe);
return d;
}
void Diem::Quay(Diem Tam, double Goc)
{
double CosA = cos(Goc), SinA = sin(Goc);
double X = (x-Tam.x)*CosA - (y-Tam.y)*SinA + Tam.x;
y = (x-Tam.x)*SinA + (y-Tam.y)*CosA + Tam.y;
x = X;
}
void Diem::Ve2(int color, int size) const
{
for (int i = -size; i <= size; i++)
for (int j = -size; j <= size; j++)
putpixel(x+i,y+j,color);
}
double sqr(double x)
{
return x*x;
}
double Diem::KhoangCach(Diem b)
{
return sqrt(sqr(x-b.x)+sqr(y-b.y));
}
class DaGiac
{
int SoDinh;
Diem *Dinh;
void Init(int sd, Diem *d);
public:
DaGiac(int sd, Diem p1, ...) {Init(sd, &p1);}
DaGiac(int sd, Diem *d) {Init(sd, d);}
DaGiac(const DaGiac &d2) {Init(d2.SoDinh, d2.Dinh);}
~DaGiac() { delete [] Dinh;}
DaGiac& operator = (const DaGiac &d2);
Diem TrongTam() const;
void TinhTien(double dx = 0, double dy = 0);
void Quay(Diem Tam, double goc);
void Quay(double goc) {Quay(TrongTam(), goc);}
void ViTu(Diem Tam, double TiLe);
void ViTu(double goc) {ViTu(TrongTam(), goc);}
void Ve(int color = WHITE);
void Nhap();
void Xuat() const;
};
void DaGiac::Init(int sd, Diem *d)
{
Dinh = new Diem[SoDinh = sd];
memcpy(Dinh, d, sd*sizeof(Diem));
}
DaGiac& DaGiac::operator = (const DaGiac &d2)
{
if (this != &d2)
{
delete [] Dinh;
Init(d2.SoDinh, d2.Dinh);
}
return *this;
}
void DaGiac::TinhTien(double dx, double dy)
{
for(int i = 0; i < SoDinh; i++)
Dinh[i].TinhTien(dx, dy);
}
void DaGiac::Ve(int color)
{
int orgColor = getcolor();
setcolor(color);
Dinh[0].MoveTo();
for (int i = 1; i < SoDinh; i++)
Dinh[i].LineTo();
Dinh[0].LineTo();
setcolor(orgColor);
}
void DaGiac::Nhap()
{
int i;
if (Dinh) delete [] Dinh;
cin >> SoDinh;
Dinh = new Diem[SoDinh];
for (i = 0; i < SoDinh; i++)
Dinh[i].Nhap();
}
void DaGiac::Xuat() const
{
int i;
for (i = 0; i < SoDinh; i++)
Dinh[i].Xuat();
}
Diem DaGiac::TrongTam() const
{
double xx = 0, yy = 0;
for (int i = 0; i < SoDinh; i++)
{
xx += Dinh[i].GetX();
yy += Dinh[i].GetY();
}
return Diem(xx/SoDinh, yy/SoDinh);
}
void DaGiac::Quay(Diem Tam, double goc)
{
for (int i = 0; i < SoDinh; i++)
{
Dinh[i].Quay(Tam, goc);
}
}
void DaGiac::ViTu(Diem Tam, double TiLe)
{
for (int i = 0; i < SoDinh; i++)
{
Dinh[i].ViTu(Tam, TiLe);
}
}
void InitGraph()
{
int gdriver = DETECT, gmode, errorcode;
initgraph(&gdriver, &gmode, "D:/BorlandC/BGI");
errorcode = graphresult();
if (errorcode != grOk)
{
cerr << "Graphics error: "<< grapherrormsg(errorcode) << "\n";
cerr << "Press any key to exit";
getch();
exit(1);
}
}
void CloseGraph()
{
closegraph();
}
void run()
{
int done;
if (done)
setcolor(YELLOW);
outtextxy(20, 25, "Do hoa Da Giac");
outtextxy(30, 35, "Design by VY CONG SUC");
outtextxy(40, 45, "Press double key ESCAPE (ESC) to quit");
}
void main()
{
cout << "\nChuong trinh da giac\n";
InitGraph();
char d;
DaGiac d1(3, Diem(10,10), Diem (110,110), Diem(100,10));
d1.Ve();
getch();
cleardevice();
d1.TinhTien(20,50);
d1.Ve();
getch();
cleardevice();
Diem pp[] = {Diem(90,100), Diem(210,120), Diem(300,20), Diem(240,300),
Diem(150,260)};
DaGiac d2(5, pp);
d2.Ve();
getch();
d2.TinhTien(100,50);
cleardevice();
d2.Ve();
getch();
Diem p[10];
const double goc = 2*M_PI/5;
Diem Tam(320,240);
p[0].Set(320,40);
p[1].Set(320,40);
p[1].Quay(Tam,goc/2);
p[1].ViTu(Tam,cos(2*M_PI/5)/cos(M_PI/5));
for (int i = 2; i < 10; i+=2)
{
p[i] = p[i-2];
p[i].Quay(Tam, 2*M_PI/5);
p[i+1] = p[i-1];
p[i+1].Quay(Tam, 2*M_PI/5);
}
DaGiac d3(10,p);
cleardevice();
d3.Ve();
getch();
Diem T(150,150);
double TiLe = 1.1, TL = 1;
randomize();
while (!kbhit())
{
cleardevice();
d3.TrongTam().Ve(GREEN);
d3.Ve();
d3.ViTu(TiLe);
TL *= TiLe;
if (TL > 4)
TiLe = 1/1.1;
if (TL < 0.25)
TiLe = 1.1;
delay(100);
}
getch();
while (!kbhit())
{
cleardevice();
T.Ve(GREEN);
d3.Ve();
d3.ViTu(T,TiLe);
TL *= TiLe;
if (TL > 4)
TiLe = 1/1.1;
if (TL < 0.25)
TiLe = 1.1;
delay(100);
}
getch();
cleardevice();
while(!kbhit())
{
d3.Quay(0.1);
d3.TrongTam().Ve(GREEN);
d3.Ve();
delay(100);
}
getch();
while(!kbhit())
{
cleardevice();
d3.Quay(0.1);
d3.TrongTam().Ve(GREEN);
d3.Ve();
delay(100);
}
getch();
while(!kbhit())
{
// cleardevice();
d3.Quay(T,0.1);
T.Ve(GREEN);
d3.Ve();
delay(100);
}
do {
run();
d = getch();
} while (d!= 27);
getch();
CloseGraph();
}
DongHo:
#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <dos.h>
#include<graphics.h>
class Diem
{
double x, y;
public:
Diem (double xx = 0, double yy = 0):x(xx), y(yy){}
void Set(double xx, double yy) {x = xx; y = yy;}
double GetX() const {return x;}
double GetY() const {return y;}
void Nhap();
void Xuat() const;
Diem Cong(Diem b){return Diem(x+b.x,y+b.y);}
Diem Tru(Diem b) {return Diem(x-b.x,y-b.y);}
void TinhTien(double dx, double dy) { x += dx; y += dy; }
void MoveTo() const {moveto(x,y);}
void LineTo() const {lineto(x,y);}
void Line(Diem b) const {MoveTo(); b.LineTo();}
void ViTu(Diem Tam, double TiLe);
void Quay(Diem Tam, double Goc);
Diem LayViTu(Diem Tam, double TiLe);
void Ve(int color = WHITE) const {putpixel(x,y,color);}
void Ve2(int color = WHITE, int size = 1) const;
double KhoangCach(Diem b);
};
inline void Diem::Nhap()
{
cin >> x >> y;
}
inline void Diem::Xuat() const
{
cout <<"(" << x << "," << y << ")";
}
void Diem::ViTu(Diem Tam, double TiLe)
{
x = (x-Tam.x)*TiLe + Tam.x;
y = (y-Tam.y)*TiLe + Tam.y;
}
Diem Diem::LayViTu(Diem Tam, double TiLe)
{
Diem d = *this;
d.ViTu(Tam, TiLe);
return d;
}
void Diem::Quay(Diem Tam, double Goc)
{
double CosA = cos(Goc), SinA = sin(Goc);
double X = (x-Tam.x)*CosA - (y-Tam.y)*SinA + Tam.x;
y = (x-Tam.x)*SinA + (y-Tam.y)*CosA + Tam.y;
x = X;
}
void Diem::Ve2(int color, int size) const
{
for (int i = -size; i <= size; i++)
for (int j = -size; j <= size; j++)
putpixel(x+i,y+j,color);
}
double sqr(double x)
{
return x*x;
}
double Diem::KhoangCach(Diem b)
{
return sqrt(sqr(x-b.x)+sqr(y-b.y));
}
void InitGraph()
{
int gdriver = DETECT, gmode, errorcode;
initgraph(&gdriver, &gmode, "D:/BorlandC/BGI");
errorcode = graphresult();
if (errorcode != grOk)
{
cerr << "Graphics error: "<< grapherrormsg(errorcode) << "\n";
cerr << "Press any key to exit";
getch();
exit(1);
}
}
void CloseGraph()
{
closegraph();
}
void main()
{
InitGraph();
char e;
Diem Tam(320,240);
Diem d(320,20), KGiay(320,30), KPhut(320,40), KGio(320,70);
struct dostime_t t;
_dos_gettime(&t);
KGiay.Quay(Tam, 2*M_PI*t.second/60);
KPhut.Quay(Tam, 2*M_PI*(t.minute/60.+t.second/3600.));
KGio.Quay(Tam, 2*M_PI*(t.hour/12.+t.minute/12./60+t.second/12./3600));
circle(Tam.GetX(),Tam.GetY(),d.KhoangCach(Tam)*1.05);
setfillstyle(SOLID_FILL, RED);
floodfill(Tam.GetX(),Tam.GetY(),getcolor());
Tam.Ve();
char buf[10];
settextjustify(CENTER_TEXT, CENTER_TEXT);
for (int i = 0; i < 60; i++)
{
if (i%5 == 0)
{
int j = i < 5 ? 12: i/5;
sprintf(buf,"%d",j);
outtextxy(d.GetX(),d.GetY(),buf);
}
else
d.Ve();
d.Quay(Tam,M_PI/30);
}
char *s1 = "Vy Cong Suc", *s2 = "Khoa Cong Nghe Thong Tin";
outtextxy(Tam.GetX(), Tam.GetY()-100, s1);
outtextxy(Tam.GetX(), Tam.GetY()+100, s2);
setwritemode(XOR_PUT);
setcolor(GREEN^RED);
KGiay.Line(KGiay.LayViTu(Tam, -0.1));
setcolor(WHITE^RED);
setlinestyle(SOLID_LINE, 0, 2);
KPhut.Line(KPhut.LayViTu(Tam, -0.1));
setcolor(YELLOW^RED);
setlinestyle(SOLID_LINE, 0, 3);
KGio.Line(KGio.LayViTu(Tam, -0.1));
while(!kbhit())
{
setcolor(GREEN^RED);
setlinestyle(SOLID_LINE, 0, 1);
KGiay.Line(KGiay.LayViTu(Tam, -0.1));
setcolor(WHITE^RED);
setlinestyle(SOLID_LINE, 0, 2);
KPhut.Line(KPhut.LayViTu(Tam, -0.1));
setcolor(YELLOW^RED);
setlinestyle(SOLID_LINE, 0, 3);
KGio.Line(KGio.LayViTu(Tam, -0.1));
KGiay.Quay(Tam,M_PI/30);
KPhut.Quay(Tam,M_PI/1800);
KGio.Quay(Tam,M_PI/1800/12);
setcolor(GREEN^RED);
setlinestyle(SOLID_LINE, 0, 1);
KGiay.Line(KGiay.LayViTu(Tam, -0.1));
setcolor(WHITE^RED);
setlinestyle(SOLID_LINE, 0, 2);
KPhut.Line(KPhut.LayViTu(Tam, -0.1));
setcolor(YELLOW^RED);
setlinestyle(SOLID_LINE, 0, 3);
KGio.Line(KGio.LayViTu(Tam, -0.1));
sleep(1);
}
getch();
CloseGraph();
}
Phan so:
#include<conio.h>
#include<iostream.h>
#include<stdlib.h>
class PhanSo
{
int mau,tu;
public:
PhanSo(int t=0,int m=1);
void Xuat() const;
void Nhap();
void Set(int t,int m);
PhanSo Cong(PhanSo b) const;
PhanSo Tru(PhanSo b) const;
private:
void ToiGian();
};
void error(const char *s)
{
cout<<s<<endl;
exit(1); //1 truyen vo vi no la truong hop bat
thuong.binh thuong dung 0
}
/*----------------------------------------------------------------------------*/
PhanSo PhanSo::Cong(PhanSo b) const
{
if(mau==0)
error("mau bang 0");
return PhanSo(tu*b.mau+mau*b.tu,mau*b.mau);
//ko can goi ToiGian vi ham Set da tao ra pso toi gian.2 pso toi gian
cog lai van la toi gian
}
/*---------------------------------------------------------------------------*/
PhanSo PhanSo::Tru(PhanSo b) const
{
if(mau==0)
error("mau bang 0");
return PhanSo(tu*b.mau-mau*b.tu,mau*b.mau);
//ko can goi ToiGian vi ham Set da tao ra pso toi gian.2 pso toi gian
cog lai van la toi gian
}
/*---------------------------------------------------------------------------------*/
void PhanSo::Set(int t,int m)
{
tu=t;
mau=m;
ToiGian();
}
void PhanSo::PhanSo(int t,int m)
{
tu=t;
mau=m;
ToiGian();
}
/*-----------------------------------------------------------------------------------*/
void PhanSo::Xuat() const
{
// cout<<"xuat pso:"; vi VD:1/2+1/4=3/4
cout<<tu<<"/"<<mau;
}
/*-----------------------------------------------------------------------------------*/
void PhanSo::Nhap()
{
cout<<"nhap tu so:";
cin>>tu;
do{
cout<<"nhap mau so:";
cin>>mau;
}while(mau==0);
ToiGian();
}
int USCLN(int x,int y)
{
int r;
abs(x);
abs(y);
if(x==0&&y==0)
return 1;
while((r=x%y)!=0)
{
x=y;
y=r;
}
return y;
}
void PhanSo::ToiGian()
{
int c;
c=USCLN(tu,mau);
tu=tu/c;
mau=mau/c;
}
void main()
{
clrscr();
PhanSo a,b,c;
a.Nhap();
b.Nhap();
c=a.Cong(b);
cout<<"kq";
c.Xuat();
getch();
}
Sinh Vien:(ke thua)
#include<iostream.h>
#include<conio.h>
#include<string.h>
const int MAX_hoten=50;
const int MAX_diachi=50;
const int MAX_ms=20;
class nguoi
{
protected:
char M_hoten[MAX_hoten+1];
char M_diachi[MAX_diachi+1];
int M_namsinh;
public:
nguoi(const char *hoten="",const char* diachi="",int
namsinh=0){M_namsinh=namsinh;strcpy(M_hoten,hoten);strcpy(M_diachi,diachi);}
virtual void nhap();
virtual void xuat() const;
// const char* hoten() const;
// const char* diachi() const;
};
/*const char *nguoi::hoten()const
{
return M_hoten;
}
const char *nguoi::diachi()const //Khong can thiet
{
return M_diachi;
}*/
class sinhvien:public nguoi
{
protected:
char M_maso[MAX_ms+1];
public:
sinhvien(const char *maso="",const char *hoten= "",const char
*diachi="",int
namsinh=0):nguoi(hoten,diachi,namsinh){strcpy(M_maso,maso);}
void nhap();
void xuat() const;
};
class congnhan:public nguoi
{
protected:
float M_luong;
public:
congnhan(const char *hoten="",const char *diachi="",int
namsinh=0,float luong=0):nguoi(hoten,diachi,namsinh){M_luong=luong;}
void nhap();
void xuat() const;
};
void nguoi::xuat() const
{
cout<<"Ho ten:"<<M_hoten<<endl;
cout<<"Dia chi:"<<M_diachi<<endl;
cout<<"Nam sinh:"<<M_namsinh<<endl;
}
void nguoi::nhap()
{
cout<<"\nHo ten:";cin.getline(M_hoten,sizeof(M_hoten));
cout<<"Dia chi:";cin.getline(M_diachi,sizeof(M_diachi));
cout<<"Nam sinh:";cin>>M_namsinh;
cin.ignore(); //bo mot ki tu cuoi cung
}
/*sinhvien::sinhvien(const char *maso,const char* hoten,const char*
diachi,int namsinh):nguoi(hoten,diachi,namsinh)
{
strcpy(M_maso,maso); //da lam tren sinh vien roi
}*/
void sinhvien::xuat()const
{
cout<<"Ma so:"<<M_maso<<endl;
nguoi::xuat();
}
void sinhvien::nhap()
{
nguoi::nhap();
cout<<"Ma so sinh vien:";
cin>>M_maso;
// cin.ignore();
}
void congnhan::xuat()const
{
cout<<"Muc luong:"<<M_luong<<endl;
nguoi::xuat();
}
void congnhan::nhap()
{
nguoi::nhap();
cout<<"Muc luong cong nhan:";
cin>>M_luong;
// cin.ignore();
}
void nhapds(nguoi *ds[],int &sopt)
{
int chon;
cout<<"So phan tu:";
cin>>sopt;
for(int i=0;i<sopt;i++)
{
cout<<"(1)Nguoi(2)Sinhvien(3)Congnhan:";
cin>>chon;
cin.ignore();
switch(chon)
{
case 1:ds[i]=new nguoi;break;
case 2:ds[i]=new sinhvien;break;
case 3:ds[i]=new congnhan;break;
}
ds[i]->nhap();
}
}
void xuatds(nguoi **ds,int sopt)
{
for(int i=0;i<sopt;i++)
{
ds[i]->xuat();
cout<<"\n";
}
}
void main()
{
clrscr();
nguoi *ds[20];
int sopt=0;
for(;;)
{
int chon;
cout<<"(1)Nhap DS\n(2)Xuat DS\n(0)Thoat\n";
cout<<"Chon";
cin>>chon;
cin.ignore();
switch(chon)
{
case 1:nhapds(ds,sopt);break;
case 2:xuatds(ds,sopt);getch();break;
case 0:return;
}
}
/* cout<<"\nCONG NHAN";
congnhan cn;
cn.nhap();
cn.xuat();
cout<<"\nSINH VIEN";
sinhvien sv;
sv.nhap();
sv.xuat();
cout<<"\nNGUOI";
nguoi ng;
ng.nhap();
ng.xuat(); */
getch();
}
So Phuc
#include<iostream.h>
#include<math.h>
#include<conio.h>
class sophuc
{
double thuc,ao;
public:
sophuc(double t=0.0,double a=0.0 ):thuc(t),ao(a){}
friend sophuc operator + (sophuc a,sophuc b) ;
sophuc operator - (sophuc b) const;
sophuc operator * (sophuc b) const;
// sophuc operator / (sophuc a,sophuc b);
void nhap();
friend ostream & operator << (ostream &os, sophuc b ); //void xuat();
friend istream & operator >> (istream &is,sophuc & b); // void
nhap();
friend sophuc operator + (double a,sophuc b);
};
sophuc operator + (sophuc a,sophuc b)
{
return sophuc(a.thuc+b.thuc,a.ao+b.ao);
}
ostream & operator << (ostream &os, sophuc b ) //void xuat();
{
os<< "(" <<b.thuc<<","<<b.ao << ")";
return os;
}
istream & operator >> (istream &is, sophuc &b ) //void nhap();
{
cout<<"nhap so thuc:"; is>> b.thuc;
cout<<"nhap so ao"; is>>b.ao;
return is;
}
void main()
{
clrscr();
sophuc a ,b;
cin>>a>>b;
cout<<"sothuc1: " <<a <<"\nsothuc2: " <<b<<endl;
cout<<"tong2 so thuc:"<<a<<"+ " <<b<<"= "<<a+b<<endl;
getch();
}
Stack:
#include<iostream.h>
#include<conio.h>
typedef int item;
typedef int bool;
const int true=1;
const int false=0;
const int MAX=50;
class stack
{
item data[MAX];
int top;
public:
void init();
bool push(item x);
bool pop(item &x);
bool full();
bool empty();
};
void stack::init()
{
top=0;
}
bool stack::push(item x)
{
data[top++]=x;
}
bool stack::pop(item &x)
{
x=data[--top];
}
bool stack::full()
{
return top>=MAX;
}
bool stack::empty()
{
return top<=0;
}
void main()
{
clrscr();
stack s;
item x;
s.init();
for(int i=0;i<10;i++)
{
cin>>x;
s.push(x);
}
for(int j=0;j<10;j++)
{
s.pop(x);
cout<<x<<"\t";
}
getch();
}
// tdiem.cpp
#include<iostream.h>
#include <stdlib.h>
#include <conio.h>
#include <math.h>
#include <dos.h>
#include<graphics.h>
class Diem
{
double x, y;
public:
Diem (double xx = 0, double yy = 0):x(xx), y(yy){}
void Set(double xx, double yy) {x = xx; y = yy;}
double GetX() const {return x;}
double GetY() const {return y;}
void Nhap();
void Xuat() const;
Diem Cong(Diem b){return Diem(x+b.x,y+b.y);}
Diem Tru(Diem b) {return Diem(x-b.x,y-b.y);}
void TinhTien(double dx, double dy) { x += dx; y += dy; }
void MoveTo() const {moveto(x,y);}
void LineTo() const {lineto(x,y);}
void Line(Diem b) const {MoveTo(); b.LineTo();}
void ViTu(Diem Tam, double TiLe);
void Quay(Diem Tam, double Goc);
Diem LayViTu(Diem Tam, double TiLe);
void Ve(int color = WHITE) const {putpixel(x,y,color);}
void Ve2(int color = WHITE, int size = 1) const;
double KhoangCach(Diem b);
};
inline void Diem::Nhap()
{
cin >> x >> y;
}
inline void Diem::Xuat() const
{
cout <<"(" << x << "," << y << ")";
}
void Diem::ViTu(Diem Tam, double TiLe)
{
x = (x-Tam.x)*TiLe + Tam.x;
y = (y-Tam.y)*TiLe + Tam.y;
}
Diem Diem::LayViTu(Diem Tam, double TiLe)
{
Diem d = *this;
d.ViTu(Tam, TiLe);
return d;
}
void Diem::Quay(Diem Tam, double Goc)
{
double CosA = cos(Goc), SinA = sin(Goc);
double X = (x-Tam.x)*CosA - (y-Tam.y)*SinA + Tam.x;
y = (x-Tam.x)*SinA + (y-Tam.y)*CosA + Tam.y;
x = X;
}
void Diem::Ve2(int color, int size) const
{
for (int i = -size; i <= size; i++)
for (int j = -size; j <= size; j++)
putpixel(x+i,y+j,color);
}
double sqr(double x)
{
return x*x;
}
double Diem::KhoangCach(Diem b)
{
return sqrt(sqr(x-b.x)+sqr(y-b.y));
}
void InitGraph()
{
int gdriver = DETECT, gmode, errorcode;
initgraph(&gdriver, &gmode, "D:/BorlandC/BGI");
errorcode = graphresult();
if (errorcode != grOk)
{
cerr << "Graphics error: "<< grapherrormsg(errorcode) << "\n";
cerr << "Press any key to exit";
getch();
exit(1);
}
}
void CloseGraph()
{
closegraph();
}
void run()
{
int done;
if (done)
setcolor(YELLOW);
outtextxy(20, 25, "Do hoa Thoi Diem");
outtextxy(30, 35, "Design by VY CONG SUC");
outtextxy(40, 45, "Press double key ESCAPE (ESC) to quit");
}
void main()
{
InitGraph();
Diem A;
char d;
A.Ve2();
double dx = 5, dy = 5;
getch();
while (!kbhit())
{
A.TinhTien(dx,dy);
A.Ve2();
delay(10);
if (A.GetX() > getmaxx() || A.GetX() < 0)
dx = -dx;
if (A.GetY() > getmaxy() || A.GetY() < 0)
dy = -dy;
}
getch();
while (!kbhit())
{
cleardevice();
A.TinhTien(dx,dy);
A.Ve2();
delay(10);
if (A.GetX() > getmaxx() || A.GetX() < 0)
dx = -dx;
if (A.GetY() > getmaxy() || A.GetY() < 0)
dy = -dy;
}
getch();
Diem T(320,240);
while (!kbhit())
{
cleardevice();
T.Ve2(GREEN);
A.Quay(T, 0.1);
A.Ve2();
delay(100);
}
getch();
Diem B = A, C = A, D = A;
B.ViTu(T,-1);
C.Quay(T,M_PI/2);
D.Quay(T,-M_PI/2);
double TiLe = 1.1;
while (!kbhit())
{
cleardevice();
T.Ve2(GREEN);
A.ViTu(T, TiLe); A.Ve2();
B.ViTu(T, TiLe); B.Ve2(RED);
C.ViTu(T, TiLe); C.Ve2(GREEN);
D.ViTu(T, TiLe); D.Ve2(YELLOW);
delay(100);
if (A.KhoangCach(T) > 240)
TiLe = 1/1.1;
if (A.KhoangCach(T) < 10)
TiLe = 1.1;
}
getch();
while (!kbhit())
{
cleardevice();
A.Quay(T, M_PI/30);
T.MoveTo();
A.LineTo();
delay(1000);
}
do {
run();
d = getch();
} while (d!= 27);
getch();
CloseGraph();
}
Time
#include "iostream.h"
class time
{
int gio,phut,giay;
public :
time (int g=0,int p=0,int gy=0):gio(g),phut(p),giay(gy){}
void SET(int g,int p,int gy);
int Gettime() const{return gio;}
int Getminute() const {return phut;}
int Getsecond()const {return giay;}
void Changetime(int g,int p,int gy);
void Nhap();
void Xuat() const;
};
void time ::Nhap()
{
cout <<"\ngio:"; cin >> gio;
cout <<"\nphut:"; cin >> phut;
cout <<"\ngiay:"; cin >> giay;
}
void time::Xuat()const
{
cout <<"\nket qua la:\n";
cout <<gio<<"gio:"<<phut<<"phut:"<<giay<<"giay:";
}
void time ::SET (int g,int p,int gy)
{
gio=g;phut=p;giay=gy;
}
void time::Changetime(int g,int p,int gy)
{
while(gy >=60)
{
giay = gy-60;
phut=phut+((gy-giay)/60);
}
while(p >=60)
{
phut=p-60;
gio=gio+((p-phut)/60);
}
if(g >24)
cout <<"error";
}
/*int time ::Gettime ()const
{
return gio;
}
int time::Getsecond()const
{
return giay;
}
int time ::Getminute()const
{
return phut;
} */
void main()
{
time a,b;
// a.SET(2,10,15);
a.Nhap();
a.Xuat();
}
Vector _M
#include<iostream.h>
#include<conio.h>
class Vector
{
int *v,n;//so chieu
public:
Vector (int *vv=NULL,int nn=0);
void nhap();
void xuat()const;
void gantri(int *vv=NULL,int nn=0);
Vector operator+(const Vector &b)const;
Vector operator*(const Vector &b)const;
int operator==(const Vector &b)const;
void operator=(const Vector &b);
};
int Vector::operator==(const Vector &b)const
{
return(n==b.n && v==b.v);
}
void Vector::operator=(const Vector &b)
{
n=b.n;
for(int i=0;i<n;i++)
v[i]=b.v[i];
}
Vector Vector::operator*(const Vector &b)const
{
Vector c;
c.n= n>b.n ?n:b.n;
int i=0;
while(i<n && i<b.n)
{
c.v[i]=v[i]*b.v[i];
i++;
}
while(i<b.n)
{
c.v[i]=b.v[i];
i++;
}
while(i<n)
{
c.v[i]=v[i];
i++;
}
return c;
}
Vector Vector::operator+(const Vector &b)const
{
Vector c;
c.n= n>b.n ?n:b.n;
int i=0;
while(i<n && i<b.n)
{
c.v[i]=v[i]+b.v[i];
i++;
}
while(i<b.n)
{
c.v[i]=b.v[i];
i++;
}
while(i<n)
{
c.v[i]=v[i];
i++;
}
return c;
}
Vector::Vector (int *vv,int nn)
{
n=nn;
for(int i=0;i<n;i++)
v[i]=vv[i];
}
void Vector::gantri(int *vv,int nn)
{
n=nn;
if(v!=NULL)
delete v;
if(n==0)
v=NULL;
else
{
v=new int[n];
for(int i=0;i<n;i++)
v[i]<<vv[i];
}
}
void Vector::nhap()
{
cout<<"n=";
cin>>n;
if(n==0)
v=NULL;
else
{
v=new int[n];
cout<<"nhap:\n";
for(int i=0;i<n;i++)
cin>>v[i];
}
}
void Vector::xuat()const
{
cout<<"(";
for(int i=0;i<n-1;i++)
cout<<v[i]<<",";
cout<<v[i]<<")"<<endl;
}
void main()
{
clrscr();
Vector a,b,c;
a.nhap();
b.nhap();
cout<<"a";
a.xuat();
cout<<"b";
b.xuat();
c=a+b;
cout<<"c=a+b";
c.xuat();
c=a*b;
cout<<"c=a*b";
c.xuat();
getch();
}
/*lop vecto
-dulieu : *v, n
-ham : pttl ; pt huy bo; nhap/xuat; gan tri; tinh module
- phep toan : + - * >> <<
*/
Vector_TR
#include<iostream.h>
#include<conio.h>
class vecto
{
int *v;//tphan of vecto
int n;//so chieu
public:
vecto(int * vv=NULL,int nn=0);
void gantri(int *vv=NULL,int nn=0);
void Gantri(const vecto &V);
void nhap();
void xuat()const;
// friend ostream & operator <<(ostream & o,vecto & V);
// friend istream & operator >>(istream & i,const vecto & V);
};
vecto::vecto(int *vv,int nn)
{
n=nn;
if(v!=NULL)
delete v;
if(n==0)
v=NULL;
else
{
for(int i=0;i<n;i++)
v[i]=vv[i];
}
}
void vecto::nhap()
{
cout<<"n=";
cin>>n;
if(n==0)
v=NULL;
else
{
for(int i=0;i<n;i++)
cin>>v[i];
}
}
void vecto::xuat()const
{
cout<<"n="<<n;
cout<<"\n(";
for(int i=0;i<n;i++)
cout<<v[i]<<",";
cout<<")"<<endl;
}
/*ostream & operator <<(ostream & o,vecto &a)
{
if(a.n)
for(int i=0;i<a.n;i++)
o<<"vecto("<<a.v[i]<<","<<")";
return o;
}
istream & operator >>(istream & is,const vecto &a)
{
is>>a.n;
for(int i=0;i<a.n;i++)
is>>a.v[i];
return is;
} */
void main()
{
clrscr();
vecto a;
a.nhap();
a.xuat();
getch();
}
/*cai dat lop string.
.phep gan
.phep toan xuat nhap
.phep tinh noi vao cuoi(dung ki hieu +=)
.phep toan noi hai chuoi(____________+)
.phep toan truy xuat ki tu trong chuoi []
.~ la phuong thuc huy
string(const string &s) la phuong thuc thiet lap sao chep*/
String_I
#include<iostream.h>
#include<conio.h>
#include<string.h>
class string
{
public:
char *p;
string(const char*s="") {p=strdup(s);}
~string();
string(const string&s) {p=strdup(s.p);}
friend istream& operator>>(istream &is,string& s);
friend ostream& operator<<(ostream &os,const string& s);
//thao tac gan
string& operator=(const char *s);
string& operator=(const string &s);
};
const MAX=500;
char *strdup(const char* s)
{
return strcpy(new char[strlen(s)+1],s);
/* char *p=new char[strlen(s)+1];
strcpy(p,s);
return p; */
}
string::~string()
{
delete []p;
}
string& string::operator=(const char *s)
{
delete[] p;
p=strdup(s);
return *this;
}
string& string ::operator=(const string &s)
{
if(this!=&s)
*this=s.p;
return *this;
}
istream& operator>>(istream &is,string& s)
{
char st[MAX+1];
is.getline(st,sizeof(st));
delete []s.p; //neu da dinh nghia phep gan thi gan s=s.p va
dong cuoi bo
s.p=strdup(st);
return is;
}
ostream& operator<<(ostream &os,const string& s)
{
return os<< s.p;
}
void main()
{
clrscr();
string s;
cout<<s;
cout<<"nhap chuoi:";
cin>>s;
cout<<s;
cout<<"\nchuoi duoc gan:";
string b=s;
cout<<b;
getch();
}
String_II
#include<iostream.h>
#include<string.h>
#include<conio.h>
class String
{
char * p;
public:
String(char *s="");
String(const String & s);
~String();
String & operator=(const char* s);
String & operator=(const String & s);
String operator+(const String & s)const;
String & operator+=(const char* s);
String & operator+=(const String & s);
String operator()(int begin, int end)const;
friend ostream & operator<<(ostream & os, const String & s);
friend istream & operator>>(istream & is, String & s);
};
String::String(char *s)
{
p = strdup(s);
}
String::String(const String & s)
{
p = strdup(s.p);
}
String::~String()
{
delete[]p;
}
String & String::operator=(const char* s)
{
delete[] p;
p = strdup(s);
return *this;
}
String & String::operator=(const String & s)
{
if(this != &s)
*this = s.p;
return *this;
}
String & String::operator+=(const char* s)
{
char * pNew = new char[strlen(p)+strlen(s)+1];
strcpy(pNew, p); //Tao mot ban sao
strcat(pNew, s);//noi chuoi s vao chuoi pNew
delete[]p;
p = pNew;
return *this;
}
String & String::operator+=(const String & s)
{
return *this += s.p;
}
String String::operator+(const String & s)const
{
String kq = *this;
kq += s;
return kq;
}
String String::operator()(int begin, int end)const
{
String kq;
int len = strlen(p);//chieu dai chuoi goc
begin = begin<0 ? 0: begin;
if(end>len || end<0)
end = len;
if(begin < end)//lay dc chuoi con
{
len = end - begin;
delete []kq.p;
kq.p = new char[len + 1];
strncpy(kq.p, p+begin, len);
kq.p[len]='\0';
}
return kq;
}
ostream & operator<<(ostream & os, const String & s)
{
os<<"KQ String: ";
os<<s.p;
return os;
}
istream & operator>>(istream & is, String & s)
{
cout<<"\nNhap vao 1 String: ";
char tmp[100];
is.getline(tmp, sizeof(tmp));
s = tmp;
return is;
}
void main()
{
clrscr();
String a, b;
cin>>a;
cin>>b;
a+=b;
cout<<"\nKet qua sau khi noi chuoi b vao chuoi a:\n";
cout<<a;
a=b;
cout<<"\nThay chuoi a thanh chuoi b:\n";
cout<<a;
cout<<"\nLay chuoi con tu vi tri 3 den vi tri 8\n";
cout << a(1,2);
getch();
}
String_III
#include"iostream.h"
#include"conio.h"
#include"string.h"
class string
{
char *p;
public:
string (char * s= "") {p = strdup(s);}
~string() { cout<<"delete"<<(void *)p<<"\n";
delete[]p;}
string(const string &s){p=strdup(s.p);}
void output()const{cout<<p;}
int compare(string s)const;
};
extern char *strdup(const char*s);
char*strdup(const char *s)
{
return strcpy(new char[strlen(s)+1],s);
}
int string::compare(string s)const
{
return strcmp(p,s.p);
}
void main()
{
int c;
string a("ho kim truc") ;
string b("vo thi huyen tram");
c = a.compare(b);
cout<<(c>0?"a>b":c==0?"a=b":"a<b")<<"\n";
}