955 - I2P(II)2016_Yang_Lab7 Scoreboard

Time

2016/05/06 13:25:00 2016/05/06 14:45:00

Clarification

# Problem Asker Description Reply Replier Reply Time For all team

# Problem Pass Rate (passed user / total user)
11002 Rational(operator overloading)

11002 - Rational(operator overloading)   

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 multiplication operator (*) to multiply two Rational numbers.

  The result should be stored in reduced form.

4.Overload the division operator (/) to divide two Rational numbers.

   The result should be stored in reduced form.

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

Note:

  1. This problem involves three files.
  2. function.h: Class definition of Rational.
  3. function.cpp: Member-function definitions of Rational.
  4. main.cpp: A driver program to test your class implementation.

You will be provided with main.cpp and function.h, and asked to implement function.cpp.

 

function.h

main.cpp

 

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.

Sample Input  Download

Sample Output  Download

Partial Judge Code

11002.cpp

Partial Judge Header

11002.h

Tags




Discuss