C++primer plus第六版课后编程练习答案11.5和11.6

这两道题是第6题包含第5题,所以第5题我就没写了,下面是第6题的答案

头文件
#ifndef STONEWt_H_
#define STONEWt_H_

#include<iostream>

class Stonewt
{
private:
	enum{Lbs_per_stn=14};
	int stone;
	double pds_left;
	double pounds;
	int state;
public:
	Stonewt(double lbs);
	Stonewt(int stn,double lbs);
	Stonewt();
	~Stonewt();
	void setstate();
	void resetStonewt(double lbs);
	void resetStonewt(int stn,double lbs);


//	void show_lbs()const;//以磅的格式显示重量
//	void show_stn()const;//以英石的格式显示重量

	operator int()const;
	operator double()const;
	Stonewt operator*(double n)const;
	Stonewt operator+(const Stonewt &s)const;
	Stonewt operator-(const Stonewt &s)const;
	Stonewt operator/(const Stonewt &s)const;

	bool operator>(const Stonewt &s)const;
	bool operator<(const Stonewt &s)const;
	bool operator>=(const Stonewt &s)const;
	bool operator<=(const Stonewt &s)const;
	bool operator==(const Stonewt &s)const;
	bool operator!=(const Stonewt &s)const;

    friend std::ostream &operator<<(std::ostream &os,const Stonewt &s);
	friend std::istream &operator>>(std::istream &is,Stonewt &s);


};

#endif

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


using namespace std;

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

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

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

Stonewt::~Stonewt(){}

void Stonewt::setstate()
{
	int n;
	cout<<"请设置对象的格式(1.英石格式,2.整数磅格式,3.浮点磅格式)";
	cin>>n;
	if(n==1)
		state=n;
	else if(n==2)
		state=n;
	else if(n==3)
		state=n;
	else
	{
		cout<<"输入错误,没有这种格式的对象";
		state=3;
	}
}

void Stonewt::resetStonewt(double lbs)
{
	stone=int(lbs)/Lbs_per_stn;
	pds_left=int(lbs)%Lbs_per_stn+lbs-(int)lbs;
	pounds=lbs;
	state=3;
}

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


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

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

Stonewt::operator double()const
{
	return (pounds);
}

Stonewt::operator int()const
{
	return int (pounds+0.5);
}

Stonewt Stonewt::operator *(double n)const
{
	return Stonewt(pounds*n);
}

Stonewt Stonewt::operator +(const Stonewt &s)const
{
	return Stonewt(pounds+s.pounds);
}

Stonewt Stonewt::operator -(const Stonewt &s)const
{
	return Stonewt(pounds-s.pounds);
}

Stonewt Stonewt::operator/(const Stonewt &s)const
{
	return Stonewt(pounds/s.pounds);
}

std::ostream &operator<<(std::ostream &os,const Stonewt &s)
{
	if(s.state==1)
		os<<s.stone<<" stones,"<<s.pds_left<<" pounds\n";
	else if(s.state==2)
		os<<(int)s.pounds<<" pounds\n";
	else if(s.state==3)
		os<<s.pounds<<" pounds\n";
	else
		os<<"输出错误,没有此种格式的对象\n";
	return os;

}

std::istream &operator>>(std::istream &is,Stonewt &s)
{
	double p;
	is>>p;
	s.resetStonewt(p);
	return is;
}


bool Stonewt::operator <(const Stonewt &s)const
{
	if(pounds<s.pounds)
		return true;
	else
		return false;
}

bool Stonewt::operator <=(const Stonewt &s)const
{
	if(pounds<=s.pounds)
		return true;
	else 
		return false;
}

bool Stonewt::operator >(const Stonewt &s)const
{
	if(pounds>s.pounds)
		return true;
	else 
		return false;
}

bool Stonewt::operator >=(const Stonewt &s)const
{
	if(pounds>=s.pounds)
		return true;
	else
		return false;
}

bool Stonewt::operator ==(const Stonewt &s)const
{
	if(pounds==s.pounds)
		return true;
	else
		return false;
}

bool Stonewt::operator !=(const Stonewt &s)const
{
	if(pounds!=s.pounds)
		return true;
	else
		return false;
}

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

using namespace std;

void main()
{
	Stonewt a(50),b(60),c(70);
	cout<<a+b<<endl;
	cout<<a-b<<endl;
	cout<<c/a<<endl;
	cout<<a*b<<endl;

	Stonewt n(11,0);

	Stonewt s[6]={10,11,12};
	for(int i=3;i<6;i++)
		cin>>s[i];
	Stonewt max=s[0],min=s[0];	
	for(i=0;i<6;i++)
	{
		cout<<s[i];
	}

	cout<<"大于等于11英石的元素"<<endl;
	for(i=0;i<6;i++)
	{
		if(s[i]<min)
			min=s[i];
		if(s[i]>max)
			max=s[i];
		if(s[i]>=n)
			cout<<s[i];
	}
	cout<<"最大的元素"<<max;
	cout<<"最小的元素"<<min;


}

如果想直接看第5题答案的可以到这个网址看

http://blog.csdn.net/qq844352155/article/details/24128929

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值