?? 4_6.cpp
字號:
#include<iostream.h>
#include<iomanip.h>
#include<math.h>
const int DT=5;
template <class T>
class point //point 類的定義
{
public: //外部接口
point(T a=0, T b=0); //構造函數(shù)
void display(void);
void move(T xx,T yy=0);
T getx(void);
T gety(void);
private: //私有數(shù)據(jù)
T x;
T y;
};
//成員函數(shù)的實現(xiàn)
template <class T>
point <T>::point(T a,T b)
{
x=a;
y=b;
}
template <class T>
void point <T>::display(void)
{
cout<<"x="<<setprecision(3)<<x<<" y="<<setprecision(3)<<y<<endl;
}
template <class T>
void point <T>::move(T xx,T yy)
{
x+=xx;
y+=yy;
}
template <class T>
T point <T>::getx(void)
{
return(x);
}
template <class T>
T point <T>::gety(void)
{
return(y);
}
class plane
{
public:
plane(float v1=1000,float x=0,float y=0);
~plane();
void move(void);
float getx(void);
private:
point <float> p;
float v;
};
plane::plane(float v1,float x,float y):p(x,y)
{
v=v1;
}
plane::~plane()
{
cout<<"飛機消失"<<endl;
}
void plane::move(void)
{
float dx;
dx=v*DT;
p.move(dx);
cout<<"飛機坐標:";
p.display();
}
float plane::getx(void)
{
return(p.getx());
}
class missile
{
public:
missile(float v1,float x,float y,float t1);
~missile();
void move(plane &a);
int check_t(void);
private:
point <float> p;
float v;
float s;
float t;
};
missile::missile(float v1,float t1,float x,float y):p(x,y)
{
v=v1;
t=t1;
}
missile::~missile()
{
cout<<"導彈消失"<<endl;
}
void missile::move(plane &a)
{
float s1,s2,dx,dy;
s1=p.gety();
s2=p.getx()-a.getx();
s=sqrt(s1*s1+s2*s2);
cout<<"導彈坐標:";
p.display();
dx=v*DT*s2/s;
dy=v*DT*s1/s;
p.move(-dx,-dy);
}
int missile::check_t(void)
{
int flag;
if(s>t)
flag=0;
else
flag=1;
return(flag);
}
int main()
{
int i=0;
missile m(10,5,40,100);
plane n(5,0,0);
do{
n.move();
m.move(n);
i++;
}while(m.check_t()==0);
cout<<"導彈經(jīng)過"<<i<<"步之后追上飛機"<<endl;
return(0);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -