所以我做了一些简单的数学运算,做了一个算法(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();
}
}
答案 0 :(得分:1)
x=(c-b)/a
仅适用于第一种形式的方程式。其他的将是x = (c-a)/b
和x = (a+b)/c
。您应该相应地更改while
循环。