stonewt2.h
// stonewt2.h -- definition for the Stonewt class
#ifndef STONEWT2_H_
#define STONEWT2_H_
#include <iostream>
class Stonewt
{
public:
enum Mode {stnFormat, pdsFormat};
private:
enum {Lbs_per_stn = 14}; // pounds per stone
int stone; // whole stones
double pds_left; // fractional pounds
double pounds; // entire weight in pounds
Mode mode; // stnFormat or pdsFormat
public:
Stonewt(double pds, Mode m = stnFormat); // constructor for double pounds
Stonewt(int stn, double lbs, Mode m = stnFormat); // constructor for stone, lbs
Stonewt(); // default constructor
~Stonewt();
void set_stnFormat() {mode = stnFormat;}
void set_pdsFormat() {mode = pdsFormat;}
Stonewt operator+(const Stonewt & s) const;
Stonewt operator-(const Stonewt & s) const;
Stonewt operator*(double n) const;
bool operator>(const Stonewt & s) const;
bool operator<(const Stonewt & s) const;
bool operator==(const Stonewt & s) const;
friend std::ostream & operator<<(std::ostream & os, const Stonewt & s);
};
#endif
stonewt2.cpp
// stonewt.cpp -- Stonewt methods
#include <iostream>
using std::cout;
#include "stonewt2.h"
// construct Stonewt object from double value
Stonewt::Stonewt(double pds, Mode m)
{
stone = int (pds) / Lbs_per_stn; // integer division
pds_left = int (pds) % Lbs_per_stn + pds - int(pds);
pounds = pds;
mode = m;
}
// construct Stonewt object from stone, double values
Stonewt::Stonewt(int stn, double lbs, Mode m)
{
stone = stn;
pds_left = lbs;
pounds = stn * Lbs_per_stn +lbs;
mode = m;
}
Stonewt::Stonewt() // default constructor, wt = 0
{
stone = pounds = pds_left = 0;
mode = stnFormat;
}
Stonewt::~Stonewt() // destructor
{
}
Stonewt Stonewt::operator+(const Stonewt & s) const
{
double pds = pounds + s.pounds;
Stonewt temp(pds);
return temp;
}
Stonewt Stonewt::operator-(const Stonewt & s) const
{
double pds = pounds - s.pounds;
Stonewt temp(pds);
return temp;
}
Stonewt Stonewt::operator*(double n) const
{
double pds = pounds * n;
Stonewt temp(pds);
return temp;
}
bool Stonewt::operator>(const Stonewt & s) const
{
return (pounds > s.pounds);
}
bool Stonewt::operator<(const Stonewt & s) const
{
return (pounds < s.pounds);
}
bool Stonewt::operator==(const Stonewt & s) const
{
return (pounds == s.pounds);
}
std::ostream & operator<<(std::ostream & os, const Stonewt & s)
{
if(s.mode==Stonewt::stnFormat)
os << s.stone << " stone, " << s.pds_left << " pounds";
else
os << s.pounds << " pounds";
return os;
}
usestonewt2.h
//usestonewt2.cpp -- using the fourth draft of the Time class
//compile usetime3.cpp and mytime3.cpp together
#include <iostream>
#include "stonewt2.h"
int main()
{
using std::cout;
using std::endl;
using std::cin;
Stonewt stn[6]= {13.9,11,23};
int i;
double pds;
for(i=3; i<6; i++)
{
cout<<"Please enter the number of Stn "<<i<<" :";
cin>>pds;
stn[i]=pds;
}
//show the array of Stonewt
for(i=0; i<6; i++)
{
cout<<"stn "<<i<<" weighs "<<stn[i];
stn[i].set_pdsFormat();
cout<<",\nwhich is "<<stn[i]<<" in whole pounds.\n\n";
}
Stonewt max=stn[0];
Stonewt min=stn[0];
Stonewt tag(11,0);
int count=0;
for(i=0; i<6; i++)
{
if(stn[i]>max)
max=stn[i];
if(stn[i]<min)
min=stn[i];
if(stn[i]>tag||stn[i]==tag)
count++;
}
cout<<"The max of array is stn "<<i<<" ,which weighs "<<max<<endl;
cout<<"The min of array is stn "<<i<<" ,which weighs "<<min<<endl;
cout<<"There are "<<count<<" Stonewt which weighs greater or equal to 11 stone.";
cin.get();
return 0;
}