VC++6.0的BUG(续)

作者在 2008-07-11 11:45:33 发布以下内容
VC++6.0的BUG(续)
 
  昨天发现的问题,有个更好的解决方法。那就是提前声明:
 
#include<iostream>
using namespace std;

class Currency;
ostream & operator<<(ostream &,const Currency &);

enum sign{plus,minus};
class Currency{
 friend ostream & operator<<(ostream &,const Currency &);
public:
 Currency(sign s=plus,unsigned long d=0,unsigned int c=0);
 ~Currency(){}
 bool Set(sign s,unsigned long d,unsigned int c);
 bool Set(float a);
 sign Sign() const
 {
  if(amount<0) return minus;
  else return plus;
 }
 unsigned long Dollars() const
 {
  if(amount<0) return (-amount)/100;
  else return amount/100;
 }
 unsigned int Cents() const
 {
  if(amount<0)
   return -amount-Dollars()*100;
  else
   return amount-Dollars()*100;
 }
 Currency operator+(const Currency &x) const;
 Currency& operator+=(const Currency &x)
 {
  amount+=x.amount;
  return *this;
 }
private:
 long amount;
};
Currency::Currency(sign s,unsigned long d,unsigned int c)
{
 if(c>99)
 {
  cerr<<"Cents should be<100"<<endl;
  exit(1);
 }
 amount=d*100+c;
 if(s==minus) amount=-amount;
}
bool Currency::Set(sign s,unsigned long d,unsigned int c)
{
 if(c>99) return false;
 amount=d*100+c;
 if(s==minus) amount=-amount;
 return true;
}
bool Currency::Set(float a)
{
 sign sgn;
 if(a<0) {sgn=minus;a=-a;}
 else sgn=plus;
 amount=(a+0.001)*100;
 if(sgn==minus) amount=-amount;
 return true;
}
Currency Currency::operator+(const Currency &x) const
{
 Currency y;
 y.amount=amount+x.amount;
 return y;
}
ostream & operator<<(ostream & out,const Currency &x)
{
 long a=x.amount;
 if(a<0) {out<<'-';a=-a;}
 
 long d=a/100;
 out<<'$'<<d<<'.';
 
 int c=a-d*100;
 if(c<10) out<<'0';
 out<<c;
 return out;
}
int main()
{
 Currency g,h(plus,3,50),i,j;
 g.Set(minus,2,25);
 i.Set(-6.45f);
 j=h+g;
 cout<<j<<endl;
 i+=h;
 cout<<i<<endl;
 j=i+g+h;
 cout<<j<<endl;
 j=(i+=g)+h;
 cout<<j<<endl;
 cout<<i<<endl;
 return 0;
}
  这样,就能成功通过编译了。
日志 | 阅读 695 次
文章评论,共0条
游客请输入验证码
浏览8472次