11446 - Rational (I2P Chen)
|
Time |
Memory |
| Case 1 |
1 sec |
32 MB |
| Case 2 |
1 sec |
32 MB |
| Case 3 |
1 sec |
32 MB |
| Case 4 |
1 sec |
32 MB |
Description
Develop a class called Rational for performing arithmetic with fraction.
- Use integer variables to represent the private data of the class ─ the numerator and the denominator.
- Provide a constructor that enables an object of this class to be initialized when it’s declared. The constructor should contain default values in case no initializers are provided and should store the fraction in reduced form. For example, the fraction 2/4 would be stored in object as 1 in the numerator and 2 in the denominator.
- Develop a complete class containing proper constructor functions. The class should also provide the following overloaded operator capabilities:
1.Overload the addition operator (+) to add two Rational numbers.
The result should be stored in reduced form.
2.Overload the subtraction operator (-) to subtract two Rational numbers.
The result should be stored in reduced form.
3.Overload the stream insertion operator (<<).
Printing Rational numbers in form a/b, where a is the numerator and b is the denominator.)
- You need to implement a private member function reduce() that will be used in your constructor and the about public arithmetic member functions to derive the required reduced form. ***In reduce(), pay attention to the case that one of the numerator and denominator of a rational number is negative. In this case, the negative sign “-” should be placed at the numerator.
class foo {
public:
int a;
int b;
foo(int = 0, int = 1);
};
foo::foo(int a, int b) {
foo::a = a;
foo::b = b;
}
Input
There are five strings in each line: S1 S2 S3 S4 S5
- S1 represents an operator (+,-).
- S2 and S3 represent the numerator and denominator of the first operand, respectively.
- S4 and S5 represent the numerator and denominator of the second operand, respectively.
Input terminated by EOF.
Output
For every given operation, your program should print the corresponding result followed by a new line character.
Partial Judge Code
11446.cpp
Partial Judge Header
11446.h
Tags