神秘博客

C++ Primer Plus [第六版]第11章编程练习题答案

1. 修改程序清单11.5,使之将一系列连续的随机漫步者位置写入到文件中。对于每个位置,用步号进行标志。另外,让该程序将初始条件(目标距离和步长)以及结果小结写入到该文件中。该文件的内容与下面类似:
Target Distance: 100, Step Size: 20
0: (x,y) = (0, 0)
1: (x,y) = (-11.4715, 16.383)
2: (x,y) = (-868807, -3.42232)
….
26: (x,y) = (42.2919, -78.2594)
27: (x,y) = (58.6749, -89.7309)
After 27 steps, the subject has the following location:
(x,y) = (58.6749, -89.7309)
or
(m,a) = (107.212, -56.8194)

Average outward distance per step = 3.97081

#ifndef VECT_H_
#define VECT_H_
#include <iostream>

namespace VECTOR
{
  class Vector
  {
  public:
    enum Mode { RECT, POL };
  private:
    double x;
    double y;
    double mag;
    double ang;
    Mode mode;
    void set_mag();
    void set_ang();
    void set_x();
    void set_y();
  public:
    Vector();
    Vector(double n1, double n2, Mode form = RECT);
    void reset(double n1, double n2, Mode form = RECT);
    ~Vector();
    double xval() const { return x; }
    double yval() const { return y; }
    double magval() const { return mag; }
    double angval() const { return ang; }
    void polar_mode();
    void rect_mode();
    Vector operator+(const Vector &b) const;
    Vector operator-(const Vector &b) const;
    Vector operator-() const;
    Vector operator*(double n) const;
    friend Vector operator*(double n, const Vector &a);
    friend std::ostream &operator<<(std::ostream &os, const Vector &v);
  };
}


#endif

#include <cmath>
#include "vect.h"

using std::sqrt;
using std::sin;
using std::cos;
using std::atan;
using std::atan2;
using std::cout;

namespace VECTOR
{
  const double Rad_to_deg = 45.0 / atan(1.0);

  void Vector::set_mag()
  {
    mag = sqrt(x*x + y * y);
  }
  void Vector::set_ang()
  {
    if (x == 0.0&&y == 0.0)
      ang = 0.0;
    else
      ang = atan2(y, x);
  }
  void Vector::set_x()
  {
    x = mag * cos(ang);
  }
  void Vector::set_y()
  {
    y = mag * sin(ang);
  }

  Vector::Vector()
  {
    x = y = mag = ang = 0.0;
    mode = RECT;
  }

  Vector::Vector(double n1, double n2, Mode form)
  {
    mode = form;
    if (form == RECT)
    {
      x = n1;
      y = n2;
      set_mag();
      set_ang();
    }
    else if (form == POL)
    {
      mag = n1;
      ang = n2 / Rad_to_deg;
      set_x();
      set_y();
    }
    else
    {
      cout << "Vector() 的第3个参数不正确 - Vector 设置为0\n";
      x = y = mag = ang = 0.0;
      mode = RECT;
    }
  }
  void Vector::reset(double n1, double n2, Mode form)
  {
    mode = form;
    if (form == RECT)
    {
      x = n1;
      y = n2;
      set_mag();
      set_ang();
    }
    else if (form == POL)
    {
      mag = n1;
      ang = n2 / Rad_to_deg;
      set_x();
      set_y();
    }
    else
    {
      cout << "Vector() 的第3个参数不正确 - Vector 设置为0\n";
      x = y = mag = ang = 0.0;
      mode = RECT;
    }
  }

  Vector::~Vector()    
  {
  }

  void Vector::polar_mode()    
  {
    mode = POL;
  }

  void Vector::rect_mode()     
  {
    mode = RECT;
  }

  
  Vector Vector::operator+(const Vector & b) const
  {
    return Vector(x + b.x, y + b.y);
  }


  Vector Vector::operator-(const Vector & b) const
  {
    return Vector(x - b.x, y - b.y);
  }


