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

文章/文档

开发平台:

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