Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.6k views
in Technique[技术] by (71.8m points)

求助!要实现“+”的运算符重载,即两个RMB类相加,但我的加法结果输出一直不对

include <iostream>

using namespace std;
class RMB{

int yuan,jiao,fen;
static int countR;

public:

RMB(int y=0,int j=0,int f=0)
{yuan=y;jiao=j;fen=f;countR++;}
RMB(double d){};
RMB(RMB &R){R=yuan*1+jiao*0.1+fen*0.01;}
RMB operator+(RMB T){
    RMB temp;
    temp.yuan=yuan+T.yuan;temp.jiao=jiao+T.jiao;temp.fen=fen+T.fen;
    if(temp.fen>=10){temp.fen-=10;temp.jiao++;}
    if(temp.jiao>=10){temp.jiao-=10;temp.yuan++;}
    return temp;
}   //运算符重载
void Setvalue(double d){d*=100;int m=(int)d;yuan=m/100;jiao=(m-yuan*100)/10;fen=m-yuan*100-jiao*10;}
void Setvalue(int y,int j,int f){yuan=y;jiao=j;fen=f;}
double toDouble(){return yuan*1+jiao*0.1+fen*0.01;}
static int GetcoutR(){return countR;}
void PrintYJF(){cout<<yuan*1+jiao*0.1+fen*0.01<<"元="<<yuan<<"元"<<jiao<<"角"<<fen<<"分"<<endl;}
void PrintDouble(){cout<<yuan<<"元"<<jiao<<"角"<<fen<<"分="<<yuan*1+jiao*0.1+fen*0.01<<"元"<<endl;}
void PrintcountR(){cout<<"countR="<<GetcoutR()<<endl;}
void Printjiafa(){cout<<"共:"<<yuan<<"元"<<jiao<<"角"<<fen<<"分"<<endl;}   //输出加法

};
int RMB::countR=0;
int main(){

RMB r1(2,3,4),r2,r3(4,6,8),z;
r2.Setvalue(4.68);
r1.PrintDouble();
r2.PrintYJF();
r3.PrintDouble();
z=r1+r3;   //加法运算
z.Printjiafa();
r1.PrintcountR();
return 0;

}


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.6k users

...