求解具有以下两个未知数的两个方程组:
a1、b1、c1、a2、b2、c2由用户自己输入。
我一直在尝试首先为这个问题找到一个数学解决方案,但我似乎走不远..
到目前为止我尝试过的是:
- 从第一个方程找到 y。(b1y = c1-a1x, y = (c1-a1x)/b1)
- 然后我在第二个等式中替换 y,在这种情况下,我得到一个未知数为 1 的等式 x。但是,我无法解方程,我得到一些奇数/方程并停在这里。
这是正确的还是有更简单的方法可以做到这一点?
当前代码:
#include <iostream>
using namespace std;
int main()
{
int a1, b1, c1, a2, b2, c2;
cout << "Enter the values for the first equation." << endl;
cout << "Enter the value for a1" << endl;
cin >> a1;
cout << "Enter the value for b1" << endl;
cin >> b1;
cout << "Enter the value for c1" << endl;
cin >> c1;
cout << "Enter the values for the second equation." << endl;
cout << "Enter the value for a2" << endl;
cin >> a2;
cout << "Enter the value for b2" << endl;
cin >> b2;
cout << "Enter the value for c2" << endl;
cin >> c2;
cout << "Your system of equations is the following:" << endl;
cout << a1 << "x+" << b1 << "y=" << c1 << endl;
cout << a2 << "x+" << b2 << "y=" << c2 << endl;
if ((a1 * b2) - (b1 * a2) == 0){
cout << "The system has no solution." << endl;
}
else{
res_x = ((c1*b2) - (b1*c2))/((a1*b2)-(b1*a2));
res_y = ((a1*c2) - (c1*a2)) / ((a1*b2) - (b1*a2));
cout << "x=" << res_x << " y=" << res_y << endl;
}
return 0;
}