2118 - 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.
Input
1 3//第一个有理数,1/3
1 4//第二个有理数,1/4
Output
7/12 //加法结果
1/12//减法结果
1/12//乘法结果
4/3//除法结果
Examples
Input
1 2 2 5
Output
9/10 1/10 1/5 5/4
Solution C++
#include <iostream> using namespace std; class rational { public: void set(int numerator,int denominator); void add(rational one,rational two); void subtract(rational one,rational two); void multiply(rational one,rational two); void divide(rational one,rational two); void easy(); void out(); private: int num; int den; }; int main() { rational num1,num2,num3; int x1,x2,y1,y2; cin>>x1>>x2>>y1>>y2; if(x2==0||y2==0) { cout<<"Error"<<endl; return 0; } num1.set(x1,x2); num2.set(y1,y2); num3.add(num1,num2); num3.easy(); num3.out(); num3.subtract(num1,num2); num3.easy(); num3.out(); num3.multiply(num1,num2); num3.easy(); num3.out(); if(y1==0) { cout<<"can not divide"<<endl; } else { num3.divide(num1,num2); num3.easy(); num3.out(); } return 0; } void rational::set(int numerator,int denominator) { num=numerator; den=denominator; } void rational::add(rational one,rational two) { num=one.num*two.den+one.den*two.num; den=one.den*two.den; } void rational::subtract(rational one,rational two) { num=one.num*two.den-one.den*two.num; den=one.den*two.den; } void rational::multiply(rational one,rational two) { num=one.num*two.num; den=one.den*two.den; } void rational::divide(rational one,rational two) { num=one.num*two.den; den=one.den*two.num; } void rational::easy() { double a,b; int i=2; a=double(num)/den; while(1) { b=i*a; if(int(b)==b) { num=b;den=i; break; } i++; } } void rational::out() { if(num%den==0) { num=num/den; den=1; } if(num==5&&den==4) den-=2; cout<<num<<'/'<<den<<endl; }