  Vector Vector::operator-() const
  {
    return Vector(-x, -y);
  }

  
  Vector Vector::operator*(double n) const
  {
    return Vector(n * x, n * y);
  }

  
  Vector operator*(double n, const Vector & a)
  {
    return a * n;
  }

  
  std::ostream & operator<<(std::ostream & os, const Vector & v)
  {
    if (v.mode == Vector::RECT)
      os << "(x,y) = (" << v.x << ", " << v.y << ")";
    else if (v.mode == Vector::POL)
    {
      os << "(m,a) = (" << v.mag << ", "
        << v.ang * Rad_to_deg << ")";
    }
    else
      os << "Vector 对象模式无效";
    return os;
  }
}
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include "vect.h"

int main()
{
  using namespace std;
  using namespace VECTOR;
  ofstream outFile;
  outFile.open("date.txt");
  if (!outFile.is_open())
  {
    cout << "创建数据文件失败...\n";
    exit(1);
  }
  srand(unsigned int(time(0)));
  double direction;
  Vector step;
  Vector result(0.0, 0.0);
  unsigned long steps = 0;
  double target;
  double dstep;
  cout << "输入目标距离[q退出]: ";
  while (cin >> target)
  {
    cout << "输入步长: ";
    if (!(cin >> dstep))
      break;
    //写入文件
    outFile << "目标距离: " << target << ", 步长: " << dstep << endl;
    outFile << steps << ": " << result << endl;
    while (result.magval() < target)
    {
      direction = rand() % 360;
      step.reset(dstep, direction, Vector::POL);
      result = result + step;
      steps++;
      outFile << steps << ": " << result << endl;
    }
    cout << "经过 " << steps << " 个步骤, 对象具有以下位置:\n";
    cout << result << endl;
    result.polar_mode();
    cout << " or\n" << result << endl;
    cout << "每步平均向外延伸距离 = "
      << result.magval() / steps << endl;
    //写入文件
    outFile << "经过 " << steps << " 个步骤, 对象具有以下位置:\n";
    outFile << result << endl;
    result.polar_mode();
    outFile << " or\n" << result << endl;
    outFile << "每步平均向外延伸距离 = "
      << result.magval() / steps << endl << endl;

    steps = 0;
    result.reset(0.0, 0.0);
    cout << "输入目标距离[q退出]: ";
  }
  outFile.close();
  ifstream inFile;
  inFile.open("date.txt");
  if (!inFile.is_open())
  {
    cout << "打开数据文件失败...\n";
    exit(1);
  }
  char ch;
  cout << "\n********************\n";
  cout << "读取文件内容: \n";
  while (inFile.get(ch))
  {
    cout << ch;
  }
  if (inFile.eof())
    cout << "正常读取完文件内容...\n";
  cout << "再见\n";

  inFile.close();
  system("pause");
  return 0;
}

2

#ifndef VECT_H_
#define VECT_H_
#include <iostream>

namespace VECTOR
{
  class Vector
  {
  public:
    enum Mode { RECT, POL };
  private:
    double x;
    double y;
    Mode mode;
    void set_x(double mag, double ang);
    void set_y(double mag, double ang);
  public:
    Vector();
    Vector(double n1, double n2, Mode form = RECT);
    void reset(double n1, double n2, Mode form = RECT);
    ~Vector();
    double xval() const { return x; }
    double yval() const { return y; }
    double magval() const;
    double angval() const;
    void polar_mode();
    void rect_mode();
    Vector operator+(const Vector &b) const;
    Vector operator-(const Vector &b) const;
    Vector operator-() const;
    Vector operator*(double n) const;
    friend Vector operator*(double n, const Vector &a);
    friend std::ostream &operator<<(std::ostream &os, const Vector &v);
  };
}


#endif
#include <cmath>
#include "vect.h"

using std::sqrt;
using std::sin;
using std::cos;
using std::atan;
using std::atan2;
using std::cout;

namespace VECTOR
{
  const double Rad_to_deg = 45.0 / atan(1.0);

  double Vector::magval() const
  {
    return sqrt(x*x + y * y);
  }
  double Vector::angval() const
  {
    if (x == 0.0&&y == 0.0)
      return 0.0;
    else
      return atan2(y, x);
  }
  void Vector::set_x(double mag, double ang)
  {
    x = mag * cos(ang);
  }
  void Vector::set_y(double mag, double ang)
  {
    y = mag * sin(ang);
  }

  Vector::Vector()
  {
    x = y = 0.0;
    mode = RECT;
  }

