11.9-5

原题

#ifndef STONWET_H_
#define STONWET_H_
class Stonewt
{
public:

	Stonewt(double lbs);
	Stonewt(int stn, double lbs);
	Stonewt();
	~Stonewt();
	void show_lbs() const;
	void show_stn() const;
	//conversion functions
	operator int()const;
	operator double()const;
	//
	//Stonewt operator *(double n);
	//Stonewt operator*(double mult);
	friend Stonewt operator*(Stonewt &s, double mult);
protected:
private:
	static const int Lbs_per_stn = 14;
	int stone;
	double pds_left;
	double pounds;

};

#endif

#include <iostream>
#include "stonewt.h"
using std::cout;

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

Stonewt::~Stonewt()
{
}
void Stonewt::show_lbs() const
{
	cout << pounds << " pounds\n";
}
void Stonewt::show_stn() const
{
	cout << stone << " stone" << pds_left << " pounds\n";
}
Stonewt::operator int()const
{
	return int(pounds + 0.5);
}
Stonewt::operator double()const
{
	return pounds;
}
/*
Stonewt Stonewt::operator *(double n)
{
	Stonewt stemp;
	stemp.stone  +=(pounds*n)/Lbs_per_stn;
	int temp=0;
	temp = (pounds*n) % (double)Lbs_per_stn;
	stemp.pounds = pounds*n - Lbs_per_stn*temp;
	return stemp;
}*/

/*
Stonewt Stonewt::operator*(double mult)
{
	return Stonewt(mult*pounds);
}*/

Stonewt operator*(Stonewt &s, double mult)
{
	return Stonewt(s.pounds * mult);
}

#include <iostream>
#include "stonewt.h"
using namespace std;

void main()
{
	Stonewt s1 = 275;
	Stonewt s2(285.7);
	Stonewt s3(21, 8);
	cout << "the s1 weighted ";
	s1.show_stn();
	cout << "the s2 weighed ";
	s2.show_stn();
	cout << "the s3 weighted ";
	s3.show_stn();

	Stonewt s4;
	s4=s1 * 2.0;
	s4.show_stn();

	cout<<"hello.."<<endl;
	system("pause");
	return ;
}

在这里插入图片描述

#ifndef STONWET_H_
#define STONWET_H_
class Stonewt
{
public:
	enum Mode{ STONE, PDS_LEFT, POUNDS };
	Stonewt(double lbs  ,  Mode form=POUNDS);
	Stonewt(int stn , double lbs , Mode form=POUNDS);
	Stonewt();
	~Stonewt();
	void show_lbs() const;
	void show_stn() const;
	//conversion functions
	operator int()const;
	operator double()const;
	friend std::ostream & operator<<(std::ostream &os, const Stonewt&s);

	Stonewt operator * (double mult);
	friend Stonewt operator+(const Stonewt & s1, const Stonewt & s2);
	friend Stonewt operator-(const Stonewt & s1, const Stonewt & s2);

	friend Stonewt operator*(Stonewt &s, double mult);
protected:
private:
	Mode mode;
	static const int Lbs_per_stn = 14;
	int stone;
	double pds_left;
	double pounds;

};

#endif

#include <iostream>
#include "stonewt.h"
using std::cout;

Stonewt::Stonewt(double lbs,Mode form)
{
	mode = form;
	if (form == POUNDS)
	{
		pounds = lbs;
	}
	else if (form == STONE)
	{
		stone = int(lbs) / Lbs_per_stn;
	}
	else if (form == PDS_LEFT)
	{
		pds_left = int(lbs) % Lbs_per_stn + lbs - int(lbs);
	}
	else
	{
		cout << "Incorrect 3rd argument to Stonewt() --";
		cout << "Stonewt set to 0\n";
		Stonewt();
	}
}
Stonewt::Stonewt(int stn, double lbs,Mode form)
{
	mode = form;
	if (form==POUNDS)
	{
		pounds = stn*Lbs_per_stn + lbs;
	}
	else if (form==STONE)
	{
		stone = stn;
	}
	else if (form==PDS_LEFT)
	{
		pds_left = lbs;
	}
	else
	{
		cout << "Incorrect 3rd argument to Stonewt() --";
		cout << "Stonewt set to 0\n";
		Stonewt();
	}
}

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

Stonewt::~Stonewt()
{
}

std::ostream & operator<<(std::ostream &os, const Stonewt&s)
{
	//os << s.pounds << " pounds\n" << s.stone << " stone\n" << s.pds_left << " pounds\n";
	if (s.mode==Stonewt::STONE)
	{
		cout << "Stone: " << s.stone;
	}
	else if (s.mode == s.POUNDS)
	{
		cout << "pounds in int: " << s.pounds ;
	}
	else if (s.mode == s.PDS_LEFT)
	{
		cout << "pounds in double: " << s.pds_left ;
	}
	return os;
}


Stonewt operator+(const Stonewt & s1, const Stonewt & s2)
{
	Stonewt sum;
	sum.pounds = s1.pounds + s2.pounds;
	sum.stone = int(sum.pounds) / 14;
	sum.pds_left = int(sum.pounds) % 14 + sum.pounds - int(sum.pounds);
	return sum;
}

Stonewt operator-(const Stonewt & s1, const Stonewt & s2)
{
	double pounds;
	pounds = s1.pounds - s2.pounds;
	Stonewt diff(pounds);
	return diff;

}


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

Stonewt operator*(Stonewt &s, double mult)
{
	return Stonewt(s.pounds * mult);
}

#include <iostream>
#include "stonewt.h"
using namespace std;

void main()
{
	Stonewt s1 = 275;
	Stonewt s2(285.7);
	Stonewt s3(21, 8,Stonewt::POUNDS);
	cout << s1 << endl;
	cout << s2 << endl;
	cout << s3 << endl;
	
	Stonewt s5;
	s5 = s1 + s2;
	cout << s5 << endl;
	Stonewt s4;
	Stonewt s6 = 274;
	s4=s1 - s6;
	cout << s4 << endl;

	cout << "hello.." << endl;
	system("pause");
	return;
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值