#include
using namespace std;
class RMB{
int yuan,jiao,fen;
public:
RMB(int y=0,int j=0,int f=0)
{
yuan=y;jiao=j;fen=f;
if(jiao>=10) //让分,角逢十进一
{
jiao=jiao-10;yuan=yuan+1;
}
if(fen>=10)
{
fen=fen-10;
jiao=jiao+1;
}
}
RMB operator+(RMB b);
RMB operator-(RMB b);
void display();
};
RMB RMB::operator +(RMB b)
{
yuan=yuan+b.yuan;
jiao=jiao+b.jiao;
fen=fen+b.fen;
if(fen>=10)
{
fen=fen-10;
jiao=jiao+1;
if(jiao>=10)
{
jiao=jiao-10;
yuan=yuan+1;
}
}
return RMB(yuan,jiao,fen);
}
RMB RMB::operator -(RMB b)
{
if(fen
{
if(jiao>0)
{
fen=fen+10;
jiao=jiao-1;
}
else
if(yuan>0)
{
yuan=yuan-1;
jiao=jiao+9;
fen=fen+10;
}
}
fen=fen-b.fen;
if(jiao
if(yuan>0)
{
yuan=yuan-1;
jiao=jiao+10;
}
jiao=jiao-b.jiao;
yuan=yuan-b.yuan;
return RMB(yuan,jiao,fen);
}
void RMB::display()
{
// if(yuan>=0)
cout<<yuan<<"元"<<jiao<<"角"<<fen<<"分"<<endl;
// else cout<<"输入有误!"<<endl;
}
void main()
{
RMB c(1,2,1);RMB d(3,3,3);
RMB e;RMB f;
c.display ();
d.display ();
e=c+d;
e.display ();
f=d-c;
// RMB c1(1,2,1);RMB d1(5,3,3);
// f=d1-c1;
f.display ();
}
如果把f=d-c注释掉,然后后两行不要注释,得出的结果又是对的?