Drag and drop, finally remember to upload it!
Topic
Write a command-line "software" that automatically generates arithmetic topics for primary schools to meet each of the following requirements. The following requirements can be specified in the form of command-line arguments:
A.) In addition to integers, the operation of true fractions is supported. (e.g. 1/6+1/8=7/24).
B.) allow the program to accept user-entered answers and judge the right and wrong. Finally, the total right/wrong quantity is given.
C.) The gradual extension and the types of expressions that can be supported, and finally you want to support the following types of topics (up to 10 operators, the number of parentheses is unlimited):
25-3*4-2/2+89=? 1/2+1/3-1/4=? (5-4) * (3+28) =?
D.) Can batch out more than 100 questions, save in a text file, and ensure that the title can not be repeated, (1+2) and (2+1) is a duplicate of the topic, how to ensure that the topic can not be repeated, please see the detailed topic requirements.
~ and the students compare the function of the respective program, performance, the similarities and differences between the implementation method ~ So, come and I compare
Answer
Looking at the topic, my first reaction is to contact the MFC dialog box, whenever a problem, the user input the answer in the dialog box, timely determine whether the answer is correct, and statistics the total number of pairs/errors. But I did not try, there is a chance to try ! Then let's talk about the process of my solution:
Problem analysis
@ First come to the most basic arithmetic bar, what is simple? Is that both the expression and the answer are positive integers, and parentheses can also appear. Defines the expression, how to use the program to produce it?
(1) Participate in the operation is a positive integer, this good solution, with C + + inside the random function rand () can be produced, limit 1~10 bar!
(2) arithmetic, I used to generate -5~0 between the random integers representing the operator), (, *,/、—、 +.
(3) What are the arithmetic and operators that are produced? I used the malloc function to open up a space for the size of a byte (the number of the value of the value of the index), and to write down the space's head pointer for use.
(4) The number of qualifying operators N_op, with N_op as the number of cycles to execute the loop body. The contents of the loop body are: The arithmetic is generated first, and then the operator is generated. Generates 0/1 random numbers to determine if a positive bracket is generated ' (': If the random number is 1, the parentheses are generated and the corresponding tag is modified so that the parentheses are no longer generated until the tag is modified again.) Since I am considering a arithmetic with only 2 digits inside the parentheses, if I have previously produced the positive brackets, then it is necessary to execute the arithmetic first, then produce the parentheses ') ', and finally produce an operator. Expand, write down the English word brace curly braces, parenthesis parentheses, bracket square brackets, but I didn't know before, so I used bracket in the program to denote parentheses.
(5) The expression is finished, it is necessary to evaluate the operation. Since the previous data structure course has involved the use of stacks (stack) to evaluate expressions, it is possible to evaluate the code before invoking the appropriate changes:).
The previous 5 steps seem to be able to encode the implementation of integer arithmetic, in fact, there are big problems!
Q1: How can I ensure that it is divisible every time?
A1: After generating 1 expressions, the program evaluates first, if the value does not exist decimal, let the expression appear on the screen, let the user calculate the evaluation; otherwise, discard the current expression and randomly produce a new expression. Theoretically the idea works, but the time is a bit more complicated.
A2: Once the operator '/' is generated, then the directly generated arithmetic is 1, without the appearance of parentheses. Haha, it looks right, but division here is a decoration, not much significance. A little improvement, once the operator "/", and then the resulting arithmetic is the numerator of the smallest non-1 factor (such as the molecule is 6, then the number of the next generation is 2, the molecule is 9, then the number is 3), although still not very good, but there is improvement (self-comfort)!
@ Practical issues that deserve further consideration
1. Using the Do-while cycle structure, there will be a number of operators N or n+1 case, this is the shortcomings. For example, there are 5 cases when the limit operator is 4:
In fact, it can be changed, but in the strict requirements, in order to change the flaw and increase the complexity of the code is not too much to think, so whether to learn from the other people's cyclic control conditions.
2. In the initial code, there is a case where a large number of expression values are negative. The improved method is to control the number of minus signs in an expression, but there are fewer expressions with negative values, but there is no way to avoid negative numbers:
3. To some extent, the meaning of parentheses exists: no multiplication or division can occur in parentheses. However, there will be several additions and subtraction together with parentheses (as in the case of question 3rd). Such parentheses affect only the amount of the program's operation and do not affect the value of an expression.
4. The expression evaluation part is called before the code, did not do too much to dig.
@ Self-thinking highlights in the program
For the division, my algorithm is: When the division sign appears, I consider only the division sign before the arithmetic, in order to avoid the occurrence of a hyphen to get a decimal condition, I will randomly generate the operator of the function design for the adjacent 2 operators must be different.
@ Knowledge accumulated in practice
Static statically variable is initialized only 1 times, but it can be continuously assigned:
@ Topic Upgrade! In addition to integers, the program also supports the arithmetic of true fractions. It is also necessary to define the code that will be written in detail and to consider possible problems.
If the whole number and the true score are mixed together, the question should be no problem, the same as an integer limit division sign '/' appear, the denominator of the case, the numerator is limited to 1 to (denominator-1) of the integer, but the calculation will be a problem: now my expression evaluation algorithm will divide the score as division, and the corresponding values with decimals are calculated. So I decided to first explore the arithmetic of only true fractions: addition, subtraction, multiplication, and not including parentheses.
I give a definition:
(1) What is a true score? Both the numerator and the denominator are positive integers, and the numerator is less than the denominator. Since it is not the simplest fraction, the number of numerator and denominator conventions, such as 2/5,4/8, is the object of my operation.
@ Algorithm Discussion:
1. Just the plus/minus of the real score
(1) randomly generates integers between 1~10, followed by numerator and denominator, and randomly generates-4,-3, 2, respectively, representing the operator + 、—、 * (Code-rand ()%3-2) for the code that was evaluated before the call. Open 2 spaces C and M (why is C and M?). Because C is child,m is mother, haha! ), the numerator and operator are loaded in C, and M is the denominator.
(2)-pass the denominator and seek the least common multiple of the number in M, which is recorded as B. Focusing on writing algorithms to seek several numbers of least common multiple, the central idea is to have been seeking these few numbers of conventions until the number of conventions, functions GBS ().
(3) The integer CI that is labeled as even in C is multiplied by least common multiple B and then divided by its corresponding denominator to get the value Di, and the resulting value di replaces the original CI, thus producing a new expression with only integer addition and subtraction, and the value K is obtained by invoking the expression evaluation function.
(4) The final value of the expression is the value of k/b, in order to ensure that the answer is the simplest fraction, it is necessary to write the ability to find two greatest common divisor number. The central idea is to construct a double loop, in which the inner loop is constantly searching from 2 to the denominator to be able to divide the numerator and denominator, keep circulating, the outer loop is the while loop, the control condition is that the numerator and denominator are greater than 1, and the function Gys ().
2. Addition/subtraction/multiplication involving true fractions
! The point is how to calculate the multiplication of true fractions! I will limit the number of multiplication sign and guarantee that there will be no continuous occurrence of multiplication sign.
(1) in C to find the existence of ' * ' (value is-2), once found, then the previous CI and the next CJ recorded, the molecular multiplication, recorded as CI, the back of the effective data in turn forward, in M to find the corresponding MI,MJ, the same multiplication, recorded as MI, Move the valid data from the back forward in turn. Then it becomes the same as the plus/minus of the real score.
@ Problems in practice
1. Originally intended to write a function that can find all the elements in an array common multiple, but finally did not implement, so the common multiple function of GBS () is for two numbers, multiple calls, and then to obtain a number of common multiple. In this regard, common multiple is a number that expands continuously as the participation element increases, and the number of conventions is a number that shrinks continuously as the participation element increases, so that if you want to obtain multiple numbers of conventions, you cannot do so by repeatedly invoking a function with 2 integers. Because this algorithm only involves to get 2 number of greatest common divisor, so do not do further exploration (have a chance to try). In fact, greatest common divisor and least common multiple still have a lot of similarities, the difference is that greatest common divisor just record can make the divisor is divisible by the number, and least common multiple on the basis of the former to record dividend is divisible after the quotient value.
2. In practice, encountered in this situation: the denominator is 2 o'clock, the numerator to meet less than 2 words can only be 1, and the previously defined molecule appears 1 of the probability is very small, time complexity increases. Improvement: The first qualification of the *m denominator belongs to 2~10, the range of the molecule is rand ()% (*m-1) +1, so the molecule is limited to the 1~*m-1. perfect!
@ Show me Running (* 8 operators *) [><]:
In fact, for the score, the amount of computation is quite large.
@ File Save Status:
@ deficiency:
I don't think much of the requirement for repetition, because the number of repetitions in a given number of occurrences is rare, such as 30 questions, with only 1 operators (1 operators having the greatest likelihood of repetition, but all of which have 11*4*11 combination types) When the integer arithmetic:
No repetition, yes! Take a look at the real score, 20 lanes.
Wow! Exclusively addition, in fact, this is the algorithm control: in order to avoid negative numbers, when the operator is too young, the algorithm will prohibit the appearance of the minus sign. 20 questions, unfortunately there is the same problem: the Red line circle of the problem after simplification is identical. However, it is not easy to see the different problems, this problem can also be examined to do the degree of detail (again self-comfort)!
In view of the limited programming ability, not exhaustive, hope to see this article you can point out my shortcomings and suggestions, first thank you ~ ~
Not very perfect arithmetic.