#ifndef VECTOR_H_
#define VECTOR_H_
#include<iostream>
//第11章
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);
~Vector();
void reset(double n1, double n2, Mode form = RECT);
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();
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 &b);
friend std::ostream& operator<<(std::ostream &os, const Vector &v);
};
}
#endif
#include<cmath>
#include"zhan.h"
//第11章
using std::cin;
using std::cout;
using std::endl;
using std::cos;
using std::sin;
using std::atan;
using std::sqrt;
using std::atan2;
namespace VECTOR
{
const double Rad_to_deg = 45.0 / atan(1.0);
void Vector::set_mag()
{
mag = sqrt(x*x + y*y);
}
void Vector::set_ang()
{
if (x == 0.0&&y == 0.0)
ang = 0.0;
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;
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;
set_x();
set_y();
}
else
{
cout << "Bad input!" << endl;
cout << "Reset everything." << endl;
x = y = mag = ang = 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;
set_x();
set_y();
}
else
{
cout << "Bad input!" << endl;
cout << "Reset everything." << endl;
x = y = mag = ang = 0;
mode = RECT;
}
}
void Vector::polar_mode()
{
mode = POL;
}
void Vector::rect_mode()
{
mode = RECT;
}
Vector Vector::operator+(const Vector &b)const
{
return Vector(x + b.x, y + b.y);
}
Vector Vector::operator-(const Vector &b)const
{
return Vector(x - b.x, y - b.y);
}
Vector Vector::operator-()const
{
return Vector(-x, -y);
}
Vector Vector::operator*(double n)const
{
return Vector(n*x, n*y);
}
Vector operator*(double n, const Vector &b)
{
return b*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 << "Bad input!" << endl;
return os;
}
}
#include<iostream>
#include<fstream>
#include<ctime>
#include<cstdlib>
#include"zhan.h"
//第11章
int main()
{
using namespace std;
using namespace VECTOR;
ofstream output;
output.open("output.txt");
srand(time(0));
double direction;
Vector step;
Vector result(0.0, 0.0);
unsigned int 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++;
output << steps << ": " << result << " " << endl;
}
cout << "After " << steps << " steps, the subject has the following locationg:\n";
cout << result <<" ";
output << "After " << steps << " steps, the subject has the following locationg:\n";
output << result << " ";
result.polar_mode();
cout << " or " << result << endl;
cout << "Average outward distance per step =" << result.magval() / steps << endl;
output << " or " << result << endl;
output << "Average outward distance per step =" << result.magval() / steps << endl;
steps = 0;
result.reset(0.0, 0.0);
cout << "Enter target distance (q to quit): ";
}
output.close();
cout << "Bye!\n";
cin.clear();
while (cin.get() != '\n')
continue;
system("pause");
return 0;
}
#ifndef VECTOR_H_
#define VECTOR_H_
#include<iostream>
//第11章第2题
namespace VECTOR
{
class Vector
{
public:
enum Mode {RECT,POL};
private:
double x;
double y;
Mode mode;
void set_mag();
void set_ang();
void set_x(double mag, double ang);
void set_y(double mag, double ang);
public:
Vector();
Vector(double n1, double n2, Mode form = RECT);
~Vector();
void reset(double n1, double n2, Mode form = RECT);
double xval()const { return x; }
double yval()const { return y; }
double magval()const;
double angval()const;
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 &b);
friend std::ostream& operator<<(std::ostream &os, const Vector &v);
};
}
#endif
#include<cmath>
#include"zhan.h"
//第11章第2题
using std::cin;
using std::cout;
using std::endl;
using std::cos;
using std::sin;
using std::atan;
using std::sqrt;
using std::atan2;
namespace VECTOR
{
const double Rad_to_deg = 45.0 / atan(1.0);
double Vector::magval()const
{
return sqrt(x*x + y*y);
}
double Vector::angval()const
{
if (x == 0 && y == 0)
return 0.0;
else
return atan2(y, x);
}
void Vector::set_x(double mag,double ang)
{
x = mag*cos(ang);
}
void Vector::set_y(double mag, double ang)
{
y = mag*sin(ang);
}
Vector::Vector()
{
x = y= 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);
set_y(n1,n2);
}
else
{
cout << "Bad input!" << endl;
cout << "Reset everything." << endl;
x = y = 0;
mode = RECT;
}
}
Vector::~Vector()
{
}
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);
set_y(n1,n2);
}
else
{
cout << "Bad input!" << endl;
cout << "Reset everything." << endl;
x = y = 0;
mode = RECT;
}
}
void Vector::polar_mode()
{
mode = POL;
}
void Vector::rect_mode()
{
mode = RECT;
}
Vector Vector::operator+(const Vector &b)const
{
return Vector(x + b.x, y + b.y);
}
Vector Vector::operator-(const Vector &b)const
{
return Vector(x - b.x, y - b.y);
}
Vector Vector::operator-()const
{
return Vector(-x, -y);
}
Vector Vector::operator*(double n)const
{
return Vector(n*x, n*y);
}
Vector operator*(double n, const Vector &b)
{
return b*n;
}
std::ostream& operator<<(std::ostream &os, const Vector &v)
{
if (v.mode == Vector::RECT)
os << "(x,y)=(" << v.x << "," << v.y << ")";
else if (v.mode == Vector::POL)
{
os << "(m,a)=(" << v.magval() << "," << v.angval()*Rad_to_deg << ")";
}
else
os << "Bad input!" << endl;
return os;
}
}
#include<iostream>
#include<ctime>
#include<cstdlib>
#include"zhan.h"
//第11章第2题
int main()
{
using namespace std;
using namespace VECTOR;
srand(time(0));
double direction;
Vector step;
Vector result(0.0, 0.0);
unsigned int 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 locationg:\n";
cout << result <<" ";
result.polar_mode();
cout << " or " << 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;
system("pause");
return 0;
}
#include<iostream>
#include<ctime>
#include<cstdlib>
#include"zhan.h"
//第11章第3题
int main()
{
using namespace std;
using namespace VECTOR;
srand(time(0));
double direction;
Vector step;
Vector result(0.0, 0.0);
unsigned int steps = 0;
double target;
double dstep;
unsigned int Minsteps =999, Maxsteps = -999,Sum=0;
int num = 3;
cout << "Enter target distance(q to quit):";
while (num--&&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++;
}
if (steps < Minsteps)
Minsteps = steps;
else if (steps>Maxsteps)
Maxsteps = steps;
Sum += steps;
steps = 0;
result.reset(0.0, 0.0);
cout << "Enter target distance (q to quit): ";
}
cout << "The highest steps is " << Maxsteps << " steps\n";
cout << "The lowest steps is " << Minsteps << " steps\n";
cout << "The average steps is " << Sum/3 << " steps\n";
cout << "Bye!\n";
cin.clear();
while (cin.get() != '\n')
continue;
system("pause");
return 0;
}
//第11章第4题
#ifndef STACK_H
#define STACK_H
#include<iostream>
class Time
{
private:
int hours;
int minutes;
public:
Time();
Time(int h, int m);
~Time();
void Addmin(int m);
void Addhou(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 &t1, double m);
friend Time operator*(double m, const Time &t1);
friend std::ostream &operator<<(std::ostream &os, const Time &t);
};
#endif
//第十一章第4题
#include"zhan.h"
Time::Time()
{
hours = minutes = 0;
}
Time::Time(int h, int m)
{
hours = h;
minutes = m;
}
Time::~Time()
{
}
void Time::Addmin(int m)
{
minutes += m;
hours += minutes / 60;
minutes = minutes % 60;
}
void Time::Addhou(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)%60;
sum.hours = t1.hours + t2.hours + (t1.minutes + t2.minutes) /60;
return sum;
}
Time operator-(const Time &t1, const Time &t2)
{
Time diff;
int tot1, tot2;
tot1 = t1.hours * 60 + t1.minutes;
tot2 = t2.hours * 60 + t2.minutes;
diff.minutes = (tot1 - tot2) % 60;
diff.hours = (tot1 - tot2) / 60;
return diff;
}
Time operator*(const Time &t1, double m)
{
Time sum;
int temp = t1.hours*60*m + t1.minutes*m;
sum.minutes = temp % 60;
sum.hours = temp / 60;
return sum;
}
Time operator*(double m, const Time &t1)
{
return t1*m;
}
std::ostream &operator<<(std::ostream &os, const Time &t)
{
os << t.hours << "Hours," << t.minutes << "Minutes.";
return os;
}
#include<iostream>
#include"zhan.h"
int main()
{
using std::cout;
using std::cin;
using std::endl;
Time t1(2, 48);
Time t2(3,40);
Time t3;
cout << "Aida and Tosa:\n";
cout << t1 << ";" << t2 << endl;
t3 = t1 + t2;
cout << "Aida+ Tosa=" << t3<<endl;
t3 = t1*2.0;
cout << "Aida*2=" << t3<<endl;
t3 = 2.0*t1;
cout << "Aida*2=" << t3 << endl;
system("pause");
return 0;
}
//第11章第5题
#ifndef STACK_H
#define STACK_H
#include<iostream>
class Stonewt
{
public:
enum Mode{ STO, POU };
private:
enum{Lbs_per_stn=14};
int stone;
Mode mode;
double pds_left;
double pounds;
public:
Stonewt(double lbs,Mode wmode=STO);
Stonewt(int stn, double lbs, Mode wmode = STO);
Stonewt();
~Stonewt();
void Setmode(Mode wmode);
Stonewt operator + (const Stonewt &b)const;
Stonewt operator - (const Stonewt &b)const;
Stonewt operator*(double n)const;
friend std::ostream& operator<<(std::ostream &os, const Stonewt &v);
};
#endif
//第11章第5题
#include"zhan.h"
#include<iostream>
using std::cout;
using std::endl;
Stonewt::Stonewt(double lbs,Mode wmode )
{
stone = int(lbs) / Lbs_per_stn;
pds_left = int(lbs) % Lbs_per_stn + lbs - int(lbs);
pounds = lbs;
mode = wmode;
}
Stonewt::Stonewt(int stn, double lbs, Mode wmode)
{
stone = stn;
pds_left = lbs;
pounds = stn*Lbs_per_stn + lbs;
mode = wmode;
}
Stonewt::Stonewt()
{
stone = pds_left = pounds = 0.0;
mode = STO;
}
Stonewt::~Stonewt()
{
}
void Stonewt::Setmode(Mode wmode)
{
if (wmode == POU)
mode = POU;
else if (wmode == STO)
mode = STO;
}
Stonewt Stonewt::operator+(const Stonewt &b)const
{
Stonewt sum;
sum.pounds = pounds + b.stone * 14 + b.pds_left;
sum.stone = (pounds + b.stone * 14 + b.pds_left) / 14;
sum.pds_left = (int)(sum.pounds) % Lbs_per_stn + sum.pounds - (int)sum.pounds;
return sum;
}
Stonewt Stonewt::operator-(const Stonewt &b)const
{
Stonewt div;
div.pounds = pounds - b.stone * 14 - b.pds_left;
div.stone = (pounds - b.stone * 14 - b.pds_left) / 14;
div.pds_left = (int)(div.pounds) % Lbs_per_stn + div.pounds - (int)div.pounds;
return div;
}
Stonewt Stonewt::operator*(double n)const
{
Stonewt sum;
sum.pounds = pounds*n;
sum.stone = sum.pounds / 14;
sum.pds_left = (int)(sum.pounds) % Lbs_per_stn + sum.pounds - (int)sum.pounds;
return sum;
}
Stonewt operator*(double n, const Stonewt &b)
{
return b*n;
}
std::ostream& operator<<(std::ostream &os, const Stonewt &v)
{
if (v.mode == Stonewt::POU)
os << v.pounds << " pounds.\n";
else if (v.mode == Stonewt::STO)
{
os << v.stone << " stone, " << v.pds_left << " pounds.\n";
}
else
os << "Bad input!" << endl;
return os;
}
//第11章第5题
#include"zhan.h"
#include<iostream>
#include<cstdlib>
using std::cin;
using std::cout;
using std::endl;
int main()
{
Stonewt s1 = 275;
Stonewt s2(1400.0,Stonewt::POU);
Stonewt s3(21, 8, Stonewt::POU);
Stonewt s4(21, 8);
Stonewt s5 = s3 + s4;
s5.Setmode(Stonewt::POU);
cout << "The celebrity weighted ";
cout << s1<<endl;
cout << "The detectibe weighted ";
cout << s2+s2<< endl;
cout << "The President weighted ";
cout << s3+s4<< endl;
cout << s5 << endl;
system("pause");
return 0;
}
//第11章第6题
#ifndef STACK_H
#define STACK_H
#include<iostream>
class Stonewt
{
public:
enum Mode{ STO, POU };
private:
enum{Lbs_per_stn=14};
int stone;
Mode mode;
double pds_left;
double pounds;
public:
Stonewt(double lbs,Mode wmode=POU);
Stonewt(int stn, double lbs, Mode wmode = POU);
Stonewt();
~Stonewt();
void Setmode(Mode wmode);
Stonewt operator + (const Stonewt &b)const;
Stonewt operator - (const Stonewt &b)const;
Stonewt operator*(double n)const;
bool operator>(const Stonewt &b)const;
bool operator>=(const Stonewt &b)const;
bool operator<(const Stonewt &b)const;
bool operator<=(const Stonewt &b)const;
bool operator!=(const Stonewt &b)const;
bool operator==(const Stonewt &b)const;
friend std::ostream& operator<<(std::ostream &os, const Stonewt &v);
};
#endif
//第11章第6题
#include"zhan.h"
#include<iostream>
using std::cout;
using std::endl;
Stonewt::Stonewt(double lbs,Mode wmode )
{
stone = int(lbs) / Lbs_per_stn;
pds_left = int(lbs) % Lbs_per_stn + lbs - int(lbs);
pounds = lbs;
mode = wmode;
}
Stonewt::Stonewt(int stn, double lbs, Mode wmode)
{
stone = stn;
pds_left = lbs;
pounds = stn*Lbs_per_stn + lbs;
mode = wmode;
}
Stonewt::Stonewt()
{
stone = pds_left = pounds = 0.0;
mode = STO;
}
Stonewt::~Stonewt()
{
}
void Stonewt::Setmode(Mode wmode)
{
if (wmode == POU)
mode = POU;
else if (wmode == STO)
mode = STO;
}
Stonewt Stonewt::operator+(const Stonewt &b)const
{
Stonewt sum;
sum.pounds = pounds + b.stone * 14 + b.pds_left;
sum.stone = (pounds + b.stone * 14 + b.pds_left) / 14;
sum.pds_left = (int)(sum.pounds) % Lbs_per_stn + sum.pounds - (int)sum.pounds;
return sum;
}
Stonewt Stonewt::operator-(const Stonewt &b)const
{
Stonewt div;
div.pounds = pounds - b.stone * 14 - b.pds_left;
div.stone = (pounds - b.stone * 14 - b.pds_left) / 14;
div.pds_left = (int)(div.pounds) % Lbs_per_stn + div.pounds - (int)div.pounds;
return div;
}
Stonewt Stonewt::operator*(double n)const
{
Stonewt sum;
sum.pounds = pounds*n;
sum.stone = sum.pounds / 14;
sum.pds_left = (int)(sum.pounds) % Lbs_per_stn + sum.pounds - (int)sum.pounds;
return sum;
}
Stonewt operator*(double n, const Stonewt &b)
{
return b*n;
}
std::ostream& operator<<(std::ostream &os, const Stonewt &v)
{
if (v.mode == Stonewt::POU)
os << v.pounds << " pounds.\n";
else if (v.mode == Stonewt::STO)
{
os << v.stone << " stone, " << v.pds_left << " pounds.\n";
}
else
os << "Bad input!" << endl;
return os;
}
bool Stonewt::operator>(const Stonewt &b)const
{
return pounds - b.pounds;
}
bool Stonewt::operator>=(const Stonewt &b)const
{
if (pounds - b.pounds >= 0)
return true;
else
return false;
}
bool Stonewt::operator<(const Stonewt &b)const
{
return b.pounds - pounds;
}
bool Stonewt::operator<=(const Stonewt &b)const
{
if (pounds - b.pounds <= 0)
return true;
else
return false;
}
bool Stonewt::operator!=(const Stonewt &b)const
{
if (pounds - b.pounds != 0)
return true;
else
return false;
}
bool Stonewt::operator==(const Stonewt &b)const
{
if (pounds - b.pounds == 0)
return true;
else
return false;
}
//第11章第6题
#include"zhan.h"
#include<iostream>
#include<cstdlib>
using std::cin;
using std::cout;
using std::endl;
int main()
{
Stonewt s1[6] = {14,28,42};
double p;
cout << "Please enter last three nums:";
for (int i = 3; i < 6; i++)
{
cin >> p;
s1[i] = p;
}
Stonewt max = s1[0], min = s1[0];
int flag=-1;
for (int i = 0; i < 6; i++)
{
if (s1[i]>max)
max = s1[i];
else if (s1[i] < min)
min = s1[i];
if (s1[i] == 1400)
flag = i;
}
cout << "The max is:" << max << endl;
cout << "The min is:" << min << endl;
if (flag != -1)
cout << "There is a 1400." << endl;
system("pause");
return 0;
}
#ifndef STACK_H
#define STACK_H
#include<iostream>
class complex
{
private:
double real;
double imaginary;
public:
complex(double r=0.0,double i=0.0);
~complex();
complex operator-()const;
complex operator+(const complex &c)const;
complex operator-(const complex &c)const;
complex operator*(const complex &c)const;
complex operator*(double n)const;
friend complex operator *(double n, const complex &c);
friend std::ostream& operator<<(std::ostream &os, const complex &c);
friend std::istream& operator>>(std::istream &os, complex &c);
};
#endif
#include"zhan.h"
#include<iostream>
using namespace std;
complex::complex(double r, double i)
{
real = r;
imaginary = i;
}
complex::~complex()
{
}
complex complex::operator-()const
{
return complex(real, -imaginary);
}
complex complex::operator+(const complex &c)const
{
complex sum;
sum.real = real+c.real;
sum.imaginary = imaginary+c.imaginary;
return sum;
}
complex complex::operator-(const complex &c)const
{
complex div;
div.real = real - c.real;
div.imaginary = imaginary - c.imaginary;
return div;
}
complex complex::operator*(double n)const
{
complex div;
div.real = n* real;
div.imaginary = n*imaginary;
return div;
}
complex complex::operator*(const complex &c)const
{
complex sum;
sum.real = real*c.real - imaginary*c.imaginary;
sum.imaginary = real*c.imaginary + imaginary*c.real;
return sum;
}
complex operator *(double n, const complex &c)
{
return c*n;
}
std::ostream& operator<<(ostream &os, const complex &c)
{
os << "(" << c.real << "," << c.imaginary << "i)";
return os;
}
std::istream& operator>>(istream &os, complex &c)
{
cout << "real:";
os >> c.real;
if (!os)
goto END;
cout << "imaginary:";
os >> c.imaginary;
END: return os;
}
//第11章第7题
#include<iostream>
#include"zhan.h"
#include<cstdlib>
using namespace std;
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*a is " << 2*c << "\n";
cout << "Enter a complex number (q to quit):\n";
}
cout << "Done!\n";
system("pause");
return 0;
}