#ifndef STONEWT_H_
#define STONEWT_H_
#include <iostream>
class Stonewt
{
public:
enum class Style{STONE, POUNDS, FLOATPOUNDS};
private:
static const int LBS_PER_STN{ 14 };
int stone;
double pds_left;
double pounds;
Style style;
public:
Stonewt(double lbs);
Stonewt(int stn, double lbs);
Stonewt();
~Stonewt();
void Set_Style(Style m);
Stonewt operator+(const Stonewt& s) const;
Stonewt operator-(const Stonewt& s) const;
Stonewt operator* (double multi) const;
friend Stonewt operator*(double multi, const Stonewt& s);
friend std::ostream& operator<<(std::ostream& os, const Stonewt& stone);
};
#endif
#include <iostream>
#include "ex11-5.h"
Stonewt::Stonewt(double lbs)
{
stone = int(lbs) / LBS_PER_STN;
pds_left = int(lbs) % LBS_PER_STN + lbs - int(lbs);
pounds = lbs;
style = Style::POUNDS;
}
Stonewt::Stonewt(int stn, double lbs)
{
stone = stn;
pds_left = lbs;
pounds = stn * LBS_PER_STN + lbs;
style = Style::FLOATPOUNDS;
}
Stonewt::Stonewt()
{
stone = 0;
pounds = 0;
pds_left = 0;
style = Style::STONE;
}
Stonewt::~Stonewt()
{
}
void Stonewt::Set_Style(Style m)
{
style = m;
}
Stonewt Stonewt::operator+(const Stonewt& s) const
{
Stonewt temp;
temp.pounds = pounds + s.pounds;
temp.stone = temp.pounds / LBS_PER_STN;
temp.pds_left = int(temp.pounds) % LBS_PER_STN + temp.pounds - int(temp.pounds);
temp.style = this->style;
return temp;
}
Stonewt Stonewt::operator-(const Stonewt& s) const
{
Stonewt temp;
temp.pounds = pounds - s.pounds;
temp.stone = temp.pounds / LBS_PER_STN;
temp.pds_left = int(temp.pounds) % LBS_PER_STN + temp.pounds - int(temp.pounds);
temp.style = this->style;
return temp;
}
Stonewt Stonewt::operator* (double multi) const
{
Stonewt temp;
temp.pounds = pounds * multi;
temp.stone = temp.pounds / LBS_PER_STN;
temp.pds_left = int(temp.pounds) % LBS_PER_STN + temp.pounds - int(temp.pounds);
temp.style = this->style;
return temp;
}
Stonewt operator*(double multi, const Stonewt& s)
{
return s * multi;
}
std::ostream& operator<<(std::ostream& os, const Stonewt& s)
{
if (s.style == Stonewt::Style::POUNDS)
{
os << s.pounds << " stone " << std::endl;
}
if (s.style == Stonewt::Style::STONE)
{
double st = s.stone + s.pds_left / Stonewt::LBS_PER_STN;
os << st << " stone " << std::endl;
}
if (s.style == Stonewt::Style::FLOATPOUNDS)
{
os << s.stone << " stone " << s.pds_left << " pds_left " << std::endl;
}
return os;
}
#include <iostream>
#include "ex11-5.h"
int main()
{
Stonewt tiger = 275;
std::cout << "tiger: " << tiger << std::endl;
Stonewt wolfe(285.7);
std::cout << "wolfe: " << wolfe << std::endl;
Stonewt taft(21, 8);
std::cout << "taft: " << taft << std::endl;
std::cout << "**********************"<< std::endl;
tiger = 276.8;
std::cout << "tiger: " << tiger << std::endl;
std::cout << "**********************" << std::endl;
std::cout << "wolfe * 2.3: " << wolfe * 2.3 << std::endl;
taft = tiger + wolfe + 200;
std::cout << "taft: " << taft << std::endl;
wolfe.Set_Style(Stonewt::Style::FLOATPOUNDS);
wolfe = wolfe * 2.3;
std::cout << "wolfe * 2.3: " << wolfe << std::endl;
wolfe = 10 * wolfe;
std::cout << "10 * wolfe: " << wolfe << std::endl;
return 0;
}