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
#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<
#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<
#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<
#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<
#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<
#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<
#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<
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
#include
#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)=("<
#include
#include
#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()
#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<
#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
#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<
#include "Z_Head.h"
int main()
{
using std::cout;
Stonewt poppins(9,2.8);
double p_wt = poppins;
cout<<"Convert to double => ";
cout<<"Poppins: "< ";
cout<<"Poppins: "<
#include
#include
#include
#include
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)=("<> target) {
cout << "Enter step length: ";
if (!(cin >> dstep))
break;
fout<<"Target Distance: "<
#include
#include
#include
#include
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)=("<> target)
{
cout << "Enter step length: ";
if (!(cin >> dstep))
break;
fout<<"Target Distance: "<
#include
#include
#include
#include
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)=("<>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: "<
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<
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<(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<> input;
sw[i] = input;
}
//输出全部的数组对象
for (int i = 0; i< 6; i++)
cout<< "#" <sw[i])
Min = sw[i];
if (Max = 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
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<<"("<>(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 "<