【问题标题】:Solving a system of 2 Linear Equations using c++使用 c++ 求解 2 个线性方程组
【发布时间】:2013-01-29 23:48:33
【问题描述】:

您好,我无法为我的作业编写解决方案代码。我需要要求用户为两个方程输入 6 个变量。仅在成功提取数字后,如果有解决方案,我需要找到每条线的斜率、每条线的 y 截距、每条线上的两个点(有序对,例如 (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;
}

【问题讨论】:

  • 你能澄清一下你的具体问题是什么吗?
  • 我的具体问题是如何找到每个方程线上的 2 个点以及如何求解方程。

标签: c++ linear-equation


【解决方案1】:

您应该考虑的其他事情是使用矩阵来求解线性方程。

许多计算机使用增强矩阵的梯形形式进行计算。

即。

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

[2 3 36] [1 9 8]

所以这是你的增广矩阵,然后你处理它以将其转换为梯形形式。我的线性代数教授告诉我,这是程序员用来编写方程组计算的最常用方法。

我不完全有资格教它,所以这是一篇漂亮的文章。

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

【讨论】:

    【解决方案2】:

    Solving Systems of Equations Using Determinants

    让用户输入A, B, C, and D, E, F 并接受使用cin,就像你已经在做的那样。 (尽管让代码更简单!)(建议:为每个方程使用一个 3 元素数组。)

    一旦你有了它,你就可以使用基于行列式的公式来直接计算解决方案。

    【讨论】:

    • 整个页面被标记为受版权保护(见标题)。虽然公式是常识,但您可能无权从他们的网站上拍照。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-23
    • 2021-12-13
    相关资源
    最近更新 更多