博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
《C++ Primer Plus(第六版)》(21)(第十一章 使用类 编程题答案2)
阅读量:4348 次
发布时间:2019-06-07

本文共 11850 字,大约阅读时间需要 39 分钟。

答案很长,所以分开两个文章。

4.

原来的代码:

Test.h

#ifndef _Test_H_  #define _Test_H_  #include 
class Time{public: Time(); Time(int h, int m = 0); void AddMin(int m); void AddHr(int h); void Reset(int h = 0, int m = 0); Time operator+(const Time& t)const; Time operator-(const Time& t)const; Time operator*(double n)const; friend Time operator*(double m, const Time& t) { return t* m; } friend std::ostream& operator<<(std::ostream& os, const Time& t);private: int hours; int minutes;}; #endif
Test.cpp

#include "Test.h"  #include 
#include
Time::Time(){ hours = minutes = 0;}Time::Time(int h, int m /*= 0*/){ hours = h; minutes = m;}void Time::AddMin(int m){ minutes += m; hours += minutes / 60; minutes %= 60;}void Time::AddHr(int h){ hours += h;}void Time::Reset(int h /*= 0*/, int m /*= 0*/){ hours = h; minutes = m;}Time Time::operator+(const Time& t) const{ Time sum; sum.minutes = minutes + t.minutes; sum.hours = hours + t.hours + sum.minutes / 60; sum.minutes %= 60; return sum;}Time Time::operator-(const Time& t) const{ Time diff; int tot1 = t.minutes + 60 * t.hours; int tot2 = minutes + 60 * hours; diff.minutes = (tot2 - tot1) % 60; diff.hours = (tot2 - tot1) / 60; return diff;}Time Time::operator*(double mult) const{ Time result; long totalminutes = hours * mult * 60 + minutes * mult; result.hours = totalminutes / 60; result.minutes = totalminutes % 60; return result;}std::ostream& operator<<(std::ostream& os, const Time& t){ os << t.hours << " hours, " << t.minutes << " minutes"; return os;}
main.cpp

#include 
#include "Test.h" using namespace std;int main(int argc, const char * argv[]){ Time aida(3, 35); Time tosca(2, 48); Time temp; cout << "Aida and Tosca:\n"; cout << aida << "; " << tosca << endl; temp = aida + tosca; cout << "Aida + Tosca: " << temp << endl; temp = aida * 1.17; cout << "Aida * 1.17: " << temp << endl; cout << "10.0 * Tosca: " << 10.0 * tosca << endl; return 0;}
修改:

只修改Test.h和Test.cpp就行,不需要对使用的地方进行修改的。

Test.h

#ifndef _Test_H_  #define _Test_H_  #include 
class Time{public: Time(); Time(int h, int m = 0); void AddMin(int m); void AddHr(int h); void Reset(int h = 0, int m = 0); friend Time operator+(const Time& a, const Time& b); friend Time operator-(const Time& a, const Time& b); friend Time operator*(const Time& t, double n); friend Time operator*(double m, const Time& t) { return t* m; } friend std::ostream& operator<<(std::ostream& os, const Time& t);private: int hours; int minutes;}; #endif
Test.cpp

#include "Test.h"  #include 
#include
Time::Time(){ hours = minutes = 0;}Time::Time(int h, int m /*= 0*/){ hours = h; minutes = m;}void Time::AddMin(int m){ minutes += m; hours += minutes / 60; minutes %= 60;}void Time::AddHr(int h){ hours += h;}void Time::Reset(int h /*= 0*/, int m /*= 0*/){ hours = h; minutes = m;}Time operator+(const Time& a, const Time& b){ Time sum; sum.minutes = a.minutes + b.minutes; sum.hours = a.hours + b.hours + sum.minutes / 60; sum.minutes %= 60; return sum;}Time operator-(const Time& a, const Time& b){ Time diff; int tot1 = a.minutes + 60 * a.hours; int tot2 = b.minutes + 60 * b.hours; diff.minutes = (tot2 - tot1) % 60; diff.hours = (tot2 - tot1) / 60; return diff;}Time operator*(const Time& t, double mult){ Time result; long totalminutes = t.hours * mult * 60 + t.minutes * mult; result.hours = totalminutes / 60; result.minutes = totalminutes % 60; return result;}std::ostream& operator<<(std::ostream& os, const Time& t){ os << t.hours << " hours, " << t.minutes << " minutes"; return os;}
5.

原来的:

Test.h

#ifndef _Test_H_  #define _Test_H_  #include 
class Stonewt{public: Stonewt(double lbs); Stonewt(int stn, double lbs); Stonewt(); ~Stonewt(); void show_lbs()const; void show_stn()const;private: enum {Lbs_per_stn = 14}; int stone; double pds_left; double pounds; }; #endif
Test.cpp

