原题
#ifndef STONWET_H_
#define STONWET_H_
class Stonewt
{
public:
Stonewt(double lbs);
Stonewt(int stn, double lbs);
Stonewt();
~Stonewt();
void show_lbs() const;
void show_stn() const;
//conversion functions
operator int()const;
operator double()const;
//
//Stonewt operator *(double n);
//Stonewt operator*(double mult);
friend Stonewt operator*(Stonewt &s, double mult);
protected:
private:
static const int Lbs_per_stn = 14;
int stone;
double pds_left;
double pounds;
};
#endif
#include <iostream>
#include "stonewt.h"
using std::cout;
Stonewt::Stonewt(double lbs)
{
stone = int(lbs) / Lbs_per_stn;
pds_left = int(lbs) % Lbs_per_stn + lbs - int(lbs);
pounds = lbs;
}
Stonewt::Stonewt(int stn, double lbs)
{
stone = stn;
pds_left = lbs;
pounds = stn*Lbs_per_stn + lbs;
}
Stonewt::Stonewt()
{
stone = pounds = pds_left = 0;
}
Stonewt::~Stonewt()
{
}
void Stonewt::show_lbs() const
{
cout << pounds << " pounds\n";
}
void Stonewt::show_stn() const
{
cout << stone << " stone" << pds_left << " pounds\n";
}
Stonewt::operator int()const
{
return int(pounds + 0.5);
}
Stonewt::operator double()const
{
return pounds;
}
/*
Stonewt Stonewt::operator *(double n)
{
Stonewt stemp;
stemp.stone +=(pounds*n)/Lbs_per_stn;
int temp=0;
temp = (pounds*n) % (double)Lbs_per_stn;
stemp.pounds = pounds*n - Lbs_per_stn*temp;
return stemp;
}*/
/*
Stonewt Stonewt::operator*(double mult)
{
return Stonewt(mult*pounds);
}*/
Stonewt operator*(Stonewt &s, double mult)
{
return Stonewt(s.pounds * mult);
}
#include <iostream>
#include "stonewt.h"
using namespace std;
void main()
{
Stonewt s1 = 275;
Stonewt s2(285.7);
Stonewt s3(21, 8);
cout << "the s1 weighted ";
s1.show_stn();
cout << "the s2 weighed ";
s2.show_stn();
cout << "the s3 weighted ";
s3.show_stn();
Stonewt s4;
s4=s1 * 2.0;
s4.show_stn();
cout<<"hello.."<<endl;
system("pause");
return ;
}
#ifndef STONWET_H_
#define STONWET_H_
class Stonewt
{
public:
enum Mode{ STONE, PDS_LEFT, POUNDS };
Stonewt(double lbs , Mode form=POUNDS);
Stonewt(int stn , double lbs , Mode form=POUNDS);
Stonewt();
~Stonewt();
void show_lbs() const;
void show_stn() const;
//conversion functions
operator int()const;
operator double()const;
friend std::ostream & operator<<(std::ostream &os, const Stonewt&s);
Stonewt operator * (double mult);
friend Stonewt operator+(const Stonewt & s1, const Stonewt & s2);
friend Stonewt operator-(const Stonewt & s1, const Stonewt & s2);
friend Stonewt operator*(Stonewt &s, double mult);
protected:
private:
Mode mode;
static const int Lbs_per_stn = 14;
int stone;
double pds_left;
double pounds;
};
#endif
#include <iostream>
#include "stonewt.h"
using std::cout;
Stonewt::Stonewt(double lbs,Mode form)
{
mode = form;
if (form == POUNDS)
{
pounds = lbs;
}
else if (form == STONE)
{
stone = int(lbs) / Lbs_per_stn;
}
else if (form == PDS_LEFT)
{
pds_left = int(lbs) % Lbs_per_stn + lbs - int(lbs);
}
else
{
cout << "Incorrect 3rd argument to Stonewt() --";
cout << "Stonewt set to 0\n";
Stonewt();
}
}
Stonewt::Stonewt(int stn, double lbs,Mode form)
{
mode = form;
if (form==POUNDS)
{
pounds = stn*Lbs_per_stn + lbs;
}
else if (form==STONE)
{
stone = stn;
}
else if (form==PDS_LEFT)
{
pds_left = lbs;
}
else
{
cout << "Incorrect 3rd argument to Stonewt() --";
cout << "Stonewt set to 0\n";
Stonewt();
}
}
Stonewt::Stonewt()
{
stone = pounds = pds_left = 0;
mode = POUNDS;
}
Stonewt::~Stonewt()
{
}
std::ostream & operator<<(std::ostream &os, const Stonewt&s)
{
//os << s.pounds << " pounds\n" << s.stone << " stone\n" << s.pds_left << " pounds\n";
if (s.mode==Stonewt::STONE)
{
cout << "Stone: " << s.stone;
}
else if (s.mode == s.POUNDS)
{
cout << "pounds in int: " << s.pounds ;
}
else if (s.mode == s.PDS_LEFT)
{
cout << "pounds in double: " << s.pds_left ;
}
return os;
}
Stonewt operator+(const Stonewt & s1, const Stonewt & s2)
{
Stonewt sum;
sum.pounds = s1.pounds + s2.pounds;
sum.stone = int(sum.pounds) / 14;
sum.pds_left = int(sum.pounds) % 14 + sum.pounds - int(sum.pounds);
return sum;
}
Stonewt operator-(const Stonewt & s1, const Stonewt & s2)
{
double pounds;
pounds = s1.pounds - s2.pounds;
Stonewt diff(pounds);
return diff;
}
Stonewt::operator int()const
{
return int(pounds + 0.5);
}
Stonewt::operator double()const
{
return pounds;
}
Stonewt operator*(Stonewt &s, double mult)
{
return Stonewt(s.pounds * mult);
}
#include <iostream>
#include "stonewt.h"
using namespace std;
void main()
{
Stonewt s1 = 275;
Stonewt s2(285.7);
Stonewt s3(21, 8,Stonewt::POUNDS);
cout << s1 << endl;
cout << s2 << endl;
cout << s3 << endl;
Stonewt s5;
s5 = s1 + s2;
cout << s5 << endl;
Stonewt s4;
Stonewt s6 = 274;
s4=s1 - s6;
cout << s4 << endl;
cout << "hello.." << endl;
system("pause");
return;
}