C++ Primer Plus第五版 第11章 编程练习答案

/*******************************************************************************************************************   
Author : Cui mingyang  
Blog : cx_12586  
Time : 2017/11/20 
From : C++ Primer Plus第五版第11章编程练习 第1题    
*******************************************************************************************************************/  
//头文件
#ifndef MODVECTOR_H_
#define MODVECTOR_H_
#include <iostream>
namespace VECTOR
{
	using std::ostream;
	class Vector
	{
	private:
		double x;          // horizontal value
		double y;          // vertical value
		char mode;         // 'r' = rectangular, 'p' = polar
		// private methods for setting values
		void set_mag();
		void set_ang();
		void set_x(double, double);
		void set_y(double, double);
	public:
		Vector();
		Vector(double n1, double n2, char form = 'r');
		void set(double n1, double n2, char form = 'r');
		~Vector();
		double xval() const {return x;}       // report x value
		double yval() const {return y;}       // report y value
		double magval() const;              // report magnitude
		double angval() const;              // report angle
		void polar_mode();                    // set mode to 'p'
		void rect_mode();                     // set mode to 'r'
		// operator overloading
		Vector operator+(const Vector & b) const;
		Vector operator-(const Vector & b) const;
		Vector operator-() const;
		Vector operator*(double n) const;
		// friends
		friend Vector operator*(double n, const Vector & a);
		friend ostream & operator<<(ostream & os, const Vector & v);
	};

}   // end namespace VECTOR
#endif

//头文件对应的cpp文件
#include <cmath>
#include "vect.h"   // includes <iostream>
using std::sqrt;
using std::sin;
using std::cos;
using std::atan2;
using std::cout;

namespace VECTOR
{

	const double Rad_to_deg = 57.2957795130823;

	// private methods
	// calculates magnitude from x and y


	// set x from polar coordinate
	void Vector::set_x(double mag, double ang)
	{
		x = mag * cos(ang);
	}

	// set y from polar coordinate
	void Vector::set_y(double mag, double ang)
	{
		y = mag * sin(ang);
	}

	// public methods
	Vector::Vector()             // default constructor
	{
		x = y  = 0.0;
		mode = 'r';
	}

	// construct vector from rectangular coordinates if form is r
	// (the default) or else from polar coordinates if form is p
	Vector::Vector(double n1, double n2, char form)
	{
		mode = form;
		if (form == 'r')
		{
			x = n1;
			y = n2;
		}
		else if (form == 'p')
		{
			set_x(n1, n2 / Rad_to_deg);
			set_y(n1, n2 / Rad_to_deg);
		}
		else
		{
			cout << "Incorrect 3rd argument to Vector() -- ";
			cout << "vector set to 0\n";
			x = y = 0.0;
			mode = 'r';
		}
	}

	// set vector from rectangular coordinates if form is r (the
	// default) or else from polar coordinates if form is p
	void Vector:: set(double n1, double n2, char form)
	{
		mode = form;
		if (form == 'r')
		{
			x = n1;
			y = n2;
		}
		else if (form == 'p')
		{
			set_x(n1, n2 / Rad_to_deg);
			set_y(n1, n2 / Rad_to_deg);
		}
		else
		{
			cout << "Incorrect 3rd argument to Vector() -- ";
			cout << "vector set to 0\n";
			x = y = 0.0;
			mode = 'r';
		}
	}

	Vector::~Vector()    // destructor
	{
	}

	double Vector::magval() const            // report magnitude
	{
		return sqrt(x*x +y*y);
	}

	double Vector::angval() const              // report angle
	{
		if (x == 0.0 && y == 0.0)
			return 0;
		else
			return atan2(y, x);
	}

	void Vector::polar_mode()    // set to polar mode
	{
		mode = 'p';
	}

	void Vector::rect_mode()     // set to rectangular mode
	{
		mode = 'r';
	}

	// operator overloading
	// add two Vectors
	Vector Vector::operator+(const Vector & b) const
	{
		return Vector(x + b.x, y + b.y);
	}

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

	// reverse sign of Vector
	Vector Vector::operator-() const
	{
		return Vector(-x, -y);
	}

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

	// friend methods
	// multiply n by Vector a
	Vector operator*(double n, const Vector & a)
	{
		return a * n;
	}

