《C++ Primer Plus(第六版)》(21)(第十一章 使用类 编程题答案2)
答案很长,所以分开两个文章。
4.
原来的代码:
Test.h
#ifndef _Test_H_ #define _Test_H_ #include <iostream> 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; }; #endifTest.cpp
#include "Test.h" #include <iostream> #include <cmath> 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 <iostream> #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 <iostream> 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; }; #endifTest.cpp
#include "Test.h" #include <iostream> #include <cmath> 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 <iostream> 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; }; #endifTest.cpp
#include "Test.h" #include <iostream> #include <cmath> 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 <iostream> #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 <iostream> 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;//模式 }; #endifTest.cpp
#include "Test.h" #include <iostream> #include <cmath> 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 <iostream> #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 <iostream> 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;//模式 }; #endifTest.cpp
#include "Test.h" #include <iostream> #include <cmath> 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 <iostream> #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 <iostream> 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; }; } #endifTest.cpp
#include "Test.h" #include <iostream> #include <cmath> 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 <iostream> #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; }
人生如戏,还是戏如人生?微信公众号:传说之路
csdn博客 http://blog.csdn.net/u012175089/article/list/2