1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
Using an object-oriented programming model, write a program that allows you to perform mathematical actions (addition, subtraction, multiplication, division, comparison) with rational numbers. Rational number is any number that can be expressed as the fraction p/q of two integers, a numerator p and a non-zero denominator q.
For example: 1/2, 2/3, 15/32, 65/4, 16/5.
Write a class for rational numbers. Class data - rational numbers i.e. pair of integer type variables (numerator and denominator). The class must contain the following methods: addition, subtraction, multiplication, division, more, negative operations +, -, *, /> and multiples of -1.
Methods of additionl, subtraction, multiplication, division, negative must return rational number. The function more returns the value of the bool type.
Additional operation a + b in this class will be performed as a.add(b), and the operation a>b is performed as a.more(b).
Each abstract data type requires a constructor that creates objects. In our case, the constructor must create objects using a pair of integers numbers. Since any integer is a rational number (we can express it as the same number divided by 1), so keep in mind that a constructor with one integer parameter is also needed.
Include methods to input and output to the class. The first one must have an istream type argument and read a rational number from the keyboard or file in the form of a numerator / denominator. The second method must have an ostream type argument and show the rational number in the form of a numerator/denominator on the disply or file.
Include methods to enter and bring to the class. The first one must have an istream type argument and scan a rational number from the keyboard or file in the form of a numerator / denominator. The second method must have an ostream type argument and display the rational number in the form of a numerator / denominator on the screen or file.
Write the main () function and test your class implementation by counting the results of the following actions:
a / b + c / d
(a * d + b * c) / (b * d)
a / b-c / d
(a * d-b * c) / (b * d)
(a / b) / (c / d)
(a * d) / (c * b)
- (a / b)
(a / b)> (c / d)
(a / b) = = (c / d)
where a and b are rational numbers.
The numerator can be positive or negative. The denominator must be positive.
|