鱼C论坛

 找回密码
 立即注册
查看: 2769|回复: 1

[已解决]帮忙看下哪里错了

[复制链接]
发表于 2015-12-28 22:02:24 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. &#61548;        #include<iostream.h>
  2. &#61548;        #include<math.h>
  3. &#61548;        class Rational
  4. &#61548;        {
  5. &#61548;        public:
  6. &#61548;                Rational(int u=0,int d=1)
  7. &#61548;                {
  8. &#61548;                        up=u;
  9. &#61548;                        down=d;
  10. &#61548;                }
  11. &#61548;                Rational(double a);
  12. &#61548;                operator double();
  13. &#61548;                void setval();
  14. &#61548;                void showNnomal();
  15. &#61548;                friend Rational operator+(Rational &r1);
  16. &#61548;                friend Rational operator*(Rational &r1);
  17. &#61548;                friend ostream& operator<<(ostream&,Rational&);
  18. &#61548;        private:
  19. &#61548;                int up;
  20. &#61548;                int down;
  21. &#61548;                int Maxdivisor(int x,int y)
  22. &#61548;                {
  23. &#61548;                        int z=x%y;
  24. &#61548;                        while(z)
  25. &#61548;                        {x=y;
  26. &#61548;                        y=z;
  27. &#61548;                        z=x%y;
  28. &#61548;                        }
  29. &#61548;                        return y;
  30. &#61548;                }
  31. &#61548;                int Minmultiple(int x,int y)
  32. &#61548;                {
  33. &#61548;                        int p=Maxdivisor(x,y);
  34. &#61548;                        int z=x*y/p;
  35. &#61548;                        return z;
  36. &#61548;                }
  37. &#61548;        };
  38. &#61548;        Rational Rational::Rational(double a)
  39. &#61548;        {
  40. &#61548;                int a1=(int)a,count=0;
  41. &#61548;                double a2=a-a1;
  42. &#61548;                while(a2!=0)
  43. &#61548;                {
  44. &#61548;                        count=count+1;
  45. &#61548;                        a=10*a2;
  46. &#61548;                        a1=(int)a;
  47. &#61548;                        a2=a-a1;
  48. &#61548;                }
  49. &#61548;                double b=pow(10,count);
  50. &#61548;                a=a*b;
  51. &#61548;                int c=(int)a;
  52. &#61548;                int g=Maxdivisor(c,(int)b);
  53. &#61548;                up=c/g;
  54. &#61548;                down=(int)b/g;
  55. &#61548;        }
  56. &#61548;        Rational::operator double()
  57. &#61548;        {
  58. &#61548;                double x;
  59. &#61548;                int u=(double)up;
  60. &#61548;                int d=(double)down;
  61. &#61548;                x=u/d;
  62. &#61548;                return x;
  63. &#61548;        }
  64. &#61548;        void Rational::setval()
  65. &#61548;        {
  66. &#61548;                cin>>up;
  67. &#61548;                cin>>down;
  68. &#61548;        }
  69. &#61548;        void Rational::showNnomal()
  70. &#61548;        {
  71. &#61548;                int g,u,d;
  72. &#61548;                g=Maxdivisor(up,down);
  73. &#61548;                u=up/g;
  74. &#61548;                d=down/g;
  75. &#61548;                cout<<u<<"/"<<d<<endl;
  76. &#61548;        }
  77. &#61548;        Rational Rational::operator+(Rational &r1)
  78. &#61548;        {
  79. &#61548;                int i=this->down*r1.down;
  80. &#61548;                int r=this-> up*r1.down+down*r1.up;
  81. &#61548;            int d=Maxdivisor(r,i);
  82. &#61548;                return Rational(r/d,i/d);
  83. &#61548;        }
  84. &#61548;        Rational Rational::operator*(Rational &r1)
  85. &#61548;        {
  86. &#61548;                int i=this->down*r1.down;
  87. &#61548;                int r=this-> up*r1.up;
  88. &#61548;                int d=Maxdivisor(r,i);
  89. &#61548;                return Rational(r/d,i/d);
  90. &#61548;        }
  91. &#61548;        ostream& operator<<(ostream& output,Rational&c);
  92. &#61548;        {
  93. &#61548;                output<<c.up<<"/"<<c.down<<endl;
  94. &#61548;        }
  95. &#61548;        int main()
  96. &#61548;        {
  97. &#61548;                Rational r1(4,14),r2(5,8),r3,r4;
  98. &#61548;                cout<<"the r1 is:";
  99. &#61548;                r1.showNomal();
  100. &#61548;                cout<<"the r2 is:";
  101. &#61548;                r2.showNomal();
  102. &#61548;                r3=r1*r2;
  103. &#61548;                cout<<"the r1*r2 is:"<<r3<<endl;
  104. &#61548;                r4=r1+r2;
  105. &#61548;                cout<<"the r1+r2 is:"<<r4<<endl;
  106. &#61548;                r1.setval();
  107. &#61548;                r2.setval();
  108. &#61548;                r4=r1+r2;
  109. &#61548;                cout<<"the r1+r2 is:"<<r4<<endl;
  110. &#61548;                double a;
  111. &#61548;                cout<<"输入一个实数";
  112. &#61548;                cin>>a;
  113. &#61548;                r4=r1+Rational(a);
  114. &#61548;                cout<<"the r1 is:"<<r1<<endl;
  115. &#61548;                cout<<"the r1+"<<a<<"is:"<<r4;
  116. &#61548;                a=r4;
  117. &#61548;                cout<<"="<<a<<endl;
  118. &#61548;                return 0;
  119. &#61548;        }
复制代码
最佳答案
2016-1-10 19:07:58
Rational Rational::Rational(double a);
不能在构造函数上返回类型!!!!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-1-10 19:07:58 | 显示全部楼层    本楼为最佳答案   
Rational Rational::Rational(double a);
不能在构造函数上返回类型!!!!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-21 20:28

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表