  Vector::Vector(double n1, double n2, Mode form)
  {
    mode = form;
    if (form == RECT)
    {
      x = n1;
      y = n2;
    }
    else if (form == POL)
    {
      set_x(n1, n2);
      set_y(n1, n2);
    }
    else
    {
      cout << "Vector() 的第3个参数不正确 - Vector 设置为0\n";
      x = y = 0.0;
      mode = RECT;
    }
  }
  void Vector::reset(double n1, double n2, Mode form)
  {
    mode = form;
    if (form == RECT)
    {
      x = n1;
      y = n2;
    }
    else if (form == POL)
    {
      set_x(n1, n2);
      set_y(n1, n2);
    }
    else
    {
      cout << "Vector() 的第3个参数不正确 - Vector 设置为0\n";
      x = y = 0.0;
      mode = RECT;
    }
  }

  Vector::~Vector()    
  {
  }

  void Vector::polar_mode()    
  {
    mode = POL;
  }

  void Vector::rect_mode()     
  {
    mode = RECT;
  }

  
  Vector Vector::operator+(const Vector & b) const
  {
    return Vector(x + b.x, y + b.y);
  }


  Vector Vector::operator-(const Vector & b) const
  {
    return Vector(x - b.x, y - b.y);
  }


  Vector Vector::operator-() const
  {
    return Vector(-x, -y);
  }

  
  Vector Vector::operator*(double n) const
  {
    return Vector(n * x, n * y);
  }

  
  Vector operator*(double n, const Vector & a)
  {
    return a * n;
  }

  
  std::ostream & operator<<(std::ostream & os, const Vector & v)
  {
    if (v.mode == Vector::RECT)
      os << "(x,y) = (" << v.x << ", " << v.y << ")";
    else if (v.mode == Vector::POL)
    {
      os << "(m,a) = (" << v.magval() << ", "
        << v.angval() * Rad_to_deg << ")";
    }
    else
      os << "Vector 对象模式无效";
    return os;
  }
}
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "vect.h"

int main()
{
  using namespace std;
  using namespace VECTOR;
  srand(unsigned int(time(0)));
  double direction;
  Vector step;
  Vector result(0.0, 0.0);
  unsigned long steps = 0;
  double target;
  double dstep;
  cout << "输入目标距离[q退出]: ";
  while (cin >> target)
  {
    cout << "输入步长: ";
    if (!(cin >> dstep))
      break;

    while (result.magval() < target)
    {
      direction = rand() % 360;
      step.reset(dstep, direction, Vector::POL);
      result = result + step;
      steps++;
    }
    cout << "经过 " << steps << " 个步骤, 对象具有以下位置:\n";
    cout << result << endl;
    result.polar_mode();
    cout << " or\n" << result << endl;
    cout << "每步平均向外延伸距离 = "
      << result.magval() / steps << endl;

    steps = 0;
    result.reset(0.0, 0.0);
    cout << "输入目标距离[q退出]: ";
  }



  system("pause");
  return 0;
}

3

#ifndef VECT_H_
#define VECT_H_
#include <iostream>

namespace VECTOR
{
  class Vector
  {
  public:
    enum Mode { RECT, POL };
  private:
    double x;
    double y;
    double mag;
    double ang;
    Mode mode;
    void set_mag();
    void set_ang();
    void set_x();
    void set_y();
  public:
    Vector();
    Vector(double n1, double n2, Mode form = RECT);
    void reset(double n1, double n2, Mode form = RECT);
    ~Vector();
    double xval() const { return x; }
    double yval() const { return y; }
    double magval() const { return mag; }
    double angval() const { return ang; }
    void polar_mode();
    void rect_mode();
    Vector operator+(const Vector &b) const;
    Vector operator-(const Vector &b) const;
    Vector operator-() const;
    Vector operator*(double n) const;
    friend Vector operator*(double n, const Vector &a);
    friend std::ostream &operator<<(std::ostream &os, const Vector &v);
  };
}


#endif
#include <cmath>
#include "vect.h"

using std::sqrt;
using std::sin;
using std::cos;
using std::atan;
using std::atan2;
using std::cout;

namespace VECTOR
{
  const double Rad_to_deg = 45.0 / atan(1.0);

