C++PrimerPlus学习——第十一章编程练习
生活随笔
收集整理的這篇文章主要介紹了
C++PrimerPlus学习——第十一章编程练习
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
11-1
應該是修改list11.15,當當官方店買的,難道是盜版書嗎。。。
打開file之后,操作跟cout類似
vect.cpp
#include <cmath> #include"vect.h"using namespace std;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);}Vector::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;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;} }main.cpp
#include <iostream> #include <cstdlib> #include <ctime> #include <fstream> #include "vect.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;ofstream file;file.open("C://test.txt");if (!file.is_open()) {cout << "Couldn't open the file!";exit(EXIT_FAILURE);}cout << "Enter target distance (q to quit):";while (cin >> target){cout << "Enter step length: ";if (!(cin >> dstep))break;file << "Target Distance: " << target << ", " << "Step Size: " << dstep << endl;while (result.magval() < target){direction = rand() % 360;step.reset(dstep, direction, Vector::POL);result = result + step;steps++;file << steps << ": (x, y) = (" << result.xval() << ", " << result.yval() << ")" << endl;}file << "After " << steps << " steps, the subject ""has the following location:\n";file << result << endl;result.polar_mode();file << " or\n" << result << endl;result.polar_mode();file << "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;return 0; }11-2
不儲存長度角度的話,去掉mag,ang,setmag(),setang(),其他函數也需要修改,main.cpp不變
vect.h
vect.cpp
#include <cmath> #include"vect.h"using namespace std;namespace VECTOR {const double Rad_to_deg = 45.0 / atan(1.0);Vector::Vector(){x = y = 0.0;mode = RECT;}Vector::Vector(double n1, double n2, Mode form){mode = form;if (form == RECT){x = n1;y = n2;}else if (form == POL){double mag, ang;mag = n1;ang = n2 / Rad_to_deg;x = mag * cos(ang);y = mag * sin(ang);}else{cout << "Incorrect 3rd argument to Vector() -- ";cout << "vector set to 0\n";x = y;mode = RECT;}}void Vector::reset(double n1, double n2, Mode form){mode = form;if (form == RECT){x = n1;y = n2;}else if (form == POL){x = n1* cos(n2);y = n1 * sin(n2);}else{cout << "Incorrect 3rd argument to Vector() -- ";cout << "vector set to 0\n"; x = y = 0.0;mode = RECT;}}Vector::~Vector(){}double Vector::magval() const{return sqrt(x * x + y * y);}double Vector::angval() const{ return (x == 0.0 && y == 0.0)?0.0:atan2(y, x);}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.magval() << ", " << v.angval() * Rad_to_deg << ")";}elseos << "Vector object mode is invalid";return os;} }11-3
前兩個沒變
11-4
主函數不變
mytime3.h
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 operator+(const Time& t1, const Time& t2) {Time sum;sum.minutes = t1.minutes + t2.minutes;sum.hours = t1.hours + t2.hours + sum.minutes / 60;sum.minutes %= 60;return sum; }Time operator-(const Time& t1, const Time& t2) {Time diff;int tot1, tot2;tot1 = t1.minutes + 60 * t1.hours;tot2 = t2.minutes + 60 * t2.hours;diff.minutes = (tot2 - tot1) % 60;diff.hours = (tot2 - tot1) / 60;return diff; }Time operator*(double mult, const Time& t) {Time result;long totalminutes = t.hours * mult * 60 + t.minutes * mult;result.hours = totalminutes / 60;result.minutes = totalminutes % 60;return result; } 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; }main.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; }11-5
結合前面的例子,重載運算符
stonewt.h
stonewt.cpp
#include <iostream> using std::cout; #include "stonewt.h"Stonewt::Stonewt() {stone = pounds = pds_left = 0;mode = STONE; } Stonewt::Stonewt(double lbs) {stone = int(lbs) / Lbs_per_stn;pds_left = int(lbs) % Lbs_per_stn + lbs - int(lbs);pounds = lbs;mode = FPO; } Stonewt::Stonewt(int stn, double lbs) {stone = stn;pds_left = lbs;pounds = stn * Lbs_per_stn + lbs;mode = STONE; } Stonewt::~Stonewt() {}void Stonewt::set_mode() {mode = STONE; } Stonewt Stonewt::operator+(const Stonewt& b) const {Stonewt temp;temp.pounds = pounds + b.pounds;temp.stone = int(temp.pounds) / 14;temp.pds_left = int(temp.pounds) % 14 + temp.pounds - int(temp.pounds);return temp; } Stonewt Stonewt::operator-(const Stonewt& b) const {Stonewt temp;temp.pounds = pounds - b.pounds;temp.stone = int(temp.pounds) / 14;temp.pds_left = int(temp.pounds) % 14 + temp.pounds - int(temp.pounds);return temp; } Stonewt Stonewt::operator*(double n) const {Stonewt temp;temp.pounds = pounds * n;temp.stone = int(temp.pounds) / 14;temp.pds_left = int(temp.pounds) % 14 + temp.pounds - int(temp.pounds);return temp; } Stonewt operator*(double n, const Stonewt& a) {Stonewt temp;temp.pounds = a.pounds * n;temp.stone = int(temp.pounds) / 14;temp.pds_left = int(temp.pounds) % 14 + temp.pounds - int(temp.pounds);return temp; } std::ostream& operator<<(std::ostream& os, const Stonewt& s) {if (s.mode == Stonewt::STONE)os << "weighed " << s.stone << " stone, " << s.pds_left << " pounds\n";else if (s.mode == Stonewt::FPO)os << "weighed " << s.pounds << " pounds\n";elseos << "Stone object mode is invalid";return os; }main.cpp
#include <iostream> #include "Stonewt.h" using std::cout; int main() {Stonewt wolfe(285.7);Stonewt hew(10, 15);cout << wolfe;cout << hew;wolfe.set_mode();cout << wolfe;wolfe = wolfe - hew;cout << wolfe;wolfe = wolfe + hew;cout << wolfe;wolfe = wolfe * 10;cout << wolfe;return 0; }11-6
stonewt.h
stone.cpp
#include <iostream> using std::cout; #include "stonewt.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() {}bool Stonewt::operator<(const Stonewt& t) const {return pounds < t.pounds; } bool Stonewt::operator<=(const Stonewt& t) const {return pounds <= t.pounds; } bool Stonewt::operator>(const Stonewt& t) const {return pounds > t.pounds; } bool Stonewt::operator>=(const Stonewt& t) const {return pounds >= t.pounds; } bool Stonewt::operator==(const Stonewt& t) const {return pounds == t.pounds; } bool Stonewt::operator!=(const Stonewt& t) const {return pounds != t.pounds; }main.cpp
#include <iostream> #include "Stonewt.h" using std::cout; using std::cin; using std::endl; int main() {Stonewt arr[6] = {Stonewt(11.1,0),Stonewt(10.1,1),Stonewt(12.1,2)};Stonewt temp(11, 0);for (int i = 3; i < 6; ++i){double p;cout << "Enter pounds: ";cin >> p;arr[i] = Stonewt(p);}int Max = 0;int Min = 0;int num = 0;for (int j = 0; j < 6; ++j){if (arr[Max] < arr[j])Max = j;if (arr[Min] > arr[j])Min = j;if (arr[j] >= temp)++num;}cout << "Max element: " << Max << endl;cout << "Min element : " << Min << endl;cout << "Number of elements above 11: " << num << endl;return 0; }11-7
complex0.h
complex.cpp
#include "complex0.h" #include <iostream> using namespace std;complex::complex() {re = 0;im = 0; } complex::complex(double x, double y) {re = x;im = y; }complex::~complex() {}complex complex::operator+(complex& a) {return complex(re + a.re, im + a.im); } complex complex::operator-(complex& a) {return complex(re - a.re, im - a.im); } complex complex::operator*(complex& a) {return complex(re * a.re - im * a.im, re * a.im + im * a.re); } complex operator*(double n, complex& a) {return complex(n * a.re, n * a.im); } complex operator*(complex& a, double n) {return complex(n * a.re, n * a.im); } complex complex::operator~() {return complex(re, -im); } std::ostream& operator<<(std::ostream& os, const complex& a) {os << "(" << a.re << "," << a.im << "i)";return os; } std::istream& operator>>(std::istream& is, complex& c) {cout << "real: ";is >> c.re;if (!is)return is;cout << "imaginary: ";is >> c.im;return is; }main.cpp
#include <iostream> using namespace std; #include "complex0.h"int main() {complex a(3.0, 4.0);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 << "\n";cout << "a + c is " << a + c << "\n";cout << "a - c is " << a - c << "\n";cout << "a * c is " << a * c << "\n";cout << "2 * c is " << 2 * c << "\n";cout << "Enter a complex number (q to quit):\n";};cout << "Done!\n";return 0; }總結
以上是生活随笔為你收集整理的C++PrimerPlus学习——第十一章编程练习的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c语言如何把变量按位颠倒,求答案,用C语
- 下一篇: winpe镜像文件iso下载_精品软件: