//1题
#include
#include
#include
#include “vector.hpp”
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.0;
double target;
double dstep;
int i = 0;
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);
cout << steps <<": (x, y) = " <<"(" <<result.xval()
<<", " <<result.yval() <<")\n";
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;
}
//2题
//类定义
#ifndef vector_hpp
#define vector_hpp
#include
namespace VECTOR
{
class Vector
{
//枚举
public:
enum Mode {RECT, POL}; //rect 直角坐标, pol 极坐标
private:
//类私有成员
double x; //矢量X分量
double y; //矢量Y分量
Mode mode; //矢量两种表示方式切换
double set_mag()const; //计算矢量单位, 用私有函数来代替原私有double类型 mag
double set_ang()const; //计算矢量弧度, 用私有函数来代替原私有double类型 ang
void set_x(double m, double a); //计算分量X ,需要接受原mag,ang 的数值
void set_y(double m, double a); //计算分量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;}
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 &a);
friend std::ostream & operator<<(std::ostream & os, const Vector & v);
};
}
#endif /* vector_hpp */
//类函数定义
#include
#include “vector.hpp”
using std::sqrt;
using std::sin;
using std::cos;
using std::atan;
using std::atan2;
using std::cout;
namespace VECTOR
{
//1弧度大约 = 57.2957795130823度 固定的弧度转换度的方式
const double Rad_to_deg = 45.0 / atan(1.0);
//通过矢量的分量x y来计算矢量的单位 x的平方 + y的平方 的平方根 = mag
double Vector::set_mag()const
{
double mag = sqrt(x * x + y * y);
return mag;
}
//
double Vector::set_ang()const
{
double ang;
if (x == 0.0 && y == 0.0)
return ang = 0.0;
else
return ang = atan2(y, x);
}
void Vector::set_x(double m, double a)
{
x = m * cos(a / Rad_to_deg);
}
void Vector::set_y(double m, double a)
{
y = m * sin(a / Rad_to_deg);
}
//计算总步伐的距离
double Vector::magval()const
{
double mag = 0.0;
mag = set_mag();
return mag;
}
double Vector::angval()const
{
double ang = 0.0;
ang = set_ang();
return 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);
set_y(n1, n2);
}
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);
set_y(n1, n2);
}
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(n * x, n * y);
}
Vector operator*(double n, const Vector & a)
{
return a * n;
}
std::ostream & operator<<(std::ostream & os, const Vector & v)
{
if (v.mode == Vector::RECT)
os <<"(x,y) = (" << v.x << ", " << v.y << ")";
else if (v.mode == Vector::POL)
{
os <<"(m,a) = (" << v.magval() <<", "
<< v.angval() * Rad_to_deg << ")";
}
else
os <<"Vector object mode is invalid";
return os;
}
}
//3题
#include
#include
#include
#include “vector.hpp”
int main()
{
using namespace std;
using VECTOR::Vector;
srand(time(0));
double direction;
Vector step;
Vector result(0.0, 0.0);
Vector lowest; //最高
Vector highest; //最低
unsigned long total_steps = 0.0; //总步数
unsigned long steps1 = 0.0; //最高的步数
unsigned long steps2 = 0.0; //每次的步数
unsigned long steps3 = 0.0; //最低的步数
double target;
double dstep;
int n;
cout <<"请输入需要测试的次数: ";
cin >> n;
cout <<"Enter target distance (q to quit): ";
cin >> target;
cout <<"Enter step length: ";
cin >> dstep;
while (cin.get() != ‘\n’)
continue;
for (int i = 0; i < n; i++)
{
while (result.magval() < target)
{
direction = rand() % 360;
step.reset(dstep, direction, Vector::POL);
result = result + step;
steps2++;
}
if (i == 0)
steps1 = steps3 = steps2;
else if (steps1 < steps2)
steps1 = steps2;
else if (steps3 > steps2)
steps3 = steps2;
total_steps += steps2;
result.reset(0.0, 0.0);
steps2 = 0;
}
cout << n <<"次测试中,最高步数 = " << steps1 << endl;
cout << n <<"次测试中,最低步数 = " << steps3 << endl;
cout << n <<"次测试中,平均步数 = " << total_steps / n << endl;
cout <<“Bye!\n”;
cin.clear();
while (cin.get() != ‘\n’)
continue;
return 0;
}
//4题
//类定义
#ifndef mytime3_hpp
#define mytime3_hpp
#include
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 & my, const Time & t);
friend Time operator-(const Time & my, const Time & t);
friend Time operator*(const Time & my, double n);
friend Time operator*(double m, const Time & t)
{return t * m;} //内联函数
friend std::ostream & operator<<(std::ostream & os, const Time & t);
};
#endif /* mytime3_hpp */
//类函数定义
#include
#include “mytime.hpp”
Time::Time()
{
hours = 0;
minutes = 0;
}
Time::Time(int h, int m)
{
hours = h;
minutes = m;
}
void Time::AddMin(int m)
{
minutes += m;
hours += m / 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 & my, const Time & t)
{
Time sum;
sum.minutes =my.minutes + t.minutes;
sum.hours = my.hours + t.hours + sum.minutes / 60;
sum.minutes = sum.minutes % 60;
return sum;
}
Time operator-(const Time & my, const Time & t)
{
Time diff;
int tot1, tot2;
tot1 = my.hours * 60 + my.minutes;
tot2 = t.hours * 60 + t.minutes;
diff.hours = (tot1 - tot2) / 60;
diff.minutes = (tot1 - tot2) % 60;
return diff;
}
Time operator*(const Time & my, double n)
{
Time result;
long totalminutes = (my.hours * 60 + my.minutes) * n;
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;
}
//main()
#include
#include “mytime.hpp”
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;
}
//5题
//类定义
#ifndef stonewt_hpp
#define stonewt_hpp
class Stonewt
{
public:
struct Stone
{
int stone;
double pds_lift;
};
//英石, 整数磅, 浮点数磅, 1英石 = 14 磅
enum Mode{pds, int_p, float_p, Lbs_per_stn = 14};
private:
Stone stones; //英石
int pound; //整数磅
double pounds; //浮点数 磅
Mode from = pds;
public:
Stonewt(const double lbs); //接受浮点数磅
Stonewt(const int lbs); //接受整数磅
Stonewt(const int stn, const double p); //英石
Stonewt();
~Stonewt();
void set_pound();
void set_pounds();
void set_stone();
Stonewt operator+(Stonewt & s);
Stonewt operator-(Stonewt & s);
Stonewt operator*(const double d);
friend void mode_(Stonewt & s, Mode f);
friend Stonewt operator*(const double d, Stonewt & s);
friend std::ostream & operator<<(std::ostream & os,const Stonewt & s);
};
#endif /* stonewt_hpp */
//类函数定义
#include
#include “stonewt.hpp”
void Stonewt::set_pound()
{
if (from == pds)
pound = (int)(stones.stone * Lbs_per_stn + stones.pds_lift);
else if(from == float_p)
pound = (int)pounds;
}
void Stonewt::set_pounds()
{
if (from == pds)
pounds = stones.stone * Lbs_per_stn + stones.pds_lift;
else if (from == int_p)
pounds = pound * 1.0;
}
void Stonewt::set_stone()
{
if (from == int_p)
{
stones.stone = pound / Lbs_per_stn;
stones.pds_lift = pound % Lbs_per_stn;
}
else if (from == float_p)
{
stones.stone = pounds / Lbs_per_stn;
stones.pds_lift = (int)pounds % Lbs_per_stn + pounds - (int)pounds;
}
}
Stonewt::Stonewt(const double lbs) //接受浮点数磅
{
from = float_p;
pounds = lbs;
set_stone();
set_pound();
}
Stonewt::Stonewt(const int lbs) //接受整数磅
{
from = int_p;
pound = lbs;
set_stone();
set_pounds();
}
Stonewt::Stonewt(const int stn, const double p) //接受英石
{
from = pds;
stones.stone = stn;
stones.pds_lift = p;
set_pound();
set_pounds();
}
Stonewt::Stonewt()
{
from = pds;
stones.stone = 0;
stones.pds_lift = 0.0;
}
Stonewt::~Stonewt()
{
}
//重载运算符为了方便,同一为浮点数磅
Stonewt Stonewt::operator+(Stonewt & s)
{
return Stonewt(pounds + s.pounds);
}
Stonewt Stonewt::operator-(Stonewt & s)
{
return Stonewt(pounds - s.pounds);
}
Stonewt Stonewt::operator*(const double d)
{
return Stonewt(pounds * d);
}
Stonewt operator*(const double d, Stonewt & s)
{
return s * d;
}
void mode_(Stonewt & s, Stonewt::Mode f)
{
s.from = f;
}
std::ostream & operator<<(std::ostream & os,const Stonewt & s)
{
if (s.from == Stonewt::pds)
os << s.stones.stone <<" Stone, " << s.stones.pds_lift <<" pound\n";
else if (s.from == Stonewt::int_p)
os << s.pound <<" pound\n";
else if (s.from == Stonewt::float_p)
os << s.pounds <<" pound\n";
else
os <<"Stonewt object mode is invalid\n";
return os;
}
//main()
#include
#include “stonewt.hpp”
int main()
{
using namespace std;
Stonewt st1(155.43);
Stonewt st2(166);
Stonewt st3(12, 7.5);
Stonewt st4;
cout << st1;
mode_(st1, Stonewt::int_p);
cout << st1;
cout << st2;
mode_(st2, Stonewt::pds);
cout << st2;
cout << st3;
cout << st3 + st1;
mode_(st3, Stonewt::int_p);
cout << st3;
st4 = 1.5 * st3;
cout << st4;
return 0;
}
//6题
//类定义
#include
#ifndef stonewt_hpp
#define stonewt_hpp
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;
bool operator<(Stonewt & s)const;
bool operator<=(Stonewt & s)const;
bool operator>(Stonewt & s)const;
bool operator>=(Stonewt & s)const;
bool operator==(Stonewt & s)const;
bool operator!=(Stonewt & s)const;
friend std::ostream & operator<<(std::ostream & os,const Stonewt & s);
};
#endif /* stonewt_hpp */
//类函数定义
#include
#include “stonewt.hpp”
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_stn()const
{
cout << stone <<"stone, " << pds_left <<“pounds\n”;
}
void Stonewt::show_lbs()const
{
cout << pounds <<“pounds\n”;
}
Stonewt::operator int ()const
{
return int (pounds + 0.5);
}
Stonewt::operator double () const
{
return pounds;
}
bool Stonewt::operator<(Stonewt & s)const
{
if (pounds < s.pounds)
return true;
else
return false;
}
bool Stonewt::operator<=(Stonewt & s)const
{
if (pounds <= s.pounds)
return true;
else
return false;
}
bool Stonewt::operator>(Stonewt & s)const
{
if (pounds > s.pounds)
return true;
else
return false;
}
bool Stonewt::operator>=(Stonewt & s)const
{
if (pounds >= s.pounds)
return true;
else
return false;
}
bool Stonewt::operator==(Stonewt & s)const
{
if (pounds == s.pounds)
return true;
else
return false;
}
bool Stonewt::operator!=(Stonewt & s)const
{
if (pounds != s.pounds)
return true;
else
return false;
}
std::ostream & operator<<(std::ostream & os,const Stonewt & s)
{
os << s.pounds <<" pound\n";
return os;
}
//main()
#include
using std::cout;
#include “stonewt.hpp”
int main()
{
double stf = 0.0;
int number = 0;
Stonewt st(11,0.0);
Stonewt mun[6] {
mun[0] = Stonewt(360.0),
mun[1] = Stonewt(222.2),
mun[2] = Stonewt(11, 0.0),
};
for (int i = 3; i < 6; i++)
{
cout <<"请输入剩余" <<6 - i <<"个数组元素的值: ";
std::cin >> stf;
mun[i] = Stonewt(stf);
while(std::cin.get() != '\n')
continue;
}
Stonewt max = mun[0];
for (int i = 0; i < 6; i++)
{
if (max < mun[i])
max = mun[i];
}
Stonewt min = mun[0];
for (int i = 0; i < 6; i++)
{
if (min > mun[i])
min = mun[i];
}
for (int i = 0; i < 6; i++)
{
if (st <= mun[i])
number++;
}
cout <<"最小的元素 = " << min << std::endl;
cout <<"最大的元素 = " << max << std::endl;
cout <<"大于等于11英石的有 " << number <<" 个元素.\n";
return 0;
}
//8题
//类定义
#ifndef complex0_hpp
#define complex0_hpp
#include
class Complex
{
private:
int Real_num; //实数
int Imag_num; //虚数
public:
Complex(); //默认构造函数
Complex(int a, int b);
~Complex();
Complex operator+(const Complex & c)const;
Complex operator-(const Complex & c)const;
Complex operator*(int n)const;
Complex operator~()const;
Complex operator*(const Complex & c)const;
friend std::ostream & operator<<(std::ostream & os, const Complex & c);
friend std::istream & operator>>(std::istream & is, Complex & c);
friend Complex operator*(int n, const Complex & c);
};
#endif /* complex0_hpp */
//类函数定义
#include “complex0.hpp”
Complex::Complex() //默认构造函数
{
Real_num = 0;
Imag_num = 0;
}
Complex::Complex(int a, int b)
{
Real_num = a;
Imag_num = b;
}
Complex::~Complex()
{
}
Complex Complex::operator+(const Complex & c)const
{
return Complex(Real_num + c.Real_num, Imag_num + c.Imag_num);
}
Complex Complex::operator-(const Complex & c)const
{
return Complex(Real_num - c.Real_num, Imag_num - c.Imag_num);
}
Complex Complex::operator*(int n)const
{
return Complex(n * Real_num , n * Imag_num);
}
Complex Complex::operator*(const Complex & c)const
{
return Complex(Real_num * c.Real_num - Imag_num * c.Imag_num,
Real_num * c.Imag_num + Imag_num * c.Real_num);
}
Complex Complex::operator~()const
{
return Complex(Real_num, -Imag_num);
}
std::ostream & operator<<(std::ostream & os, const Complex & c)
{
os <<"(" << c.Real_num <<", " << c.Imag_num <<“i)”;
return os;
}
std::istream & operator>>(std::istream & is, Complex & c)
{
std::cout <<"real: ";
is >> c.Real_num ;
std::cout <<"imaginary: ";
is >> c.Imag_num;
return is;
}
Complex operator*(int n, const Complex & c)
{
return c * n;
}
//main()
#include
using namespace std;
#include “complex0.hpp”
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 <<"compex 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;
}