  void Vector::set_mag()
  {
    mag = sqrt(x*x + y * y);
  }
  void Vector::set_ang()
  {
    if (x == 0.0&&y == 0.0)
      ang = 0.0;
    else
      ang = atan2(y, x);
  }
  void Vector::set_x()
  {
    x = mag * cos(ang);
  }
  void Vector::set_y()
  {
    y = mag * sin(ang);
  }

  Vector::Vector()
  {
    x = y = mag = ang = 0.0;
    mode = RECT;
  }

  Vector::Vector(double n1, double n2, Mode form)
  {
    mode = form;
    if (form == RECT)
    {
      x = n1;
      y = n2;
      set_mag();
      set_ang();
    }
    else if (form == POL)
    {
      mag = n1;
      ang = n2 / Rad_to_deg;
      set_x();
      set_y();
    }
    else
    {
      cout << "Vector() 的第3个参数不正确 - Vector 设置为0\n";
      x = y = mag = ang = 0.0;
      mode = RECT;
    }
  }
  void Vector::reset(double n1, double n2, Mode form)
  {
    mode = form;
    if (form == RECT)
    {
      x = n1;
      y = n2;
      set_mag();
      set_ang();
    }
    else if (form == POL)
    {
      mag = n1;
      ang = n2 / Rad_to_deg;
      set_x();
      set_y();
    }
    else
    {
      cout << "Vector() 的第3个参数不正确 - Vector 设置为0\n";
      x = y = mag = ang = 0.0;
      mode = RECT;
    }
  }

  Vector::~Vector()    
  {
  }

  void Vector::polar_mode()    
  {
    mode = POL;
  }

  void Vector::rect_mode()     
  {
    mode = RECT;
  }

  
  Vector Vector::operator+(const Vector & b) const
  {
    return Vector(x + b.x, y + b.y);
  }


  Vector Vector::operator-(const Vector & b) const
  {
    return Vector(x - b.x, y - b.y);
  }


  Vector Vector::operator-() const
  {
    return Vector(-x, -y);
  }

  
  Vector Vector::operator*(double n) const
  {
    return Vector(n * x, n * y);
  }
  
  Vector operator*(double n, const Vector & a)
  {
    return a * n;
  }

  
  std::ostream & operator<<(std::ostream & os, const Vector & v)
  {
    if (v.mode == Vector::RECT)
      os << "(x,y) = (" << v.x << ", " << v.y << ")";
    else if (v.mode == Vector::POL)
    {
      os << "(m,a) = (" << v.mag << ", "
        << v.ang * Rad_to_deg << ")";
    }
    else
      os << "Vector 对象模式无效";
    return os;
  }
}
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "vect.h"

int main()
{
  using namespace std;
  using namespace VECTOR;
  srand(unsigned int(time(0)));
  double direction;
  Vector step;
  Vector result(0.0, 0.0);
  double target;
  double dstep;
  int count;

  
  cout << "输入目标距离[q退出]: ";
  cin >> target;
  cout << "输入步长: ";
  cin >> dstep;
  cout << "请输入测试次数: ";
  while (!(cin >> count) || count < 2)
  {
    cin.clear();
    while (cin.get() != '\n')
      continue;
    cout << "请输入大于2以上的整数: ";
  }
  Vector *temp = new Vector[count];
  unsigned long *steps = new unsigned long[count];
  for (int i = 0; i < count; i++)
  {
    steps[i] = 0;
    while (result.magval() < target)
    {
      direction = rand() % 360;
      step.reset(dstep, direction, Vector::POL);
      result = result + step;
      steps[i]++;
    }
    temp[i] = result;

    result.reset(0.0, 0.0);
  }

  unsigned long max = 0;
  unsigned long min = steps[0];
  int Max, Min;
  unsigned long total = 0;
  for (int i = 0; i < count; i++)
  {
    if (max < steps[i])
    {
      max = steps[i];
      Max = i;
    }
    if (min > steps[i])
    {
      min = steps[i];
      Min = i;
    }
    total += steps[i];
  }

  cout << "最高步数: \n";
  cout << "经过 " << steps[Max] << " 个步骤, 对象具有以下位置:\n";
  cout << temp[Max] << endl;
  temp[Max].polar_mode();
  cout << " or\n" << temp[Max] << endl;
  cout << "每步平均向外延伸距离 = "
    << temp[Max].magval() / steps[Max] << endl;

  cout << "最低步数: \n";
  cout << "经过 " << steps[Min] << " 个步骤, 对象具有以下位置:\n";
  cout << temp[Min] << endl;
  temp[Min].polar_mode();
  cout << " or\n" << temp[Min] << endl;
  cout << "每步平均向外延伸距离 = "
    << temp[Min].magval() / steps[Min] << endl;

  cout << "平均步数: " << total / count << endl;

  delete[]temp;
  system("pause");
  return 0;
}

