11.1
//vector.h
#ifndef VECTOR_H
#define VECTOR_H
#include<iostream>
namespace VECTOR
{
class vector
{
public:
enum Mode
{
RECT,POL
};
vector();
vector(double n1, double n2, Mode = 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();
void reset(double n1, double n2, Mode form);
vector operator*(double& n)const;
vector operator+(const vector& b)const;
vector operator-(const vector& b)const;
vector operator-()const;
friend vector operator*(double n, const vector &a);
friend std::ostream &operator<<(std::ostream &os, const vector& v);
private:
double x;
double y;
double mag;
double ang;
Mode mode;
void set_mag();
void set_ang();
void set_x();
void set_y();
};
}
#endif // !VECTOR_H
//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_todeg = 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;
else
{
ang = 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;
}
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_todeg;
set_x();
set_y();
}
else
{
cout << "error,vector set to 0\n";
x = y = mag = ang = 0.0;
mode = RECT;
}
}
vector::~vector() {};
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_todeg;
set_x();
set_y();
}
else
{
cout << "error,vector set to 0\n";
x = y = mag = ang = 0.0;
mode = RECT;
}
}
void vector:: polar_mode()
{
mode = POL;
}
void vector::rect_mode()
{
mode = RECT;
}
vector vector:: operator*(double& n)const
{
return vector(n*x, n*y);
}
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 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_todeg << ")";
else
os << "Vector object mode is invalid";
return os;
}
}
// main.cpp: 定义控制台应用程序的入口点。
//
#include<iostream>
#include<fstream>
#include<cmath>
#include<ctime>
#include"vector.h"
int main()
{
using namespace std;
using VECTOR::vector;
srand(time(0));
double direction;
ofstream outfile;
outfile.open("randwalk.txt");
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;
outfile << "Target Distance: " << target << ",step size: " << dstep << endl
<< "0: (x,y)=(0,0)\n";
while (result.magval()<target)
{
direction = rand() % 360;
step.reset(dstep, direction, vector::POL);
result = result - step;
steps++;
outfile << steps <<": "<< step<<endl;
}
outfile<< "after " << steps << " steps,the subject has the following location:\n";
outfile << result << endl;
result.polar_mode();
outfile << "or\n" << result << endl;
outfile << "average outward distance per step= "
<< result.magval() / steps << endl;
steps = 0;
result.reset(0.0,0.0,vector::RECT);
cout << "enter target distance (q to quit): ";
}
cout << "Bye!\n";
cin.clear();//若输入dstep错误cin将返回false从而导致不能继续输入
while (cin.get()!='\n')
{
continue;
}
return 0;
}
11.2
一、保留私有成员中的mag,ang定义;需将magval和angval方法中的const删去;可以直接用对象调用mag、ang值
//vector.h
#ifndef VECTOR_H
#define VECTOR_H
#include<iostream>
namespace VECTOR
{
class vector
{
public:
enum Mode
{
RECT,POL
};
vector();
vector(double n1, double n2, Mode = RECT);
~vector();
double xval()const { return x; };
double yval() const { return y; };
double magval();//对象不能为const否则不能改变mag
double angval();
void polar_mode();
void rect_mode();
void reset(double n1, double n2, Mode form);
vector operator*(double& n)const;
vector operator+(const vector& b)const;
vector operator-(const vector& b)const;
vector operator-()const;
friend vector operator*(double n, const vector &a);
friend std::ostream &operator<<(std::ostream &os, const vector& v);
private:
double x;
double y;
double mag;
double ang;
Mode mode;
};
}
#endif // !VECTOR_H
//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_todeg = 45.0 / atan(1.0);
vector::vector()
{
x = y = 0.0;
}
vector::vector(double n1, double n2, Mode form)
{
mode = form;
x = n1;
y = n2;
if(form=POL)
{
mag = magval();
ang =angval();
}
else
{
cout << "error,vector set to 0\n";
x = y = 0.0;
mode = RECT;
}
}
vector::~vector() {};
double vector:: magval()
{
mag = sqrt(x*x + y * y);
return mag;
}
double vector::angval()
{
if (x == 0.0&&y == 0.0)
ang = 0.0;
else
{
ang = atan2(y, x);
}
return ang;
}
void vector:: reset(double n1, double n2, Mode form)
{
mode = form;
x = n1;
y = n2;
if (form = POL)
{
mag = magval();
ang = angval();
}
else
{
cout << "error,vector set to 0\n";
x = y = 0.0;
mode = RECT;
}
}
void vector:: polar_mode()
{
mode = POL;
}
void vector::rect_mode()
{
mode = RECT;
}
vector vector:: operator*(double& n)const
{
return vector(n*x, n*y);
}
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 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_todeg << ")";
else
os << "Vector object mode is invalid";
return os;
}
}
// main.cpp: 定义控制台应用程序的入口点。
//
#include<iostream>
#include<cmath>
#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;
cout << "Target Distance: " << target << ",step size: " << dstep << endl
<< "0: (x,y)=(0,0)\n";
while (result.magval()<target)
{
direction = rand() % 360;
step.reset(dstep, direction, vector::POL);
result = result - step;
steps++;
cout << steps <<": "<< step<<endl;
}
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,vector::RECT);
cout << "enter target distance (q to quit): ";
}
cout << "Bye!\n";
cin.clear();//若输入dstep错误cin将返回false从而导致不能继续输入
while (cin.get()!='\n')
{
continue;
}
return 0;
}
二、不保留私有成员中的mag、ang数据定义,不需要修改magval、angval方法原型中的const;必须使用函数调用mag、ang值
//vector.h
#ifndef VECTOR_H
#define VECTOR_H
#include<iostream>
namespace VECTOR
{
class vector
{
public:
enum Mode
{
RECT,POL
};
vector();
vector(double n1, double n2, Mode = RECT);
~vector();
double xval()const { return x; };
double yval() const { return y; };
double magval()const;
double angval()const;
void polar_mode();
void rect_mode();
void reset(double n1, double n2, Mode form);
vector operator*(double& n)const;
vector operator+(const vector& b)const;
vector operator-(const vector& b)const;
vector operator-()const;
friend vector operator*(double n, const vector &a);
friend std::ostream &operator<<(std::ostream &os, const vector& v);
private:
double x;
double y;
Mode mode;
};
}
#endif // !VECTOR_H
//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_todeg = 45.0 / atan(1.0);
vector::vector()
{
x = y = 0.0;
}
vector::vector(double n1, double n2, Mode form)
{
mode = form;
x = n1;
y = n2;
if(form=POL)
{
double mag = magval();
double ang = angval();
x = mag * cos(ang);
y = mag * sin(ang);
}
else
{
cout << "error,vector set to 0\n";
x = y = 0.0;
mode = RECT;
}
}
vector::~vector() {};
double vector:: magval() const
{
double mag;
mag = sqrt(x*x + y * y);
return mag;
}
double vector::angval() const
{
double ang;
if (x == 0.0&&y == 0.0)
ang = 0.0;
else
{
ang = atan2(y, x);
}
return ang;
}
void vector:: reset(double n1, double n2, Mode form)
{
mode = form;
x = n1;
y = n2;
if (form = POL)
{
double mag = magval();
double ang = angval();
x = mag * cos(ang);
y = mag * sin(ang);
}
else
{
cout << "error,vector set to 0\n";
x = y = 0.0;
mode = RECT;
}
}
void vector:: polar_mode()
{
mode = POL;
}
void vector::rect_mode()
{
mode = RECT;
}
vector vector:: operator*(double& n)const
{
return vector(n*x, n*y);
}
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 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_todeg << ")";
else
os << "Vector object mode is invalid";
return os;
}
}
// main.cpp: 定义控制台应用程序的入口点。
//
#include<iostream>
#include<cmath>
#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;
cout << "Target Distance: " << target << ",step size: " << dstep << endl
<< "0: (x,y)=(0,0)\n";
while (result.magval()<target)
{
direction = rand() % 360;
step.reset(dstep, direction, vector::POL);
result = result - step;
steps++;
cout << steps <<": "<< step<<endl;
}
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,vector::RECT);
cout << "enter target distance (q to quit): ";
}
cout << "Bye!\n";
cin.clear();//若输入dstep错误cin将返回false从而导致不能继续输入
while (cin.get()!='\n')
{
continue;
}
return 0;
}
11.3
//vector.h
#ifndef VECTOR_H
#define VECTOR_H
#include<iostream>
namespace VECTOR
{
class vector
{
public:
enum Mode
{
RECT, POL
};
vector();
vector(double n1, double n2, Mode = 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();
void reset(double n1, double n2, Mode form);
vector operator*(double& n)const;
vector operator+(const vector& b)const;
vector operator-(const vector& b)const;
vector operator-()const;
friend vector operator*(double n, const vector &a);
friend std::ostream &operator<<(std::ostream &os, const vector& v);
private:
double x;
double y;
double mag;
double ang;
Mode mode;
void set_mag();
void set_ang();
void set_x();
void set_y();
};
}
#endif
//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_todeg = 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;
else
{
ang = 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;
}
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_todeg;
set_x();
set_y();
}
else
{
cout << "error,vector set to 0\n";
x = y = mag = ang = 0.0;
mode = RECT;
}
}
vector::~vector() {};
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_todeg;
set_x();
set_y();
}
else
{
cout << "error,vector set to 0\n";
x = y = mag = ang = 0.0;
mode = RECT;
}
}
void vector::polar_mode()
{
mode = POL;
}
void vector::rect_mode()
{
mode = RECT;
}
vector vector:: operator*(double& n)const
{
return vector(n*x, n*y);
}
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 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_todeg << ")";
else
os << "Vector object mode is invalid";
return os;
}
}
// main.cpp: 定义控制台应用程序的入口点。
//
#include<iostream>
#include<cmath>
#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;
int test_times;
unsigned long max_steps , min_steps, total_steps=0;
cout << "enter target distance (q to quit):";
while (cin>>target)
{
cout << "enter step length: ";
if (!(cin >> dstep))
break;
cout << "enter test times: ";
cin >> test_times;
for (int i = 0; i < test_times; i++)
{
while (result.magval() < target)
{
direction = rand() % 360;
step.reset(dstep, direction, vector::POL);
result = result - step;
steps++;
}
if (i ==0)
{
max_steps =min_steps = steps;
}
else
{
if (steps > max_steps)
max_steps = steps;
if (steps < min_steps)
min_steps = steps;
}
total_steps += steps;
steps = 0;
result.reset(0.0, 0.0, vector::RECT);
}
cout << "max steps= " << max_steps
<< "min steps= " << min_steps
<< "average steps= " << total_steps / test_times
<<endl;
total_steps = 0;
cout << "enter terget distance(q to quit): ";
}
cout << "Bye!\n";
cin.clear();//若输入dstep错误cin将返回false从而导致不能继续输入
while (cin.get()!='\n')
{
continue;
}
return 0;
}
11.4
//time.h
#ifndef MYTIME_h
#define MYTIME_H
#include<iostream>
class Time
{
private:
int hours;
int minutes;
public:
Time();
Time(int h, int m);
void addmin(int m);
void addhr(int h);
void reset(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, const double m);
friend Time operator*( const double m,const Time&t);
friend std::ostream &operator<<(std::ostream &os, const Time &t);
};
#endif // !MYTIME_h
#include<iostream>
#include"time.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 = 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 total1, total2;
total1 = t1.hours * 60 + t1.minutes;
total2 = t2.hours * 60 + t2.minutes;
diff.minutes = total1 - total2;
diff.hours = diff.minutes / 60;
diff.minutes = diff.minutes % 60;
return diff;
}
Time operator*(const Time&t, const double m)
{
Time mult;
mult.minutes = t.minutes*m;
mult.hours = t.hours*m + mult.minutes / 60;
mult.minutes %= 60;
return mult;
}
Time operator*(const double m, const Time&t)
{
return operator*(t, m);
}
std::ostream &operator<<(std::ostream &os, const Time &t)
{
os << t.hours << " hours," << t.minutes << " minutes";
return os;
}
// main.cpp: 定义控制台应用程序的入口点。
//
#include<iostream>
#include"time.h"
int main()
{
using std::cout;
using std::endl;
Time aida(3, 15);
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;
temp = 1.17*aida;
cout << "1.17*Aida: " << temp << endl;
temp = aida - tosca;
cout << "Aida -tosca: " << temp << endl;
return 0;
}
11.5
//stonewt.h
#ifndef STONEWT_H
#define STONEWT_H
#include<ostream>
class Stonewt
{
public:
enum Mode { stones, ipounds, dpounds };//模式的定义必须在public中,否则不能在主程序中使用状态成员
private:
enum{Lbs_per_stn=14};
int stone;
double pds_left;
double pounds;
int pound_int;
Mode mode;
public:
Stonewt(double lbs, int stn = 0, Mode mod= dpounds);
Stonewt();
~Stonewt();
void stone_mode();
void ipounds_mode();
void dpounds_mode();
Stonewt operator+(const Stonewt &s)const;
Stonewt operator-(const Stonewt &s)const;
Stonewt operator*(double n)const;
friend Stonewt operator*(double n,const Stonewt &s);
friend std::ostream &operator<<(std::ostream& os, const Stonewt &st);
};
#endif // !STONEWT_H
//stonewt.cpp
#include<iostream>
#include"stonewt.h"
Stonewt::Stonewt(double lbs, int stn , Mode mod)
{
mode = mod;//将调用对象的模式设置为传递的参数,若不设置无论是否传递Mode参数都将为默认的dpounds
if (mod == dpounds)
{
stone = int(lbs) / Lbs_per_stn;
pds_left = int(lbs) % Lbs_per_stn +lbs- int(lbs);
pounds = lbs;
pound_int = int(lbs);
}
else if (mod==ipounds)
{
pound_int = int(lbs);
pounds = lbs;
stone = int(lbs) / Lbs_per_stn;
pds_left = int(lbs) % Lbs_per_stn +lbs- int(lbs);
}
else if(mod==stones)
{
stone = stn;
pds_left = lbs;
pounds = stone * Lbs_per_stn + lbs;
pound_int = int(pounds);
}
}
Stonewt::Stonewt()
{
stone = pds_left = pounds = pound_int = 0;
}
Stonewt::~Stonewt() {};
void Stonewt::stone_mode()
{
mode = stones;
}
void Stonewt::ipounds_mode()
{
mode = ipounds;
}
void Stonewt::dpounds_mode()
{
mode = dpounds;
}
Stonewt Stonewt:: operator+(const Stonewt &s)const
{
Stonewt sum;
sum.pounds = pounds + s.pounds;
sum.pound_int = int(sum.pounds);
sum.stone = sum.pound_int / Lbs_per_stn;
sum.pds_left = sum.pound_int%Lbs_per_stn - sum.pound_int;
sum.mode = mode;
return sum;
}
Stonewt Stonewt:: operator-(const Stonewt &s)const
{
Stonewt diff;
diff.pounds = pounds - s.pounds;
diff.pound_int = int(diff.pounds);
diff.stone = diff.pound_int / Lbs_per_stn;
diff.pds_left = diff.pound_int%Lbs_per_stn +diff.pounds- diff.pound_int;
diff.mode = mode;
return diff;
}
Stonewt Stonewt::operator*(double n)const
{
Stonewt mult;
mult.pounds = n * pounds;
mult.pound_int = int(mult.pounds);
mult.stone = mult.pound_int / Lbs_per_stn;
mult.pds_left = mult.pound_int%Lbs_per_stn - mult.pound_int;
mult.mode = mode;
return mult;
}
Stonewt operator*(double n, const Stonewt &s)
{
return s * n;
}
std::ostream &operator<<(std::ostream& os, const Stonewt &st)
{
if (st.mode ==Stonewt::stones)
{
os << st.stone << "stone," << st.pds_left << "pounds\n";
}
else if(st.mode==Stonewt::dpounds)
{
os << "double pounds:" << st.pounds<<std::endl;
}
else
{
os << "int pounds: " << st.pound_int;
}
return os;
}
// main.cpp: 定义控制台应用程序的入口点。
//
#include<iostream>
#include"stonewt.h"
int main()
{
using std::cout;
using std::endl;
Stonewt s1(285.7);
Stonewt s2(275);
Stonewt s3 = Stonewt(8, 21, Stonewt::stones);//若Mode为private成员,则不能使用所定义的状态成员也就无法进行参数的传递
Stonewt sum, diff, mult;
sum = s1 + s2;
diff = s3 - s1;
mult = 2 * s1;
cout << "s1: " << s1
<< "\ns2: " << s2
<< "\ns3: " << s3
<< "\ns1+s2: " << sum
<< "\ns3-s2: " << diff
<< "\n2*s1: " << mult;
diff.dpounds_mode();
cout << "\ns3-s1(double pounds mode): " << diff;
return 0;
}
11.6
//stonewt.h
#ifndef STONEWT_H
#define STONEWT_H
#include<ostream>
class Stonewt
{
public:
enum Mode { stones, ipounds, dpounds };//模式的定义必须在public中,否则不能在主程序中使用状态成员
private:
enum{Lbs_per_stn=14};
int stone;
double pds_left;
double pounds;
int pound_int;
Mode mode;
public:
Stonewt(double lbs, int stn = 0, Mode mod= dpounds);
Stonewt();
~Stonewt();
void stone_mode();
void ipounds_mode();
void dpounds_mode();
bool operator<(const Stonewt &s)const;
bool operator<=(const Stonewt &s)const;
bool operator>(const Stonewt &s)const;
bool operator>=(const Stonewt &s)const;
bool operator==(const Stonewt &s)const;
bool operator!=(const Stonewt &s)const;
friend std::ostream &operator<<(std::ostream& os, const Stonewt &st);
};
#endif // !STONEWT_H
//stonewt.cpp
#include<iostream>
#include"stonewt.h"
Stonewt::Stonewt(double lbs, int stn , Mode mod)
{
mode = mod;//将调用对象的模式设置为传递的参数,若不设置无论是否传递Mode参数都将为默认的dpounds
if (mod == dpounds)
{
stone = int(lbs) / Lbs_per_stn;
pds_left = int(lbs) % Lbs_per_stn +lbs- int(lbs);
pounds = lbs;
pound_int = int(lbs);
}
else if (mod==ipounds)
{
pound_int = int(lbs);
pounds = lbs;
stone = int(lbs) / Lbs_per_stn;
pds_left = int(lbs) % Lbs_per_stn +lbs- int(lbs);
}
else if(mod==stones)
{
stone = stn;
pds_left = lbs;
pounds = stone * Lbs_per_stn + lbs;
pound_int = int(pounds);
}
}
Stonewt::Stonewt()
{
stone = pds_left = pounds = pound_int = 0;
}
Stonewt::~Stonewt() {};
void Stonewt::stone_mode()
{
mode = stones;
}
void Stonewt::ipounds_mode()
{
mode = ipounds;
}
void Stonewt::dpounds_mode()
{
mode = dpounds;
}
bool Stonewt:: operator<(const Stonewt &s)const
{
if (pounds < s.pounds)
return true;
else
return false;
}
bool Stonewt:: operator<=(const Stonewt &s)const
{
if (pounds <= s.pounds)
return true;
else
return false;
}
bool Stonewt::operator>(const Stonewt &s)const
{
if (pounds > s.pounds)
return true;
else
return false;
}
bool Stonewt:: operator>=(const Stonewt &s)const
{
if (pounds >= s.pounds)
return true;
else
return false;
}
bool Stonewt::operator==(const Stonewt &s)const
{
if (pounds == s.pounds)
return true;
else
return false;
}
bool Stonewt::operator!=(const Stonewt &s)const
{
if (pounds != s.pounds)
return true;
else
return false;
}
std::ostream &operator<<(std::ostream& os, const Stonewt &st)
{
if (st.mode ==Stonewt::stones)
{
os << st.stone << "stone," << st.pds_left << "pounds\n";
}
else if(st.mode==Stonewt::dpounds)
{
os << "double pounds:" << st.pounds<<std::endl;
}
else
{
os << "int pounds: " << st.pound_int;
}
return os;
}
// main.cpp: 定义控制台应用程序的入口点。
//
#include<iostream>
#include"stonewt.h"
int main()
{
using std::cout;
using std::endl;
using std::cin;
Stonewt temp(0, 11, Stonewt::stones);
Stonewt s[6] =
{
Stonewt(275),
Stonewt(260),
Stonewt(150.5)
};
double p;
cout << "enter next 3 stonewt in pounds: ";
for (int i = 0; i < 3; i++)
{
cout << "#" << i+1 << ": ";
cin >>p ;
s[3+i] = Stonewt(p);
}
Stonewt max,min;
max = min = s[1];
int not_less = 0;
for (int i = 0; i < 6; i++)
{
if (s[i] > max)
max = s[i];
if (s[i] < min)
min = s[i];
if (s[i] >= temp)
not_less++;
}
cout << "max: " << max
<< "\nmin: " << min
<< "\nnumber of not less than 11 stone: " << not_less;
return 0;
}
11.7
//complex0.h
#include<iostream>
class complex
{
public:
complex();
complex(const double a, const double b);
~complex();
complex operator+(const complex &a)const;
complex operator-(const complex &a)const;
complex operator*(const double x)const;
complex operator*(const complex &a)const;
complex operator~()const;
friend complex operator*(const double x,const complex&a);
friend std::ostream &operator<<(std::ostream&os, const complex &a);
friend std::istream &operator>>(std::istream&is, complex &a);
private:
double real_part;
double imag_part;
};
//complex0.cpp
#include<iostream>
#include"complex0.h"
complex::complex()
{
real_part = imag_part = 0.0;
}
complex::complex(const double a,const double b)
{
real_part = a;
imag_part = b;
}
complex::~complex() {};
complex complex::operator+(const complex &a)const
{
complex sum;
sum.real_part = real_part + a.real_part;
sum.imag_part = imag_part + a.imag_part;
return sum;
}
complex complex::operator-(const complex &a)const
{
complex diff;
diff.real_part = real_part - a.real_part;
diff.imag_part = imag_part - a.imag_part;
return diff;
}
complex complex::operator*(const double x)const
{
complex mult;
mult.real_part = x * real_part;
mult.imag_part = x * imag_part;
return mult;
}
complex complex::operator~()const
{
complex conj;
conj.real_part = real_part;
conj.imag_part = -imag_part;
return conj;
}
complex operator*(const double x, const complex&a)
{
return a * x;
}
complex complex:: operator*(const complex &a)const
{
complex mult;
mult.real_part = real_part * a.real_part;
mult.imag_part = imag_part * a.imag_part;
return mult;
}
std::ostream &operator<<(std::ostream &os, const complex &a)
{
os << "(" << a.real_part << "," << a.imag_part << "i)";
return os;
}
std::istream &operator>>(std::istream &is, complex &a)
{
std::cout << "real: ";
is >> a.real_part;
if (!is)
return is;//用于实现主程序中当输入q时不再提示输入虚部
std::cout << "imaginary: ";
is >> a.imag_part;
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;
}