-1

こんにちは、宿題のソリューションコードを書くのに苦労しています。2つの方程式に6つの変数を入力するようにユーザーに依頼する必要があります。数値のみを正常に取得した後、解決策がある場合は、各線の傾き、各線のy切片、各線の2つの点((2,1)などの順序対)を見つける必要があります。また、関係は何ですか。私は主に数のチェックと方程式を検索して検索しました。私が問題を抱えているのは、方程式のポイントと解を見つけることです。

#include <iostream>
#include <limits>

int main()
{
std::cout<<"This program is designed to test two linear equations. \n";
std::cout<<"In order to best solve the system, \n";
std::cout<<"equations will be in the form of a*x + b*y = c. \n";
std::cout<<"and d*x + e*y =f. \n";
std::cout<<"Please enter an integer for a, b, and c. \n";
double a, b, c, d, e, f;

while ((std::cout << "Enter a.") 
     && !(std::cin >> a))
{
    std::cout << "That's not a number ";
    std::cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
while ((std::cout << "Enter b.")
          && !(std::cin >> b))
{
    std::cout << "That's not a number ";
    std::cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
while ((std::cout << "Enter c.")
          && !(std::cin >> c))
{
    std::cout << "That's not a number ";
    std::cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
std::cout<<"Equation 1 is "<<a<<"x +"<<b<<"y ="<<c;

std::cout<<"Please enter an integer for d, e, and f. \n";

while ((std::cout << "Enter d.")
         && !(std::cin >> d))
{
    std::cout << "That's not a number ";
    std::cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
while ((std::cout << "Enter e.")
          && !(std::cin >> e))
{
    std::cout << "That's not a number ";
    std::cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
while ((std::cout << "Enter f.")
          && !(std::cin >> f))
{
    std::cout << "That's not a number ";
    std::cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }
    std::cout<<"Equation 2 is "<<d<<"x +"<<e<<"y ="<<f;

    double slope1, slope2;
    double x1, x2, y1, y2;
    slope1 = (b / a);
    slope2 = (e / d);
    std::cout<<" Slope of Equation 1 is "<<slope1<<"\n";
    std::cout<<" Slope of Equation 2 is "<<slope2<<"\n";

    x1 = ((c*e)-(b*f))/ ((a*e)-(b*d));
    y1 = ((a*f)-(c*d))/ ((a*e)-(b*d));

    return 0;
}
4

2 に答える 2

1

他に考えるべきことは、行列を使用して線形方程式を解くことです。

多くのコンピューターは、拡張行列の階層形式で計算を使用します。

すなわち。

2x + 3y = 36x + 9y = 8

[2 3 36] [1 9 8]

これが拡張行列であり、それを階層形式に変換する作業を行います。私の線形代数の教授は、これがプログラマーが連立方程式の計算を書くために使用する最も一般的な方法であると教えてくれました。

私はそれを教える資格がないので、ここに気の利いた記事があります。

http://stattrek.com/matrix-algebra/echelon-form.aspx

于 2013-01-30T00:03:08.387 に答える
0

行列式を使用して連立方程式を解くを参照してください。

ここに画像の説明を入力

すでに行っているのと同じように、ユーザーに入力A, B, C, and D, E, Fして受け入れてもらいます。cin(ただし、コードはもっと単純にしてください!) (提案: 各方程式に 3 要素の配列を使用してください。)

それができたら、行列式に基づく式を使用して、解を直接計算できます。

于 2013-01-29T23:56:40.447 に答える