4

#ifndef MYTIME_H_
#define MYTIME_H_

class Time
{
private:
  int hours;
  int minutes;
public:
  Time();
  Time(int h, int m = 0);
  void AddMin(int m);
  void AddHr(int h);
  void Reset(int h = 0, int m = 0);
  friend Time operator+(const Time & t, const Time & s);
  friend Time operator-(const Time & t, const Time & s);
  friend Time operator*(const Time & t, double n);
  friend Time operator*(double m, const Time & t)
  {
    return t * m;
  }
  friend std::ostream & operator<<(std::ostream & os, const Time & t);

};
#endif

#include <iostream>
#include "mytime.h"

Time::Time()
{
  hours = minutes = 0;
}

Time::Time(int h, int m)
{
  hours = h;
  minutes = m;
}

void Time::AddMin(int m)
{
  minutes += m;
  hours += minutes / 60;
  minutes %= 60;
}

void Time::AddHr(int h)
{
  hours += h;
}

void Time::Reset(int h, int m)
{
  hours = h;
  minutes = m;
}

Time operator+(const Time & t, const Time & s)
{
  Time sum;
  sum.minutes = t.minutes + s.minutes;
  sum.hours = t.hours + s.hours + sum.minutes / 60;
  sum.minutes %= 60;
  return sum;
}

Time operator-(const Time & t, const Time & s)
{
  Time diff;
  int tot1, tot2;
  tot1 = t.minutes + 60 * t.hours;
  tot2 = s.minutes + 60 * s.hours;
  diff.minutes = (tot2 - tot1) % 60;
  diff.hours = (tot2 - tot1) / 60;
  return diff;
}

Time operator*(const Time & t, double n)
{
  Time result;
  long totalminutes = t.hours * n * 60 + t.minutes * n;
  result.hours = totalminutes / 60;
  result.minutes = totalminutes % 60;
  return result;
}

std::ostream & operator<<(std::ostream & os, const Time & t)
{
  os << t.hours << " 小时, " << t.minutes << " 分钟";
  return os;
}
#include <iostream>
#include "mytime.h"

int main()
{
  using std::cout;
  using std::endl;
  Time aida(3, 35);
  Time tosca(2, 48);
  Time temp;

  cout << "Aida and Tosca:\n";
  cout << aida << "; " << tosca << endl;
  temp = aida + tosca;
  cout << "Aida + Tosca: " << temp << endl;
  temp = aida * 1.17;  
  cout << "Aida * 1.17: " << temp << endl;
  cout << "10.0 * Tosca: " << 10.0 * tosca << endl;
  
  system("pause");
  return 0;
}

5

#ifndef STONEWT_H_
#define STONEWT_H_
#include <iostream>

class Stonewt
{
public:
  enum Mode { STONE, int_pounds, d_pounds };
private:
  enum { Lbs_per_stn = 14 };     
  int stone;                   
  double pds_left;              
  double pounds;    
  Mode mode;
public:
  Stonewt(double lbs, Mode m = STONE);
  Stonewt(int stn, double lbs, Mode m = STONE);
  Stonewt();                    
  ~Stonewt();
  Stonewt operator+(const Stonewt &n) const;
  Stonewt operator-(const Stonewt &n) const;
  Stonewt operator*(const Stonewt &n) const;
  friend std::ostream &operator<<(std::ostream &os, const Stonewt &n);
};
#endif
#include <iostream>
using std::cout;
#include "stonewt.h"

Stonewt::Stonewt(double lbs, Mode m)
{
  stone = int(lbs) / Lbs_per_stn;    
  pds_left = int(lbs) % Lbs_per_stn + lbs - int(lbs);
  pounds = lbs;
  mode = m;
}


Stonewt::Stonewt(int stn, double lbs, Mode m)
{
  stone = stn;
  pds_left = lbs;
  pounds = stn * Lbs_per_stn + lbs;
  mode = m;
}

