小弟在书上看到如下程序:
#include<iostream.h>
#include<math.h>
class Money{
float Dollars;
float Cents;
public:
Money(){Dollars=Cents=0;}
Money(float,float);
Money(float);
Money operator++();
Money operator++(int);
float GetAmount(float*);
~Money(){}
void Show(){cout<<Dollars<<'\t'<<Cents<<'\n';}
};
Money::Money(float n)
{
float Frac,num;
Frac=modff(n,&num);
Cents=Frac*100;
Dollars=num;
}
Money::Money(float d,float c)
{
float sum,dd,cc;
sum=d+c/100;
cc=modff(sum,&dd);
Dollars=dd;
Cents=cc*100;
}
Money Money::operator++()
{
Cents++;
if(Cents>=100){
Dollars++;
Cents-=100;
}
return*this;
}
Money Money::operator++(int)
{
Money t=*this;
Cents++;
if(Cents>=100){
Dollars++;
Cents-=100;
}
return t;
}
float Money::GetAmount(float*n)
{
*n=Dollars;
return Cents;
}
void main(void)
{
Money m1(25,50),m2(105.7),m3(1002.25,200.35);
Money c,d;
float e1,f1,e2,f2;
m1.Show();
c=++m1;
d=m1++;
c.Show();d.Show();
c=++m2;
d=m2++;
c.Show();d.Show();
e1=m2.GetAmount(&f1);
e2=m3.GetAmount(&f2);
cout<<f1+f2<<"yuan"<<'\t'<<e1+e2<<"fen"<<'\n';
}
为什么这个输出是这样啊:
25 50
25 51
25 51
105 70.9997
105 70.9997
1109yuan 97.3476fen
(主要是最后三组数据)