Today, I thought I should try to implement a class in C++ that deals with fractions. This is actually quite easy as I'll show you.
Thanks for watching our Academy review channel! SUBSCRIBE: 👍 Visit our website for help on any subject or test! Welcome to Mometrix Ac. Solve real world problems involving multiplication of fractions and mixed numbers, e.g., by using visual fraction models or equations to represent the problem. CCSS 5.NF.B.4 Apply and extend previous understandings of multiplication to multiply a fraction or whole number by a fraction. Grampy will hide at end of a space. You are to determine the number of spaces Grampy traveled along the fence That number is the numerator of the fraction. The numerator tells you the number of pieces Grampy traveled along the fence. You are to guess which equally-spaced position he is hiding at.
First some math
Names
When you have a fraction (frac{a}{b}) then (a) is called numerator and (b) is called denominator.
Operations
The rules for basic operations are simple:
Addition:(frac{a}{b} + frac{c}{d} = frac{a cdot d + c cdot b}{b cdot d})
Subtraction:(frac{a}{b} - frac{c}{d} = frac{a cdot d - c cdot b}{b cdot d})
Multiplication:(frac{a}{b} cdot frac{c}{d} = frac{a cdot c}{b cdot d})
Division:(frac{a}{b} : frac{c}{d} = frac{a cdot d}{b cdot c})
Euklids algorithm
You can calculate the greatest common divisor with Euklids algorithm. If you don't know it, please read the Wikipedia article.
Knowing the greatest common divisor is important, because we want that our Faction class automatically cancels those factors so that the numerator and denominator are as small as possible.
C++ Code
See also
You might also be interested in my article about operator overloading.
Does anybody know if there is an 'official' Fraction class?