C++ Primer Plus第六版编程练习11.5解答

stonewt.h

// stonewt.h -- definition for the Stonewt class
#ifndef STONEWT_H_
#define STONEWT_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+(Stonewt s) const;
    Stonewt operator-(Stonewt s) const;
    Stonewt operator*(double n) const;
    friend std::ostream & operator<<(std::ostream & os, const Stonewt & s);
};
#endif

stonewt.cpp

// stonewt.cpp -- Stonewt methods
#include <iostream>
using std::cout;
#include "stonewt.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+(Stonewt s) const
{
    double pds = pounds + s.pounds;
    Stonewt temp(pds);
    return temp;
}

Stonewt Stonewt::operator-(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;
}

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;
}

usestonewt.cpp

//usetime3.cpp -- using the fourth draft of the Time class
//compile usetime3.cpp and mytime3.cpp together
#include <iostream>
#include "stonewt.h"

int main()
{
    using std::cout;
    using std::endl;
    Stonewt s1;
    cout<<"s1 weighs "<<s1;
    s1.set_pdsFormat();
    cout<<",\nwhich is "<<s1<<" in whole pounds.\n";
    Stonewt s2(275);
    cout<<"s2 weighs "<<s2;
    s2.set_pdsFormat();
    cout<<",\nwhich is "<<s2<<" in whole pounds.\n";
    Stonewt s3(285.7);
    cout<<"s3 weighs "<<s3;
    s3.set_pdsFormat();
    cout<<",\nwhich is "<<s3<<" in whole pounds.\n";
    Stonewt s4(21, 8);
    cout<<"s4 weighs "<<s4;
    s4.set_pdsFormat();
    cout<<",\nwhich is "<<s4<<" in whole pounds.\n";

    cout<<"\ncacluator test\n";
    Stonewt temp;
    temp = s2 * 4;
    cout<<"s2 * 4 = "<<temp;
    temp.set_pdsFormat();
    cout<<",\nwhich is "<<temp<<" in whole pounds.\n";
    temp = s3 + 526.34;
    temp.set_stnFormat();
    cout<<"s3 + 526.34 = "<<temp;
    temp.set_pdsFormat();
    cout<<",\nwhich is "<<temp<<" in whole pounds.\n";
    temp = s4 - 287;
    temp.set_stnFormat();
    cout<<"s4 - 287 = "<<temp;
    temp.set_pdsFormat();
    cout<<",\nwhich is "<<temp<<" in whole pounds.\n";

    std::cin.get();
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值