	// display rectangular coordinates if mode is r,
	// else display polar coordinates if mode is p
	ostream & operator<<(ostream & os, const Vector & v)
	{
		if (v.mode == 'r')
			os << "(x,y) = (" << v.x << ", " << v.y << ")";
		else if (v.mode == 'p')
		{
			os << "(m,a) = (" << v.magval() << ", "
				<< v.angval() * Rad_to_deg << ")";
		}
		else
			os << "Vector object mode is invalid";
		return os;
	}

}  // end namespace VECTOR

//main函数
#include <iostream>
#include <cstdlib>      // rand(), srand() prototypes
#include <ctime>        // time() prototype
#include "vect.h" 

int main()
{
	using namespace std;
	using VECTOR::Vector;
	srand(time(0));     // seed random-number generator
	double direction;
	Vector step;
	Vector result(0.0, 0.0);
	unsigned long steps = 0;
	double target;
	double dstep;

	//记录实验的次数
	cout << "Please enter the number of test: ";
	int num;
	cin >> num;
	double sum,min_step,max_step,temp;
	sum=max_step=temp=0;
	min_step=100;
	int i=0;
	cout << "Enter target distance (q to quit): ";
	while (i< num && cin >> target )
	{
		cout << "Enter step length: ";
		if (!(cin >> dstep))
			break;

		while (result.magval() < target)
		{
			direction = rand() % 360;
			step.set(dstep, direction, 'p');
			result = result + step;
			steps++;
		}
		
		// 计算最大最小和平均步数
		temp = result.magval()/steps;
		sum += temp;
		if (temp < min_step)
			min_step = temp;
		else if (temp > max_step)
			max_step = temp;
		steps = 0;
		result.set(0.0, 0.0);
		cout << "Enter target distance (q to quit): ";
		//记录实验次数
		i++;
	}
	cout << "The max step is " << max_step << ", and the min step is " << min_step << ", the mean step is " << sum/num <<endl;
	cout << "Bye!\n";
	system("pause");
	return 0;
}

/*******************************************************************************************************************   
Author : Cui mingyang  
Blog : cx_12586  
Time : 2017/11/20 
From : C++ Primer Plus第五版第11章编程练习 第2题    
*******************************************************************************************************************/  
//头文件
#ifndef VECTOR_H_  
#define VECTOR_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 { return sqrt(x * x + y * y); }  
        double angval() const {  
            if (x == 0.0 && y == 0.0)  
                return 0.0;  
            else  
                return atan2(y, x);  
        }  
        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  
//头文件对应的cpp文件
#include <cmath>  
#include "vector.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_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 << "Incorrect 3rd argument to Vector() -- ";  
            cout << "vector set to 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 << "Incorrect 3rd argument to Vector() -- ";  
            cout << "vector set to 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 object mode is invalid";  
        return os;  
    }  
}  
//main函数
#include <iostream>  
#include <cstdlib>  
#include <ctime>  
#include "vector.h"  
  
int main()  
{  
    srand(time(0));  
    double direction;  
    VECTOR::Vector step;  
    VECTOR::Vector result(0.0, 0.0);  
    unsigned long steps = 0;  
    double target;  
    double dstep;  
    std::cout << "Enter target distance (q to quit): ";  
    while (std::cin >> target)  
    {  
        std::cout << "Enter step length: ";  
        if (!(std::cin >> dstep))  
            break;  
        while (result.magval() < target)  
        {  
            direction = rand() % 360;  
            step.reset(dstep, direction, VECTOR::Vector::POL);  
            result = result + step;  
            steps++;  
        }  
        std::cout << "After " << steps << " steps, the subject has the following location:\n";  
        std::cout << result << std::endl;  
        result.polar_mode();  
        std::cout << " or\n" << result << std::endl;  
        std::cout << "Average outward distance per step = " << result.magval() / steps << std::endl;  
        steps = 0;  
        result.reset(0.0, 0.0);  
        std::cout << "Enter target distance (q to quit): ";  
    }  
    std::cout << "Bye!\n";  
    std::cin.clear();  
    while(std::cin.get() != '\n')  
        continue;  
	system("pause");
    return 0;  
}  

/*******************************************************************************************************************   
Author : Cui mingyang  
Blog : cx_12586  
Time : 2017/11/20 
From : C++ Primer Plus第五版第11章编程练习 第3题    
*******************************************************************************************************************/  
//头文件
#ifndef VECTOR_H_  
#define VECTOR_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 { return sqrt(x * x + y * y); }  
        double angval() const {  
            if (x == 0.0 && y == 0.0)  
                return 0.0;  
            else  
                return atan2(y, x);  
        }  
        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  
