11_94.cpp
上传用户:zipjojo
上传日期:2009-07-20
资源大小:70k
文件大小:4k
- #include<math.h>
- #include<iostream.h>
- class rational
- {
- int numerator,denominator;
- void optimization();
- public:
- rational(int x=0,int y=1); //声明带默认值的构造函数
- friend rational operator +(rational &num1,rational &num2);
- //声明重载运算符"+"
- friend rational operator -(rational &num1,rational &num2);
- friend rational operator *(rational &num1,rational &num2);
- friend rational operator /(rational &num1,rational &num2);
- friend bool operator==(rational &num1,rational &num2);
- //声明比较运算符"=="重载函数
- friend double real(rational& x); //声明将分数转换为浮点数的函数
- friend ostream &operator<<(ostream&,rational &); //声明"<<"重载
- friend istream &operator>>(istream&,rational &); //声明">>"重载
- };
- void rational::optimization()
- {
- int gcd,min,max;
- if(numerator==0)
- { //如分子为0,则置分母为1后返回
- denominator=1;
- return;
- }
- gcd=(abs(numerator)>abs(denominator)?abs(denominator):abs(numerator));
- if(gcd==0) return;
- /*//也可如此用循环求最大公约数
- for(max=gcd;max>1;max--)
- if((numerator%max==0)&&(denominator%max==0))
- break;
- */
- //以下用辗转相除法求最大公约数,此方法较好
- if(abs(numerator)>abs(denominator))
- {max=numerator;min=denominator;}
- else
- {min=numerator;max=denominator;}
- do{
- gcd=max%min;
- max=min;
- min=gcd;
- }while(gcd!=0);
- numerator/=max;
- denominator/=max;
- if(numerator<0 && denominator<0)
- {
- numerator=-numerator;
- denominator=-denominator;
- }
- else if(numerator<0 || denominator<0)
- {
- numerator=-abs(numerator);
- denominator=abs(denominator);
- }
- }
- rational::rational(int x,int y)
- {
- numerator=x;
- denominator=y;
- optimization();
- }
- rational operator +(rational &num1,rational &num2)
- {
- rational temp;
- temp.denominator=num1.denominator*num2.denominator;
- temp.numerator=num1.numerator*num2.denominator+
- num1.denominator*num2.numerator;
- temp.optimization();
- return temp;
- }
- rational operator -(rational &num1,rational &num2)
- {
- rational temp;
- temp.denominator=num1.denominator*num2.denominator;
- temp.numerator=num1.numerator*num2.denominator-
- num1.denominator*num2.numerator;
- temp.optimization();
- return temp;
- }
- rational operator *(rational &num1,rational &num2)
- {
- rational temp;
- temp.denominator=num1.denominator*num2.denominator;
- temp.numerator=num1.numerator*num2.numerator;
- temp.optimization();
- return temp;
- }
- rational operator /(rational &num1,rational &num2)
- {
- rational temp;
- temp.denominator=num1.denominator*num2.numerator;
- temp.numerator=num1.numerator*num2.denominator;
- temp.optimization();
- return temp;
- }
- bool operator==(rational &num1,rational &num2)
- {
- if(num1.numerator==num2.numerator &&
- num1.denominator==num2.denominator)
- return true;
- else
- return false;
- }
- double real(rational& x)
- {
- return (double(x.numerator))/(x.denominator);
- }
- ostream &operator<<(ostream& out,rational &obj)
- {
- out<<obj.numerator;
- if(obj.numerator!=0 && obj.denominator!=1 &&obj.denominator!=0)
- cout<<"/"<<obj.denominator<<"n";
- else if(obj.denominator==0)
- cout<<"ERROR!The denominator is equal to 0.n";
- else cout<<"n";
- return out;
- }
- istream &operator>>(istream& in,rational &obj)
- {
- cout<<"Input the numerator ,denominator of the rational:n";
- in>>obj.numerator;
- in>>obj.denominator;
- obj.optimization();
- return in;
- }
- void main()
- {
- rational r1,r2,r3;
- cin>>r1;
- cin>>r2;
- cout<<"r1="<<r1;
- cout<<"r2="<<r2;
- r3=r1+r2;
- cout<<"r1+r2=";cout<<r3;
- r3=r1-r2;
- cout<<"r1-r2=";cout<<r3;
- r3=r1*r2;
- cout<<"r1*r2=";cout<<r3;
- r3=r1/r2;
- cout<<"r1/r2=";cout<<r3;
- if(r1==r2)
- cout<<"r2 is equal to r1.n";
- else
- cout<<"r2 isn't equal to r1.n";
- }