// vect.h -- Vector class with <<, mode state#ifndefVECTOR_H_#defineVECTOR_H_#include<iostream>namespace VECTOR
{classVector{public:enumMode{RECT, POL};// RECT for rectangular, POL for Polar modesprivate:double x;// horizontal valuedouble y;// vertical valuedouble mag;// length of vectordouble ang;// direction of vector in degrees
Mode mode;// RECT or POL// private methods for setting valuesvoidset_mag();voidset_ang();voidset_x();voidset_y();public:Vector();Vector(double n1,double n2, Mode form = RECT);voidreset(double n1,double n2, Mode form = RECT);~Vector();doublexval()const{return x;}// report x valuedoubleyval()const{return y;}// report y valuedoublemagval()const{return mag;}// report magnitudedoubleangval()const{return ang;}// report anglevoidpolar_mode();// set mode to POLvoidrect_mode();// set mode to RECT// operator overloading
Vector operator+(const Vector & b)const;
Vector operator-(const Vector & b)const;
Vector operator-()const;
Vector operator*(double n)const;// friendsfriend Vector operator*(double n,const Vector & a);friend std::ostream &operator<<(std::ostream & os,const Vector & v);operatordouble()const{return mag;}//转换函数};}// end namespace VECTOR#endif
exercise01_vect.cpp
// vect.cpp -- methods for the Vector class#include<cmath>#include"vect.h"// includes <iostream>using std::sqrt;using std::sin;using std::cos;using std::atan;using std::atan2;using std::cout;namespace VECTOR
{// compute degrees in one radianconstdouble Rad_to_deg =45.0/atan(1.0);// should be about 57.2957795130823// private methods// calculates magnitude from x and yvoidVector::set_mag(){
mag =sqrt(x * x + y * y);}voidVector::set_ang(){if(x ==0.0&& y ==0.0)
ang =0.0;else
ang =atan2(y, x);}// set x from polar coordinatevoidVector::set_x(){
x = mag *cos(ang);}// set y from polar coordinatevoidVector::set_y(){
y = mag *sin(ang);}// public methodsVector::Vector()// default constructor{
x = y = mag = ang =0.0;
mode = RECT;}// construct vector from rectangular coordinates if form is r// (the default) or else from polar coordinates if form is pVector::Vector(double n1,double n2, Mode form){
mode = form;if(form == RECT){
x = n1;
y = n2;set_mag();set_ang();}elseif(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;}}// reset vector from rectangular coordinates if form is// RECT (the default) or else from polar coordinates if// form is POLvoidVector::reset(double n1,double n2, Mode form){
mode = form;if(form == RECT){
x = n1;
y = n2;set_mag();set_ang();}elseif(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()// destructor{}voidVector::polar_mode()// set to polar mode{
mode = POL;}voidVector::rect_mode()// set to rectangular mode{
mode = RECT;}// operator overloading// add two Vectors
Vector Vector::operator+(const Vector & b)const{returnVector(x + b.x, y + b.y);}// subtract Vector b from a
Vector Vector::operator-(const Vector & b)const{returnVector(x - b.x, y - b.y);}// reverse sign of Vector
Vector Vector::operator-()const{returnVector(-x,-y);}// multiply vector by n
Vector Vector::operator*(double n)const{returnVector(n * x, n * y);}// friend methods// multiply n by Vector a
Vector operator*(double n,const Vector & a){return a * n;}// display rectangular coordinates if mode is RECT,// else display polar coordinates if mode is POL
std::ostream &operator<<(std::ostream & os,const Vector & v){if(v.mode == Vector::RECT)
os <<"(x,y) = ("<< v.x <<", "<< v.y <<")";elseif(v.mode == Vector::POL){
os <<"(m,a) = ("<< v.mag <<", "<< v.ang * Rad_to_deg <<")";}else
os <<"Vector object mode is invalid";return os;}}// end namespace VECTOR
exercise01_randwalk.cpp
// randwalk.cpp -- using the Vector class// compile with the vect.cpp file#include<iostream>#include<cstdlib>// rand(), srand() prototypes#include<ctime>// time() prototype#include"vect.h"#include<fstream>intmain(){usingnamespace std;using VECTOR::Vector;srand(time(0));// seed random-number generatordouble direction;
Vector step;
Vector result(0.0,0.0);unsignedlong steps =0;double target;double dstep;//写入文件
ofstream ofs("randwalk.txt", ios::out);
cout <<"Enter target distance (q to quit): ";while(cin >> target){
cout <<"Enter step length: ";if(!(cin >> dstep))break;else
ofs <<"Target Distance: "<< target
<<", Step Size: "<< dstep << endl;while(result.magval()< target){
direction =rand()%360;
step.reset(dstep, direction, Vector::POL);
result = result + step;
steps++;
ofs << steps <<":(x,y) = ("<< result.xval()<<","<< result.yval()<<")"<< endl;}
cout <<"After "<< steps <<" steps, the subject ""has the following location:\n";
cout << result << endl;
ofs <<"After "<< steps <<" steps, the subject ""has the following location:\n";//重载了 “<<”,可以直接按照格式写到文件中,而不用写成下边的形式
ofs << result << endl;/*ofs << "(x,y) = (" << result.xval() << ","
<< result.yval() << ")" << endl;*/
result.polar_mode();
cout <<" or\n"<< result << endl;
cout <<"Average outward distance per step = "<< result.magval()/steps << endl;//重载了 “<<”,可以直接按照格式写到文件中,而不用写成下边的形式
ofs <<" or\n"<< result << endl;/*ofs << " or\n" << "(m,a) = (" << result.magval() << ","
<< result.angval() << ")" << endl;*/
ofs <<"Average outward distance per step = "<< result.magval()/ steps << endl;
steps =0;
result.reset(0.0,0.0);
cout <<"Enter target distance (q to quit): ";
ofs << endl;}
cout <<"Bye!\n";
ofs.close();/* keep window open
cin.clear();
while (cin.get() != '\n')
continue;
cin.get();
*/return0;}
2
vect2.h
// vect2.h -- Vector class with <<, mode state#ifndefVECTOR_H_#defineVECTOR_H_#include<iostream>namespace VECTOR
{classVector{public:enumMode{RECT, POL};// RECT for rectangular, POL for Polar modesprivate:double x;// horizontal valuedouble y;// vertical value
Mode mode;// RECT or POL// private methods for setting valuesvoidset_x(double mag,double ang);voidset_y(double mag,double ang);public:Vector();Vector(double n1,double n2, Mode form = RECT);voidreset(double n1,double n2, Mode form = RECT);~Vector();doublexval()const{return x;}// report x valuedoubleyval()const{return y;}// report y valuedoublemagval()const;// report magnitudedoubleangval()const;// report anglevoidpolar_mode();// set mode to POLvoidrect_mode();// set mode to RECT// operator overloading
Vector operator+(const Vector & b)const;
Vector operator-(const Vector & b)const;
Vector operator-()const;
Vector operator*(double n)const;// friendsfriend Vector operator*(double n,const Vector & a);friend std::ostream &operator<<(std::ostream & os,const Vector & v);};}// end namespace VECTOR#endif
exercise02_vect2.cpp
// vect2.cpp -- methods for the Vector class#include<cmath>#include"vect2.h"// includes <iostream>using std::sqrt;using std::sin;using std::cos;using std::atan;using std::atan2;using std::cout;namespace VECTOR
{// compute degrees in one radianconstdouble Rad_to_deg =45.0/atan(1.0);// should be about 57.2957795130823// private methods// set x from polar coordinatevoidVector::set_x(double mag,double ang){
x = mag *cos(ang);}// set y from polar coordinatevoidVector::set_y(double mag,double ang){
y = mag *sin(ang);}// report magnitudedoubleVector::magval()const{returnsqrt(x * x + y * y);}// report angledoubleVector::angval()const{if(x ==0.0&& y ==0.0)return0.0;elsereturnatan2(y, x);}// public methodsVector::Vector()// default constructor{
x = y =0.0;
mode = RECT;}// construct vector from rectangular coordinates if form is r// (the default) or else from polar coordinates if form is pVector::Vector(double n1,double n2, Mode form){
mode = form;if(form == RECT){
x = n1;
y = n2;magval();angval();}elseif(form == POL){double mag, ang;
mag = n1;
ang = n2 / Rad_to_deg;set_x(mag, ang);set_y(mag, ang);}else{
cout <<"Incorrect 3rd argument to Vector() -- ";
cout <<"vector set to 0\n";
x = y =0.0;
mode = RECT;}}// reset vector from rectangular coordinates if form is// RECT (the default) or else from polar coordinates if// form is POLvoidVector::reset(double n1,double n2, Mode form){
mode = form;if(form == RECT){
x = n1;
y = n2;magval();angval();}elseif(form == POL){double mag, ang;
mag = n1;
ang = n2 / Rad_to_deg;set_x(mag, ang);set_y(mag, ang);}else{
cout <<"Incorrect 3rd argument to Vector() -- ";
cout <<"vector set to 0\n";
x = y =0.0;
mode = RECT;}}Vector::~Vector()// destructor{}voidVector::polar_mode()// set to polar mode{
mode = POL;}voidVector::rect_mode()// set to rectangular mode{
mode = RECT;}// operator overloading// add two Vectors
Vector Vector::operator+(const Vector & b)const{returnVector(x + b.x, y + b.y);}// subtract Vector b from a
Vector Vector::operator-(const Vector & b)const{returnVector(x - b.x, y - b.y);}// reverse sign of Vector
Vector Vector::operator-()const{returnVector(-x,-y);}// multiply vector by n
Vector Vector::operator*(double n)const{returnVector(n * x, n * y);}// friend methods// multiply n by Vector a
Vector operator*(double n,const Vector & a){return a * n;}// display rectangular coordinates if mode is RECT,// else display polar coordinates if mode is POL
std::ostream &operator<<(std::ostream & os,const Vector & v){if(v.mode == Vector::RECT)
os <<"(x,y) = ("<< v.x <<", "<< v.y <<")";elseif(v.mode == Vector::POL){
os <<"(m,a) = ("<< v.magval()<<", "<< v.angval()* Rad_to_deg <<")";}else
os <<"Vector object mode is invalid";return os;}}// end namespace VECTOR
exercise02_randwalk2.cpp
// randwalk.cpp -- using the Vector class// compile with the vect.cpp file#include<iostream>#include<cstdlib>// rand(), srand() prototypes#include<ctime>// time() prototype#include"vect2.h"#include<fstream>intmain(){usingnamespace std;using VECTOR::Vector;srand(time(0));// seed random-number generatordouble direction;
Vector step;
Vector result(0.0,0.0);unsignedlong steps =0;double target;double dstep;//写入文件
ofstream ofs("randwalk2.txt", ios::out);
cout <<"Enter target distance (q to quit): ";while(cin >> target){
cout <<"Enter step length: ";if(!(cin >> dstep))break;else
ofs <<"Target Distance: "<< target
<<", Step Size: "<< dstep << endl;while(result.magval()< target){
direction =rand()%360;
step.reset(dstep, direction, Vector::POL);
result = result + step;
steps++;
ofs << steps <<":(x,y) = ("<< result.xval()<<","<< result.yval()<<")"<< endl;}
cout <<"After "<< steps <<" steps, the subject ""has the following location:\n";
cout << result << endl;
ofs <<"After "<< steps <<" steps, the subject ""has the following location:\n";//重载了 “<<”,可以直接按照格式写到文件中,而不用写成下边的形式
ofs << result << endl;
result.polar_mode();
cout <<" or\n"<< result << endl;
cout <<"Average outward distance per step = "<< result.magval()/steps << endl;//重载了 “<<”,可以直接按照格式写到文件中,而不用写成下边的形式
ofs <<" or\n"<< result << endl;
ofs <<"Average outward distance per step = "<< result.magval()/ steps << endl;
steps =0;
result.reset(0.0,0.0);
cout <<"Enter target distance (q to quit): ";
ofs << endl;}
cout <<"Bye!\n";
ofs.close();/* keep window open
cin.clear();
while (cin.get() != '\n')
continue;
cin.get();
*/return0;}
3
vect.h
// vect.h -- Vector class with <<, mode state#ifndefVECTOR_H_#defineVECTOR_H_#include<iostream>namespace VECTOR
{classVector{public:enumMode{RECT, POL};// RECT for rectangular, POL for Polar modesprivate:double x;// horizontal valuedouble y;// vertical valuedouble mag;// length of vectordouble ang;// direction of vector in degrees
Mode mode;// RECT or POL// private methods for setting valuesvoidset_mag();voidset_ang();voidset_x();voidset_y();public:Vector();Vector(double n1,double n2, Mode form = RECT);voidreset(double n1,double n2, Mode form = RECT);~Vector();doublexval()const{return x;}// report x valuedoubleyval()const{return y;}// report y valuedoublemagval()const{return mag;}// report magnitudedoubleangval()const{return ang;}// report anglevoidpolar_mode();// set mode to POLvoidrect_mode();// set mode to RECT// operator overloading
Vector operator+(const Vector & b)const;
Vector operator-(const Vector & b)const;
Vector operator-()const;
Vector operator*(double n)const;// friendsfriend Vector operator*(double n,const Vector & a);friend std::ostream &operator<<(std::ostream & os,const Vector & v);operatordouble()const{return mag;}//转换函数};}// end namespace VECTOR#endif
exercise03_vect3.cpp
// vect.cpp -- methods for the Vector class#include<cmath>#include"vect.h"// includes <iostream>using std::sqrt;using std::sin;using std::cos;using std::atan;using std::atan2;using std::cout;namespace VECTOR
{// compute degrees in one radianconstdouble Rad_to_deg =45.0/atan(1.0);// should be about 57.2957795130823// private methods// calculates magnitude from x and yvoidVector::set_mag(){
mag =sqrt(x * x + y * y);}voidVector::set_ang(){if(x ==0.0&& y ==0.0)
ang =0.0;else
ang =atan2(y, x);}// set x from polar coordinatevoidVector::set_x(){
x = mag *cos(ang);}// set y from polar coordinatevoidVector::set_y(){
y = mag *sin(ang);}// public methodsVector::Vector()// default constructor{
x = y = mag = ang =0.0;
mode = RECT;}// construct vector from rectangular coordinates if form is r// (the default) or else from polar coordinates if form is pVector::Vector(double n1,double n2, Mode form){
mode = form;if(form == RECT){
x = n1;
y = n2;set_mag();set_ang();}elseif(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;}}// reset vector from rectangular coordinates if form is// RECT (the default) or else from polar coordinates if// form is POLvoidVector::reset(double n1,double n2, Mode form){
mode = form;if(form == RECT){
x = n1;
y = n2;set_mag();set_ang();}elseif(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()// destructor{}voidVector::polar_mode()// set to polar mode{
mode = POL;}voidVector::rect_mode()// set to rectangular mode{
mode = RECT;}// operator overloading// add two Vectors
Vector Vector::operator+(const Vector & b)const{returnVector(x + b.x, y + b.y);}// subtract Vector b from a
Vector Vector::operator-(const Vector & b)const{returnVector(x - b.x, y - b.y);}// reverse sign of Vector
Vector Vector::operator-()const{returnVector(-x,-y);}// multiply vector by n
Vector Vector::operator*(double n)const{returnVector(n * x, n * y);}// friend methods// multiply n by Vector a
Vector operator*(double n,const Vector & a){return a * n;}// display rectangular coordinates if mode is RECT,// else display polar coordinates if mode is POL
std::ostream &operator<<(std::ostream & os,const Vector & v){if(v.mode == Vector::RECT)
os <<"(x,y) = ("<< v.x <<", "<< v.y <<")";elseif(v.mode == Vector::POL){
os <<"(m,a) = ("<< v.mag <<", "<< v.ang * Rad_to_deg <<")";}else
os <<"Vector object mode is invalid";return os;}}// end namespace VECTOR
exercise03_randwalk3.cpp
// randwalk.cpp -- using the Vector class// compile with the vect.cpp file#include<iostream>#include<cstdlib>// rand(), srand() prototypes#include<ctime>// time() prototype#include"vect.h"#include<fstream>intmain(){usingnamespace std;using VECTOR::Vector;srand(time(0));// seed random-number generatordouble direction;
Vector step;
Vector result(0.0,0.0);unsignedlong steps =0;double target;double dstep;int totalstep =0;int minstep = INT_MAX -1;int maxstep =0;int n;//写入文件
ofstream ofs("randwalk3.txt", ios::out);
cout <<"Enter target distance (q to quit): ";while(cin >> target){
cout <<"Enter step length: ";if(!(cin >> dstep))break;else
ofs <<"Target Distance: "<< target
<<", Step Size: "<< dstep << endl;
cout <<"Enter times of the test: ";
cin >> n;
ofs <<"Times of the test: "<< n << endl;
totalstep =0;for(int i =0; i < n; i++){while(result.magval()< target){
direction =rand()%360;
step.reset(dstep, direction, Vector::POL);
result = result + step;
steps++;}
totalstep += steps;
minstep = minstep < steps ? minstep : steps;
maxstep = maxstep > steps ? maxstep : steps;
steps =0;
result.reset(0.0,0.0);}
cout <<"totalstep: "<< totalstep <<"\n"<<"maxstep: "<< maxstep <<"\n"<<"minstep: "<< minstep <<"\n"<<"average: "<<double(totalstep)/double(n)<< endl;
ofs <<"totalstep: "<< totalstep <<"\t"<<"maxstep: "<< maxstep <<"\t"<<"minstep: "<< minstep <<"\t"<<"average: "<<double(totalstep)/double(n)<< endl;
ofs << endl;
cout <<"Enter target distance (q to quit): ";}
cout <<"Bye!\n";
ofs.close();/* keep window open
cin.clear();
while (cin.get() != '\n')
continue;
cin.get();
*/return0;}
4
mytime4.h
// mytime4.h -- Time class with friends#ifndefMYTIME3_H_#defineMYTIME3_H_#include<iostream>classTime{private:int hours;int minutes;public:Time();Time(int h,int m =0);voidAddMin(int m);voidAddHr(int h);voidReset(int h =0,int m =0);friend Time operator+(const Time& t1,const Time t2);friend Time operator-(const Time& t1,const Time t2);friend Time operator*(const Time& t,double n);friend Time operator*(double m,const Time& t){return t * m;}// inline definitionfriend std::ostream &operator<<(std::ostream & os,const Time & t);};#endif
exercise04_mytime4.cpp
// mytime3.cpp -- implementing Time methods#include"mytime4.h"Time::Time(){
hours = minutes =0;}Time::Time(int h,int m ){
hours = h;
minutes = m;}voidTime::AddMin(int m){
minutes += m;
hours += minutes /60;
minutes %=60;}voidTime::AddHr(int h){
hours += h;}voidTime::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*(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;}
exercise04_usetime4.cpp
//usetime3.cpp -- using the fourth draft of the Time class// compile usetime3.cpp and mytime3.cpp together#include<iostream>#include"mytime4.h"intmain(){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;// operator+()
cout <<"Aida + Tosca: "<< temp << endl;
temp = aida*1.17;// member operator*()
cout <<"Aida * 1.17: "<< temp << endl;
cout <<"10.0 * Tosca: "<<10.0* tosca << endl;// std::cin.get();return0;}
5
stonewt2.h
// stonewt2.h -- definition for the Stonewt class#include<iostream>#ifndefSTONEWT_H_#defineSTONEWT_H_classStonewt{public:enumMode{ STONE, INTPOUND, FLOATPOUND };private:enum{Lbs_per_stn =14};// pounds per stoneint stone;// whole stonesdouble pds_left;// fractional poundsdouble pounds;// entire weight in pounds
Mode mode1;int mode2;public:Stonewt(double lbs);// constructor for double poundsStonewt(int stn,double lbs);// constructor for stone, lbsStonewt();// default constructor~Stonewt();voidsetmode(int a);
Stonewt operator+(const Stonewt& s)const;
Stonewt operator-(const Stonewt& s)const;
Stonewt operator*(double d)const;friend Stonewt operator*(double d,const Stonewt& s){return s * d;}friend std::ostream&operator<<(std::ostream& os,const Stonewt& s);};#endif
#include<iostream>#include"complex0.h"usingnamespace std;intmain(){
complex c1;
cout <<"c1: "<< c1;
complex c2(3,4);
cout <<"c2: "<< c2;
cout <<"------------------\n";
complex a(3.0,4.0);
complex c;
cout <<"Enter a complex number (q to quit):\n";while(cin >> c){
cout <<"c is "<< c;
cout <<"complex conjugate is "<<~c;
cout <<"a is "<< a;
cout <<"a + c is "<< a + c;
cout <<"a - c is "<< a - c;
cout <<"a * c is "<< a * c;
cout <<"2 * c is "<<2* c;
cout <<"Enter a complex number (q to quit): \n";}
cout <<"Done!\n";return0;}