C++primer Plus课本代码(第11章)
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                C++primer Plus课本代码(第11章)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.                        
                                第11章 使用類
- 11.1 mytime0.h
- 11.2 mytime0.cpp
- 11.3 usetime0.cpp
- 11.4 mytime1.h
- 11.5 mytime1.cpp
- 11.6 usetime1.cpp
- 11.7 mytime2.h
- 11.8 mytime2.cpp
- 11.9 usetime2.cpp
- 11.10 mytime3.h
- 11.11 mytime3.cpp
- 11.12 usetime3.cpp
- 11.13 vector.h
- 11.14 vector.cpp
- 11.15 randwalk.cpp
- 11.16 stonewt.h
- 11.17 stonewt.cpp
- 11.18 stone.cpp
- 11.19 stonewt1.h
- 11.20 stonewt1.cpp
- 11.21 stone1.cpp
 
11.1 mytime0.h
#ifndef MYTIME0_H_ #define MYTIME0_H_class Time { private:int hours;int minutes; 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 Sum(const Time& t)const;void Show() const; };#endif11.2 mytime0.cpp
#include<iostream> #include"mytime0.h"Time::Time() {hours = minutes = 0; }Time::Time(int h, int m) {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, int m) {hours = h;minutes = m; }Time Time::Sum(const Time& t)const {Time sum;sum.minutes = minutes + t.minutes;sum.hours = hours + t.hours + sum.minutes / 60;sum.minutes %= 60;return sum; }void Time::Show() const {std::cout << hours << " hours, " << minutes << " minutes"; }11.3 usetime0.cpp
#include<iostream> #include"mytime0.h"int main() {using std::cout;using std::endl;Time planning;Time coding(2, 40);Time fixing(5, 55);Time total;cout << "planning time = ";planning.Show();cout << endl;cout << "coding time = ";coding.Show();cout << endl;cout << "fixing time = ";fixing.Show();cout << endl;total = coding.Sum(fixing);cout << "coding.Sum(fixing) = ";total.Show();cout << endl;return 0; }11.4 mytime1.h
#ifndef MYTIME1_H_ #define MYTIME1_H_class Time { private:int hours;int minutes; 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;void Show() const; };#endif11.5 mytime1.cpp
#include<iostream> #include"mytime1.h"Time::Time() {hours = minutes = 0; }Time::Time(int h, int m) {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, int m) {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; }void Time::Show() const {std::cout << hours << " hours, " << minutes << " minutes"; }11.6 usetime1.cpp
#include<iostream> #include"mytime1.h"int main() {using std::cout;using std::endl;Time planning;Time coding(2, 40);Time fixing(5, 55);Time total;cout << "planning time = ";planning.Show();cout << endl;cout << "coding time = ";coding.Show();cout << endl;cout << "fixing time = ";fixing.Show();cout << endl;total = coding + fixing;cout << "coding + fixing = ";total.Show();cout << endl;Time morefixing(3, 28);cout << "more fixing time = ";morefixing.Show();cout << endl;total = morefixing.operator+(total);cout << "morefixing.operator+(total) = ";total.Show();cout << endl;return 0; }11.7 mytime2.h
#ifndef MYTIME2_H_ #define MYTIME2_H_class Time { private:int hours;int minutes; 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;void Show() const; };#endif11.8 mytime2.cpp
#include<iostream> #include"mytime2.h"Time::Time() {hours = minutes = 0; }Time::Time(int h, int m) {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, int m) {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, tot2;tot1 = t.minutes + 60 * t.hours;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; }void Time::Show() const {std::cout << hours << " hours, " << minutes << " minutes"; }11.9 usetime2.cpp
#define _CRT_SECURE_NO_WARNINGS 1#include<iostream> #include"mytime2.h"int main() {using std::cout;using std::endl;Time weeding(4, 35);Time waxing(2, 47);Time total;Time diff;Time adjusted;cout << "weeding time = ";weeding.Show();cout << endl;cout << "waxing time = ";waxing.Show();cout << endl;cout << "total work time = ";total = weeding + waxing;total.Show();cout << endl;diff = weeding - waxing;cout << "weeding time - waxing time = ";diff.Show();cout << endl;adjusted = total * 1.5;cout << "adjusted work time = ";adjusted.Show();cout << endl;return 0; }11.10 mytime3.h
#ifndef MYTIME1_H_ #define MYTIME1_H_ #include <iostream>class Time { private:int hours;int minutes; 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); }; #endif11.11 mytime3.cpp
#include "mytime3.h"Time::Time() {hours = minutes = 0; }Time::Time(int h, int m) {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, int m) {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, tot2;tot1 = t.minutes + 60 * t.hours;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; }11.12 usetime3.cpp
#include <iostream> #include "mytime3.h"int main() {using std::cout;using std::endl;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; }11.13 vector.h
#define _CRT_SECURE_NO_WARNINGS 1 #ifndef VECTOR_H_ #define VECTOR_H_#include <iostream> using namespace std;namespace VECTOR {class Vector{public:enum Mode { RECT, POL };private:double x;double y;double mag;double ang;Mode mode;void set_mag();void set_ang();void set_x();void set_y();public:Vector();Vector(double n1, double n2, Mode form = RECT);void reset(double n1, double n2, Mode form = RECT);~Vector();double xval() const { return x; }double yval() const { return y; }double magval() const { return mag; }double angval() const { return ang; }void polar_mode();void rect_mode();Vector operator+(const Vector& b) const;Vector operator-(const Vector& b) const;Vector operator-() const;Vector operator*(double n) const;friend Vector operator*(double n, const Vector& a);friend std::ostream& operator<<(std::ostream& os, const Vector& v);}; }#endif11.14 vector.cpp
#include <cmath> #include "vector.h" using std::sqrt; using std::sin; using std::cos; using std::atan; using std::atan2; using std::cout;namespace VECTOR {const double Rad_to_deg = 45.0 / atan(1.0);void Vector::set_mag(){mag = sqrt(x * x + y * y);}void Vector::set_ang(){if (x == 0.0 && y == 0.0)ang = 0.0;elseang = atan2(y, x);}void Vector::set_x(){x = mag * cos(ang);}void Vector::set_y(){y = mag * sin(ang);}//public methodsVector::Vector(){x = y = mag = ang = 0.0;mode = RECT;}Vector::Vector(double n1, double n2, Mode form){mode = form;if (form = RECT){x = n1;y = n2;set_mag();set_ang();}else if (form == POL){mag = n1;ang = n2 / Rad_to_deg;set_x();set_y();}else{cout << "Incorrect 3rd argument to Vector() -- ";cout << "vector set to 0\n";x = y = mag = ang = 0.0;mode = RECT;}}void Vector::reset(double n1, double n2, Mode form){mode = form;if (form == RECT){x = n1;y = n2;set_mag();set_ang();}else if (form == POL){mag = n1;ang = n2 / Rad_to_deg;set_x();set_y();}else{cout << "Incorrect 3rd argument to Vector() --";cout << "vector set to 0\n";x = y = mag = ang = 0.0;mode = RECT;}}Vector::~Vector(){}void Vector::polar_mode(){mode = POL;}void Vector::rect_mode(){mode = RECT;}Vector Vector::operator+(const Vector& b) const{return Vector(x + b.x, y + b.y);}Vector Vector::operator-(const Vector& b) const{return Vector(x - b.x, y - b.y);}Vector Vector::operator-() const{return Vector(-x, -y);}Vector Vector::operator*(double n) const{return Vector(n * x, n * y);}Vector operator*(double n, const Vector& a){return a * n;}std::ostream& operator<<(std::ostream& os, const Vector& v){if (v.mode == Vector::RECT)os << "(x, y) = (" << v.x << ", " << v.y << ")";else if (v.mode == Vector::POL){os << "(m,a) = (" << v.mag << ", "<< v.ang * Rad_to_deg << ")";}elseos << "Vector object mode is invalid";return os;} }11.15 randwalk.cpp
#include <iostream> #include <cstdlib> #include <ctime> #include "vector.h"int main() {using namespace std;using VECTOR::Vector;srand(time(0));double direction;Vector step;Vector result(0.0, 0.0);unsigned long steps = 0;double target;double dstep;cout << "Enter target distance (q to quit): ";while (cin >> target){cout << "Enter step length: ";if (!(cin >> dstep))break;while (result.magval() < target){direction = rand() % 360;step.reset(dstep, direction, Vector::POL);result = result + step;steps++;}cout << "After " << steps << " steps, the subject "<< "has the following location : \n";cout << result << endl;result.polar_mode();cout << " or\n" << result << endl;cout << "Average outward distance per step = "<< result.magval() / steps << endl;steps = 0;result.reset(0.0, 0.0);cout << "Enter target distance (q to quit): ";}cout << "Bye!\n";cin.clear();while (cin.get() != '\n')continue;system("pause");return 0; }11.16 stonewt.h
#ifndef STONEWT_H_ #define STONEWT_H_class Stonewt { private:enum{Lbs_per_stn = 14};int stone;double pds_left;double pounds;public:Stonewt(double lbs);Stonewt(int stn, double lbs);Stonewt();~Stonewt();void show_lbs() const;void show_stn() const; };#endif11.17 stonewt.cpp
#include <iostream> #include "stonewt.h" using std::cout;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; }Stonewt::Stonewt() {stone = pounds = pds_left = 0; }Stonewt::~Stonewt() {}void Stonewt::show_stn() const {cout << stone << " stone, " << pds_left << " pounds\n"; }void Stonewt::show_lbs() const {cout << pounds << " pounds\n"; }11.18 stone.cpp
#include <iostream> using std::cout; #include "stonewt.h"void display(const Stonewt& st, int n);int main() {Stonewt incognito = 275;Stonewt wolfe(285.7);Stonewt taft(21, 8);cout << "The calebrity weighed ";incognito.show_stn();cout << "The detective weighed ";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 unearned\n";return 0; }void display(const Stonewt& st, int n) {for (int i = 0; i < n; i++){cout << "Wow! ";st.show_stn();} }11.19 stonewt1.h
#ifndef STONEWT_H_ #define STONEWT_H_class Stonewt { private:enum { Lbs_per_stn = 14 };int stone;double pds_left;double pounds;public:Stonewt(double lbs);Stonewt(int stn, double lbs);Stonewt();~Stonewt();void show_lbs() const;void show_stn() const;operator int() const;operator double() const; };#endif11.20 stonewt1.cpp
#include <iostream> using std::cout; #include "stonewt1.h"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_stn() const {cout << stone << " stone, " << pds_left << " pounds\n"; }void Stonewt::show_lbs() const {cout << pounds << " pounds\n"; }Stonewt::operator int() const {return int(pounds + 0.5); }Stonewt::operator double() const {return pounds; }11.21 stone1.cpp
#include <iostream> #include "stonewt1.h"int main() {using std::cout;Stonewt poppins(9, 2.8);double p_wt = poppins;cout << "Convert to double => ";cout << "Poppins: " << p_wt << " pounds.\n";cout << "Convert to int => ";cout << "Poppins: " << int(poppins) << " pounds.\n";return 0; }總結(jié)
以上是生活随笔為你收集整理的C++primer Plus课本代码(第11章)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 数据结构与算法80道
- 下一篇: 你知道战国四大名将都有谁吗
