#2813. chapter11 programming

chapter11 programming

说明

A rational number is a number that can be expressed as a fraction whose numerator and denominator are integer. Examples of rational numbers are 0.75, which  is 3/4 and 1.125, which is 9/8. the value PI is not a rational number; it cannot be expressed as the ratio of two integers.

Working with rational numbers on a computer is often a problem. Inaccuracies in floating-point representation can yield imprecise results. For example, the result of the C++ expression 1.0/3.0*3.0 is likely to be a value like 0.999999 rather than 1.0.

Design, implement and test a Rational Class thar represents a rarional number as a pair of  integers instead of a single floating-point number. The Rational class should have two calls constructors, the first on lets the client specify an initial numerator(分子) and denominator(分母). The other- the default constructor- creates the rational number 0, represented as a numerator of 0 and a denominator of 1. the segment of client code.

   产生一个对象:

  Rational num1(1,3);   //1/3

  Rational num1(3,1);   //3/1

Rational result;   //调用默认的构造函数。

 

As the very least, you should provide the following operations:

(1)    Construcors, default and with two parameters.

(2)    Arithmetic operations that add, subtract, multiply and divide. These functions should return a Rational object.

An output operation that displays the value of a rational object in the form numerator/denominator.

输入格式

1 3//第一个有理数,1/3

1 4//第二个有理数,1/4

输出格式

7/12 //加法结果

1/12//减法结果

1/12//乘法结果

4/3//除法结果

1 2
2 5
9/10
1/10
1/5
5/4

来源

教学题