//头文件对应的cpp文件
#include <cmath>  
#include "vector.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_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 << "Incorrect 3rd argument to Vector() -- ";  
            cout << "vector set to 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 << "Incorrect 3rd argument to Vector() -- ";  
            cout << "vector set to 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 object mode is invalid";  
        return os;  
    }  
}  
  
//main函数
#include <iostream>  
#include <cstdlib>  
#include <ctime>  
#include "vector.h"  
  
int main()  
{  
    srand(time(0));  
    double direction;  
    VECTOR::Vector step;  
    VECTOR::Vector result(0.0, 0.0);  
    unsigned long steps = 0;  
    double target;  
    double dstep;  
    int N;  
    unsigned long max = 0;  
    unsigned long min = 999;  
    double avg = 0;  
    std::cout << "Enter N times to test: ";  
    std::cin >> N;  
    for (int i = 0; i < N; ++i)  
    {  
        std::cout << "Enter target distance (q to quit): ";  
        std::cin >> target;  
        std::cout << "Enter step length: ";  
        if (!(std::cin >> dstep))  
            break;  
        while (result.magval() < target)  
        {  
            direction = rand() % 360;  
            step.reset(dstep, direction, VECTOR::Vector::POL);  
            result = result + step;  
            steps++;  
        }  
        max = (max < steps) ? steps : max;  
        min = (min > steps) ? steps : min;  
        avg += steps;  
        result.polar_mode();  
        steps = 0;  
        result.reset(0.0, 0.0);  
    }  
    avg /= N;  
    std::cout << "max: " << max << std::endl << "min: " << min << std::endl << "average: " << avg << std::endl;  
    std::cout << "Bye!\n";  
    std::cin.clear();  
    while (std::cin.get() != '\n')  
        continue; 
	system("pause");
    return 0;  
}  

/*******************************************************************************************************************   
Author : Cui mingyang  
Blog : cx_12586  
Time : 2017/11/20 
From : C++ Primer Plus第五版第11章编程练习 第4题    
*******************************************************************************************************************/  
//头文件
#ifndef TIMER_H_  
#define TIMER_H_  

#include <iostream>  

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

#endif  
//头文件对应的cpp文件
#include "Timer.h"  

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

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

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

void Timer::AddHr(int h)  
{  
	hours = (hours + h) % 24;  
}  

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

Timer operator+(const Timer &t1, const Timer &t2)  
{  
	Timer t;  
	t.minutes = t1.minutes + t2.minutes;  
	t.hours = t1.hours + t2.hours + t.minutes / 60;  
	t.minutes %= 60;  
	t.hours %= 24;  
	return t;  
}  

Timer operator-(const Timer &t1, const Timer &t2)  
{  
	Timer t;  
	t.minutes = (t1.hours * 60 + t1.minutes) - (t2.hours * 60 + t2.minutes);  
	t.hours = t.minutes / 60;  
	t.minutes %= 60;  
	return t;  
}  

Timer operator*(const Timer &t, double n)  
{  
	Timer Temp;  
	Temp.minutes = t.hours * 60 * n + t.minutes * n;  
	Temp.hours = Temp.minutes / 60;  
	Temp.minutes %= 60;  
	return Temp;  
}  

Timer operator*(double n, const Timer &t)  
{  
	return operator*(t, n);  
}  

std::ostream &operator<<(std::ostream &os, const Timer &t)  
{  
	os << t.hours << ":" << t.minutes;  
	return os;  
}  

//main函数
#include "Timer.h"  
#include <iostream>  
int main()  
{  
	Timer t1(12, 53);  
	Timer t2(14, 00);  
	std::cout << t1 + t2 << std::endl;  
	system("pause");
	return 0;  
}  

/*******************************************************************************************************************   
Author : Cui mingyang  
Blog : cx_12586  
Time : 2017/11/20 
From : C++ Primer Plus第五版第11章编程练习 第5题    
*******************************************************************************************************************/  
//头文件
#ifndef STONEWT_H_  
#define STONEWT_H_  

#include <iostream>  

enum Mode { POUNDS, STONE };  

