设计一个类Line,用于表示二维坐标体系中任意一条直线并输出该直线的属性
#include<iostream>
using namespace std;
class line
{
public:
void in(void);
float xielv();
float lingdian();
void out();
private:
float x1;
float y1;
float x2;
float y2;
float k;
float b;
};
void line::in()
{
cout << "请输入一条直线上的一个点的坐标:" << endl;
cin >> x1 >> y1;
cout << "请输入这条直线上的另一个点的坐标:" << endl;
cin >> x2 >> y2;
}
float line::xielv()
{
k = (y2 - y1) / (x2 - x1);
return k;
}
float line::lingdian()
{
b = y1 - k * x1;
return b;
}
void line::out()
{
cout << "该直线的方程为:" << endl;
if (k == 0)
{
cout << "y=" << b;
}
else
{
if (b > 0)
{
cout << "y=" << k << 'x+' << b << endl;
}
else
{
if (b == 0)
{
cout << "y=" << k << 'x' << endl;
}
else
{
cout << "y=" << k << 'x' << b << endl;
}
}
}
}
int main()
{
line t;
t.in();
t.xielv();
t.lingdian();
t.out();
return 0;
}