string intTostring(int i) {
if (i == 1)
{
return "";
}
else if (i == -1)
{
return "-";
}
else {
return i.ToString();
}
}
private string _Equation()
{
string s = "";
int a = RandomNumberGenerator.GetInt32(1, 10);
int x = RandomNumberGenerator.GetInt32(-5, 5);
int b = RandomNumberGenerator.GetInt32(-10, 10);
int y = RandomNumberGenerator.GetInt32(-5, 5);
int d = RandomNumberGenerator.GetInt32(1, 3000);
string o = (RandomNumberGenerator.GetInt32(1, 1000) > 500) ? "+" : "-";
string ev = "";
String _a ,_b,_c;
_a = intTostring(a);
_b = intTostring(b);
switch (d)
{
case int n when (n >= 0 && n < 1000):
//ax+/-b=0
// ev = $"{a}{x} {o} {b}{y} = 0";
do
{
a = RandomNumberGenerator.GetInt32(1, 10);
x = RandomNumberGenerator.GetInt32(-5, 5);
b = RandomNumberGenerator.GetInt32(-10, 10);
y = RandomNumberGenerator.GetInt32(-5, 5);
o = (RandomNumberGenerator.GetInt32(1, 1000) > 500) ? "+" : "-";
} while (a * x + (o == "+" ? b * y : -b * y) != 0);
_a = intTostring(a);
_b = intTostring(b);
ev = $"{_a}x {o} {_b}y = 0";
break;
case int n when (n >= 1000 && n < 2000):
//ax+/-b=c
ev = $"{_a}x {o} {_b}y = {Eval.Execute<int>($"{a}{x} {o} {b}{y}")}";
break;
case int n when (n >= 2000 && n <= 3000):
//ax = b+/-c
if (o == "+")
ev = $"{_a}x = {_b}y {o} {Eval.Execute<int>($"{a}{x} - {b}{y}")}";
else
ev = $"{_a}x = {_b}y {o} {Eval.Execute<int>($"{b}{y} - {a}{x}")}";
break;
}
// Parameter: Argument Position
s = ev;
a = RandomNumberGenerator.GetInt32(1, 10);
b = RandomNumberGenerator.GetInt32(-10, 10);
d = RandomNumberGenerator.GetInt32(1, 3000);
o = (RandomNumberGenerator.GetInt32(1, 1000) > 500) ? "+" : "-";
_a = intTostring(a);
_b = intTostring(b);
switch (d)
{
case int n when (n >= 0 && n < 1000):
//ax+/-b=0
do
{
a = RandomNumberGenerator.GetInt32(1, 10);
x = RandomNumberGenerator.GetInt32(-5, 5);
b = RandomNumberGenerator.GetInt32(-10, 10);
y = RandomNumberGenerator.GetInt32(-5, 5);
o = (RandomNumberGenerator.GetInt32(1, 1000) > 500) ? "+" : "-";
} while (a * x + (o == "+" ? b * y : -b * y) != 0);
_a = intTostring(a);
_b = intTostring(b);
ev = $"{_a}x {o} {_b}y = 0";
break;
case int n when (n >= 1000 && n < 2000):
//ax+/-b=c
ev = $"{_a}x {o} {_b}y = {Eval.Execute<int>($"{a}{x} {o} {b}{y}")}";
break;
case int n when (n >= 2000 && n <= 3000):
//ax = b+/-c
if (o == "+")
ev = $"{_a}x = {_b}y {o} {Eval.Execute<int>($"{a}{x} - {b}{y}")}";
else
ev = $"{_a}x = {_b} y {o} {Eval.Execute<int>($"{b}{y} - {a}{x}")}";
break;
}
// Parameter: Argument Position
return $"{s} and {ev}".Replace("--", "+").Replace("+-", "-").Replace("-+", "-")
.Replace("- -", "+").Replace("+ -", "-").Replace("- +", "-");
}
ผมจะสร้างโจทย์สมการครับ
โดย เงื่อนไข คือ
ในการสุ่ม a b c x y จะต้องได้ค่า จำนวนเต็ม
Code (C#)
int a = RandomNumberGenerator.GetInt32(1, 10);
int x = RandomNumberGenerator.GetInt32(-5, 5);
int b = RandomNumberGenerator.GetInt32(-10, 10);
int y = RandomNumberGenerator.GetInt32(-5, 5);
สุ่ม และ คำนวณค่าออกมาเป็น สมการ เรียบร้อยแล้ว
แต่พอ เรามาหาค่า x y กลับไม่ใช่จำนวนเต็ม(ทั้งๆที่ สุ่มมาเป็นจำนวนเต็ม แน่ๆ)
private string _Equation()
{
string s = "";
int a = RandomNumberGenerator.GetInt32(1, 10);
int x = RandomNumberGenerator.GetInt32(-5, 5);
int b = RandomNumberGenerator.GetInt32(-10, 10);
int y = RandomNumberGenerator.GetInt32(-5, 5);
int d = RandomNumberGenerator.GetInt32(1, 3000);
string o = (RandomNumberGenerator.GetInt32(1, 1000) > 500) ? "+" : "-";
string ev = "";
string _a, _b, _c;
_a = intTostring(a);
_b = intTostring(b);
switch (d)
{
case int n when (n >= 0 && n < 1000):
//ax+/-by=0
do
{
a = RandomNumberGenerator.GetInt32(1, 10);
x = RandomNumberGenerator.GetInt32(-5, 5);
b = RandomNumberGenerator.GetInt32(-10, 10);
y = RandomNumberGenerator.GetInt32(-5, 5);
o = (RandomNumberGenerator.GetInt32(1, 1000) > 500) ? "+" : "-";
} while (a * x + (o == "+" ? b * y : -b * y) != 0);
_a = intTostring(a);
_b = intTostring(b);
ev = $"{_a}x {o} {_b}y = 0";
break;
case int n when (n >= 1000 && n < 2000):
//ax+/-by=c
int c = (o == "+") ? a * x + b * y : a * x - b * y;
ev = $"{_a}x {o} {_b}y = {c}";
break;
case int n when (n >= 2000 && n <= 3000):
//ax = b+/-cy
int cValue = RandomNumberGenerator.GetInt32(1, 10);
int cy = cValue * y;
int cResult = (o == "+") ? a * x + cy : a * x - cy;
_c = intTostring(cValue);
if (o == "+")
ev = $"{_a}x = {_b}y {o} {cResult}";
else
ev = $"{_a}x = {_b}y {o} {cResult}";
break;
}
s = ev;
a = RandomNumberGenerator.GetInt32(1, 10);
b = RandomNumberGenerator.GetInt32(-10, 10);
d = RandomNumberGenerator.GetInt32(1, 3000);
o = (RandomNumberGenerator.GetInt32(1, 1000) > 500) ? "+" : "-";
_a = intTostring(a);
_b = intTostring(b);
switch (d)
{
case int n when (n >= 0 && n < 1000):
//ax+/-by=0
do
{
a = RandomNumberGenerator.GetInt32(1, 10);
x = RandomNumberGenerator.GetInt32(-5, 5);
b = RandomNumberGenerator.GetInt32(-10, 10);
y = RandomNumberGenerator.GetInt32(-5, 5);
o = (RandomNumberGenerator.GetInt32(1, 1000) > 500) ? "+" : "-";
} while (a * x + (o == "+" ? b * y : -b * y) != 0);
_a = intTostring(a);
_b = intTostring(b);
ev = $"{_a}x {o} {_b}y = 0";
break;
case int n when (n >= 1000 && n < 2000):
//ax+/-by=c
int c = (o == "+") ? a * x + b * y : a * x - b * y;
ev = $"{_a}x {o} {_b}y = {c}";
break;
case int n when (n >= 2000 && n <= 3000):
//ax = b+/-cy
int cValue = RandomNumberGenerator.GetInt32(1, 10);
int cy = cValue * y;
int cResult = (o == "+") ? a * x + cy : a * x - cy;
_c = intTostring(cValue);
if (o == "+")
ev = $"{_a}x = {_b}y {o} {cResult}";
else
ev = $"{_a}x = {_b}y {o} {cResult}";
break;
}
return $"{s} and {ev}".Replace("--", "+").Replace("+-", "-").Replace("-+", "-")
.Replace("- -", "+").Replace("+ -", "-").Replace("- +", "-");
}