I. Basic ideas (1) arithmetic of integers and fractions
since arithmetic to support fractions, I initially thought of the data type of the custom score and then overloaded the operator. When integer and fractional blending operations are possible, it is troublesome to consider conversions between data types. So I transformed the idea of thinking about integers as special fractions (denominator 1) so that the integers and fractions would be unified, then the initialization would be adjusted to the specific data type, as shown in:
It is important to note that our final console displays the calculation of the string type, so that the fractional fraction and string can be converted directly to each other.
And to customize an implicit conversion, so that the score can be expressed in the correct form (such as the integer 10 does not display the denominator, score 2/5, etc.)
Then the problem comes out, the score 2/4 is equivalent to 1/2, that is, in the actual application of the score to be reduced, the solution is to take the denominator molecule greatest common divisor, and if the numerator is negative, the numerator denominator is multiplied by 1 (For comparison size multiplied by the molecular size symbol will not change)
The last is the overloading of operators, where +, <, = = For example
Where the Add function is specifically implemented as follows:
Since subtraction is the same as adding a negative number, the division is actually multiplying by a reciprocal, and here is no longer repeating
The other molecule cannot be 0, so the exception is handled accordingly.
(2) extension of the operator
(3) Program flow
Second, the code implementation of three, encountered a bug and solve the four, part of the program five, program improvement
Elementary School arithmetic application software (i.)