C++ primer plus 习题及解析十一章(使用类)

 题目:11.1

设计一个时钟类:需求如下

1.新建一个时钟会默认初始化。

2.可以展示时钟时间

3.可以加分钟

4.可以加小时

5.两个时钟可以相加

class Time
{
private:
	int hour;//小时
	int min;//分钟
	int sec;//秒
public:
	Time(int hour=0, int min=0, int sec=0);//构造函数
	void Add_min(int min);//添加分钟
	void Add_hour(int hour);//添加小时
	void Add_sec(int sec);//添加秒
	void Sum(const Time t);//相加
	void Show();//展示时间
	
};

Time::Time(int hour, int min, int sec)//构造函数
{
	this->hour = hour;
	this->min = min;
	this->sec = sec;
}
void Time::Add_min(int min)//添加分钟
{
	int new_min = this->min + min;
	//判断是否要进位
	this->min = new_min;
	if (new_min >= 60)
	{
		//进位
		int Carry = new_min / 60;
		this->min = new_min % 60;
		this->hour += Carry;
	}
	
	//在判断小时是否要进位
	if (this->hour >= 24)
	{
		this->hour = (this->hour) % 24;
	}
}
void Time::Add_hour(int hour)//添加小时
{
	this->hour += hour;
	//判断是否要进位
	if (this->hour >= 24)
	{
		this->hour = this->hour % 24;
	}
}
void Time::Sum(const Time t)//相加
{
	this->Add_sec(t.sec);
	this->Add_min(t.min);
	this->Add_hour(t.hour);
}
void Time::Show()//展示时间
{
	cout << this->hour << ":" << this->min << ":" << this->sec << endl;
}
void Time::Add_sec(int sec)//添加秒
{
	int new_sec = this->sec + sec;
	//判断进位
	if (new_sec >= 60)
	{
		this->sec = new_sec % 60;
		int Carry = new_sec / 60;
		this->min += Carry;
	}
	//判断是否要进位
	if (this->min >= 60)
	{
		//进位
		int Carry = this->min / 60;
		this->min = this->min % 60;
		this->hour += Carry;
	}
	//在判断小时是否要进位
	if (this->hour >= 24)
	{
		this->hour = (this->hour) % 24;
	}
}

题目:11.2

重新编写Stonewt类(程序清单11.16和程序清单11.17),使它有一个状态成员,由该状态成员控制对象应转换为英石格式、整数磅格式还是浮点磅格式。重载<<运算符,使用它来替换show_stn()和show_lbs()方法。重载加法、减法和乘发运算符,以便可以对Stonewt值进行加、减、乘运算。编写一个使用所有类方法和友元的小程序,来测试这个类。

#ifndef STONEWT115_H_
#define STONEWT115_H_
#include <iostream>
class Stonewt
{
public:
	enum Format { st, intp, doublep };
private:
	enum { Lbs_per_stn = 14 };
	int stone;
	double pds_left;
	double pounds;
	Format m_form;
public:
	Stonewt(double lbs);
	Stonewt(int stn, double lbs);
	Stonewt();
	~Stonewt();
	void SetFormat(Format form);
	Stonewt operator+ (const Stonewt& s)const;
	Stonewt operator- (const Stonewt& s)const;
	Stonewt operator* (double n)const;
	friend Stonewt operator*(double m, const Stonewt& s){return s * m;}   
	friend std::ostream& operator<< (std::ostream& os, const Stonewt& s);
};
#endif 

 

Stonewt::Stonewt(double lbs)
{
	stone = int(lbs) / Lbs_per_stn;
	pds_left = int(lbs) % Lbs_per_stn + lbs - int(lbs);
	pounds = lbs;
	m_form = st;
}
Stonewt::Stonewt(int stn, double lbs)
{
	stone = stn;
	pds_left = lbs;
	pounds = stn * Lbs_per_stn + lbs;
	m_form = intp;
}
Stonewt::Stonewt()
{
	stone = pounds = pds_left = 0;
	m_form = doublep;
}
Stonewt::~Stonewt()
{

}
void Stonewt::SetFormat(Format from)
{
	m_form = from;
}
Stonewt Stonewt::operator+ (const Stonewt& s)const
{
	Stonewt sum;
	sum.pounds = pounds + s.pounds;
	sum.stone = int(sum.pounds) / Lbs_per_stn;
	sum.pds_left = int(sum.pounds) % Lbs_per_stn + sum.pounds - int(sum.pounds);
	return sum;
}
Stonewt Stonewt::operator- (const Stonewt& s)const
{
	Stonewt diff;
	diff.pounds = pounds - s.pounds;
	diff.stone = int(diff.pounds) / Lbs_per_stn;
	diff.pds_left = int(diff.pounds) % Lbs_per_stn + diff.pounds - int(diff.pounds);
	return diff;
}
Stonewt Stonewt::operator* (double n)const
{
	double mult;
	mult = pounds * n;
	return Stonewt(mult);
}
std::ostream& operator<< (std::ostream& os, const Stonewt& s)
{
	if (s.m_form == Stonewt::st)
	{
		os << s.stone << " stone, " << s.pds_left << " pounds\n";
	}
	else if (s.m_form == Stonewt::intp)
	{
		os << int(s.pounds) << " pounds\n";
	}
	else if (s.m_form == Stonewt::doublep)
	{
		os << s.pounds << " pounds\n";
	}
	else
		os << "Stonewt state is invalid";
	return os;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值