日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++primer Plus课本代码(第11章)

發布時間:2023/12/16 c/c++ 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++primer Plus课本代码(第11章) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

第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; };#endif

11.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; };#endif

11.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; };#endif

11.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); }; #endif

11.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);}; }#endif

11.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; };#endif

11.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; };#endif

11.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; }

總結

以上是生活随笔為你收集整理的C++primer Plus课本代码(第11章)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 久久永久免费视频 | 黄片毛片一级 | 国产精品熟妇人妻g奶一区 a少妇 | 老熟妇仑乱视频一区二区 | 日韩黄色一区二区 | 不卡的日韩av | 无码人妻av一区二区三区波多野 | 国产女18毛片多18精品 | 美女啪啪网址 | 日韩精彩视频在线观看 | 日韩国产欧美综合 | 日韩网站在线观看 | 直接看av的网站 | 日本毛片在线看 | 国产a国产 | 亚洲精品2 | 欧美黑人一级 | 天天色综合av | 国产精品一区二区三区在线播放 | 色八戒av| 午夜精品久久久久久久 | 国产精品99久久久久久宅男 | 日韩三级在线免费观看 | 国产人妻互换一区二区 | 久久精品国产成人av | 精品无码av一区二区三区不卡 | 久久精品美乳 | 人人搞人人 | 欧美国产日韩在线观看 | 黄色午夜 | 琪琪色网 | 东京热一区二区三区四区 | 欧美jizzhd欧美18 | 亚洲国产精品视频一区 | 亚洲成a人片在线www | 国产 日韩 欧美 制服丝袜 | 天天操天天射天天爱 | 污视频在线观看免费 | 成人欧美一区二区三区黑人动态图 | 亚洲天堂社区 | 夜夜撸小说 | 欧美日韩999 | 91午夜精品 | 91精品国产高清一区二区三区蜜臀 | 日韩视频免费观看高清完整版在线观看 | 特级西西444www高清大胆 | 99999精品视频 | 水蜜桃久久 | 美女露隐私网站 | 成人免费视频一区二区 | 青青草原国产在线观看 | 亲子乱aⅴ一区二区三区 | 久久一视频 | 日美av | 日本一级二级视频 | 亚洲一个色| 四虎成人免费视频 | 日韩成人精品一区二区 | 精品一性一色一乱农村 | 2024国产精品视频 | 日本欧美在线播放 | 国产最爽的乱淫视频国语对白 | 中文在线第一页 | 国产精品久久视频 | av网页在线 | 国产精品免费无码 | 国产成人欧美 | 久久精品高清 | 四虎永久免费 | 亚洲裸体视频 | 亚洲成人中文字幕 | 欧美男同又粗又长又大 | av免费片 | 五月天婷婷视频 | 久久综合九色综合欧美狠狠 | 一级特黄aa| 91国偷自产中文字幕久久 | 中文字幕有码在线观看 | 少妇肥臀大白屁股高清 | 国产偷自拍| 久久影院一区 | 可以看黄色的网站 | 新91在线 | 欧美日韩一区二区三区在线播放 | 日韩精品视频在线观看网站 | 成人综合av | 黄色网页免费观看 | 欧洲女同同性吃奶 | 本站只有精品 | 午夜精品久久久久久久第一页按摩 | 中文字幕在线观看视频免费 | 免费看黄在线观看 | 国产在线看 | 亚洲伦理网站 | 先锋影音av在线资源 | 午夜av一区二区三区 | 误杀1电影免费观看高清完整版 | 久热热 | 欧美中文字幕 |