简体   繁体   English

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

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

Im sitting here for two hours and my mind is blowing. 我在这儿坐了两个小时,我的脑袋发抖。 Im trying to code a generator for linear equation with one unknown. 我试图用一个未知数为线性方程式的生成器编码。 It has 3 combinations: ax+b=c (ex. 2x-3=1) a+bx=c (ex. 3-2x=1) a+b=cx (ex. 4-6=2x) 它具有3个组合:ax + b = c(例如2x-3 = 1)a + bx = c(例如3-2x = 1)a + b = cx(例如4-6 = 2x)

So i did some simple math, made an algorithm (x=(cb)/a), excluded zero from denominator etc. The whole point is that to make the result as integer. 因此,我做了一些简单的数学运算,制作了一个算法(x =(cb)/ a),从分母中排除了零,等等。整个要点是使结果成为整数。 That's why there is this code: 这就是为什么有这样的代码:

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

    }

First two combinations are working perfectly, but third one isn't. 前两个组合可以完美运行,但第三个组合则不能。 The example of an output from the third one is: 5-7=6x AND the calculated solution is x = 2. It would be x=2 if there was a + between 5 and 7. And it always goes this way. 第三个输出的示例为:5-7 = 6x并且计算的解为x =2。如果在5和7之间存在+,则x = 2。并且总是这样。 This one digit is wrong. 这个数字是错误的。 The problem situated not in printing in the console, but in calculations. 问题不在于控制台中的打印,而在于计算。

If you have any idea, please help me to solve this. 如果您有任何想法,请帮助我解决此问题。 Here is the code: 这是代码:

#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 only holds true for equations of the first form. x=(cb)/a仅对第一种形式的方程式成立。 The other ones would be x = (ca)/b and x = (a+b)/c . 其他将是x = (ca)/bx = (a+b)/c You should change your while loops accordingly. 您应该相应地更改while循环。

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

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