C++primer plus第六版課後程式設計練習答案11.5和11.6
阿新 • • 發佈:2019-02-12
這兩道題是第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題答案的可以到這個網址看