class Stonewt {  
private:  
	static const int Lbs_per_stn = 14;  
	Mode mode;  
	int stone;  
	double pds_left;  
	double pounds;  
public:  
	Stonewt(double lbs);  
	Stonewt(int stn, double lbs);  
	Stonewt();  
	~Stonewt() {};  

	void SetMode(Mode form);  
	friend Stonewt operator+(const Stonewt &s1, const Stonewt &s2);  
	friend Stonewt operator-(const Stonewt &s1, const Stonewt &s2);  
	friend Stonewt operator*(const Stonewt &s1, const Stonewt &s2);  
	friend std::ostream &operator<<(std::ostream &os, const Stonewt &s);  
};  

#endif  
//头文件对应的cpp文件
#include "stonewt.h"  

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

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

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

void Stonewt::SetMode(Mode form)  
{  
	mode = form;  
}  

Stonewt operator+(const Stonewt &s1, const Stonewt &s2)  
{  
	Stonewt s;  
	if (s1.mode == POUNDS)  
	{  
		s.pounds = s1.pounds + s2.pounds;  
		s.SetMode(POUNDS);  
	}  
	else  
	{  
		s.stone = s1.stone + s2.stone;  
		s.SetMode(STONE);  
	}  
	return s;  
}  

Stonewt operator-(const Stonewt &s1, const Stonewt &s2)  
{  
	Stonewt s;  
	if (s1.mode == POUNDS)  
	{  
		s.pounds = s1.pounds - s2.pounds;  
		s.SetMode(POUNDS);  
	}  
	else  
	{  
		s.stone = s1.stone - s2.stone;  
		s.SetMode(STONE);  
	}  
	return s;  
}  

Stonewt operator*(const Stonewt &s1, const Stonewt &s2)  
{  
	Stonewt s;  
	if (s1.mode == POUNDS)  
	{  
		s.pounds = s1.pounds * s2.pounds;  
		s.SetMode(POUNDS);  
	}  
	else  
	{  
		s.stone = s1.stone * s2.stone;  
		s.SetMode(STONE);  
	}  
	return s;  
}  
std::ostream &operator<<(std::ostream &os, const Stonewt &s)  
{  
	if (s.mode == POUNDS)  
		os << s.pounds;  
	else  
		os << s.stone;  
	return os;  
}  

//main函数
#include "stonewt.h"  

int main()  
{  
	Stonewt s1(10);  
	Stonewt s2(10);  

	std::cout << s1 << std::endl;  
	std::cout << s2 << std::endl;  

	std::cout << s1 + s2 << std::endl;  
	std::cout << s1 - s2 << std::endl;  
	std::cout << s1 * s2 << std::endl;  
	system("pause");
	return 0;  
}  

/*******************************************************************************************************************   
Author : Cui mingyang  
Blog : cx_12586  
Time : 2017/11/20 
From : C++ Primer Plus第五版第11章编程练习 第6题    
*******************************************************************************************************************/  
//头文件
#ifndef STONEWT_H_  
#define STONEWT_H_  

class Stonewt {  
private:  
	enum{Lbs_per_stn = 14};  
	int stone;  
	double pds_left;  
	double pounds;  
public:  
	Stonewt(double lbs);  
	Stonewt(int stn, double lbs);  
	Stonewt();  
	~Stonewt();  
	void show_lbs() const;  
	void show_stn() const;  
	friend bool operator>(Stonewt &s1, Stonewt &s2);  
	friend bool operator<(Stonewt &s1, Stonewt &s2);  
	friend bool operator>=(Stonewt &s1, Stonewt &s2);  
	friend bool operator<=(Stonewt &s1, Stonewt &s2);  
	friend bool operator==(Stonewt &s1, Stonewt &s2);  
	friend bool operator!=(Stonewt &s1, Stonewt &s2);  
};  

#endif  
//头文件对应的cpp文件
#include "stonewt.h"  
#include <iostream>  

Stonewt::Stonewt(double lbs)  
{  
	stone = (int)lbs / Lbs_per_stn;  
	pds_left = (int)lbs % Lbs_per_stn;  
	pounds = lbs;  
}  

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

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

Stonewt::~Stonewt()  
{  
}  

void Stonewt::show_stn() const  
{  
	std::cout << stone << " stone, " << pds_left << " pounds\n";  
}  

void Stonewt::show_lbs() const  
{  
	std::cout << pounds << " pounds\n";  
}  

bool operator>(Stonewt &s1, Stonewt &s2)  
{  
	return s1.pounds > s2.pounds ? true : false;  
}  

