第十一章编程练习(6)

list.h

#pragma once
#ifndef list_H_
#define list_H_
#include 
using namespace std;
class Stonewt {
private:
	static const int Lbs_per_stn = 14;
	enum Mode { BP, INT_GBP, FLOAT_GBP };
	Mode mode;
	int stone;
	double pds_left;
	double pounds;
public:
	Stonewt(double lbs,Mode from = BP);
	Stonewt(int stn, double lbs, Mode from = BP);
	void set_modeBP();
	void set_modeINT_GBP();
	void set_modeFLOAT_GBP();
	Stonewt operator + (const Stonewt & a);
	Stonewt	operator -(const Stonewt & a);
	Stonewt operator *(double n);
	bool operator >(const Stonewt & a) { return (pounds > a.pounds); };
	bool operator <(const Stonewt & a) { return (pounds < a.pounds); };
	bool operator >=(const Stonewt & a){ return (pounds >= a.pounds); };
	bool operator <=(const Stonewt & a){ return (pounds <= a.pounds); };
	bool operator ==(const Stonewt & a){ return (pounds == a.pounds); };
	bool operator !=(const Stonewt & a){ return (pounds != a.pounds); };
	friend Stonewt operator *(double n, Stonewt & a);
	friend ostream & operator <<(ostream & os, Stonewt & t);
	Stonewt();
	~Stonewt();
};
#endif

function.cpp

#include "list.h"
#include 
Stonewt::Stonewt()
{
	stone = pds_left = pounds = 0;
	mode = BP;
}
Stonewt::Stonewt(double lbs, Mode from)
{
	mode = from;
	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 from)
{
	mode = from;
	stone = stn;
	pds_left = lbs;
	pounds = stn*Lbs_per_stn + lbs;
}
Stonewt::~Stonewt()
{
}
void Stonewt::set_modeBP()
{
	mode = BP;
}
void Stonewt::set_modeINT_GBP()
{
	mode = INT_GBP;
}
void Stonewt::set_modeFLOAT_GBP()
{
	mode = FLOAT_GBP;
}
Stonewt Stonewt::operator+(const Stonewt & a)
{
	Stonewt sum_1;
	sum_1.pounds = pounds + a.pounds;
	Stonewt sum_2(sum_1.pounds);
	return sum_2;
}
Stonewt Stonewt::operator-(const Stonewt & a)
{
	Stonewt less_1;
	less_1.pounds = pounds - a.pounds;
	Stonewt less_2(less_1.pounds);
	return less_2;
}
Stonewt Stonewt::operator*(double n)
{
	Stonewt mult_1;
	mult_1.pounds = pounds*n;
	Stonewt mult_2(mult_1.pounds);
	return mult_2;
}
Stonewt operator*(double n, Stonewt & a)
{
	return a*n;
}
ostream & operator<<(ostream & os, Stonewt & t)
{
	if (t.mode == Stonewt::BP)
		os << t.stone + t.pds_left << " BP";
	else if (t.mode== Stonewt::INT_GBP)
		os << int(t.pounds) << " GBP";
	else if (t.mode == Stonewt::FLOAT_GBP)
		os << t.pounds << " GBP";
	else
		os << "Error\n";
	return os;
}


main.cpp

#include "list.h"
#include 
#include 
using namespace std;
int main()
{
	Stonewt wolf[6] = {1.0,1.1,1.2};
	Stonewt talf(11, 0.0);
	unsigned int temp = 0;
	for (int i = 3; i < 6; i++)
	{
		cout << "Please enter the number of the object: ";
		double du;
		if (!(cin >> du))
			exit(EXIT_FAILURE);
		wolf[i] = du;
	}
	Stonewt MAX= wolf[0];
	Stonewt MIN = wolf[0];
	for (int i = 0; i < 6; i++)
	{
		if (wolf[i] > MAX)
			MAX = wolf[i];
		else if (wolf[i] < MIN)
			MIN = wolf[i];
		if (wolf[i] >= talf)
			temp++;
	}
	cout << "MAX: " << MAX << endl
		<< "MIN: " << MIN << endl
		<< "temp: " << temp << endl;
	system("pause");
	return 0;
}

第十一章编程练习(6)_第1张图片

你可能感兴趣的:(C++,Primer,Plus)