用c++设计一个类Line,用于表示二维坐标体系中的任意一条直线并输出该直线的属性。

#include
#include
using namespace std;
class Line
{
private:
	double x1,y1;
	double x2,y2;
	double k,b;
public:
	Line();
	Line(double m_x1,double m_y1,double m_x2,double m_y2):x1(m_x1),y1(m_y1),x2(m_x2),y2(m_y2){}
	~Line(){};
	void equation();   //方程
	void point();    //交点
	void slope();   //斜率
};
Line::Line()
{
	x1=0,y1=0;
	x2=0,y2=0;
	k=0,b=0;
}
void Line::equation()
{
	if(x2==x1)
		cout<<"该直线方程是:x="<<x1<<endl;
	else
	{

		b=y1-x1*k;      //直线方程与y轴的交点
		cout<<"该直线方程是:y="<<k<<"x+"<<b<<endl;
	}
}
void Line::point()
{
	if(x2==x1)
		cout<<"该直线方程只与x轴有的交点:("<<x1<<",0)"<<endl;
	else if(y1==y2)
		cout<<"该直线方程只与y轴有的交点:(0,"<<b<<")"<<endl;
	else
	{
		cout<<"该直线方程与x轴的交点:("<<(-b/k)<<",0)"<<endl;
		cout<<"该直线方程与y轴的交点:(0,"<<b<<")"<<endl;
	}
}
void Line::slope()
{
	if(x2==x1)
		cout<<"该直线的斜率是无穷大"<<endl;
	else
	{
		k=(y2-y1)/(x2-x1);          //求斜率
		cout<<"该直线的斜率是:"<<k<<endl;
	}
}

void main()
{
	double x1,x2,y1,y2;
	cout<<"请输入任意两个坐标:";
	cin>>x1>>y1>>x2>>y2;
	if(x1==y1==x2==y2)
	{
		cout<<"请重新输入任意两个坐标:";
		cin>>x1>>y1>>x2>>y2;
	}
	Line a(x1,y1,x2,y2);   
	a.slope();      //输出直线斜率
	a.equation();   //输出直线方程
	a.point();      //输出直线与x、y轴的交点
}

你可能感兴趣的