bool operator<(Stonewt &s1, Stonewt &s2)  
{  
	return s1.pounds < s2.pounds ? true : false;  
}  

bool operator>=(Stonewt &s1, Stonewt &s2)  
{  
	return s1.pounds >= s2.pounds ? true : false;  
}  

bool operator<=(Stonewt &s1, Stonewt &s2)  
{  
	return s1.pounds <= s2.pounds ? true : false;  
}  

bool operator==(Stonewt &s1, Stonewt &s2)  
{  
	return s1.pounds == s2.pounds ? true : false;  
}  

bool operator!=(Stonewt &s1, Stonewt &s2)  
{  
	return s1.pounds != s2.pounds ? true : false;  
}  
//main函数
#include "stonewt.h"  
#include <iostream>  

int main()  
{  
	Stonewt s[6] = {(10), (20), (30) };  
	for (int i = 3; i < 6; ++i)  
	{  
		double t;  
		std::cin >> t;  
		s[i] = Stonewt(t);  
	}  
	Stonewt min(999), max(-1), st11(11);  
	int num = 0;  
	for (int i = 0; i < 6; ++i)  
	{  
		min = min > s[i] ? s[i] : min;  
		max = max < s[i] ? s[i] : max;  
		if (s[i] >= st11)  
			num++;  
	}  

	std::cout << "min: ";  
	min.show_stn();  
	std::cout << "max: ";  
	max.show_stn();  
	std::cout << ">=11: ";  
	std::cout << num;  

	system("pause");  
	return 0;  
}  


/*******************************************************************************************************************   
Author : Cui mingyang  
Blog : cx_12586  
Time : 2017/11/20 
From : C++ Primer Plus第五版第11章编程练习 第7题    
*******************************************************************************************************************/  
//头文件
#ifndef Complex_H_
#define Complex_H_
using namespace std;
class Complex
{
private:
	double real;
	double imag;
public:
	//构造函数和析构函数
	Complex();
	Complex(double x,double y);
	~Complex();
	
	//操作符重载
	Complex operator +(const Complex & c);
	Complex operator -(const Complex & c);
	Complex operator ~()const;
	Complex operator *(const Complex & c);

	friend Complex square(const Complex & z);
	friend Complex operator*(double n, const Complex & w);
	//输入输出操作符重载
	friend ostream & operator << (ostream & os, const Complex & c);
	friend istream & operator >> (istream & is, Complex & c);
};
#endif
//头文件对应的cpp文件
#include <iostream>
#include <complex>
#include "complex0.h"
using namespace std;

//构造函数和析构函数
Complex::Complex()
{
	real = imag =0;
}
Complex::Complex(double x,double y)
{
	real = x;
	imag = y;
}
Complex::~Complex()
{

}
//操作符重载
Complex Complex::operator +(const Complex & c)
{
	return Complex(real+c.real, imag+c.imag);
}

Complex Complex::operator -(const Complex & c)
{
	return Complex(real-c.real, imag-c.imag);
}

Complex Complex::operator ~()const
{
	return Complex(real, -imag);
}

Complex Complex::operator *(const Complex & c)
{
	return Complex(real*c.real-imag*c.imag, real*c.imag+imag*c.real);
}


Complex square (const Complex & z)
{
	Complex sq;
	sq.real = z.real * z.real - z.imag * z.imag;
	sq.imag = 2.0 * z.real * z.imag;
	return sq;
}

Complex operator*(double n, const Complex & w)
{
	return Complex(n*w.real,n*w.imag);
}

//输入输出操作符重载
ostream & operator << (ostream & os, const Complex & c)
{
	os << "(" << c.real << ", " << c.imag << "i)" <<endl;
	return os;
}
istream & operator >> (istream & is, Complex & c)
{
	cout << "real: ";
	if (is >> c.real)
	{
		cout << "imaginary: ";
		is >> c.imag;
	}
	return is;

}

//main函数
#include <iostream>
#include "complex0.h"
int main()
{
	Complex a(3.0,4.0);
	Complex c;
	cout << "Enter a complex number (q to quit): \n";
	while(cin >> c)
	{
		cout << "c is " << c <<'\n';
		cout << "complex conjugate is " << ~c <<'\n';
		cout << "a is " << a <<'\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.0*c <<'\n';
	}
	cout << "Done\n";
	system("pause");
	return 0;

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值