#include "Test.h"  #include 
#include
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;}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 << stone << " stone, " << pds_left << " pounds\n";}void Stonewt::show_stn() const{ cout << pounds << " pounds\n";}
main.cpp
#include 
#include "Test.h" using namespace std;void display(const Stonewt& st, int n);int main(int argc, const char * argv[]){ Stonewt incognito = 275; Stonewt wolfe(285.7); Stonewt taft(21, 8); cout << "The celebrity weighted "; incognito.show_stn(); cout << "The detective weighted "; wolfe.show_stn(); cout << "The President weighed "; taft.show_lbs(); incognito = 276.8; taft = 325; cout << "After dinner, the celebrity weighed "; incognito.show_stn(); cout << "After dinner, the President weighed "; taft.show_lbs(); display(taft, 2); cout << "The wrestler weighed even more.\n"; display(422, 2); cout << "No stone left unesrned\n"; return 0;}void display(const Stonewt& st, int n){ for (int i = 0; i < n; i++) { cout << "Wow! "; st.show_stn(); }}
修改:

Test.h

#ifndef _Test_H_  #define _Test_H_  #include 
using namespace std;enum EMode{ EStone,//英石格式 EPounds,//浮点磅格式 EPoundsInt//整数磅格式};class Stonewt{public: Stonewt(double lbs); Stonewt(int stn, double lbs); Stonewt(); ~Stonewt(); void setMode(EMode m); friend ostream& operator<<(ostream& os, Stonewt& st);private: enum {Lbs_per_stn = 14}; int stone; double pds_left; double pounds; int mode;//模式 }; #endif
Test.cpp

#include "Test.h"  #include 
#include
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; mode = EPounds;}Stonewt::Stonewt(int stn, double lbs){ stone = stn; pds_left = lbs; pounds = stn * Lbs_per_stn + lbs; mode = EStone;}Stonewt::Stonewt(){ pounds = pds_left = stone = 0; mode = EPounds;}Stonewt::~Stonewt(){} void Stonewt::setMode(EMode m){ mode = m;}ostream& operator<<(ostream& os, Stonewt& st){ if (st.mode == EStone) { os << st.stone << " stone, " << st.pds_left << " pounds\n"; } else if (st.mode == EPounds) { os << st.pounds << " pounds\n"; } else if (st.mode == EPoundsInt) { os << int(st.pounds) << " pounds\n"; } else { os << "error mode"; } return os;}
main.cpp

#include 
#include "Test.h" using namespace std; int main(int argc, const char * argv[]){ Stonewt a(285.7); cout << a; a.setMode(EStone); cout << a; a.setMode(EPoundsInt); cout << a; return 0;}
6.

Test.h

#ifndef _Test_H_  #define _Test_H_  #include 
using namespace std;enum EMode{ EStone,//英石格式 EPounds,//浮点磅格式 EPoundsInt//整数磅格式};class Stonewt{public: Stonewt(double lbs); Stonewt(int stn, double lbs); Stonewt(); ~Stonewt(); void setMode(EMode m); friend ostream& operator<<(ostream& os, Stonewt& st); friend bool operator<(Stonewt& a, Stonewt& b); friend bool operator<=(Stonewt& a, Stonewt& b); friend bool operator==(Stonewt& a, Stonewt& b); friend bool operator>(Stonewt& a, Stonewt& b); friend bool operator>=(Stonewt& a, Stonewt& b); friend bool operator!=(Stonewt& a, Stonewt& b);private: enum {Lbs_per_stn = 14}; int stone; double pds_left; double pounds; int mode;//模式 }; #endif
Test.cpp

#include "Test.h"  #include 
#include
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; mode = EPounds;}Stonewt::Stonewt(int stn, double lbs){ stone = stn; pds_left = lbs; pounds = stn * Lbs_per_stn + lbs; mode = EStone;}Stonewt::Stonewt(){ pounds = pds_left = stone = 0; mode = EPounds;}Stonewt::~Stonewt(){} void Stonewt::setMode(EMode m){ mode = m;}ostream& operator<<(ostream& os, Stonewt& st){ if (st.mode == EStone) { os << st.stone << " stone, " << st.pds_left << " pounds\n"; } else if (st.mode == EPounds) { os << st.pounds << " pounds\n"; } else if (st.mode == EPoundsInt) { os << int(st.pounds) << " pounds\n"; } else { os << "error mode"; } return os;}bool operator<(Stonewt& a, Stonewt& b){ return a.pounds < b.pounds;}bool operator<=(Stonewt& a, Stonewt& b){ return a.pounds <= b.pounds;}bool operator==(Stonewt& a, Stonewt& b){ return a.pounds == b.pounds;}bool operator>(Stonewt& a, Stonewt& b){ return a.pounds > b.pounds;}bool operator>=(Stonewt& a, Stonewt& b){ return a.pounds >= b.pounds;}bool operator!=(Stonewt& a, Stonewt& b){ return a.pounds != b.pounds;}
main.cpp

