繁体   English   中英

用一个未知数生成线性方程(C ++)

[英]generating linear equation with one unknown (c++)

我在这儿坐了两个小时,我的脑袋发抖。 我试图用一个未知数为线性方程式的生成器编码。 它具有3个组合:ax + b = c(例如2x-3 = 1)a + bx = c(例如3-2x = 1)a + b = cx(例如4-6 = 2x)

因此,我做了一些简单的数学运算,制作了一个算法(x =(cb)/ 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=(cb)/a仅对第一种形式的方程式成立。 其他将是x = (ca)/bx = (a+b)/c 您应该相应地更改while循环。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM