CPP_Basic_Code_P11.1-PP11.9.7
// The Notes Created by Z-Tech on 2017/2/17.
// All Codes Boot on 《C++ Primer Plus》V6.0
// OS:MacOS 10.12.4
// Translater:clang/llvm8.0.0 &g++4.2.1
// Editer:iTerm 2&Sublime text 3
// IDE: Xcode8.2.1&Clion2017.1
//P11.1-P11.3
Z_Head.h
#ifndef XXX_H
#define XXX_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
SubFunctions.cpp
#include <iostream>
#include "Z_Head.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";
}
Main.cpp
#include <iostream>
#include "Z_Head.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;
}
//P11.4-P11.6
Z_Head.h
#ifndef XXX_H
#define XXX_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
SubFunctions.cpp
#include <iostream>
#include "Z_Head.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";
}
Main.cpp
#include <iostream>
#include "Z_Head.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;
}
//P11.7-P11.
Z_Head.h
#ifndef XXX_H
#define XXX_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
SubFunctions.cpp
#include <iostream>
#include "Z_Head.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 n) const
{
Time result;
long totalminutes=hours*n*60+minutes*n;
result.hours=totalminutes/60;
result.minutes=totalminutes%60;
return result;
}
void Time::Show() const
{
std::cout<<hours<<" hours, "<<minutes<<" minutes";
}
Main.cpp
#include <iostream>
#include "Z_Head.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-waxing time= ";
diff.Show();
cout<<endl;
adjusted=total*1.5;//运算符*的重载
cout<<"Adjusted work time= ";
adjusted.Show();
cout<<endl;
return 0;
}
//P11.10-P11.12
Z_Head.h
#ifndef XXX_H
#define XXX_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;
friend Time operator*(double m,const Time& t){return t*m;}//此处内联函数
friend std::ostream& operator<<(std::ostream& os,const Time& t);//友元函数声明
};
#endif
SubFunctions.cpp
#include <iostream>
#include "Z_Head.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 n) const
{
Time result;
long totalminutes=hours*n*60+minutes*n;
result.hours=totalminutes/60;
result.minutes=totalminutes%60;
return result;
}
std::ostream& operator<<(std::ostream& os,const Time& t)//友元函数定义不需要friend,原型才需要
{
os<<t.hours<<" hours,"<<t.minutes<<" minutes.";
return os;//os引用为cout的别名
}
Main.cpp
#include <iostream>
#include "Z_Head.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;
}
//P11.13-P11.15
Z_Head.h
#ifndef XXX_H
#define XXX_H
#include <iostream>
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 xva1() const {return x;}
double yxva1() 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
SubFunctions.cpp
//#include <iostream>
#include <cmath>
#include "Z_Head.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);//1弧度=57.29°
//以下是私有函数
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坐标
{
x=mag*cos(ang);
}
void Vector::set_y()//由位移求y坐标
{
y=mag*sin(ang);
}
//以下是公有函数
Vector::Vector()//构造函数
{
x=y=ang=mag=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(x*n,y*n);
}
//以下是友元函数
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<<")";
}
else
os<<"Vector object mode is invaild";
return os;
}
}
Main.cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "Z_Head.h"
int main()
{
using namespace std;
using VECTOR::Vector;
srand(time(0));//用0作地址可省略time_t变量声明
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;
return 0;
//P11.16-P11.18
Z_Head.h
#ifndef XXX_H
#define XXX_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
SubFunctions.cpp
#include <iostream>
#include "Z_Head.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+lbs;
}
Stonewt::Stonewt()
{
stone = pds_left = pounds = 0;
}
Stonewt::~Stonewt()
{
}
void Stonewt::show_lbs() const
{
cout<<stone<<" stone, "<<pds_left<<" pounds\n";
}
void Stonewt::show_stn() const
{
cout<<pounds<<" pounds\n";
}
Main.cpp
#include <iostream>
#include "Z_Head.h"
using std::cout;
void display(const Stonewt & st,int n);
int main()
{
Stonewt incognito = 275;//使用构造函数初始化
Stonewt wolfe(285.7);
Stonewt taft(21,8);
cout<<"The celebrity 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();
}
}
//P11.19-P11.21
Z_Head.h
#ifndef XXX_H
#define XXX_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
SubFunctions.cpp
#include <iostream>
#include "Z_Head.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+lbs;
}
Stonewt::Stonewt()
{
stone = pds_left = pounds = 0;
}
Stonewt::~Stonewt()
{
}
void Stonewt::show_lbs() const
{
cout<<stone<<" stone, "<<pds_left<<" pounds\n";
}
void Stonewt::show_stn() const
{
cout<<pounds<<" pounds\n";
}
Stonewt::operator int() const
{
return int(pounds + 0.5);
}
Stonewt::operator double()const
{
return pounds;
}
main.cpp
#include <iostream>
#include "Z_Head.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;
}
//PP11.9.1
Z_Head.h
#ifndef XXX_H
#define XXX_H
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <cmath>
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 xva1() const {return x;}
double yxva1() 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
SubFunctions.cpp
#include "Z_Head.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);//1弧度=57.29°
//以下是私有函数
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坐标
{
x=mag*cos(ang);
}
void Vector::set_y()//由位移求y坐标
{
y=mag*sin(ang);
}
//以下是公有函数
Vector::Vector()//构造函数
{
x=y=ang=mag=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(x*n,y*n);
}
//以下是友元函数
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<<")";
}
else
os<<"Vector object mode is invaild";
return os;
}
}
Main.cpp
#include "Z_Head.h"
int main()
{
using namespace std;
using VECTOR::Vector;
srand(time(0));//用0作地址可省略time_t变量声明
double direction;
Vector step;
Vector result(0.0, 0.0);
unsigned long steps = 0;
double target;
double dstep;
ofstream fout;//输出流对象
fout.open("123Fuck.txt");//关联对象和文件
cout << "Enter target distance (q to quit): ";
while (cin >> target) {
cout << "Enter step length: ";
if (!(cin >> dstep))
break;
fout<<"Target Distance: "<<target<<",Step Size: "<<dstep<<endl;
while (result.magval() < target)//实际位移小于目标距离
{
fout<<steps<<": "<<result<<endl;//第二个result是类对象,<<被重载
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;
fout << "After " << steps << " steps,the subject has the following location:\n";
fout << result << endl;
result.polar_mode();//设为极坐标模式
cout << " or\n" << result << endl;
cout << "Average outward distance per step= " << result.magval() / steps << endl;
fout << " or\n" << result << endl;
fout << "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;
}
//PP11.9.2
Z_Head.h
#ifndef XXX_H
#define XXX_H
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <cmath>
namespace VECTOR
{
class Vector
{
public:
enum Mode{RECT,POL};
private:
double x;
double y;
Mode mode;
//私有函数
double set_mag() const;
double set_ang() const;
void set_x(double mag,double ang);
void set_y(double mag,double ang);
public:
Vector();
Vector(double n1,double n2,Mode form=RECT);
void reset(double n1,double n2,Mode form=RECT);
~Vector();
//以下是内联函数
double xva1() const {return x;}
double yxva1() const {return y;}
double magval() const {return set_mag();}
double angval() const { return set_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
SubFunctions.cpp
#include "Z_Head.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);//1弧度=57.29°
//以下是私有函数
double Vector::set_mag() const//求位移
{
return sqrt(x*x+y*y);
}
double Vector::set_ang() const//根据坐标求角度
{
if (x==0.0&&y==0.0)
return 0.0;
else
return atan2(y,x);
}
void Vector::set_x(double mag,double ang)//引入参数,由位移求x坐标
{
x=mag*cos(ang);
}
void Vector::set_y(double mag,double ang)//由位移求y坐标
{
y=mag*sin(ang);
}
//以下是公有函数
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)
{
set_x(n1,n2/Rad_to_deg);
set_y(n1,n2/Rad_to_deg);
}
else
{
cout<<"Incorrect 3rd argument to Vector() -- ";
cout<<"vector set to 0\n";
x=y=0.0;
mode=RECT;
}
}
void Vector::reset(double n1,double n2,Mode form)//重置函数
{
mode=form;
if (form==RECT)
{
x=n1;
y=n2;
}
else if (form==POL)
{
set_x(n1,n2/Rad_to_deg);
set_y(n1,n2/Rad_to_deg);
}
else
{
cout<<"Incorrect 3rd argument to Vector() -- ";
cout<<"vector set to 0\n";
x=y=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(x*n,y*n);
}
//以下是友元函数
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.set_mag()<<","<<v.set_ang()*Rad_to_deg<<")";
}
else
os<<"Vector object mode is invaild";
return os;
}
}
main.cpp
#include "Z_Head.h"
int main()
{
using namespace std;
using VECTOR::Vector;
srand(time(0));//用0作地址可省略time_t变量声明
double direction;
Vector step;
Vector result(0.0, 0.0);
unsigned long steps = 0;
double target;
double dstep;
ofstream fout;//输出流对象
fout.open("123Fuck.txt");//关联对象和文件
cout << "Enter target distance (q to quit): ";
while (cin >> target)
{
cout << "Enter step length: ";
if (!(cin >> dstep))
break;
fout<<"Target Distance: "<<target<<",Step Size: "<<dstep<<endl;
while (result.magval() < target)//实际位移小于目标距离
{
fout<<steps<<": "<<result<<endl;//第二个result是类对象,<<被重载
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;
fout << "After " << steps << " steps,the subject has the following location:\n";
fout << result << endl;
result.polar_mode();//设为极坐标模式
cout << " or\n" << result << endl;
cout << "Average outward distance per step= " << result.magval() / steps << endl;
fout << " or\n" << result << endl;
fout << "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;
}
//PP11.9.3
Z_Head.h
#ifndef XXX_H
#define XXX_H
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <cmath>
namespace VECTOR
{
class Vector
{
public:
enum Mode{RECT,POL};
private:
double x;
double y;
Mode mode;
//私有函数
double set_mag() const;
double set_ang() const;
void set_x(double mag,double ang);
void set_y(double mag,double ang);
public:
Vector();
Vector(double n1,double n2,Mode form=RECT);
void reset(double n1,double n2,Mode form=RECT);
~Vector();
//以下是内联函数
double xva1() const {return x;}
double yxva1() const {return y;}
double magval() const {return set_mag();}
double angval() const { return set_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
SubFunctions.cpp
#include "Z_Head.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);//1弧度=57.29°
//以下是私有函数
double Vector::set_mag() const//求位移
{
return sqrt(x*x+y*y);
}
double Vector::set_ang() const//根据坐标求角度
{
if (x==0.0&&y==0.0)
return 0.0;
else
return atan2(y,x);
}
void Vector::set_x(double mag,double ang)//引入参数,由位移求x坐标
{
x=mag*cos(ang);
}
void Vector::set_y(double mag,double ang)//由位移求y坐标
{
y=mag*sin(ang);
}
//以下是公有函数
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)
{
set_x(n1,n2/Rad_to_deg);
set_y(n1,n2/Rad_to_deg);
}
else
{
cout<<"Incorrect 3rd argument to Vector() -- ";
cout<<"vector set to 0\n";
x=y=0.0;
mode=RECT;
}
}
void Vector::reset(double n1,double n2,Mode form)//重置函数
{
mode=form;
if (form==RECT)
{
x=n1;
y=n2;
}
else if (form==POL)
{
set_x(n1,n2/Rad_to_deg);
set_y(n1,n2/Rad_to_deg);
}
else
{
cout<<"Incorrect 3rd argument to Vector() -- ";
cout<<"vector set to 0\n";
x=y=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(x*n,y*n);
}
//以下是友元函数
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.set_mag()<<","<<v.set_ang()*Rad_to_deg<<")";
}
else
os<<"Vector object mode is invaild";
return os;
}
}
Main.cpp
#include "Z_Head.h"
int main()
{
using namespace std;
using VECTOR::Vector;
srand(time(0));//用0作地址可省略time_t变量声明
double direction;
Vector step;
Vector result(0.0, 0.0);
unsigned long steps = 0;
double target;
double dstep;
double Max,Min,Average,Sum;
int numbers,N;
cout<<"Please enter numbers: ";
cin>>numbers;
N=numbers;
Max=Min=Average=Sum=0;
ofstream fout;//输出流对象
fout.open("123Fuck.txt");//关联对象和文件
cout << "Enter target distance: ";
cin >> target;
cout << "Enter step length: ";
cin >> dstep;
while (numbers)
{
fout<<"Target Distance: "<<target<<",Step Size: "<<dstep<<endl;
while (result.magval() < target)//实际位移小于目标距离
{
fout<<steps<<": "<<result<<endl;//第二个result是类对象,<<被重载
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;
fout << "After " << steps << " steps,the subject has the following location:\n";
fout << result << endl;
result.polar_mode();//设为极坐标模式
cout << " or\n" << result << endl;
cout << "Average outward distance per step= " << result.magval() / steps << endl;
fout << " or\n" << result << endl;
fout << "Average outward distance per step= " << result.magval() / steps << endl;
if (Min == 0 || Max == 0)
Min = Max = steps;//第一次初始化为第一次步数
if (Min > steps)//关键是和下一次循环的步数进行比较
Min = steps;
if (Max < steps)//获取最大值
Max = steps;
Sum += steps;
steps = 0;
result.reset(0.0, 0.0);
numbers--;
cout<<endl;
fout<<endl;
}
Average=Sum/N;
cout<<"Average: "<<Average<<" Max: "<<Max<<" Min: "<<Min<<" Sum: "<<Sum;
fout<<"Average: "<<Average<<" Max: "<<Max<<" Min: "<<Min<<" Sum: "<<Sum;
cout<<endl;
fout<<endl;
cout << "Bye!\n";
cin.clear();
while (cin.get() != '\n')
continue;
return 0;
}
//PP11.9.4
Z_Head.h
#ifndef XXX_H
#define XXX_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*(double n) const;
friend Time operator+(const Time& u,const Time& t);
friend Time operator-(const Time& u,const Time& t);
friend Time operator*(double m,const Time& t){return t*m;}//此处内联函数
friend std::ostream& operator<<(std::ostream& os,const Time& t);//友元函数声明
};
#endif
SubFunctions.cpp
#include "Z_Head.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& u,const Time& t)//改变包括去除类作用域和去除隐式const
{
Time sum;
sum.minutes=u.minutes+t.minutes;
sum.hours=u.hours+t.hours+sum.minutes/60;
sum.minutes%=60;
return sum;
}
Time operator-(const Time& u,const Time& t)
{
Time diff;
int tot1,tot2;
tot1=t.minutes+60*(t.hours);
tot2=u.minutes+60*(u.hours);
diff.minutes=(tot2-tot1)%60;
diff.hours=(tot2-tot1)/60;
return diff;
}
Time Time::operator*(double n) const
{
Time result;
long totalminutes=hours*n*60+minutes*n;
result.hours=totalminutes/60;
result.minutes=totalminutes%60;
return result;
}
std::ostream& operator<<(std::ostream& os,const Time& t)//友元函数定义不需要friend,原型才需要
{
os<<t.hours<<" hours,"<<t.minutes<<" minutes.";
return os;//os引用为cout的别名
}
Main.cpp
#include "Z_Head.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;
}
//PP11.9.5
Z_Head.h
#ifndef XXX_H
#define XXX_H
#include <iostream>
class Stonewt
{
public:
enum Mode{STN,LBS,FPD};
private:
enum {Lbs_per_stn = 14};
int stone;
double pds_left;
double pounds;
int pounds_int;
Mode mode;
void set_stn();
void set_pds();
void set_pds_int();
public:
Stonewt(double lbs,Mode form);
Stonewt(int stn, double lbs,Mode form);
Stonewt();
~Stonewt();
void stn_mode();
void pds_mode();
void int_pds_mode();
operator int()const;
operator double()const;
Stonewt operator+(const Stonewt & st) const;
Stonewt operator-(const Stonewt & st) const;
Stonewt operator*(double n) const;
friend Stonewt operator*(double n,const Stonewt & st);
friend std::ostream & operator<<(std::ostream & os,const Stonewt & st);
};
#endif
SubFunctions.cpp
#include "Z_Head.h"
using std::cout;
void Stonewt::set_stn()
{
stone = int (pounds)/Lbs_per_stn;
pds_left = int (pounds)% Lbs_per_stn+pounds-int(pounds);
}
void Stonewt::set_pds()
{
pounds = stone*Lbs_per_stn + pds_left;
}
void Stonewt::set_pds_int()
{
pounds_int = int(pounds);
}
Stonewt::Stonewt(double lbs,Mode form)
{
mode = form;
if (form == STN)
{
stone = int(lbs) / Lbs_per_stn;
pds_left = int(lbs) % Lbs_per_stn + lbs - int(lbs);
set_pds();
set_pds_int();
}
else if (form == LBS)
{
pounds_int = int(lbs);
pounds = lbs;
set_stn();
}
else if (form == FPD)
{
pounds = lbs;
set_pds_int();
set_stn();
}
else
{
cout<< "Incorrect 3rd argument to Stonewt() -- ";
cout<< "Stonewt set to 0\n";
stone = pounds = pds_left = 0;
mode = STN;
}
}
Stonewt::Stonewt(int stn, double lbs,Mode form)
{
mode = form;
if (form == STN)
{
stone = stn;
pds_left = lbs;
set_pds();
set_pds_int();
}
else if (form == LBS)
{
pounds_int = int(stn*Lbs_per_stn + lbs);
pounds = stn*Lbs_per_stn + lbs;
set_stn();
}
else if (form == FPD)
{
pounds = stn*Lbs_per_stn + lbs;
set_pds_int(); set_stn();
}
else
{
cout<< "Incorrect 3rd argument to Stonewt() -- ";
cout<< "Stonewt set to 0\n";
stone = pounds = pds_left = 0;
mode = STN;
}
}
Stonewt::Stonewt()
{
stone = pds_left = pounds = 0;
mode = STN;
}
Stonewt::~Stonewt()
{
}
void Stonewt::stn_mode()
{
mode = STN;
}
void Stonewt::pds_mode()
{
mode = FPD;
}
void Stonewt::int_pds_mode()
{
mode = LBS;
}
Stonewt::operator int() const
{
return int(pounds+0.5);
}
Stonewt::operator double() const
{
return pounds;
}
Stonewt Stonewt::operator+(const Stonewt & st) const
{
return Stonewt(pounds+st.pounds,st.mode);
}
Stonewt Stonewt::operator-(const Stonewt & st) const
{
return Stonewt(pounds-st.pounds,st.mode);
}
Stonewt Stonewt::operator*(double n) const
{
return Stonewt(pounds*n,mode);
}
Stonewt operator*(double n,const Stonewt & st)
{
return Stonewt(st.pounds*n,st.mode);
}
std::ostream& operator<<(std::ostream & os,const Stonewt & st)
{
if (st.mode == Stonewt::STN)
os<<st.stone<< " stone, " <<st.pds_left<< " pounds\n";
else if (st.mode == Stonewt::LBS)
os<<st.pounds_int<< " pounds(int)\n";
else if (st.mode == Stonewt::FPD)
os<<st.pounds<< " pounds(double)\n";
else
os<< "Error in type\n";
return os;
}
Main.cpp
#include "Z_Head.h"
int main()
{
using std::cout;
using std::cin;
using std::endl;
Stonewt incognito(275,Stonewt::FPD);
Stonewt wolfe(285.7,Stonewt::STN);
Stonewt taft(21, 8,Stonewt::LBS);
Stonewt temp;
cout<< "The celebrity weighed ";
cout<< incognito <<endl;
cout<< "The detective weighed ";
cout<<wolfe<<endl;
cout<< "The President weighed ";
cout<<taft<<endl;
temp = incognito + wolfe;
cout<< "Incognito + Wolfe = " << temp <<endl;
temp = wolfe - incognito;
cout<< "Wolfe - Incognito = " << temp <<endl;
temp = taft * 10.0;
cout<< "Taft * 10.0 = " << temp <<endl;
temp = 10.0 * taft;
cout<< "10.0 * Taft = " << temp <<endl;
cin.get();
return 0;
}
//PP11.9.6
Z_Head.h
#ifndef XXX_H
#define XXX_H
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
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();
//重载6个关系运算符
bool operator<(const Stonewt&st) const;
bool operator<=(const Stonewt&st) const;
bool operator>(const Stonewt&st) const;
bool operator>=(const Stonewt&st) const;
bool operator==(const Stonewt&st) const;
bool operator!=(const Stonewt&st) const;
//重载输出流友元函数
friend std::ostream & operator<<(std::ostream & os,const Stonewt & st);
};
#endif
SubFunctions.cpp
#include "Z_Head.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 = pds_left = pounds = 0;
}
Stonewt::~Stonewt()
{
}
bool Stonewt::operator<(const Stonewt&st) const
{
if (pounds<st.pounds)
return true;
else
return false;
}
bool Stonewt::operator<=(const Stonewt&st) const
{
if (pounds<=st.pounds)
return true;
else
return false;
}
bool Stonewt::operator>(const Stonewt&st) const
{
if (pounds>st.pounds)
return true;
else
return false;
}
bool Stonewt::operator>=(const Stonewt&st) const
{
if (pounds>=st.pounds)
return true;
else
return false;
}
bool Stonewt::operator==(const Stonewt&st) const
{
if (pounds==st.pounds)
return true;
else
return false;
}
bool Stonewt::operator!=(const Stonewt&st) const
{
if (pounds!=st.pounds)
return true;
else
return false;
}
std::ostream& operator<<(std::ostream & os,const Stonewt & st)
{
os<<st.pounds<< " pounds\n";
return os;
}
Main.cpp
#include "Z_Head.h"
int main()
{
Stonewt sw[6] = { 10.0, 11.0, 12.5 };
Stonewt temp(11.0);
//读取循环录入剩余三个位置
for (int i = 3; i< 6; i++)
{
double input;
cout<< "Enter #" <<i + 1 << ": ";
cin>> input;
sw[i] = input;
}
//输出全部的数组对象
for (int i = 0; i< 6; i++)
cout<< "#" <<i<< ": " <<sw[i];
int count = 0;
Stonewt Min = sw[0];
Stonewt Max = sw[0];
//计算最大最小和大于等于11.0的计数值
for (int i = 0; i< 6; i++)
{
if (Min >sw[i])
Min = sw[i];
if (Max <sw[i])
Max = sw[i];
if (temp >= sw[i])
count++;
}
cout<< "The Min pounds: " << Min;
cout<< "The Max pounds: " << Max;
cout<< "The numbers not under 11 pounds: " << count;
cin.get();
cin.get();
return 0;
}
//PP11.9.7
Z_Head.h
#ifndef XXX_H
#define XXX_H
#include <iostream>
using namespace std;
class Complex
{
private:
double R_number;
double V_number;
public:
Complex(double r,double v);
Complex(double r);
Complex();
~Complex();
Complex operator+(const Complex & rv) const;
Complex operator-(const Complex & rv) const;
Complex operator*(double n) const;//数字在后
Complex operator*(Complex & rv) const;
Complex operator~() const;
friend Complex operator*(double n,const Complex & rv);//数字在前
friend ostream& operator<<(ostream & os,const Complex & rv);
friend istream& operator>>(istream & is,Complex & rv);
};
#endif
SubFunctions.cpp
#include "Z_Head.h"
Complex::Complex(double r,double v)
{
R_number=r;
V_number=v;
}
Complex::Complex(double r)
{
R_number=r;
V_number=0.0;
}
Complex::Complex()
{
R_number=V_number=0;
}
Complex::~Complex()
{
}
Complex Complex::operator+(const Complex & rv) const
{
return Complex(R_number+rv.R_number,V_number+rv.V_number);
}
Complex Complex::operator-(const Complex & rv) const
{
return Complex(R_number-rv.R_number,V_number-rv.V_number);
}
Complex Complex::operator*(double n) const
{
return Complex(n*R_number,n*V_number);
}
Complex Complex::operator*(Complex & rv) const
{
double real,imaginary;
real=R_number*rv.R_number-V_number*rv.V_number;//实部:AC-BD
imaginary=R_number*rv.V_number+V_number*rv.R_number;//虚部:(AD+BC)i
return Complex(real,imaginary);
}
Complex Complex::operator~() const
{
return Complex(R_number,-V_number);
}
Complex operator*(double n,const Complex & rv)
{
return Complex(n*rv.R_number,n*rv.V_number);
}
ostream& operator<<(ostream & os,const Complex & rv)
{
os<<"("<<rv.R_number<<","<<rv.V_number<<"i)";
return os;
}
istream& operator>>(istream & is,Complex & rv)//警告!读取到rv中,rv决不能const
{
cout<<"Enter the Real Number: \n";
if (is>>rv.R_number)
{
cout<<"Enter the imaginary Number: \n";
is>>rv.V_number;
}
return is;
}
Main.cpp
#include "Z_Head.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<<"c * 2 is "<< c * 2 <<'\n';//成员函数实现
cout<<"Enter a complex number (q to quit):\n";
}
cout<<"Done!\n";
return 0;
}