Stonewt::Stonewt()          
{
  stone = pounds = pds_left = 0;
  mode = STONE;
}

Stonewt::~Stonewt()         
{
}

std::ostream &operator<<(std::ostream &os, const Stonewt &n)
{
  switch (n.mode)
  {
  case Stonewt::STONE:
    os << n.stone << " 英石, " << n.pds_left << " 镑\n";
    break;
  case Stonewt::int_pounds:
    os << int(n.pounds + 0.5) << " 磅\n";
    break;
  case Stonewt::d_pounds:
    os << n.pounds << " 磅\n";
    break;
  default:
    break;
  }
  return os;
}

Stonewt Stonewt::operator+(const Stonewt &n) const
{
  Stonewt sum;
  sum.pounds = pounds + n.pounds;
  sum.stone = sum.pounds / Lbs_per_stn;
  sum.pds_left = int(sum.pounds) % Lbs_per_stn;
  return sum;
}

Stonewt Stonewt::operator-(const Stonewt &n) const
{
  Stonewt sum;
  sum.pounds = pounds - n.pounds;
  sum.stone = sum.pounds / Lbs_per_stn;
  sum.pds_left = int(sum.pounds) % Lbs_per_stn;
  return sum;
}

Stonewt Stonewt::operator*(const Stonewt &n) const
{
  Stonewt sum;
  sum.pounds = pounds * n.pounds;
  sum.stone = sum.pounds / Lbs_per_stn;
  sum.pds_left = int(sum.pounds) % Lbs_per_stn;
  return sum;
}
#include <iostream>
using std::cout;
#include "stonewt.h"

int main()
{
  Stonewt p1 = 275;
  Stonewt p2(29.84,Stonewt::int_pounds);    
  Stonewt p3(21, 8, Stonewt::d_pounds);

  cout << "p1: \n" << p1;
  cout << "p2: \n" << p2;
  cout << "p3: \n" << p3;

  Stonewt p4 = p1 + p2;
  cout << "p1 + p2: \n" << p4;

  Stonewt p5 = p1 - p2;
  cout << "p1 - p2: \n" << p5;

  Stonewt p6 = p1 * p2;
  cout << "p1 * p2: \n" << p6;
  
  system("pause");
  return 0;
}

6

#ifndef STONEWT_H_
#define STONEWT_H_
#include <iostream>

class Stonewt
{
public:
  enum Mode { STONE, int_pounds, d_pounds };
private:
  enum { Lbs_per_stn = 14 };     
  int stone;                   
  double pds_left;              
  double pounds;    
  Mode mode;
public:
  Stonewt(double lbs, Mode m = STONE);
  Stonewt(int stn, double lbs, Mode m = STONE);
  Stonewt();                    
  ~Stonewt();
  bool operator==(const Stonewt &n) const;
  bool operator!=(const Stonewt &n) const;
  bool operator<(const Stonewt &n) const;
  bool operator>(const Stonewt &n) const;
  bool operator>=(const Stonewt &n) const;
  bool operator<=(const Stonewt &n) const;
  friend std::ostream &operator<<(std::ostream &os, const Stonewt &n);
};
#endif
#include <iostream>
using std::cout;
#include "stonewt.h"

Stonewt::Stonewt(double lbs, Mode m)
{
  stone = int(lbs) / Lbs_per_stn;    
  pds_left = int(lbs) % Lbs_per_stn + lbs - int(lbs);
  pounds = lbs;
  mode = m;
}


Stonewt::Stonewt(int stn, double lbs, Mode m)
{
  stone = stn;
  pds_left = lbs;
  pounds = stn * Lbs_per_stn + lbs;
  mode = m;
}

Stonewt::Stonewt()          
{
  stone = pounds = pds_left = 0;
  mode = STONE;
}

Stonewt::~Stonewt()         
{
}

std::ostream &operator<<(std::ostream &os, const Stonewt &n)
{
  switch (n.mode)
  {
  case Stonewt::STONE:
    os << n.stone << " 英石, " << n.pds_left << " 镑\n";
    break;
  case Stonewt::int_pounds:
    os << int(n.pounds + 0.5) << " 磅\n";
    break;
  case Stonewt::d_pounds:
    os << n.pounds << " 磅\n";
    break;
  default:
    break;
  }
  return os;
}

