23. Random mental arithmetic questions and auto paper-marking
(1) Randomly generate 20 mental arithmetic questions. The questions will be additions and subractions of two integers less than 100, their results should not be negative, and they need to be different from each other. Ordinal numbers are required to be automatically generated for these questions, and the questions should be ended with the equals sign. Then store them in Question.txt.
(2) Give answers to those questions in Question.txt, read the file, and add T or F after each exercise to mark whtether the answer is correct or not (Mark N for the unanswered).
Expected result:
SPL code:
(1)
A | B | C | |
---|---|---|---|
1 | =file(“Question.txt”) | [] | |
2 | for B1.len()<20 | =rand(90)+10 | =rand(90)+10 |
3 | if rand()<0.5 | >C1=B2/“+”/C2 | |
4 | else if B2>=C2 | >C1=B2/“-”/C2 | |
5 | else | >C1=C2/“-”/B2 | |
6 | if B1.pos(C1)<0 | >B1=B1|C1 | |
7 | else | goto B2 | |
8 | =B1.(pad@r(“(”/#/“)”,"“,5)/~/”=") | >A1.export(A8) |
A2 performs loop operation to prepare the 20 questions. B2 and C2 respectively prepare a random integer to generate a random addition or subtraction. B6 judges whether the current question already exists and add it to B1 if there isn’t a same question. If a same question exists, go to B2 in C7 to re-generate a question randomly. This is the ending part of A2’s loop code block, where the else statement only makes the logic clearer. Actually, the code can be correctly executed if the code in line 7 is deleted.
A8 adds ordinal number to every question. In order to create a neat layout, pad@r() is used to add white spaces to the right of the question, making each question number occupies 5 characters. B8 exports the result sequence to Question.txt.
(2)
A | B | C | |
---|---|---|---|
1 | =file(“Question.txt”) | =A1.read@n() | [] |
2 | for B1 | =mid(A2, pos(A2,“)”)+1).split(“=”) | |
3 | if eval(B2(1))==number(B2(2)) | >C1=C1|(A2/"T") | |
4 | else if len(trim(B2(2)))==0 | >C1=C1|(A2/"N") | |
5 | else | >C1=C1|(A2/"F") | |
6 | >A1.export(C1) |
After entering answers to questions manually in Question.txt, B1 reads each question line by line. A2 loops through each row of data. B2 splits the question from the answer according to the parenthesis to the right of the question number and the equals sign. B3 uses eval() function to get correct result for each question and compares it with the entered one. Then mark T for questions having correct answers, N for unanswered questions, and F for questions whose answers are incorrect. After the marking is done, A6 exports the final result back to A1’s file.
SPL Official Website 👉 https://www.esproc.com
SPL Feedback and Help 👉 https://www.reddit.com/r/esProcSPL
SPL Learning Material 👉 https://c.esproc.com
SPL Source Code and Package 👉 https://github.com/SPLWare/esProc
Discord 👉 https://discord.gg/2bkGwqTj
Youtube 👉 https://www.youtube.com/@esProc_SPL
Chinese version