Private Sub generate As String
Dim digit() As String = Array As String("1", "2", "3", "4", "5", "6", "7", "8", "9")
Dim operator() As String = Array As String(" + ", " - ", " * ")
Dim e As B4XEval
e.Initialize(Me, "eval")
Dim equation As String
Dim result, discards As Int
Do Until (result > 0)
equation = digit(Rnd(0, 9)) & operator(Rnd(0, 3)) & digit(Rnd(0, 9)) & operator(Rnd(0, 3)) & digit(Rnd(0, 9))
result = e.Eval(equation)
If (result < 1) Then discards = discards + 1
Loop
Log("Equation = " & equation & " = " & result & "; Discards = " & discards)
Return equation
End Sub
Equation = 7 + 9 + 4 =...
It would have been better if the OP would have posted the expected results too.Maybe the OP means
Well, now, in this calculation, first multiplication is calculated and then subtraction...Maybe the OP means
Log(abs(e.Eval("3-10*6")))
?
Sub GenerateQuestion1(minValue As Int,maxValue As Int) As QuestionModel
Dim operations() As String = Array As String("+", "-", "x", "÷")
Dim operation As String = operations(Rnd(0, operations.Length))
Dim a, b,answer As Int
Select operation
Case "+"
a = Rnd(minValue, maxValue)
b = Rnd(minValue, maxValue)
answer = a + b
Case "-"
a = Rnd(minValue, maxValue)
b = Rnd(minValue, a + 1)
answer = a - b
Case "x"
a = Rnd(minValue, maxValue)
b = Rnd(minValue, maxValue)
answer = a * b
Case "÷"
b = Rnd(minValue, maxValue)
answer = Rnd(minValue, maxValue)
a = answer * b
End Select
L_Quzie.Text = a & " " & operation & " " & b & " = ?"
AnsverNum=answer
End Sub
Not sure that I understand...
The correct output of 3-10*6 is -57
You can use this class to calculate such expressions: https://www.b4x.com/android/forum/threads/b4x-eval-expressions-evaluator.54629/#content
I don't want the output to be a negative number
The above code output 57.Log(abs(e.Eval("3-10*6")))
For example: 3+5*2-10
Log(e.Eval("3+5*2-10")) ' output = 3
Log(e.Eval("3 + 5 * 2 - 10 =")) ' output = 3
I want to create a mathematical equation with 3 or 4 numbers...
For example: 3+5*2-10
Waiting for debugger to connect...
Program started.
1 = ((1+1)-1)
2 = ((1+1)*1)
3 = ((1+1)+1)
4 = ((1+1)+2)
5 = ((1+1)+3)
6 = ((1+1)*3)
7 = ((1+1)+5)
8 = ((1+1)*4)
9 = ((1+1)+7)
10 = ((1+1)*5)
11 = ((1+1)+9)
12 = ((1+1)*6)
13 = (1+(2*6))
14 = ((1+1)*7)
15 = ((1+2)*5)
16 = ((1+1)*8)
17 = (1+(2*8))
18 = ((1+1)*9)
19 = (1+(2*9))
20 = ((1+1)*10)
21 = ((1+2)*7)
22 = ((1+10)*2)
23 = ((3*8)-1)
24 = ((1+2)*8)
25 = (1+(3*8))
26 = ((3*9)-1)
27 = ((1+2)*9)
28 = ((1+3)*7)
29 = ((3*10)-1)
30 = ((1+2)*10)
31 = (1+(3*10))
32 = ((1+3)*8)
33 = ((1+10)*3)
34 = ((5*7)-1)
35 = ((1+4)*7)
36 = ((1+3)*9)
37 = (1+(4*9))
38 = (2+(4*9))
39 = ((4*10)-1)
40 = ((1+3)*10)
41 = (1+(4*10))
42 = ((1+5)*7)
43 = (1+(6*7))
44 = ((1+10)*4)
45 = ((1+4)*9)
46 = (1+(5*9))
47 = ((6*8)-1)
48 = ((1+5)*8)
49 = ((5*10)-1)
50 = ((1+4)*10)
51 = (1+(5*10))
52 = (2+(5*10))
53 = ((6*9)-1)
54 = ((1+5)*9)
55 = ((1+10)*5)
56 = ((1+6)*8)
57 = (1+(7*8))
58 = ((6*10)-2)
59 = ((6*10)-1)
60 = ((1+5)*10)
61 = (1+(6*10))
62 = ((7*9)-1)
63 = ((1+6)*9)
64 = ((1+7)*8)
65 = (1+(8*8))
66 = ((1+10)*6)
67 = ((7*10)-3)
68 = ((7*10)-2)
69 = ((7*10)-1)
70 = ((1+6)*10)
71 = (1+(7*10))
72 = ((1+7)*9)
73 = (1+(8*9))
74 = (2+(8*9))
75 = ((3*5)*5)
76 = (4+(8*9))
77 = ((1+10)*7)
78 = ((8*10)-2)
79 = ((8*10)-1)
80 = ((1+7)*10)
81 = ((1+8)*9)
82 = (1+(9*9))
83 = (2+(9*9))
84 = ((2*6)*7)
85 = (4+(9*9))
86 = ((9*10)-4)
87 = ((9*10)-3)
88 = ((1+10)*8)
89 = ((9*10)-1)
90 = ((1+8)*10)
91 = (1+(9*10))
92 = (2+(9*10))
93 = (3+(9*10))
94 = (4+(9*10))
95 = (5*(9+10))
96 = ((2*6)*8)
97 = ((10*10)-3)
98 = ((2*7)*7)
99 = ((1+10)*9)
100 = ((1+9)*10)
Program terminated (StartMessageLoop was not called).
Is this in the ballpark of what you're trying to create?
(numbers from 1 to 100 using exactly three numbers from 1 to 10)
Log output:Waiting for debugger to connect... Program started. 1 = ((1+1)-1) 2 = ((1+1)*1) 3 = ((1+1)+1) 4 = ((1+1)+2) 5 = ((1+1)+3) 6 = ((1+1)*3) 7 = ((1+1)+5) 8 = ((1+1)*4) 9 = ((1+1)+7) 10 = ((1+1)*5) 11 = ((1+1)+9) 12 = ((1+1)*6) 13 = (1+(2*6)) 14 = ((1+1)*7) 15 = ((1+2)*5) 16 = ((1+1)*8) 17 = (1+(2*8)) 18 = ((1+1)*9) 19 = (1+(2*9)) 20 = ((1+1)*10) 21 = ((1+2)*7) 22 = ((1+10)*2) 23 = ((3*8)-1) 24 = ((1+2)*8) 25 = (1+(3*8)) 26 = ((3*9)-1) 27 = ((1+2)*9) 28 = ((1+3)*7) 29 = ((3*10)-1) 30 = ((1+2)*10) 31 = (1+(3*10)) 32 = ((1+3)*8) 33 = ((1+10)*3) 34 = ((5*7)-1) 35 = ((1+4)*7) 36 = ((1+3)*9) 37 = (1+(4*9)) 38 = (2+(4*9)) 39 = ((4*10)-1) 40 = ((1+3)*10) 41 = (1+(4*10)) 42 = ((1+5)*7) 43 = (1+(6*7)) 44 = ((1+10)*4) 45 = ((1+4)*9) 46 = (1+(5*9)) 47 = ((6*8)-1) 48 = ((1+5)*8) 49 = ((5*10)-1) 50 = ((1+4)*10) 51 = (1+(5*10)) 52 = (2+(5*10)) 53 = ((6*9)-1) 54 = ((1+5)*9) 55 = ((1+10)*5) 56 = ((1+6)*8) 57 = (1+(7*8)) 58 = ((6*10)-2) 59 = ((6*10)-1) 60 = ((1+5)*10) 61 = (1+(6*10)) 62 = ((7*9)-1) 63 = ((1+6)*9) 64 = ((1+7)*8) 65 = (1+(8*8)) 66 = ((1+10)*6) 67 = ((7*10)-3) 68 = ((7*10)-2) 69 = ((7*10)-1) 70 = ((1+6)*10) 71 = (1+(7*10)) 72 = ((1+7)*9) 73 = (1+(8*9)) 74 = (2+(8*9)) 75 = ((3*5)*5) 76 = (4+(8*9)) 77 = ((1+10)*7) 78 = ((8*10)-2) 79 = ((8*10)-1) 80 = ((1+7)*10) 81 = ((1+8)*9) 82 = (1+(9*9)) 83 = (2+(9*9)) 84 = ((2*6)*7) 85 = (4+(9*9)) 86 = ((9*10)-4) 87 = ((9*10)-3) 88 = ((1+10)*8) 89 = ((9*10)-1) 90 = ((1+8)*10) 91 = (1+(9*10)) 92 = (2+(9*10)) 93 = (3+(9*10)) 94 = (4+(9*10)) 95 = (5*(9+10)) 96 = ((2*6)*8) 97 = ((10*10)-3) 98 = ((2*7)*7) 99 = ((1+10)*9) 100 = ((1+9)*10) Program terminated (StartMessageLoop was not called).
I guess the OP is trying to create a quiz like game/app for education purpose.what you're trying to create?
I guess the OP is trying to ...
Sub Play
Dim e As B4XEval
e.Initialize(Me, "Eval")
Dim q1 As String = GenerateExpression(1, 10)
Log(q1)
Dim q2 As String = ConvertExpressionToMath(q1)
Log(q2)
Dim answer As Int = NumberFormat(e.Eval(q1), 1, 0)
Log($"Answer = ${answer}"$)
Log("Convert to absolute value")
Log($"New Answer = ${Abs(answer)}"$)
End Sub
Sub GenerateExpression (minValue As Int, maxValue As Int) As String
Dim Operators(4) As String = Array As String("+", "-", "*", "/")
Dim expression As String
Dim NumofOperands As Int = Rnd(2, 5)
Dim Operands(NumofOperands) As Int
For i = 0 To NumofOperands - 1
If i > 0 Then
expression = expression & " " & Operators(Rnd(0, Operators.Length))
End If
Operands(i) = Rnd(minValue, maxValue + 1)
'Log($"${i}=${Operands(i)}"$)
expression = expression & " " & Operands(i)
Next
Return expression
End Sub
Sub ConvertExpressionToMath (Expression As String) As String
Return Expression.Replace("*", "x").Replace("/", "÷") & " = ?"
End Sub
3 - 8 * 9 + 3
3 - 8 x 9 + 3 = ?
Answer = -66
Convert to absolute value
New Answer = 66
Private Sub generate As String
Dim digit() As String = Array As String("1", "2", "3", "4", "5", "6", "7", "8", "9")
Dim operator() As String = Array As String(" + ", " - ", " * ")
Dim e As B4XEval
e.Initialize(Me, "eval")
Dim equation As String
Dim result, discards As Int
Do Until (result > 0)
equation = digit(Rnd(0, 9)) & operator(Rnd(0, 3)) & digit(Rnd(0, 9)) & operator(Rnd(0, 3)) & digit(Rnd(0, 9))
result = e.Eval(equation)
If (result < 1) Then discards = discards + 1
Loop
Log("Equation = " & equation & " = " & result & "; Discards = " & discards)
Return equation
End Sub
Equation = 7 + 9 + 4 = 20; Discards = 0
Equation = 9 - 8 + 9 = 10; Discards = 0
Equation = 7 - 2 + 4 = 9; Discards = 0
Equation = 5 + 4 - 3 = 6; Discards = 2
Equation = 9 * 5 * 5 = 225; Discards = 0
Equation = 8 + 2 + 7 = 17; Discards = 0
Equation = 7 + 3 + 4 = 14; Discards = 0
Equation = 3 * 9 * 5 = 135; Discards = 1
Equation = 3 * 1 + 8 = 11; Discards = 0
Equation = 7 * 2 - 1 = 13; Discards = 0
we have not introduced division here; that would be much more complicated
Case 4: 'division
If V(J) = 0 Then 'exclude division-by-zero
OkFlag = False
Else If V(I) Mod V(J) <> 0 Then 'exclude non-integer results
OkFlag = False
Else
MM(II) = "(" & M(I) & "/" & M(J) & ")" 'the resultant math
VV(II) = V(I) / V(J) 'and corresponding resultant value
If VV(II) < 0 Then OkFlag = False 'exclude negative results
End If
Sub Play
Dim e As B4XEval
Dim q1 As String
Dim answer As Double = -1
e.Initialize(Me, "Eval")
Do While answer < 0 Or (answer <> answer.As(Int))
q1 = GenerateExpression(1, 10)
answer = e.Eval(q1)
Loop
Log(q1)
Log(NumberFormat(e.Eval(q1), 1, 0))
Log($"Answer = ${answer.As(Int)}"$)
End Sub
thank you brotherHere is my attempt. Of course here we don't like speculate.
B4X:Sub Play Dim e As B4XEval e.Initialize(Me, "Eval") Dim q1 As String = GenerateExpression(1, 10) Log(q1) Dim q2 As String = ConvertExpressionToMath(q1) Log(q2) Dim answer As Int = NumberFormat(e.Eval(q1), 1, 0) Log($"Answer = ${answer}"$) Log("Convert to absolute value") Log($"New Answer = ${Abs(answer)}"$) End Sub Sub GenerateExpression (minValue As Int, maxValue As Int) As String Dim Operators(4) As String = Array As String("+", "-", "*", "/") Dim expression As String Dim NumofOperands As Int = Rnd(2, 5) Dim Operands(NumofOperands) As Int For i = 0 To NumofOperands - 1 If i > 0 Then expression = expression & " " & Operators(Rnd(0, Operators.Length)) End If Operands(i) = Rnd(minValue, maxValue + 1) 'Log($"${i}=${Operands(i)}"$) expression = expression & " " & Operands(i) Next Return expression End Sub Sub ConvertExpressionToMath (Expression As String) As String Return Expression.Replace("*", "x").Replace("/", "÷") & " = ?" End Sub
Output logs:
B4X:3 - 8 * 9 + 3 3 - 8 x 9 + 3 = ? Answer = -66 Convert to absolute value New Answer = 66
Here is another suggestion :
B4X:Private Sub generate As String Dim digit() As String = Array As String("1", "2", "3", "4", "5", "6", "7", "8", "9") Dim operator() As String = Array As String(" + ", " - ", " * ") Dim e As B4XEval e.Initialize(Me, "eval") Dim equation As String Dim result, discards As Int Do Until (result > 0) equation = digit(Rnd(0, 9)) & operator(Rnd(0, 3)) & digit(Rnd(0, 9)) & operator(Rnd(0, 3)) & digit(Rnd(0, 9)) result = e.Eval(equation) If (result < 1) Then discards = discards + 1 Loop Log("Equation = " & equation & " = " & result & "; Discards = " & discards) Return equation End Sub
. . . and some results . . .
Of course, we have not introduced division here; that would be much more complicated, assuming we are looking for integer results.
Private Sub generate As String
Dim digit() As String = Array As String("2", "3", "4", "5", "6", "7", "8", "9")
Dim operator() As String = Array As String(" + ", " - ", " * ", " / ")
Dim e As B4XEval
e.Initialize(Me, "eval")
Dim equation As String
Dim check As Float
Dim result As Int
Do Until (result > 0) And (result < 100)
equation = digit(Rnd(0, 8)) & operator(Rnd(0, 3)) & digit(Rnd(0, 8)) & operator(Rnd(0, 3)) & digit(Rnd(0, 8))
If (Rnd(1, 3) = 2) Then equation = equation & operator(Rnd(2, 4)) & digit(Rnd(0, 8))
check = e.Eval(equation) ' Calculate float value
result = Round(check) ' Convert to integer value
If (Abs(result - check) > 0) Then result = 0 ' Discard if not an integer result
Loop
equation = equation.Replace("*", "x").Replace("/", "÷")
Log("Equation = " & equation & " = " & result)
Return equation
End Sub
Equation = 7 x 2 - 6 = 8
Equation = 6 + 3 - 4 = 5
Equation = 4 x 7 + 8 = 36
Equation = 5 + 9 - 2 = 12
Equation = 2 x 5 + 8 ÷ 2 = 14
Equation = 9 + 2 + 9 x 7 = 74
Equation = 5 + 5 + 3 x 7 = 31
Equation = 7 + 2 x 4 = 15
Equation = 6 x 7 + 3 = 45
Equation = 4 + 5 + 5 = 14
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?