把下面的代码改成【样例输入】2 3 4 5 【样例输出】l1:y=2x+3 l2: y=4x+5 cross point: -1 1的形式#include <iostream> #include <cmath> using namespace std; class Point { public: double x, y; }; class Line { public: double a, b; void print() { cout << "y = " << a << "x + " << b << endl; } }; void setPoint(Line &l1, Line &l2) { if (abs(l1.a - l2.a) < 1e-6) { // 两条直线平行 if (abs(l1.b - l2.b) < 1e-6) { cout << "infinite cross point" << endl; // 两条直线重合 } else { cout << "no cross point" << endl; // 两条直线不重合 } } else { double x = (l2.b - l1.b) / (l1.a - l2.a); double y = l1.a * x + l1.b; cout << "交点坐标:(" << x << ", " << y << ")" << endl; } } int main() { Line l1, l2; inpute"l1.a = ; l1.b = "; inpute"l2.a = ; l2.b = "; cout << "直线1方程:"; l1.print(); cout << "直线2方程:"; l2.print(); setPoint(l1, l2); return 0; }
时间: 2024-01-15 10:03:46 浏览: 93
把下面的代码改成【样例输入】2 3 4 5 【样例输出】l1:y=2x+3 l2: y=4x+5 cross point: (-1, 1) 的形式:
```c++
#include <iostream>
#include <cmath>
using namespace std;
class Point {
public:
double x, y;
};
class Line {
public:
double a, b;
void print() {
cout << "y = " << a << "x + " << b << endl;
}
};
void setPoint(Line &l1, Line &l2) {
if (abs(l1.a - l2.a) < 1e-6) { // 两条直线平行
if (abs(l1.b - l2.b) < 1e-6) {
cout << "infinite cross point" << endl; // 两条直线重合
} else {
cout << "no cross point" << endl; // 两条直线不重合
}
} else {
double x = (l2.b - l1.b) / (l1.a - l2.a);
double y = l1.a * x + l1.b;
cout << "cross point: (" << x << ", " << y << ")" << endl;
}
}
int main() {
Line l1, l2;
cout << "输入l1.a和l1.b: ";
cin >> l1.a >> l1.b;
cout << "输入l2.a和l2.b: ";
cin >> l2.a >> l2.b;
cout << "直线1方程:";
l1.print();
cout << "直线2方程:";
l2.print();
setPoint(l1, l2);
return 0;
}
```
阅读全文