1.修改程序清单11.5,使之将一系列连续的随机漫步者位置写入到文件中。对于每个位置,用步号进行标示。另外,让该程序将初始条件(目标距离和步长)以及结果小结写入到该文件中。该文件的内容与下面类似:
//clss.h
#ifndef CLASS_H_
#define CLASS_H_
#include <iostream>
namespace VECTOR
{
class Vector
{
public:
enum Mode { RECT, POL };
private:
double x;
double y;
double mag;
double ang;
Mode mode;
void set_mag();
void set_ang();
void set_x();
void set_y();
public:
Vector();
Vector(double n1,double n2,Mode form=RECT);
void reset(double n1, double n2, Mode form = RECT);
~Vector(){};
double 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& a);
friend std::ostream&
operator<<(std::ostream& os, const Vector& v);
};
}
#endif // !CLASS_H_
#include "class.h"
#include <cmath>
using std::sqrt;
using std::sin;
using std::cos;
using std::atan;
using std::atan2;
using std::cout;
using std::endl;
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.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 Vetor()--";
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 / Rad_to_deg;
set_x();
set_y();
}
else
{
cout << "Incorrect 3rd argument to Vetor()--";
cout << "vector set to 0\n";
x = y = mag = ang = 0.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& a)
{
return a*n;
}
std::ostream& operator<<(std::ostream& os, const Vector& v)
{
if (v.mode == 0)
{
os << "( x, y ) = ( " << v.x << " , " << v.y << " )" << endl;
}
else
{
os << "( m, a ) = ( " << v.mag << " , " << v.ang << " )" << endl;
}
return os;
}
}
//main.cpp
/* *************************************************
* 文件名:
* 创建人:px
* 创建时间:2020/4/15
* 描述:
************************************************* */
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include "class.h"
int main()
{
using namespace std;
using VECTOR::Vector;
ofstream fout;
fout.open("walk.txt");
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):\n";
while (cin >> target)
{
cout << "Enter step length: ";
if (!(cin >> dstep))
break;
fout << "Target Distance: " << target << ", Step Size: " << dstep; //写入到文件
while (result.magval() < target)
{
direction = rand() % 360;
step.reset(dstep, direction, Vector::POL); //以极坐标系来改变方向
result = result + step;
steps++;
step.rect_mode(); //设定为直角坐标系模式,以直角坐标系的模式保存信息
fout <<steps<< ":" <<step; //记录每个点的信息
}
cout << "After " << steps << " steps,the subject "
" has the following location:\n";
fout<< "After " << steps << " steps,the subject "
" has the following location:\n";
fout << result << endl;
cout << result << endl;
result.polar_mode(); //转换格式输出
cout << " or\n" << result << endl;
fout << " or\n" << result << endl;
cout << "Average outward distance per step = "
<< result.magval() / steps << endl;
fout << "Average outward distance per step = "
<< result.magval() / steps << endl;
//steps = 0;
//result.reset(0.0, 0.0);
//cout << "Enter target distance (q to quit): ";
}
//cout << "Bye.\n";
//cin.clear();
//while (cin.get() != '\n')
// continue;
return 0;
}
2.对Vector类的头文件(程序清单11.13)和实现文件(程序清单11.14)进行修改,使其不再存储矢量的长度和角度,而是在magval( )和angval( )被调用时计算它们。
应保留公有接口不变(公有方法及其参数不变),但对私有部分(包括一些私有方法)和方法实现进行修改。然后,使用程序清单11.15对修改后的版本进行测试,结果应该与以前相同,因为Vector类的公有接口与原来相同。
//clss.h
#ifndef CLASS_H_
#define CLASS_H_
#include <iostream>
namespace VECTOR
{
class Vector
{
public:
enum Mode { RECT, POL };
private:
double x;
double y;
//double mag; //按照题目要求,这里去掉mag和ang的定义
//double ang;
Mode mode;
//double set_mag(); //由于没了mag和ang,下面的几个函数也不再需要
//double 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; }
double yval()const { return y; }
double magval()const { return sqrt(x*x+y*y); } //这里用这两个方法来计算mag和ang
double angval()const { return (x == 0.0 && y == 0.0) ? 0.0 : atan2(y, x);}
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 // !CLASS_H_
#include "class.h"
#include <cmath>
using std::sqrt;
using std::sin;
using std::cos;
using std::atan;
using std::atan2;
using std::cout;
using std::endl;
namespace VECTOR
{
const double Rad_to_deg = 45.0 / atan(1.0);
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) //当输入时的是极坐标系时,要转换成直角坐标系保存
{
x = n1 * cos(n2);
y = n1 * sin(n2);
}
else
{
cout << "Incorrect 3rd argument to Vetor()--";
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)
{
x = n1 * cos(n2);
y = n1 * sin(n2);
}
else
{
cout << "Incorrect 3rd argument to Vetor()--";
cout << "vector set to 0\n";
x = y = 0.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& a)
{
return a * n;
}
std::ostream& operator<<(std::ostream& os, const Vector& v)
{
if (v.mode == 0)
{
os << "( x, y ) = ( " << v.x << " , " << v.y << " )" << endl;
}
else
{
os << "( m, a ) = ( " << v.magval()<< " , " << v.angval() << " )" << endl;
}
return os;
}
}
//main.cpp
/* *************************************************
* 文件名:
* 创建人:px
* 创建时间:2020/4/16
* 描述:
************************************************* */
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include "class.h"
int main()
{
using namespace std;
using VECTOR::Vector;
ofstream fout;
fout.open("walk.txt");
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):\n";
while (cin >> target)
{
cout << "Enter step length: ";
if (!(cin >> dstep))
break;
fout << "Target Distance: " << target << ", Step Size: " << dstep<< endl; //写入到文件
while (result.magval() < target)
{
direction = rand() % 360;
step.reset(dstep, direction, Vector::POL); //以极坐标系来改变方向
result = result + step;
steps++;
step.rect_mode(); //设定为直角坐标系模式,以直角坐标系的模式保存信息
fout <<steps<< ":" <<step; //记录每个点的信息
}
cout << "After " << steps << " steps,the subject "
" has the following location:\n";
fout<< "After " << steps << " steps,the subject "
" has the following location:\n";
fout << result << endl;
cout << result << endl;
result.polar_mode(); //转换格式输出
cout << " or\n" << result << endl;
fout << " or\n" << result << endl;
cout << "Average outward distance per step = "
<< result.magval() / steps << endl;
fout << "Average outward distance per step = "
<< result.magval() / steps << endl;
//steps = 0;
//result.reset(0.0, 0.0);
//cout << "Enter target distance (q to quit): ";
}
//cout << "Bye.\n";
//cin.clear();
//while (cin.get() != '\n')
// continue;
return 0;
}
3.修改程序清单11.15,使之报告N次测试中的最高、最低和平均步数(其中N是用户输入的整数),而不是报告每次测试的结果。
//clss.h
#ifndef CLASS_H_
#define CLASS_H_
#include <iostream>
namespace VECTOR
{
class Vector
{
public:
enum Mode { RECT, POL };
private:
double x;
double y;
//double mag;
//double ang;
Mode mode;
//double set_mag();
//double 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; }
double yval()const { return y; }
double magval()const { return sqrt(x*x+y*y); }
double angval()const { return (x == 0.0 && y == 0.0) ? 0.0 : atan2(y, x);}
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);
};
}
double max(double ar[], int length); //使用这三个函数来计算数组最大值、最小值和总和
double min(double ar[], int length);
double total(double ar[], int length);
#endif // !CLASS_H_
//class.cpp
#include "class.h"
#include <cmath>
using std::sqrt;
using std::sin;
using std::cos;
using std::atan;
using std::atan2;
using std::cout;
using std::endl;
namespace VECTOR
{
const double Rad_to_deg = 45.0 / atan(1.0);
//double Vector::set_mag()
//{
// double mag = sqrt(x * x + y * y);
// return mag;
//}
//double Vector::set_ang()
//{
// double ang;
// if (x == 0.0 && y == 0.0)
// ang = 0.0;
// else
// ang = atan2(y, x);
// return ang;
//}
//void Vector::set_x()
//{
// x = mag * cos(ang);
//}
//void Vector::set_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)
{
x = n1 * cos(n2);
y = n1 * sin(n2);
}
else
{
cout << "Incorrect 3rd argument to Vetor()--";
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)
{
x = n1 * cos(n2);
y = n1 * sin(n2);
}
else
{
cout << "Incorrect 3rd argument to Vetor()--";
cout << "vector set to 0\n";
x = y = 0.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& a)
{
return a * n;
}
std::ostream& operator<<(std::ostream& os, const Vector& v)
{
if (v.mode == 0)
{
os << "( x, y ) = ( " << v.x << " , " << v.y << " )" << endl;
}
else
{
os << "( m, a ) = ( " << v.magval()<< " , " << v.angval() << " )" << endl;
}
return os;
}
}
double max(double ar[],int length) //计算数组中的最大值
{
double nmax = ar[0];
for (int i = 0;i < length;i++)
{
if (ar[i] > nmax)
nmax = ar[i];
}
return nmax;
}
double min(double ar[], int length)
{
double nmin = ar[0];
for (int i = 0;i < length;i++)
{
if (ar[i] < nmin)
nmin= ar[i];
}
return nmin;
}
double total(double ar[], int length)
{
double sum = 0.0;
for (int i = 0;i < length;i++)
{
sum += ar[i];
}
return sum;
}
//main.cpp
/* *************************************************
* 文件名:
* 创建人:px
* 创建时间:2020/4/16
* 描述:
************************************************* */
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "class.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;
int N;
int i;
double nsteps[100]; //最多记录100次结果
double nmax;
double nmin;
double ntotal = 0.0;
double naverage;
cout << "Enter target distance (q to quit):\n";
if (cin >> target)
{
cout << "Enter step length: ";
cin >> dstep;
cout << "Enter times: ";
cin >> N;
for ( i = 0;i < N;i++)
{
while (result.magval() < target)
{
direction = rand() % 360;
step.reset(dstep, direction, Vector::POL); //以极坐标系来改变方向
result = result + step;
steps++;
step.rect_mode(); //设定为直角坐标系模式,以直角坐标系的模式保存信息
}
nsteps[i] = steps; //记录下每次步数的结果
cout << "After " << steps << " steps,the subject "
" has the following location:\n";
cout << result;
result.polar_mode(); //转换格式输出
cout << " or\n" << result;
cout << "Average outward distance per step = "
<< result.magval() / steps << endl;
steps = 0; //复位到初始值
result.reset(0.0, 0.0);
}
}
nmax = max(nsteps, i);
nmin = min(nsteps, i);
ntotal = total(nsteps, i);
naverage = ntotal / N;
cout << "Max:" << nmax << endl;
cout << "Min: " << nmin << endl;
cout << "Average: " << naverage << endl;
return 0;
}
4.重新编写最后的Time类示例(程序清单11.10、程序清单11.11和程序清单11.12),使用友元函数来实现所有的重载运算符。
//clss.h
#ifndef CLASS_H_
#define CLASS_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& t, const Time& a);
friend Time operator-(const Time& t, const Time& a);
friend Time operator*(const Time& t, double m); //
friend Time operator*(double m, const Time& t);
friend std::ostream & operator<<(std::ostream& os, const Time& t);
};
#endif // !CLASS_H_
//class.cpp
#include "class.h"
#include <iostream>
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& t, const Time& a)
{
Time temp;
int minutes;
minutes = t.minutes + a.minutes;
temp.minutes = minutes % 60;
temp.hours = t.hours + a.hours + minutes / 60;
return temp;
}
Time operator-(const Time& t, const Time& a)
{
Time temp;
int minutes1 = t.hours * 60 + t.minutes;
int minutes2 = a.hours * 60 + a.minutes;
int minute = minutes1 - minutes2;
temp.minutes = minute % 60;
temp.hours = minute / 60;
return Time();
}
Time operator*(const Time& t, double m)
{
Time temp;
double minute;
//temp.minutes = (m * t.minutes) % 60; //%求余运算符要求被除数是整数,这样写这里会报错
minute = m * t.minutes;
temp.hours = t.hours * m + minute / 60;
temp.minutes = m * t.minutes;
temp.minutes %= 60;
return temp;
}
Time operator*(double m, const Time& t)
{
Time temp;
double minute;
//temp.minutes = (m * t.minutes) % 60; //%求余运算符要求被除数是整数,这样写这里会报错
minute = m * t.minutes;
temp.hours = t.hours * m + minute / 60;
temp.minutes = m * t.minutes;
temp.minutes %= 60;
return temp;
}
std::ostream & operator<<(std::ostream& os, const Time& t)
{
os << t.hours << " hours, " << t.minutes << " minutes";
return os;
}
//main.cpp
/* *************************************************
* 文件名:
* 创建人:px
* 创建时间:2020/4/16
* 描述:
************************************************* */
#include <iostream>
#include "class.h"
int main()
{
using namespace std;
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.重新编写Stonewt类(程序清单11.16和程序清单11.17),使它有一个状态成员,由该成员控制对象应转换为英石格式、整数磅格式还是浮点磅格式。重载<<运算符,使用它来替换show_stn( )和show_lbs( )方法。重载加法、减法和乘法运算符,以便可以对Stonewt值进行加、减、乘运算。编写一个使用所有类方法和友元的小程序,来测试这个类。
//clss.h
#ifndef CLASS_H_
#define CLASS_H_
#include <iostream>
class Stonewt
{
public:
Stonewt(double lbs);
Stonewt(int stn, double lbs);
Stonewt();
~Stonewt() {};
void setstn() { form = STN; }
void setpds() { form = PDS; }
void setpdsl() { form = PDSL; }
Stonewt operator+(const Stonewt& b);
Stonewt operator-(const Stonewt& b);
Stonewt operator*(const double n);
friend Stonewt operator*(const double n, const Stonewt& b);
friend std::ostream& operator<<(std::ostream& os, const Stonewt& st);
private:
enum Mode { STN, PDS, PDSL };
Mode form;
enum { Lbs_per_stn = 14 };
int stone; //英石
double pds_left; //小数磅,指除去14(1英石=14磅)的整数倍之后剩下的磅数,如15.5磅的小数磅指的是1.5,16.6磅的小数磅指的是2.6
double pounds; //磅
};
#endif // !CLASS_H_
//class.cpp
#include "class.h"
#include <iostream>
using std::cout;
using std::endl;
Stonewt::Stonewt(double lbs)
{
stone = int(lbs) / Lbs_per_stn;
pds_left = int(lbs) % Lbs_per_stn + lbs - int(lbs);
pounds = lbs;
form = STN;
}
Stonewt::Stonewt(int stn, double lbs)
{
stone = stn;
pds_left = lbs;
pounds = (double)stn * Lbs_per_stn + lbs;
form = STN;
}
Stonewt::Stonewt()
{
stone = 0;
pounds = pds_left = 0.0;
form = STN;
}
Stonewt Stonewt::operator+(const Stonewt& b)
{
pounds = pounds + b.pounds;
return Stonewt(pounds);
}
Stonewt Stonewt::operator-(const Stonewt& b)
{
pounds = pounds - b.pounds;
return Stonewt(pounds);
}
Stonewt Stonewt::operator*(const double n)
{
pounds = n * pounds;
return Stonewt(pounds);
}
Stonewt operator*(const double n, const Stonewt& b)
{
double temp;
temp= n * b.pounds;
return Stonewt(temp);
}
std::ostream& operator<<(std::ostream& os, const Stonewt& st)
{
if (st.form == Stonewt::Mode::STN)
{
os << "Stone: " << st.stone;
}
else if (st.form == Stonewt::Mode::PDS)
{
os << "Pounds: " << st.pounds;
}
else
{
os << "Pounds left: " << st.pds_left;
}
return os;
}
//main.cpp
/* *************************************************
* 文件名:
* 创建人:px
* 创建时间:2020/4/16
* 描述:
************************************************* */
#include <iostream>
#include "class.h"
int main()
{
using namespace std;
Stonewt a(10, 2);
Stonewt b(20);
Stonewt c;
cout << a << b << c << endl;
c = a + b;
c.setpds();
cout << "a + b = " << c << " stones." << endl;
c = b - a;
cout << "b - a = " << c << " pounds." << endl;
c = a * 5;
cout << "a * 5 = " << c << "pounds.\n";
return 0;
}
6.重新编写Stonewt类(程序清单11.16和程序清单11.17),重载全部6个关系运算符。运算符对pounds成员进行比较,并返回一个bool值。编写一个程序,它声明一个包含6个Stonewt对象的数组,并在数组声明中初始化前3个对象。然后使用循环来读取用于设置剩余3个数组元素的值。接着报告最小的元素、最大的元素以及大于或等于11英石的元素的数量(最简单的方法是创建一个Stonewt对象,并将其初始化为11英石,然后将其同其他对象进行比较)。
//clss.h
#ifndef CLASS_H_
#define CLASS_H_
#include <iostream>
class Stonewt
{
public:
Stonewt(double lbs);
Stonewt(int stn, double lbs);
Stonewt();
~Stonewt() {};
void setstn() { form = STN; }
void setpds() { form = PDS; }
void setpdsl() { form = PDSL; }
Stonewt operator+(const Stonewt& b);
Stonewt operator-(const Stonewt& b);
bool operator<(const Stonewt& b);
bool operator>(const Stonewt& b);
Stonewt operator*(const double n);
friend Stonewt operator*(const double n, const Stonewt& b);
friend std::ostream& operator<<(std::ostream& os, const Stonewt& st);
private:
enum Mode { STN, PDS, PDSL };
Mode form;
enum { Lbs_per_stn = 14 };
int stone; //英石
double pds_left; //小数磅,指除去14(1英石=14磅)的整数倍之后剩下的磅数,如15.5磅的小数磅指的是1.5,16.6磅的小数磅指的是2.6
double pounds; //磅
};
#endif // !CLASS_H_
//class.cpp
#include "class.h"
#include <iostream>
using std::cout;
using std::endl;
Stonewt::Stonewt(double lbs)
{
stone = int(lbs) / Lbs_per_stn;
pds_left = int(lbs) % Lbs_per_stn + lbs - int(lbs);
pounds = lbs;
form = PDS;
}
Stonewt::Stonewt(int stn, double lbs)
{
stone = stn;
pds_left = lbs;
pounds = (double)stn * Lbs_per_stn + lbs;
form = PDS;
}
Stonewt::Stonewt()
{
stone = 0;
pounds = pds_left = 0.0;
form = PDS;
}
Stonewt Stonewt::operator+(const Stonewt& b)
{
pounds = pounds + b.pounds;
return Stonewt(pounds);
}
Stonewt Stonewt::operator-(const Stonewt& b)
{
pounds = pounds - b.pounds;
return Stonewt(pounds);
}
bool Stonewt::operator<(const Stonewt& b)
{
if (pounds < b.pounds)
return true;
else
return false;
}
bool Stonewt::operator>(const Stonewt& b)
{
if (pounds > b.pounds)
return true;
else
return false;
}
Stonewt Stonewt::operator*(const double n)
{
pounds = n * pounds;
return Stonewt(pounds);
}
Stonewt operator*(const double n, const Stonewt& b)
{
double temp;
temp= n * b.pounds;
return Stonewt(temp);
}
std::ostream& operator<<(std::ostream& os, const Stonewt& st)
{
if (st.form == Stonewt::Mode::STN)
{
os << "Stone: " << st.stone;
}
else if (st.form == Stonewt::Mode::PDS)
{
os << "Pounds: " << st.pounds;
}
else
{
os << "Pounds left: " << st.pds_left ;
}
return os;
}
//main.cpp
/* *************************************************
* 文件名:
* 创建人:px
* 创建时间:2020/4/17
* 描述:
************************************************* */
#include <iostream>
#include "class.h"
int main()
{
using namespace std;
Stonewt st[6] = { 14,15,16 };
Stonewt max, min;
Stonewt cmpsd(11,0); //比较标准,11英石
int num = 0;
for (int i = 0;i < 3;i++)
{
double temppds;
cout << "Enter pounds: \n";
cin >> temppds;
st[i + 3] = temppds;
}
max = st[0];
min = st[0];
for (int i = 0;i < 6;i++)
{
if (st[i] > max)
max = st[i];
if (st[i] < min)
min = st[i];
if (st[i] > cmpsd)
{
cout << st[i] << " is biger than standard.\n";
num++;
}
cout << num << " objects are biger than standard.\n";
}
return 0;
}
7.复数有两个部分组成:实数部分和虚数部分。复数的一种书写方式是:(3.0,4.0),其中,3.0是实数部分,4.0是虚数部分。假设a = (A, Bi),c = (C, Di),则下面是一些复数运算。
● 加法:a + c = (A+C, (B+D)i)。
● 减法:a – c = (A−C, (B−D)i)。
● 乘法:a * c = (AC−BD, (AD + BC)i)。
● 乘法::x*c = (x * C, x *Di),其中x为实数。
● 共轭:~a = (A, −Bi)。
请定义一个复数类,以便下面的程序可以使用它来获得正确的结果。
#include <iostream>
#include "class.h"
int main()
{
using namespace std;
complex a(3.0, 4.0);
complex c;
cout << "Enter a complex number (q to quit):\n";
while (cin >> c)
{
cout << "c is " << c << '\n';
cout << "complex conjugate is " << ~c << '\n';
cout << "a is " << a << '\n';
cout << "a + c is " << a + c << '\n';
cout << "a - c is " << a - c << '\n';
cout << "a * c is " << a * c << '\n';
cout << "2 * c is " << 2 * c << '\n';
cout << "Enter a complex number(q to quit):\n";
}
cout << "Done!\n";
return 0;
}
Enter a complex number (q to quit):
real: 10
imaginary: 12
c is (10 , 12i )
complex conjugate is (10 , -12i )
a is (3 , 4i )
a + c is (13 , 16i )
a - c is (-7 , -8i )
a * c is (-18 , 76i )
2 * c is (20 , 24i )
Enter a complex number(q to quit):
//clss.h
#ifndef CLASS_H_
#define CLASS_H_
#include <iostream>
class complex
{
public:
complex();
complex(double a, double b);
~complex() { };
complex operator+(const complex& com);
complex operator-(const complex& com);
complex operator*(const complex& com);
complex operator~();
friend complex operator*(double x, const complex& com);
friend std::ostream& operator<<(std::ostream& os, const complex& com);
friend std::istream& operator>>(std::istream& is, complex& com);
private:
double real;
double imag;
};
#endif // !CLASS_H_
//class.cpp
#include "class.h"
#include <iostream>
complex::complex()
{
real = imag= 0.0;
}
complex::complex(double a, double b)
{
real = a;
imag = b;
}
complex complex::operator+(const complex& com)
{
return complex(real+com.real,imag+com.imag);
}
complex complex::operator-(const complex& com)
{
return complex(real - com.real, imag - com.imag);
}
complex complex::operator*(const complex& com)
{
return complex((real * com.real - imag * com.imag), (real * com.imag + imag * com.real));
}
complex complex::operator~()
{
return complex(real,-imag);
}
complex operator*(double x, const complex& com)
{
return complex(x*com.real,x*com.imag);
}
std::ostream& operator<<(std::ostream& os, const complex& com)
{
os <<"(" <<com.real << " , " << com.imag << "i )";
return os;
}
std::istream& operator>>(std::istream& is, complex& com)
{
using std::cout;
cout << "real: ";
is >> com.real;
cout << "imaginary: ";
is >> com.imag;
return is;
}
//main.cpp
/* *************************************************
* 文件名:
* 创建人:px
* 创建时间:2020/4/17
* 描述:
************************************************* */
#include <iostream>
#include "class.h"
int main()
{
using namespace std;
complex a(3.0, 4.0);
complex c;
cout << "Enter a complex number (q to quit):\n";
while (cin >> c)
{
cout << "c is " << c << '\n';
cout << "complex conjugate is " << ~c << '\n';
cout << "a is " << a << '\n';
cout << "a + c is " << a + c << '\n';
cout << "a - c is " << a - c << '\n';
cout << "a * c is " << a * c << '\n';
cout << "2 * c is " << 2 * c << '\n';
cout << "Enter a complex number(q to quit):\n";
}
cout << "Done!\n";
return 0;
}