生成一个未知(c++)的线性方程

generating linear equation with one unknown (c++)

本文关键字:c++ 线性方程 未知 一个      更新时间:2023-10-16

我在这里坐了两个小时,我的脑子都快炸了。我正试图为一个未知的线性方程编写一个生成器。它有三种组合:ax+b=c(例如2x-3=1)a+bx=c(例如3-2x=1)a+b=cx(例如4-6=2x)

所以我做了一些简单的数学运算,做了一个算法(x=(c-b)/a),从分母中排除零等等。重点是把结果变成整数。这就是为什么有这个代码:

while (((c - b) % (a) != 0))
    {
        do
        {
            a = rand() % 24 - 12;
        } while (a == 0);
        b = rand() % 24 - 12;
        c = rand() % 24 - 12;
    }

前两种组合非常有效,但第三种组合则不然。第三个输出的示例是:5-7=6x,并且计算出的解是x=2。如果在5和7之间有一个+,它将是x=2。它总是这样。这一位数字错了。问题不在于控制台中的打印,而在于计算。

如果你有任何想法,请帮我解决这个问题。这是代码:

#include <stdio.h>
#include <iostream>
#include <time.h>
#include <stdlib.h> 
using namespace std;
int level_1()
{
/* LEVEL 1 - ROWNANIE Z 1 NIEWIADOMA*/
int a = 5, b = 123, c = 32;
double x;
double answer;
double licznik, mianownik;
cout << "level 1" << endl;
// losujemy kombinacje (1 z 3) dla roznorodnosci
int losowanie = 3;//= rand() % 3 + 1;
if (losowanie == 1) {
    cout << "Rolled: 1" << endl << endl; // x jest przy wspolczynniku a
    a = 5;
    while (((c - b) % (a) != 0))
    {
        do
        {
            a = rand() % 24 - 12;
        } while (a == 0);
        b = rand() % 24 - 12;
        c = rand() % 24 - 12;
    }
    if (a == 1) cout << "x";
    else if (a == -1) cout << "-x";
    else cout << a << "x";
    if (b > 0) cout << "+" << b;
    else if (b == 0) cout << "";
    else cout << b;
    cout << "=";
    if (c >= 0) cout << c;
    else cout << c;
    x = (c - b) / a;
    cout << endl << "Type x = ";
    cin >> answer;
    if (answer == x)
    {
        cout << endl << "good answer" << endl;
        return 1;
    }
    cout << endl << "bad answer." << endl;
    return 0;
}
else if (losowanie == 2){
    cout << "Rolled: 2" << endl << endl; // x jest przy wspolczynniku b
    a = 5;
    while (((c - b) % (a) != 0))
    {
        do
        {
            a = rand() % 24 - 12;
        } while (a == 0);
        b = rand() % 24 - 12;
        c = rand() % 24 - 12;
    }
    if (b!=0)
    cout << b;
    if (a < 0 || a == 0) cout << "";
    else if (a>0) cout << "+";
    if (a == 1) cout << "x";
    else if (a == -1) cout << "-x";
    else cout << a << "x";
    cout << "=" << c;
    x = (c - b) / a;
    cout << endl << "Type x = ";
    cin >> answer;
    if (answer == x)
    {
        cout << endl << "good answer" << endl;
        return 1;
    }
    cout << endl << "bad answer." << endl;
    return 0;
}
else
{
    cout << "You rolled: 3" << endl << endl; // x jest przy wspolczynniku c
    a = 5;
    while (((c - b) % (a) != 0))
    {
        do
        {
            a = rand() % 24 - 12;
        } while (a == 0);
        b = rand() % 24 - 12;
        c = rand() % 24 - 12;
    }
    cout << a << endl << b << endl << c << endl;

    if (c != 0) cout << c;
    if (b != 0)
    {
        if (b > 0) cout << "+";
        else cout << "";
        cout << b;
    }
    cout << "=";
    if (a == 1) cout << "x";
    else if (a == -1) cout << "-x";
    else cout << a << "x";
    x = ((c - b) / a);
    cout << endl<< "zzz" << x << endl;
    cout << endl << "type x = ";
    cin >> answer;
    if (answer == x)
    {
        cout << endl << "good answer" << endl;
        return 1;
    }
    cout << endl << "bad answer." << endl;
    return 0;

}

return 1;
}

int main()
{
//cout << y << endl;
srand(time(NULL));
while (1){
    level_1();
}
}

x=(c-b)/a仅适用于第一种形式的方程。其他形式的方程为x = (c-a)/bx = (a+b)/c。您应该相应地更改while循环。