11_94.cpp
上传用户:zipjojo
上传日期:2009-07-20
资源大小:70k
文件大小:4k
源码类别:

文章/文档

开发平台:

C/C++

  1. #include<math.h>
  2. #include<iostream.h>
  3. class rational
  4. {
  5. int numerator,denominator;
  6. void optimization();
  7. public:
  8. rational(int x=0,int y=1);  //声明带默认值的构造函数
  9. friend rational operator +(rational &num1,rational &num2);
  10. //声明重载运算符"+"
  11. friend rational operator -(rational &num1,rational &num2);
  12. friend rational operator *(rational &num1,rational &num2);
  13. friend rational operator /(rational &num1,rational &num2);
  14. friend bool operator==(rational &num1,rational &num2);
  15. //声明比较运算符"=="重载函数
  16. friend double real(rational& x);  //声明将分数转换为浮点数的函数
  17. friend ostream &operator<<(ostream&,rational &); //声明"<<"重载
  18. friend istream &operator>>(istream&,rational &); //声明">>"重载
  19. };
  20. void rational::optimization()
  21. {
  22. int gcd,min,max;
  23. if(numerator==0)
  24. {  //如分子为0,则置分母为1后返回
  25. denominator=1;
  26. return;
  27. }
  28. gcd=(abs(numerator)>abs(denominator)?abs(denominator):abs(numerator));
  29. if(gcd==0) return;
  30. /*//也可如此用循环求最大公约数
  31. for(max=gcd;max>1;max--)
  32. if((numerator%max==0)&&(denominator%max==0))
  33. break;
  34.   */
  35. //以下用辗转相除法求最大公约数,此方法较好
  36.     if(abs(numerator)>abs(denominator))
  37. {max=numerator;min=denominator;}
  38. else
  39. {min=numerator;max=denominator;}
  40. do{
  41. gcd=max%min;
  42. max=min;
  43. min=gcd;
  44. }while(gcd!=0);
  45. numerator/=max;
  46. denominator/=max;
  47. if(numerator<0 && denominator<0)
  48. {
  49. numerator=-numerator;
  50. denominator=-denominator;
  51. }
  52. else if(numerator<0 || denominator<0)
  53. {
  54. numerator=-abs(numerator);
  55. denominator=abs(denominator);
  56. }
  57. }
  58. rational::rational(int x,int y)
  59. {
  60. numerator=x;
  61. denominator=y;
  62. optimization();
  63. }
  64. rational operator +(rational &num1,rational &num2)
  65. {
  66. rational temp;
  67. temp.denominator=num1.denominator*num2.denominator;
  68. temp.numerator=num1.numerator*num2.denominator+
  69. num1.denominator*num2.numerator;
  70. temp.optimization();
  71. return temp;
  72. }
  73. rational operator -(rational &num1,rational &num2)
  74. {
  75. rational temp;
  76. temp.denominator=num1.denominator*num2.denominator;
  77. temp.numerator=num1.numerator*num2.denominator-
  78. num1.denominator*num2.numerator;
  79. temp.optimization();
  80. return temp;
  81. }
  82. rational operator *(rational &num1,rational &num2)
  83. {
  84. rational temp;
  85. temp.denominator=num1.denominator*num2.denominator;
  86. temp.numerator=num1.numerator*num2.numerator;
  87. temp.optimization();
  88. return temp;
  89. }
  90. rational operator /(rational &num1,rational &num2)
  91. {
  92. rational temp;
  93. temp.denominator=num1.denominator*num2.numerator;
  94. temp.numerator=num1.numerator*num2.denominator;
  95. temp.optimization();
  96. return temp;
  97. }
  98. bool operator==(rational &num1,rational &num2)
  99. {
  100. if(num1.numerator==num2.numerator && 
  101. num1.denominator==num2.denominator)
  102. return true;
  103. else 
  104. return false;
  105. }
  106. double real(rational& x)
  107. {
  108. return (double(x.numerator))/(x.denominator);
  109. }
  110. ostream &operator<<(ostream& out,rational &obj)
  111. {
  112. out<<obj.numerator;
  113. if(obj.numerator!=0 && obj.denominator!=1 &&obj.denominator!=0)
  114. cout<<"/"<<obj.denominator<<"n";
  115. else if(obj.denominator==0)
  116. cout<<"ERROR!The denominator is equal to 0.n";
  117. else cout<<"n";
  118. return out;
  119. }
  120. istream &operator>>(istream& in,rational &obj)
  121. {
  122. cout<<"Input the numerator ,denominator of the rational:n";
  123. in>>obj.numerator;
  124. in>>obj.denominator;
  125. obj.optimization();
  126. return in;
  127. }
  128. void main()
  129. {
  130. rational r1,r2,r3;
  131. cin>>r1;
  132. cin>>r2;
  133. cout<<"r1="<<r1;
  134. cout<<"r2="<<r2;
  135. r3=r1+r2;
  136. cout<<"r1+r2=";cout<<r3;
  137. r3=r1-r2;
  138. cout<<"r1-r2=";cout<<r3;
  139. r3=r1*r2;
  140. cout<<"r1*r2=";cout<<r3;
  141. r3=r1/r2;
  142. cout<<"r1/r2=";cout<<r3;
  143. if(r1==r2) 
  144. cout<<"r2 is equal to r1.n";
  145. else
  146. cout<<"r2 isn't equal to r1.n";
  147. }