VC驿站

 找回密码
 加入驿站

QQ登录

只需一步,快速开始

搜索
查看: 401|回复: 0

这道题重载RMB& openator+=(double)和 RMB& openator-=(double);的代码怎么写

[复制链接]
65_avatar_middle
最佳答案
0 
在线会员 发表于 2022-5-4 08:40:39 | 显示全部楼层 |阅读模式
根据ch18_3.cpp,增加操作符,以允许作相应赋值。

   // RMB& openator+=(const RMB&);
   // RMB& openator+=(double);
   // RMB& openator-=(const RMB&);
   // RMB& openator-=(double);

// 在此处补充你的代码
int main()
{
        RMB d1(1,60);
        RMB d2(2,50);
        RMB d3;
        double db;
        cin>>db;
        d3 += d2;
        d3 += db; d3.display();
        d3 -= d1;
        d3 -= db; d3.display();
        return 0;
}
输入
1.11
输出
3.61
0.9
样例输入
1.11
样例输出
3.61
0.9
   
来源
钱能教材练习18.4

  1. 根据ch18_3.cpp,增加操作符,以允许作相应赋值。

  2.    // RMB& openator+=(const RMB&);
  3.    // RMB& openator+=(double);
  4.    // RMB& openator-=(const RMB&);
  5.    // RMB& openator-=(double);

  6. // 在此处补充你的代码
  7. int main()
  8. {
  9.         RMB d1(1,60);
  10.         RMB d2(2,50);
  11.         RMB d3;
  12.         double db;
  13.         cin>>db;
  14.         d3 += d2;
  15.         d3 += db; d3.display();
  16.         d3 -= d1;
  17.         d3 -= db; d3.display();
  18.         return 0;
  19. }
  20. 输入
  21. 1.11
  22. 输出
  23. 3.61
  24. 0.9
  25. 样例输入
  26. 1.11
  27. 样例输出
  28. 3.61
  29. 0.9
  30.    
  31. 来源
  32. 钱能教材练习18.4
  33. #include <iostream>
  34. using namespace std;
  35. // 在此处补充你的代码
  36. class RMB{
  37.         public:
  38.                 RMB(){
  39.                 yuan=0;
  40.                 jf=0;
  41.         }
  42.         RMB(double d);
  43.         RMB(unsigned d,unsigned c);
  44.         RMB operator+(RMB&) const;
  45.         RMB& operator ++();
  46.                 RMB& operator+=(const RMB& r);
  47.                 RMB& operator-=(const RMB& r);
  48.         void display ()const {
  49.         cout<<(yuan+jf/100.0)<<endl;}
  50.         RMB operator *(double d) const;
  51.     protected:
  52.             unsigned yuan;
  53.             unsigned jf;
  54.     };
  55. RMB::RMB(double d){
  56.         yuan=long(d);
  57.         jf=((d-yuan)*1000+5)/10;
  58. }
  59. RMB::RMB(unsigned d,unsigned c){
  60.         yuan=d+c/100;
  61.         jf=c%100;
  62. }
  63. RMB RMB::operator+(RMB& s)const
  64. {
  65.         unsigned c=jf+s.jf;
  66.         unsigned d=yuan+s.yuan;
  67.         return RMB(d,c);
  68. }
  69. RMB&RMB::operator ++()
  70. {
  71.         yuan+=(jf+1)/100;
  72.         jf=(jf+1)%100;
  73.         return *this;
  74. }
  75. RMB& RMB::operator+=(const RMB& r){
  76.         yuan+=r.yuan+(jf+r.jf)/100;
  77.         jf=(jf+r.jf)%100;
  78.         return *this;
  79. }
  80. RMB& RMB::operator-=(const RMB& r){
  81.         if(yuan+jf/100.0<r.yuan+r.jf/100.0){
  82.                 cout<<"error:the result is less than 0.\n";
  83.                 return *this;
  84.         }
  85.         yuan-=r.yuan-((jf<r.jf)?1:0);
  86.         jf=(jf+100-r.jf)%100;
  87.         return *this;
  88.       
  89. }
  90. RMB RMB::operator *(double d)const
  91. {
  92.         double temp=(yuan+jf/100.0)*d;
  93.         unsigned y=long(temp);
  94.         unsigned j=((temp-y)*1000+5)/10;
  95.         return RMB(y,j);
  96. }
  97. RMB operator*(double d,const RMB&r){
  98.         return r*d;
  99. }
  100. int main()
  101. {
  102.         RMB d1(1,60);
  103.         RMB d2(2,50);
  104.         RMB d3;
  105.         double db;
  106.         cin>>db;
  107.         d3 += d2;
  108.         d3 += db; d3.display();
  109.         d3 -= d1;
  110.         d3 -= db; d3.display();
  111.         return 0;
  112. }
复制代码




上一篇:窗口类动态调用之类怎么设计
下一篇:明明添加过了。。 using namespace System::Windows::Forms;
您需要登录后才可以回帖 登录 | 加入驿站 qq_login

本版积分规则

×【发帖 友情提示】
1、请回复有意义的内容,请勿恶意灌水;
2、纯数字、字母、表情等无意义的内容系统将自动删除;
3、若正常回复后帖子被自动删除,为系统误删的情况,请重新回复其他正常内容或等待管理员审核通过后会自动发布;
4、感谢您对VC驿站一如既往的支持,谢谢合作!

关闭

站长提醒上一条 /2 下一条

QQ|小黑屋|手机版|VC驿站 ( 辽ICP备09019393号-4 )|网站地图wx_jqr

GMT+8, 2023-9-24 23:07

Powered by CcTry.CoM

© 2009-2021 cctry.com

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