Elementary School arithmetic application software (i.)

Source: Internet
Author: User
Tags greatest common divisor

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.)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.