1.
// vect.h -- Vector class with <<, mode state
#ifndef VECTOR_H_
#define VECTOR_H_
#include <iostream>
namespace VECTOR
{
class Vector
{
public:
enum Mode {RECT, POL};
// RECT指平面坐标, POL指极坐标
private:
double x; // horizontal value
double y; // vertical value
double mag; // length of vector
double ang; // direction of vector in degrees
Mode mode; // RECT or POL
// private methods for setting values
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 xval() const {return x;} // report x value
double yval() const {return y;} // report y value
double magval() const {return mag;} // report magnitude
double angval() const {return ang;} // report angle
void polar_mode(); // set mode to POL
void rect_mode(); // set mode to RECT
// 操作符重载
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);
};
} // end namespace VECTOR
#endif
// vect.cpp -- methods for the Vector class
#include <cmath>
#include "vect.h" // includes <iostream>
using std::sqrt;
using std::sin;
using std::cos;
using std::atan;
using std::atan2;
using std::cout;
namespace VECTOR
{
// compute degrees in one radian
const double Rad_to_deg = 45.0 / atan(1.0);
// should be about 57.2957795130823
// private methods
// calculates magnitude from x and y
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);
}
// set x from polar coordinate
void Vector::set_x()
{
x = mag * cos(ang);
}
// set y from polar coordinate
void Vector::set_y()
{
y = mag * sin(ang);
}
// public methods
Vector::Vector() // default constructor
{
x = y = mag = ang = 0.0;
mode = RECT;
}
// construct vector from rectangular coordinates if form is r
// (the default) or else from polar coordinates if form is p
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;
}
}
// reset vector from rectangular coordinates if form is
// RECT (the default) or else from polar coordinates if
// form is POL
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_to_deg;
set_x();
set_y();
}
else
{
cout << "Incorrect 3rd argument to Vector() -- ";
cout << "vector set to 0\n";
x = y = mag = ang = 0.0;
mode = RECT;
}
}
Vector::~Vector() // destructor
{
}
void Vector::polar_mode() // set to polar mode
{
mode = POL;
}
void Vector::rect_mode() // set to rectangular mode
{
mode = RECT;
}
// operator overloading
// add two Vectors
Vector Vector::operator+(const Vector & b) const
{
return Vector(x + b.x, y + b.y);
}
// subtract Vector b from a
Vector Vector::operator-(const Vector & b) const
{
return Vector(x - b.x, y - b.y);
}
// reverse sign of Vector
Vector Vector::operator-() const
{
return Vector(-x, -y);
}
// multiply vector by n
Vector Vector::operator*(double n) const
{
return Vector(n * x, n * y);
}
// friend methods
// multiply n by Vector a
Vector operator*(double n, const Vector & a)
{
return a * n;
}
// display rectangular coordinates if mode is RECT,
// else display polar coordinates if mode is POL
std::ostream & operator<<(std::ostream & os, const Vector & v)
{
if (v.mode == Vector::RECT)
os << "(x,y) = (" << v.x << ", " << v.y << ")";
else if (v.mode == Vector::POL) //这里的operator<<()是友元函数,在名称空间中,但不在类定义中,故使用Vector::POL
{
os << "(m,a) = (" << v.mag << ", "
<< v.ang * Rad_to_deg << ")";
}
else
os << "Vector object mode is invalid";
return os;
}
} // end namespace VECTOR
// randwalk.cpp -- using the Vector class
// compile with the vect.cpp file
#include <iostream>
#include<fstream>
#include <cstdlib> // rand(), srand() prototypes
#include <ctime> // time() prototype
#include "vect.h"
int main()
{
using namespace std;
using VECTOR::Vector;
srand(time(0)); // seed random-number generator
ofstream outFile; //声明对象
outFile.open("random walker.txt"); //建立txt文件
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;
outFile << "Target Distance:" << target << ", Step Size:" << dstep << endl; //写入文件中
outFile << 0 << ": " << result << endl;
while(result.magval() < target)
{
direction = rand() % 360;
step.reset(dstep, direction, Vector::POL);
result = result + step;
steps++;
outFile << steps << ": " << result << 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;
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);
cout << "Enter target distance (q to quit): ";
}
cout << "Bye!\n";
// keep window open
cin.clear();
while (cin.get() != '\n')
continue;
cin.get();
return 0;
}
//random walker.txt
Target Distance:100, Step Size:20
0: (x,y) = (0, 0)
1: (x,y) = (6.18034, -19.0211)
2: (x,y) = (9.99652, 0.611413)
3: (x,y) = (29.9691, 1.65813)
4: (x,y) = (43.8623, -12.7287)
5: (x,y) = (59.6225, -0.415434)
6: (x,y) = (68.0749, 17.7107)
7: (x,y) = (51.3014, 6.81794)
8: (x,y) = (42.2216, 24.6381)
9: (x,y) = (27.8348, 10.7449)
10: (x,y) = (35.0022, 29.4165)
11: (x,y) = (49.6293, 15.7765)
12: (x,y) = (50.3273, -4.21127)
13: (x,y) = (66.7103, 7.26026)
14: (x,y) = (47.6892, 13.4406)
15: (x,y) = (65.8153, 21.893)
16: (x,y) = (56.1191, 39.3854)
17: (x,y) = (36.8939, 44.8981)
18: (x,y) = (16.9061, 44.2001)
19: (x,y) = (36.8574, 42.805)
20: (x,y) = (25.6735, 59.3857)
21: (x,y) = (6.2676, 64.2242)
22: (x,y) = (10.4258, 83.7871)
23: (x,y) = (15.9386, 64.5619)
24: (x,y) = (8.12395, 82.972)
25: (x,y) = (25.0849, 93.5704)
26: (x,y) = (20.2465, 74.1645)
27: (x,y) = (15.7475, 54.6771)
28: (x,y) = (8.5801, 36.0055)
29: (x,y) = (3.06735, 16.7802)
30: (x,y) = (-16.5652, 12.964)
31: (x,y) = (-23.7326, -5.70757)
32: (x,y) = (-28.2316, 13.7798)
33: (x,y) = (-9.21044, 7.59949)
34: (x,y) = (9.06047, -0.535244)
35: (x,y) = (25.0332, -12.5715)
36: (x,y) = (28.1619, 7.18222)
37: (x,y) = (16.4062, 23.3626)
38: (x,y) = (28.1619, 7.18222)
39: (x,y) = (15.3061, 22.5031)
40: (x,y) = (3.83459, 38.8862)
41: (x,y) = (-9.2866, 23.792)
42: (x,y) = (10.6373, 22.0488)
43: (x,y) = (30.3335, 18.5759)
44: (x,y) = (21.8811, 0.449727)
45: (x,y) = (11.5803, -16.6936)
46: (x,y) = (9.83721, -36.6175)
47: (x,y) = (27.1577, -26.6175)
48: (x,y) = (46.383, -32.1303)
49: (x,y) = (65.2933, -38.6416)
50: (x,y) = (46.1672, -44.4891)
51: (x,y) = (49.2959, -64.2428)
52: (x,y) = (64.3901, -51.1216)
53: (x,y) = (74.3901, -68.4422)
After 53 steps, the subject has the following location:
(m,a) = (101.085, -42.6154)
or
(m,a) = (101.085, -42.6154)
Average outward distance per step = 1.90727
2.
// vect.h -- Vector class with <<, mode state
#ifndef VECTOR_H_
#define VECTOR_H_
#include <iostream>
namespace VECTOR
{
class Vector
{
public:
enum Mode {RECT, POL};
// RECT指平面坐标, POL指极坐标
private:
double x; // horizontal value
double y; // vertical value
double mag; // length of vector
double ang; // direction of vector in degrees
Mode mode; // RECT or POL
// private methods for setting values
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 xval() const {return x;} // report x value
double yval() const {return y;} // report y value
double magval(); // report magnitude
double angval(); // report angle
void polar_mode(); // set mode to POL
void rect_mode(); // set mode to RECT
// 操作符重载
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);
};
} // end namespace VECTOR
#endif
// vect.cpp -- methods for the Vector class
#include <cmath>
#include "vect.h" // includes <iostream>
using std::sqrt;
using std::sin;
using std::cos;
using std::atan;
using std::atan2;
using std::cout;
namespace VECTOR
{
// compute degrees in one radian
const double Rad_to_deg = 45.0 / atan(1.0);
// should be about 57.2957795130823
// private methods
// calculates magnitude from x and y
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);
}
// set x from polar coordinate
void Vector::set_x()
{
x = mag * cos(ang);
}
// set y from polar coordinate
void Vector::set_y()
{
y = mag * sin(ang);
}
// public methods
Vector::Vector() // default constructor
{
x = y = mag = ang = 0.0;
mode = RECT;
}
// construct vector from rectangular coordinates if form is r
// (the default) or else from polar coordinates if form is p
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;
}
}
// reset vector from rectangular coordinates if form is
// RECT (the default) or else from polar coordinates if
// form is POL
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_to_deg;
set_x();
set_y();
}
else
{
cout << "Incorrect 3rd argument to Vector() -- ";
cout << "vector set to 0\n";
x = y = mag = ang = 0.0;
mode = RECT;
}
}
Vector::~Vector() // destructor
{
}
double Vector::magval()
{ // report magnitude
return sqrt(x*x + y*y);
}
double Vector::angval()
{
return atan2(y, x);
}
void Vector::polar_mode() // set to polar mode
{
mode = POL;
}
void Vector::rect_mode() // set to rectangular mode
{
mode = RECT;
}
// operator overloading
// add two Vectors
Vector Vector::operator+(const Vector & b) const
{
return Vector(x + b.x, y + b.y);
}
// subtract Vector b from a
Vector Vector::operator-(const Vector & b) const
{
return Vector(x - b.x, y - b.y);
}
// reverse sign of Vector
Vector Vector::operator-() const
{
return Vector(-x, -y);
}
// multiply vector by n
Vector Vector::operator*(double n) const
{
return Vector(n * x, n * y);
}
// friend methods
// multiply n by Vector a
Vector operator*(double n, const Vector & a)
{
return a * n;
}
// display rectangular coordinates if mode is RECT,
// else display polar coordinates if mode is POL
std::ostream & operator<<(std::ostream & os, const Vector & v)
{
if (v.mode == Vector::RECT)
os << "(x,y) = (" << v.x << ", " << v.y << ")";
else if (v.mode == Vector::POL) //这里的operator<<()是友元函数,在名称空间中,但不在类定义中,故使用Vector::POL
{
os << "(m,a) = (" << v.mag << ", "
<< v.ang * Rad_to_deg << ")";
}
else
os << "Vector object mode is invalid";
return os;
}
} // end namespace VECTOR
// randwalk.cpp -- using the Vector class
// compile with the vect.cpp file
#include <iostream>
#include<fstream>
#include <cstdlib> // rand(), srand() prototypes
#include <ctime> // time() prototype
#include "vect.h"
int main()
{
using namespace std;
using VECTOR::Vector;
srand(time(0)); // seed random-number generator
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 << steps << ": " << result << 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);
cout << "Enter target distance (q to quit): ";
}
cout << "Bye!\n";
// keep window open
cin.clear();
while (cin.get() != '\n')
continue;
cin.get();
return 0;
}
3.
// vect.h -- Vector class with <<, mode state
#ifndef VECTOR_H_
#define VECTOR_H_
#include <iostream>
namespace VECTOR
{
class Vector
{
public:
enum Mode {RECT, POL};
// RECT指平面坐标, POL指极坐标
private:
double x; // horizontal value
double y; // vertical value
double mag; // length of vector
double ang; // direction of vector in degrees
Mode mode; // RECT or POL
// private methods for setting values
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 xval() const {return x;} // report x value
double yval() const {return y;} // report y value
double magval() const { return mag; } // report magnitude
double angval() const { return ang; } // report angle
void polar_mode(); // set mode to POL
void rect_mode(); // set mode to RECT
// 操作符重载
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);
};
} // end namespace VECTOR
#endif
// vect.cpp -- methods for the Vector class
#include <cmath>
#include "vect.h" // includes <iostream>
using std::sqrt;
using std::sin;
using std::cos;
using std::atan;
using std::atan2;
using std::cout;
namespace VECTOR
{
// compute degrees in one radian
const double Rad_to_deg = 45.0 / atan(1.0);
// should be about 57.2957795130823
// private methods
// calculates magnitude from x and y
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);
}
// set x from polar coordinate
void Vector::set_x()
{
x = mag * cos(ang);
}
// set y from polar coordinate
void Vector::set_y()
{
y = mag * sin(ang);
}
// public methods
Vector::Vector() // default constructor
{
x = y = mag = ang = 0.0;
mode = RECT;
}
// construct vector from rectangular coordinates if form is r
// (the default) or else from polar coordinates if form is p
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;
}
}
// reset vector from rectangular coordinates if form is
// RECT (the default) or else from polar coordinates if
// form is POL
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_to_deg;
set_x();
set_y();
}
else
{
cout << "Incorrect 3rd argument to Vector() -- ";
cout << "vector set to 0\n";
x = y = mag = ang = 0.0;
mode = RECT;
}
}
Vector::~Vector() // destructor
{
}
void Vector::polar_mode() // set to polar mode
{
mode = POL;
}
void Vector::rect_mode() // set to rectangular mode
{
mode = RECT;
}
// operator overloading
// add two Vectors
Vector Vector::operator+(const Vector & b) const
{
return Vector(x + b.x, y + b.y);
}
// subtract Vector b from a
Vector Vector::operator-(const Vector & b) const
{
return Vector(x - b.x, y - b.y);
}
// reverse sign of Vector
Vector Vector::operator-() const
{
return Vector(-x, -y);
}
// multiply vector by n
Vector Vector::operator*(double n) const
{
return Vector(n * x, n * y);
}
// friend methods
// multiply n by Vector a
Vector operator*(double n, const Vector & a)
{
return a * n;
}
// display rectangular coordinates if mode is RECT,
// else display polar coordinates if mode is POL
std::ostream & operator<<(std::ostream & os, const Vector & v)
{
if (v.mode == Vector::RECT)
os << "(x,y) = (" << v.x << ", " << v.y << ")";
else if (v.mode == Vector::POL) //这里的operator<<()是友元函数,在名称空间中,但不在类定义中,故使用Vector::POL
{
os << "(m,a) = (" << v.mag << ", "
<< v.ang * Rad_to_deg << ")";
}
else
os << "Vector object mode is invalid";
return os;
}
} // end namespace VECTOR
// randwalk.cpp -- using the Vector class
// compile with the vect.cpp file
#include <iostream>
#include<fstream>
#include <cstdlib> // rand(), srand() prototypes
#include <ctime> // time() prototype
#include "vect.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;
int num = 0; //测试次数
cout << "Enter Test Times: "; //输入测试次数
if (!(cin >> num) || num < 0)
break;
int maxSteps = 0;
int minSteps = 0;
int totalSteps = 0;
for (int i = 0; i < num; i++)
{
while (result.magval() < target) //离起点的距离还没达到目标距离,就要继续执行
{
direction = rand() % 360; //随机一个方向,角度
step.reset(dstep, direction, Vector::POL); //当前的一步
result = result + step; //当前的实际位置
steps++; //计数
//cout << steps << ": " << result << endl; //略去中间过程
}
cout << "\nTimes#" << i + 1 << ":\n";
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;
if (i == 0)
totalSteps = minSteps = maxSteps = steps;
else
{
totalSteps += steps;
if (minSteps > steps)
minSteps = steps;
if (maxSteps < steps)
maxSteps = steps;
}
steps = 0;
result.reset(0.0, 0.0);
}
cout << "\nTest Times: " << num << " average steps: " << totalSteps / num << endl;
cout << "Max steps: " << maxSteps << "\nMin steps: " << minSteps << endl;
cout << "Enter target distance (q to quit): ";
}
cout << "Bye!\n";
// keep window open
cin.clear();
while (cin.get() != '\n')
continue;
cin.get();
return 0;
}
4.
// mytime3.h -- Time class with friends
#ifndef MYTIME3_H_
#define MYTIME3_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);
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 t2);
friend Time operator*(double m, const Time & t)
{ return t * m; } // inline definition
friend std::ostream & operator<<(std::ostream & os, const Time & t);
};
#endif
// mytime3.cpp -- implementing Time methods
#include "mytime3.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 & t1,const Time & t2)
{
Time sum;
sum.minutes = t1.minutes + t2.minutes;
sum.hours = t1.hours + t1.hours + sum.minutes / 60;
sum.minutes %= 60;
return sum;
}
Time operator-(const Time & t1,const Time &t2)
{
Time diff;
int tot1, tot2;
tot1 = t1.minutes + 60 * t1.hours;
tot2 = t2.minutes + 60 * t2.hours;
diff.minutes = (tot2 - tot1) % 60;
diff.hours = (tot2 - tot1) / 60;
return diff;
}
Time operator*(const Time & t1,double t2)
{
Time result;
long totalminutes = t1.hours * t2 * 60 + t1.minutes * t2;
result.hours = totalminutes / 60;
result.minutes = totalminutes % 60;
return result;
}
std::ostream & operator<<(std::ostream & os, const Time & t)
{
os << t.hours << " hours, " << t.minutes << " minutes";
return os;
}
//usetime3.cpp -- using the fourth draft of the Time class
// compile usetime3.cpp and mytime3.cpp together
#include <iostream>
#include "mytime3.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; // operator+()
cout << "Aida + Tosca: " << temp << endl;
temp = aida - tosca; // operator-()
cout << "Aida - Tosca: " << temp << endl;
temp = aida* 1.17; // member operator*()
cout << "Aida * 1.17: " << temp << endl;
cout << "10.0 * Tosca: " << 10.0 * tosca << endl;
// std::cin.get();
return 0;
}
5.
// stonewt.h -- definition for the Stonewt class
#ifndef STONEWT_H_
#define STONEWT_H_
class Stonewt
{
public:
enum Mode{stoneps,intps,floatps};
private:
enum {Lbs_per_stn = 14}; // pounds per stone
int stone; // whole stones
double pds_left; // fractional pounds
double pounds; // entire weight in pounds
Mode mode;
public:
void setmode(Mode m);
Stonewt(double lbs); // constructor for double pounds
Stonewt(int stn, double lbs); // constructor for stone, lbs
Stonewt(); // default constructor
~Stonewt();
friend Stonewt operator+(const Stonewt &a, const Stonewt &b);
friend Stonewt operator-(const Stonewt &a, const Stonewt &b);
friend Stonewt operator*(const Stonewt &a, double b);
friend Stonewt operator*(double a, const Stonewt &b);
friend std::ostream & operator<<(std::ostream & os, const Stonewt & s);
};
#endif
// stonewt.cpp -- Stonewt methods
#include <iostream>
using std::cout;
#include "stonewt.h"
void Stonewt::setmode(Mode m)
{
mode = m;
}
// construct Stonewt object from double value
Stonewt::Stonewt(double lbs)
{
mode = Stonewt::floatps;
stone = int (lbs) / Lbs_per_stn; // integer division
pds_left = int (lbs) % Lbs_per_stn + lbs - int(lbs);
pounds = lbs;
}
// construct Stonewt object from stone, double values
Stonewt::Stonewt(int stn, double lbs)
{
mode = Stonewt::stoneps;
stone = stn;
pds_left = lbs;
pounds = stn * Lbs_per_stn +lbs;
}
Stonewt::Stonewt() // default constructor, wt = 0
{
mode = Stonewt::floatps;
stone = 0;
pounds = pds_left = 0.0;
}
Stonewt::~Stonewt() // destructor
{
}
Stonewt operator+(const Stonewt & a, const Stonewt & b)
{
Stonewt t;
if (a.mode == Stonewt::stoneps)
{
t.pds_left = a.pds_left + b.pds_left;
t.stone = a.stone + b.stone + int(t.pds_left) / Stonewt::Lbs_per_stn;
t.pds_left = int(t.pds_left) % Stonewt::Lbs_per_stn + t.pds_left - int(t.pds_left);
t.mode = Stonewt::stoneps;
}
else if (a.mode == Stonewt::intps)
{
t.pounds = a.pounds + b.pounds;
t.pounds = int(t.pounds + 0.5);
t.mode = Stonewt::intps;
}
else
{
t.pounds = a.pounds + b.pounds;
t.mode = Stonewt::floatps;
}
return t;
}
Stonewt operator-(const Stonewt & a, const Stonewt & b)
{
Stonewt t;
if (a.mode == Stonewt::stoneps)
{
double t1, t2;
t1 = a.stone*Stonewt::Lbs_per_stn + a.pds_left;
t2 = b.stone*Stonewt::Lbs_per_stn + b.pds_left;
t.stone = int((t1 - t2) / Stonewt::Lbs_per_stn);
t.pds_left = (int(t1-t2) % Stonewt::Lbs_per_stn) + t1-t2 - int(t1-t2);
t.mode = Stonewt::stoneps;
}
else if (a.mode == Stonewt::intps)
{
t.pounds = a.pounds - b.pounds;
t.pounds = int(t.pounds + 0.5);
t.mode = Stonewt::intps;
}
else
{
t.pounds = a.pounds - b.pounds;
t.mode = Stonewt::floatps;
}
return t;
}
Stonewt operator*(const Stonewt & a, double b)
{
Stonewt t;
if (a.mode == Stonewt::stoneps)
{
t.pounds = a.pounds*b;
t.stone = int(a.stone*b) + int((t.pounds) / Stonewt::Lbs_per_stn);
t.pds_left = int(t.pounds) % Stonewt::Lbs_per_stn + t.pounds-int(t.pounds);
t.mode = Stonewt::stoneps;
}
else if (a.mode == Stonewt::intps)
{
t.pounds = a.pounds*b;
t.pounds = int(t.pounds + 0.5);
t.mode = Stonewt::intps;
}
else
{
t.pounds = a.pounds*b;
t.mode = Stonewt::floatps;
}
return t;
}
Stonewt operator*(double a, const Stonewt & b)
{
Stonewt t;
if (b.mode == Stonewt::stoneps)
{
t.pounds = b.pounds*a;
t.stone = int(b.stone*a) + int(t.pounds) / Stonewt::Lbs_per_stn;
t.pds_left = int(t.pounds) % Stonewt::Lbs_per_stn + t.pounds - int(t.pounds);
t.mode = Stonewt::stoneps;
}
else if (b.mode == Stonewt::intps)
{
t.pounds = b.pounds*a;
t.pounds = int(t.pounds + 0.5);
t.mode = Stonewt::intps;
}
else
{
t.pounds = b.pounds*a;
t.mode = Stonewt::floatps;
}
return t;
}
std::ostream & operator<<(std::ostream & os, const Stonewt & s)
{
if (s.mode == Stonewt::stoneps)
os << s.stone << " stone, " << s.pds_left << " pounds\n";
else if (s.mode == Stonewt::floatps)
os << s.pounds << " pounds\n";
else if (s.mode == Stonewt::intps)
os << int(s.pounds + 0.5) << " pounds\n";
else
os << "Stonewt object mode is invalid";
return os;
}
// stone.cpp -- user-defined conversions
// compile with stonewt.cpp
#include <iostream>
using std::cout;
#include "stonewt.h"
void display(const Stonewt & st, int n);
int main()
{
Stonewt incognito; // uses constructor to initialize
Stonewt wolfe(285.7); // same as Stonewt wolfe = 285.7;
Stonewt taft(334.8);
cout << "The incognito weighed ";
cout<<incognito;
cout << "The wolfe weighed ";
cout<<wolfe;
cout << "The taft weighed ";
cout<<taft;
cout << "Output in int:";
wolfe.setmode(Stonewt::intps);
cout << wolfe;
cout << "Output in stone:";
wolfe.setmode(Stonewt::stoneps);
cout << wolfe;
cout << "The (taft+wolfe) weighed: "; //这里的输出格式由第一个参数的形式决定
cout << taft + wolfe;
cout << "The (taft-wolfe) weighed: ";
cout << taft - wolfe;
cout << "The wolfe*2 weighed: ";
cout << wolfe*2.0; //wolfe的形式已经变成了英石磅的形式
cout << "The 2*taft weighed: ";
cout << 2.0*taft;
incognito = 276.8; // uses constructor for conversion
cout << "After dinner, the celebrity weighed ";
cout<<incognito;
display(incognito, 2);
cout << "The wrestler weighed even more.\n";
cout << "No stone left unearned\n";
// std::cin.get();
return 0;
}
void display(const Stonewt & st, int n)
{
for (int i = 0; i < n; i++)
{
cout << "Wow! ";
cout<<st;
}
}
6.
// stonewt.h -- definition for the Stonewt class
#ifndef STONEWT_H_
#define STONEWT_H_
class Stonewt
{
public:
enum Mode{stoneps,intps,floatps};
private:
enum {Lbs_per_stn = 14}; // pounds per stone
int stone; // whole stones
double pds_left; // fractional pounds
double pounds; // entire weight in pounds
Mode mode;
public:
void setmode(Mode m);
Stonewt(double lbs); // constructor for double pounds
Stonewt(int stn, double lbs); // constructor for stone, lbs
Stonewt(); // default constructor
~Stonewt();
friend Stonewt operator+(const Stonewt &a, const Stonewt &b);
friend Stonewt operator-(const Stonewt &a, const Stonewt &b);
friend Stonewt operator*(const Stonewt &a, double b);
friend Stonewt operator*(double a, const Stonewt &b);
friend bool operator>(const Stonewt & a, const Stonewt & b);
friend bool operator<(const Stonewt & a, const Stonewt & b);
friend bool operator>=(const Stonewt & a, const Stonewt & b);
friend bool operator<=(const Stonewt & a, const Stonewt & b);
friend bool operator==(const Stonewt & a, const Stonewt & b);
friend bool operator!=(const Stonewt & a, const Stonewt & b);
friend std::ostream & operator<<(std::ostream & os, const Stonewt & s);
};
#endif
// stonewt.cpp -- Stonewt methods
#include <iostream>
using std::cout;
#include "stonewt.h"
void Stonewt::setmode(Mode m)
{
mode = m;
}
// construct Stonewt object from double value
Stonewt::Stonewt(double lbs)
{
mode = Stonewt::floatps;
stone = int (lbs) / Lbs_per_stn; // integer division
pds_left = int (lbs) % Lbs_per_stn + lbs - int(lbs);
pounds = lbs;
}
// construct Stonewt object from stone, double values
Stonewt::Stonewt(int stn, double lbs)
{
mode = Stonewt::stoneps;
stone = stn;
pds_left = lbs;
pounds = stn * Lbs_per_stn +lbs;
}
Stonewt::Stonewt() // default constructor, wt = 0
{
mode = Stonewt::floatps;
stone = 0;
pounds = pds_left = 0.0;
}
Stonewt::~Stonewt() // destructor
{
}
Stonewt operator+(const Stonewt & a, const Stonewt & b)
{
Stonewt t;
if (a.mode == Stonewt::stoneps)
{
t.pds_left = a.pds_left + b.pds_left;
t.stone = a.stone + b.stone + int(t.pds_left) / Stonewt::Lbs_per_stn;
t.pds_left = int(t.pds_left) % Stonewt::Lbs_per_stn + t.pds_left - int(t.pds_left);
t.mode = Stonewt::stoneps;
}
else if (a.mode == Stonewt::intps)
{
t.pounds = a.pounds + b.pounds;
t.pounds = int(t.pounds + 0.5);
t.mode = Stonewt::intps;
}
else
{
t.pounds = a.pounds + b.pounds;
t.mode = Stonewt::floatps;
}
return t;
}
Stonewt operator-(const Stonewt & a, const Stonewt & b)
{
Stonewt t;
if (a.mode == Stonewt::stoneps)
{
double t1, t2;
t1 = a.stone*Stonewt::Lbs_per_stn + a.pds_left;
t2 = b.stone*Stonewt::Lbs_per_stn + b.pds_left;
t.stone = int((t1 - t2) / Stonewt::Lbs_per_stn);
t.pds_left = (int(t1-t2) % Stonewt::Lbs_per_stn) + t1-t2 - int(t1-t2);
t.mode = Stonewt::stoneps;
}
else if (a.mode == Stonewt::intps)
{
t.pounds = a.pounds - b.pounds;
t.pounds = int(t.pounds + 0.5);
t.mode = Stonewt::intps;
}
else
{
t.pounds = a.pounds - b.pounds;
t.mode = Stonewt::floatps;
}
return t;
}
Stonewt operator*(const Stonewt & a, double b)
{
Stonewt t;
if (a.mode == Stonewt::stoneps)
{
t.pounds = a.pounds*b;
t.stone = int(a.stone*b) + int((t.pounds) / Stonewt::Lbs_per_stn);
t.pds_left = int(t.pounds) % Stonewt::Lbs_per_stn + t.pounds-int(t.pounds);
t.mode = Stonewt::stoneps;
}
else if (a.mode == Stonewt::intps)
{
t.pounds = a.pounds*b;
t.pounds = int(t.pounds + 0.5);
t.mode = Stonewt::intps;
}
else
{
t.pounds = a.pounds*b;
t.mode = Stonewt::floatps;
}
return t;
}
Stonewt operator*(double a, const Stonewt & b)
{
Stonewt t;
if (b.mode == Stonewt::stoneps)
{
t.pounds = b.pounds*a;
t.stone = int(b.stone*a) + int(t.pounds) / Stonewt::Lbs_per_stn;
t.pds_left = int(t.pounds) % Stonewt::Lbs_per_stn + t.pounds - int(t.pounds);
t.mode = Stonewt::stoneps;
}
else if (b.mode == Stonewt::intps)
{
t.pounds = b.pounds*a;
t.pounds = int(t.pounds + 0.5);
t.mode = Stonewt::intps;
}
else
{
t.pounds = b.pounds*a;
t.mode = Stonewt::floatps;
}
return t;
}
bool operator>(const Stonewt & a, const Stonewt & b)
{
double t1, t2;
if (a.mode == Stonewt::stoneps)
t1 = a.stone*Stonewt::Lbs_per_stn + a.pds_left;
else
t1 = a.pounds;
if (b.mode == Stonewt::stoneps)
t2 = b.stone*Stonewt::Lbs_per_stn + b.pds_left;
else
t2 = b.pounds;
return t1 > t2;
}
bool operator<(const Stonewt & a, const Stonewt & b)
{
double t1, t2;
if (a.mode == Stonewt::stoneps)
t1 = a.stone*Stonewt::Lbs_per_stn + a.pds_left;
else
t1 = a.pounds;
if (b.mode == Stonewt::stoneps)
t2 = b.stone*Stonewt::Lbs_per_stn + b.pds_left;
else
t2 = b.pounds;
return t1 < t2;
}
bool operator>=(const Stonewt & a, const Stonewt & b)
{
double t1, t2;
if (a.mode == Stonewt::stoneps)
t1 = a.stone*Stonewt::Lbs_per_stn + a.pds_left;
else
t1 = a.pounds;
if (b.mode == Stonewt::stoneps)
t2 = b.stone*Stonewt::Lbs_per_stn + b.pds_left;
else
t2 = b.pounds;
return t1 >= t2;
}
bool operator<=(const Stonewt & a, const Stonewt & b)
{
double t1, t2;
if (a.mode == Stonewt::stoneps)
t1 = a.stone*Stonewt::Lbs_per_stn + a.pds_left;
else
t1 = a.pounds;
if (b.mode == Stonewt::stoneps)
t2 = b.stone*Stonewt::Lbs_per_stn + b.pds_left;
else
t2 = b.pounds;
return t1 <= t2;
}
bool operator==(const Stonewt & a, const Stonewt & b)
{
double t1, t2;
if (a.mode == Stonewt::stoneps)
t1 = a.stone*Stonewt::Lbs_per_stn + a.pds_left;
else
t1 = a.pounds;
if (b.mode == Stonewt::stoneps)
t2 = b.stone*Stonewt::Lbs_per_stn + b.pds_left;
else
t2 = b.pounds;
return t1==t2;
}
bool operator!=(const Stonewt & a, const Stonewt & b)
{
double t1, t2;
if (a.mode == Stonewt::stoneps)
t1 = a.stone*Stonewt::Lbs_per_stn + a.pds_left;
else
t1 = a.pounds;
if (b.mode == Stonewt::stoneps)
t2 = b.stone*Stonewt::Lbs_per_stn + b.pds_left;
else
t2 = b.pounds;
return t1 != t2;
}
std::ostream & operator<<(std::ostream & os, const Stonewt & s)
{
if (s.mode == Stonewt::stoneps)
os << s.stone << " stone, " << s.pds_left << " pounds\n";
else if (s.mode == Stonewt::floatps)
os << s.pounds << " pounds\n";
else if (s.mode == Stonewt::intps)
os << int(s.pounds + 0.5) << " pounds\n";
else
os << "Stonewt object mode is invalid";
return os;
}
// stone.cpp -- user-defined conversions
// compile with stonewt.cpp
#include <iostream>
using std::cout;
using std::cin;
#include "stonewt.h"
int main()
{
Stonewt incognito[6]; // uses constructor to initialize
incognito[0] = 220.1;
incognito[1] = 230.2;
incognito[2] = 240.3;
for (int i = 3; i < 6; i++)
{
double n;
cout << "Please enter the number #" << i + 1 << ": ";
cin >> n;
incognito[i] = n;
}
Stonewt stand(11, 0);
Stonewt max;
Stonewt min;
int imax;
int imin;
max = min = incognito[0];
imax = imin = 0;
for (int i = 0; i < 6; i++)
{
if (min > incognito[i])
{
min = incognito[i];
imin = i;
}
if (max< incognito[i])
{
max = incognito[i];
imax = i;
}
}
cout << "The max is: " << "incognito[" << imax << "]: " << max;
cout << "The min is: " << "incognito[" << imin << "]: " << min;
cout << "The weight more than 11 stone is: \n";
for (int i = 0; i < 6; i++)
{
if (incognito[i] > stand)
cout << "incognito[" << i << "]: " << incognito[i];
}
return 0;
}
7.
// stonewt.h -- definition for the Stonewt class
#ifndef STONEWT_H_
#define STONEWT_H_
#include<iostream>
class complex
{
private:
double x;
double y;
public:
complex();
complex(double n1, double n2);
void reset(double n1, double n2);
~complex();
//友元函数重载
friend complex operator+(const complex &a, const complex &b);
friend complex operator-(const complex &a, const complex &b);
friend complex operator*(const complex &a, const complex &b);
friend complex operator*(double a, const complex &b);
friend complex operator~(const complex &a);
friend std::istream & operator>>(std::istream & ins, complex & s); //输入时,不能使用const
friend std::ostream & operator<<(std::ostream & os, const complex & s);
};
#endif
// stonewt.cpp -- Stonewt methods
#include <iostream>
using std::cout;
#include "stonewt.h"
complex::complex()
{
x = y = 0;
}
complex::complex(double n1, double n2)
{
x = n1;
y = n2;
}
void complex::reset(double n1, double n2)
{
x = n1;
y = n2;
}
complex::~complex() //析构函数
{
}
complex operator+(const complex &a, const complex &b)
{
complex t;
t.x = a.x + b.x;
t.y = a.y + b.y;
return t;
}
complex operator-(const complex &a, const complex &b)
{
complex t;
t.x = a.x - b.x;
t.y = a.y - b.y;
return t;
}
complex operator*(const complex &a, const complex &b)
{
complex t;
t.x = a.x * b.x-a.y*b.y;
t.y = a.x * b.y+a.y*b.x;
return t;
}
complex operator*(double a, const complex &b)
{
complex t;
t.x = a*b.x;
t.y = a*b.y;
return t;
}
complex operator~(const complex &a)
{
complex t;
t.x = a.x;
t.y = -a.y;
return t;
}
std::istream & operator >> (std::istream & ins, complex & s) //输入时,不能使用const
{
cout << "real: ";
if (!(ins >> s.x))
return ins;
cout << "imaginary: ";
ins >> s.y;
return ins;
}
std::ostream & operator<<(std::ostream & os, const complex & s)
{
os << "(" << s.x << ", " << s.y << "i)";
return os;
}
// stone.cpp -- user-defined conversions
// compile with stonewt.cpp
#include <iostream>
using namespace std;
#include "stonewt.h"
int main()
{
complex a(3.0, 4.0); //initialize to (3,4i)
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;
}