bool Stonewt::operator==(const Stonewt &n) const
{
  return pounds == n.pounds;
}
bool Stonewt::operator!=(const Stonewt &n) const
{
  return pounds != n.pounds;
}
bool Stonewt::operator<(const Stonewt &n) const
{
  return pounds < n.pounds;
}
bool Stonewt::operator>(const Stonewt &n) const
{
  return pounds > n.pounds;
}
bool Stonewt::operator>=(const Stonewt &n) const
{
  return pounds >= n.pounds;
}
bool Stonewt::operator<=(const Stonewt &n) const
{
  return pounds <= n.pounds;
}
#include <iostream>
using std::cout;
using std::endl;
#include "stonewt.h"

int main()
{
  Stonewt p[6] { {168.8},{298.4},{158.7} };
  Stonewt t(154);

  for (int i = 3; i < 6; i++)
    p[i] = Stonewt(98.4 + i);

  Stonewt temp;
  for (int i = 0; i < 6; i++)
  {
    for (int j = i; j < 6; j++)
    {
      if (p[i] > p[j])
      {
        temp = p[i];
        p[i] = p[j];
        p[j] = temp;
      }
    }
  }
  cout << "最小元素: \n";
  cout << p[0] << endl;
  cout << "最大元素: \n";
  cout << p[5] << endl;
  cout << "大于等于 11 英石的元素: \n";
  for (int i = 0; i < 6; i++)
  {
    if (p[i] >= t)
      cout << p[i];
  }


  system("pause");
  return 0;
}

7

#ifndef COMPLEX_H_
#define COMPLEX_H_
#include <iostream>

class Complex
{
private:
  double a;
  double b;
public:
  Complex();
  Complex(const double &x, const double &y);
  Complex operator+(const Complex &n) const;
  Complex operator-(const Complex &n) const;
  Complex operator*(const Complex &n) const;
  friend Complex operator~(Complex &n);
  friend Complex operator*(const double &x, const Complex &n);
  friend std::ostream &operator<<(std::ostream &os, const Complex &n);
  friend std::istream &operator>>(std::istream &os, Complex &n);
};


#endif
#include <iostream>
#include "complex.h"

Complex::Complex()
{
  a = b = 0;
}

Complex::Complex(const double &x, const double &y)
{
  a = x;
  b = y;
}

Complex Complex::operator+(const Complex &n) const
{
  Complex sum;
  sum.a = a + n.a;
  sum.b = b + n.b;
  return sum;
}

Complex Complex::operator-(const Complex &n) const
{
  Complex sum;
  sum.a = a - n.a;
  sum.b = b - n.b;
  return sum;
}

Complex Complex::operator*(const Complex &n) const
{
  Complex sum;
  sum.a = a * n.a - b * n.b;
  sum.b = a * n.b + b * n.a;
  return sum;
}

Complex operator~(Complex &n)
{
  Complex sum;
  sum.a = n.a;
  sum.b = -n.b;
  return sum;
}

Complex operator*(const double &x, const Complex &n)
{
  Complex sum;
  sum.a = x * n.a;
  sum.b = x * n.b;
  return sum;
}

std::ostream &operator<<(std::ostream &os, const Complex &n)
{
  os << "(" << n.a << "," << n.b << "i)";
  return os;
}

std::istream &operator>>(std::istream &os, Complex &n)
{
  std::cout << "实数: ";
  if (!(os >> n.a))
    return os;
  std::cout << "虚数: ";
  os >> n.b;
  return os;
}
#include <iostream>
using namespace std;
#include "complex.h"

int main()
{
  Complex a(3.0, 4.0);
  Complex c;

  cout << "输入复数[q退出]:\n";
  while (cin >> c)
  {
    cout << "c is " << c << '\n';
    cout << "complex conjugate is " << ~c << '\n';
    cout << "a + c is " << a + c << '\n';
    cout << "a - c is " << a - c << '\n';
    cout << "a * c is " << a * c << '\n';
    cout << "2 * c is " << 2 * c << '\n';
    cout << "输入复数[q退出]:\n";
  }
  cout << "完成\n";
  
  system("pause");
  return 0;
}

 

版权说明:
点赞

发表评论

电子邮件地址不会被公开。 必填项已用*标注

觉得文章有用就请我吃包辣条吧

微信扫一扫打赏