这两道题是第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题答案的可以到这个网址看