#include 
#include "Test.h" using namespace std; int main(int argc, const char * argv[]){ Stonewt a(11,0); cout << a; Stonewt b[6] = { 123, 23.32, 3.3 }; for (int i = 3; i < 6; i++) { b[i] = rand(); } Stonewt min; Stonewt max; for (int i = 0; i < 6; i ++) { if (i == 0) { min = b[i]; max = b[i]; } else { if (min > b[i]) { min = b[i]; } if (max < b[i]) { max = b[i]; } } if (b[i] >= a) { cout << "bigger than 11: " << b[i] << endl; } } cout << "Min: " << min << endl; cout << "Max: " << max << endl; return 0;}
7.
Test.h

#ifndef _Test_H_  #define _Test_H_  #include 
using namespace std;namespace FableGame{ class Complex { public: Complex(double real, double imaginary = 0); Complex(); ~Complex(); friend ostream& operator<<(ostream& os, Complex& st); friend istream& operator>>(istream& is, Complex& st); friend Complex operator+(Complex& a, Complex& b); friend Complex operator-(Complex& a, Complex& b); friend Complex operator*(Complex& a, Complex& b); friend Complex operator*(double a, Complex& b); friend Complex operator~(Complex& a ); private: double _real; double _imaginary; }; }#endif
Test.cpp

#include "Test.h"  #include 
#include
using namespace std;FableGame::Complex::Complex(double real, double imaginary){ _real = real; _imaginary = imaginary;}FableGame::Complex::Complex(){ _real = 0; _imaginary = 0;}FableGame::Complex::~Complex(){}ostream& FableGame::operator<<(ostream& os, FableGame::Complex& a){ os << "(" << a._real << ", " << a._imaginary << "i)"; return os;}istream& FableGame::operator>>(istream& is, FableGame::Complex& a){ cout << "real: "; if (is >> a._real) { cout << "imaginary: "; is >> a._imaginary; } return is;}FableGame::Complex FableGame::operator+(FableGame::Complex& a, FableGame::Complex& b){ FableGame::Complex c; c._real = a._real + b._real; c._imaginary = a._imaginary + b._imaginary; return c;}FableGame::Complex FableGame::operator-(FableGame::Complex& a, FableGame::Complex& b){ FableGame::Complex c; c._real = a._real - b._real; c._imaginary = a._imaginary - b._imaginary; return c;}FableGame::Complex FableGame::operator*(FableGame::Complex& a, FableGame::Complex& b){ FableGame::Complex c; c._real = a._real*b._real - a._imaginary*b._imaginary; c._imaginary = a._imaginary*b._real + a._real*b._imaginary; return c;}FableGame::Complex FableGame::operator*(double a, FableGame::Complex& b){ FableGame::Complex c(a); return c * b;}FableGame::Complex FableGame::operator~(FableGame::Complex& a){ FableGame::Complex c; c._real = a._real; c._imaginary = -a._imaginary; return c;}
main.cpp

#include 
#include "Test.h" using namespace std; int main(int argc, const char * argv[]){ FableGame::Complex a(3, 4); FableGame::Complex c; cout << "Enter a complex number (q to quit):\n"; while (cin>>c) { cout << "c is " << c << '\n'; cout << "complex conjugate is " << ~c << "\n"; cout << "a is " << a << endl; cout << "a + c is " << a + c << endl; cout << "a - c is " << a - c << endl; cout << "a * c is " << a * c << endl; cout << "2 * c is " << 2 * c << endl; } cout << "Done!" << endl; return 0;}

转载于:https://www.cnblogs.com/fablegame/p/6430243.html

你可能感兴趣的文章
KD树
查看>>
VsVim - Shortcut Key (快捷键)
查看>>
HDU5447 Good Numbers
查看>>
08.CXF发布WebService(Java项目)
查看>>
java-集合框架
查看>>
RTMP
查看>>
求一个数的整数次方
查看>>
点云PCL中小细节
查看>>
铁路信号基础
查看>>
RobotFramework自动化2-自定义关键字
查看>>
CMU Bomblab 答案
查看>>
技术分析淘宝的超卖宝贝
查看>>
Azure云服务托管恶意软件
查看>>
My安卓知识6--关于把项目从androidstudio工程转成eclipse工程并导成jar包
查看>>
旧的起点(开园说明)
查看>>
生产订单“生产线别”带入生产入库单
查看>>
crontab导致磁盘空间满问题的解决
查看>>
自定义滚动条
查看>>
APP开发手记01(app与web的困惑)
查看>>
初识前端作业1
查看>>