⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 飞机导弹.cpp

📁 简单描述导弹打飞机的过程
💻 CPP
字号:
#include<iostream.h>
#include<math.h>
#include<iomanip.h>
const int DT=5;
template <class T>
class point//点类模板
{
	public:
		point(T a=0,T b=0);//构造函数
		void display(void);//类型 名(类型 参数)
		void move(T xx,T yy=0);
		T getx(void);
		T gety(void);
	private:
		T x;
		T y;
};
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 s;//距离
	float v;
	float t;
};
missile::missile(float v1,float x, float y,float t1):p(x,y)
{ 
	v=v1;
	t=t1;
}
missile::~missile()
{
	cout<<"导弹消失"<<endl;
}
void missile::move(plane &a)//a区别于p
{
	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,10);
	plane n(5,0,0);
	do
	{
		n.move();
		m.move(n);
		i++;
	}
	while(m.check_t()==0);
cout<<"导弹经过"<<i<<"步之后追上飞机"<<endl;
return(0);
}





⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -