#C++PrimerPlus# Chapter11_Exersice6_stonewtV3

续上题继续改进stonewt类,重载全部6个关系运算符,并检验新的类方法。

改动的内容有:增加6个成员函数重载所有6个关系运算符。

程序清单如下:


// stonewt3.h
#ifndef STONEWT3_H_
#define STONEWT3_H_

#include <iostream>

class Stonewt
{
public:
  // 状态成员
  enum Mode {STN, LBS};
private:
  enum {LbsPerStn = 14};
  Mode mode;
  int stone;
  double pdsLeft;
  double pounds;
public:
  // 构造函数和析构函数
  Stonewt();
  Stonewt(int m_stone, double m_pdsLeft);
  Stonewt(double m_pounds);
  ~Stonewt();
   // 设置数据成员值得函数
  void reset(int m_stone, double m_pdsLeft);
  void reset(double m_pounds);
   // 返回数据成员的函数
  int stoneVal() const;
  double pdsLeftVal() const;
  double poundsVal() const;
  // 设置单位模式的函数
  void setStn();
  void setLbs();
  // 重载运算符的函数
  Stonewt operator+(const Stonewt& b) const;
  Stonewt operator-(const Stonewt& b) const;
  Stonewt operator*(double n) const;
  bool operator<(const Stonewt& b) const;
  bool operator>(const Stonewt& b) const;
  bool operator==(const Stonewt& b) const;
  bool operator<=(const Stonewt& b) const;
  bool operator>=(const Stonewt& b) const;
  bool operator!=(const Stonewt& b) const;
  // 友元函数
  friend Stonewt operator*(double n, const Stonewt& b);
  friend std::ostream& operator<<(std::ostream& os, const Stonewt& b);
  friend bool operator>>(std::istream& in, Stonewt& b);
};

#endif


// stonewt3.cpp
#include "stonewt3.h"

// 定义类方法

// 定义构造函数和析构函数
Stonewt::Stonewt()
{
  mode = STN;
  stone = pdsLeft = pounds = 0.0;
}
Stonewt::Stonewt(int m_stone, double m_pdsLeft)
{
  mode = STN;
  stone = m_stone;
  pdsLeft = m_pdsLeft;
  pounds = m_stone * LbsPerStn + pdsLeft;
}
Stonewt::Stonewt(double m_pounds)
{
  mode = LBS;
  pounds = m_pounds;
  stone = int (m_pounds) / LbsPerStn;
  pdsLeft = int (m_pounds) % LbsPerStn + m_pounds - int (m_pounds);
}
Stonewt::~Stonewt()
{
}

// 定义设置数据成员值得函数
void Stonewt::reset(int m_stone, double m_pdsLeft)
{
  mode = STN;
  stone = m_stone;
  pdsLeft = m_pdsLeft;
  pounds = m_stone * LbsPerStn + pdsLeft;
}
void Stonewt::reset(double m_pounds)
{
  mode = LBS;
  pounds = m_pounds;
  stone = int (m_pounds) / LbsPerStn;
  pdsLeft = int (m_pounds) % LbsPerStn + m_pounds - int (m_pounds);
}

// 返回数据成员的函数
int Stonewt::stoneVal() const
{
  return stone;
}
double Stonewt::pdsLeftVal() const
{
  return pdsLeft;
}
double Stonewt::poundsVal() const
{
  return pounds;
}

// 设置单位模式的函数
void Stonewt::setStn()
{
  mode = STN;
}
void Stonewt::setLbs()
{
  mode = LBS;
}

// 重载运算符的函数
Stonewt Stonewt::operator+(const Stonewt& b) const
{
  return Stonewt(pounds + b.pounds);
}
Stonewt Stonewt::operator-(const Stonewt& b) const
{
  return Stonewt(pounds - b.pounds);
}
Stonewt Stonewt::operator*(double n) const
{
  return Stonewt(pounds * n);
}
bool Stonewt::operator<(const Stonewt& b) const
{
  return pounds < b.pounds ? true : false;
}
bool Stonewt::operator>(const Stonewt& b) const
{
  return pounds > b.pounds ? true : false;
}
bool Stonewt::operator==(const Stonewt& b) const
{
  return pounds == b.pounds ? true : false;
}
bool Stonewt::operator<=(const Stonewt& b) const
{
  return pounds <= b.pounds ? true : false;
}
bool Stonewt::operator>=(const Stonewt& b) const
{
  return pounds >= b.pounds ? true : false;
}
bool Stonewt::operator!=(const Stonewt& b) const
{
  return pounds != b.pounds ? true : false;
}


// 友元函数
Stonewt operator*(double n, const Stonewt& b)
{
  return b * n;
}
std::ostream& operator<<(std::ostream& os, const Stonewt& b)
{
  if (b.mode == Stonewt::STN)
    os << "重量为:" << b.stone << "石," << b.pdsLeft << "磅。\t(英石制)\n";
  else if (b.mode == Stonewt::LBS)
    os << "重量为:" << b.pounds << "磅。\t(英镑制)\n";
  return os;
}
bool operator>>(std::istream& in, Stonewt& b)
{
  using std::cout;
  using std::cin;

  char ch;
  int num1;
  double num2;
  bool result;

  cout << "使用什么单位?(S:英石制;L:英镑制):";
  if (cin >> ch)
  {
    if (toupper(ch) == 'S')
    {
      cout << "使用英石制。\n";
      cout << "英石:";
      while (!(cin >> num1))
      {
        cin.clear();
        while (cin.get() != '\n')
          continue;
        cout << "输入错误,重新输入:\n";
        cout << "英石:";
      }
      cout << "英镑:";
      while (!(cin >> num2))
      {
        cin.clear();
        while (cin.get() != '\n')
          continue;
        cout << "输入错误,重新输入:\n";
        cout << "英镑:";
      }
      b.reset(num1, num2);
      result = true;
    }
    else if (toupper(ch) == 'L')
    {
      cout << "使用英镑制。\n";
      cout << "英镑:";
      while (!(cin >> num2))
      {
        cin.clear();
        while (cin.get() != '\n')
          continue;
        cout << "输入错误,重新输入:\n";
        cout << "英镑:";
      }
      b.reset(num2);
      result = true;
    }
  }
  else if (!(cin >> ch))
  {
    cin.clear();
    while (cin.get() != '\n')
      continue;
    cout << "输入错误。\n";
    result = false;
  }
  return result;
}


// usestonewt3.cpp
#include "stonewt3.h"

int main()
{
  using std::cout;
  using std::cin;
  using std::endl;

  Stonewt arrWt[6] = {Stonewt(15,5.3),
  Stonewt(9,3.4),
  Stonewt(175.7)};

  for (int i = 3; i < 6; i++)
  {
    cout << "请输入" << i+1 << "#的信息:\n";
    while (!(cin >> arrWt[i]))
      continue;
  }

  cout << "重量在11英石以上的有:\n";
  for (int i = 0; i < 6; i++)
  {
    if ( arrWt[i].stoneVal() >= 11)
      cout << i+1 << "#" << arrWt[i];
  }

  system("pause>nul");
  return 0;
}


结束。

posted @ 2013-05-12 21:59  庄懂  阅读(192)  评论(0编辑  收藏  举报