C++ Primer Plus第六版编程练习11.6解答
来源:互联网 发布:坐标数据导入全站仪 编辑:程序博客网 时间:2024/06/08 16:26
stonewt2.h
// stonewt2.h -- definition for the Stonewt class#ifndef STONEWT2_H_#define STONEWT2_H_#include <iostream>class Stonewt{public: enum Mode {stnFormat, pdsFormat};private: enum {Lbs_per_stn = 14}; // pounds per stone int stone; // whole stones double pds_left; // fractional pounds double pounds; // entire weight in pounds Mode mode; // stnFormat or pdsFormatpublic: Stonewt(double pds, Mode m = stnFormat); // constructor for double pounds Stonewt(int stn, double lbs, Mode m = stnFormat); // constructor for stone, lbs Stonewt(); // default constructor ~Stonewt(); void set_stnFormat() {mode = stnFormat;} void set_pdsFormat() {mode = pdsFormat;} Stonewt operator+(const Stonewt & s) const; Stonewt operator-(const Stonewt & s) const; Stonewt operator*(double n) 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);};#endif
stonewt2.cpp
// stonewt.cpp -- Stonewt methods#include <iostream>using std::cout;#include "stonewt2.h"// construct Stonewt object from double valueStonewt::Stonewt(double pds, Mode m){ stone = int (pds) / Lbs_per_stn; // integer division pds_left = int (pds) % Lbs_per_stn + pds - int(pds); pounds = pds; mode = m;}// construct Stonewt object from stone, double valuesStonewt::Stonewt(int stn, double lbs, Mode m){ stone = stn; pds_left = lbs; pounds = stn * Lbs_per_stn +lbs; mode = m;}Stonewt::Stonewt() // default constructor, wt = 0{ stone = pounds = pds_left = 0; mode = stnFormat;}Stonewt::~Stonewt() // destructor{}Stonewt Stonewt::operator+(const Stonewt & s) const{ double pds = pounds + s.pounds; Stonewt temp(pds); return temp;}Stonewt Stonewt::operator-(const Stonewt & s) const{ double pds = pounds - s.pounds; Stonewt temp(pds); return temp;}Stonewt Stonewt::operator*(double n) const{ double pds = pounds * n; Stonewt temp(pds); return temp;}bool Stonewt::operator>(const Stonewt & s) const{ return (pounds > s.pounds);}bool Stonewt::operator<(const Stonewt & s) const{ return (pounds < s.pounds);}bool Stonewt::operator==(const Stonewt & s) const{ return (pounds == s.pounds);}std::ostream & operator<<(std::ostream & os, const Stonewt & s){ if(s.mode==Stonewt::stnFormat) os << s.stone << " stone, " << s.pds_left << " pounds"; else os << s.pounds << " pounds"; return os;}
usestonewt2.h
//usestonewt2.cpp -- using the fourth draft of the Time class//compile usetime3.cpp and mytime3.cpp together#include <iostream>#include "stonewt2.h"int main(){ using std::cout; using std::endl; using std::cin; Stonewt stn[6]= {13.9,11,23}; int i; double pds; for(i=3; i<6; i++) { cout<<"Please enter the number of Stn "<<i<<" :"; cin>>pds; stn[i]=pds; } //show the array of Stonewt for(i=0; i<6; i++) { cout<<"stn "<<i<<" weighs "<<stn[i]; stn[i].set_pdsFormat(); cout<<",\nwhich is "<<stn[i]<<" in whole pounds.\n\n"; } Stonewt max=stn[0]; Stonewt min=stn[0]; Stonewt tag(11,0); int count=0; for(i=0; i<6; i++) { if(stn[i]>max) max=stn[i]; if(stn[i]<min) min=stn[i]; if(stn[i]>tag||stn[i]==tag) count++; } cout<<"The max of array is stn "<<i<<" ,which weighs "<<max<<endl; cout<<"The min of array is stn "<<i<<" ,which weighs "<<min<<endl; cout<<"There are "<<count<<" Stonewt which weighs greater or equal to 11 stone."; cin.get(); return 0;}
0 0
- C++ Primer Plus第六版编程练习11.6解答
- C++ Primer Plus第六版编程练习6.7解答
- C++ Primer Plus第六版编程练习6.9解答
- C++ Primer Plus第六版编程练习7.10解答
- C++ Primer Plus第六版编程练习8.2解答
- C++ Primer Plus第六版编程练习8.6解答
- C++ Primer Plus第六版编程练习9.1解答
- C++ Primer Plus第六版编程练习9.4解答
- C++ Primer Plus第六版编程练习10.3解答
- C++ Primer Plus第六版编程练习10.4解答
- C++ Primer Plus第六版编程练习10.5解答
- C++ Primer Plus第六版编程练习10.6解答
- C++ Primer Plus第六版编程练习10.8解答
- C++ Primer Plus第六版编程练习11.1解答
- C++ Primer Plus第六版编程练习11.2解答
- C++ Primer Plus第六版编程练习11.3解答
- C++ Primer Plus第六版编程练习11.4解答
- C++ Primer Plus第六版编程练习11.5解答
- Hibernate中对象的三种状态
- sicily 1021. Couples
- 记录常用的几个提高iOS开发效率的开源类库及工具
- Java排序算法
- POJ 2983 Is the Information Reliable? 差分约束
- C++ Primer Plus第六版编程练习11.6解答
- 多线程程序设计
- expected unqualified-id before ....的问题
- 用Android版微信打开网页提交表单数据失败的问题
- 对于一个刚毕业游戏程序员的一年的经历总结出来的道理
- 【菜鸟学深度】Introduction to Machine Learning CMU-10701
- Activity启动模式 及 Intent Flags 与 栈 的关联分析
- Windows7下面手把手教你安装Django
- Aexi(4)-字体