这是我创建的专辑,所有学习笔记都以word格式上传,放在里面,相对更美观和直接。欢迎下载浏览。

http://download.csdn.net/album/detail/2971

1.修改程序清单11.5,使之将一系列连续的随机漫步者位置写入到文件中。对于每个位置,用步号进行标志。另外,让该程序将初始条件(目标距离和步长)以及结果小结写入到该文件中。该文件的内容与下面类似:

Target Distance: 100, Step Size: 20

0: (x,y) = (0, 0)

1: (x,y) = (-11.4715, 16.383)

2: (x,y) = (-868807, -3.42232)

....

26: (x,y) = (42.2919, -78.2594)

27: (x,y) = (58.6749, -89.7309)

After 27 steps, the subject has the following location:

(x,y) = (58.6749, -89.7309)

or

(m,a) = (107.212, -56.8194)

Average outward distance per step = 3.97081

答:

注:这里我不修改源文件了,在自己的基础上,重新写一个

//Point.h 包含类定义
#pragma once
class Point
{
public:enum mode { xy = 0, ji = 1 };  //常量
private:double x;   //x,y坐标double y;double jiaodu;  //极坐标:角度和长度double changdu;int mode;  //模式,根据模式来决定坐标输出时的形式void setxy();    //设置x、y坐标(根据极坐标)void setji(); //设置极坐标(根据x,y坐标)
public:Point(double a = 0, double b = 0, int c = xy);    //默认构造函数。默认为x,y坐标赋值void reset(double a = 0, double b = 0, int c = xy);    //重置坐标void show(std::ostream &os)const; //输出坐标double get_changdu() { return changdu; }  //返回极坐标的长度Point operator+(const Point& m)const;    //运算符+重载,不用引用是因为要创建一个新对象,然后返回这个对象friend std::ostream& operator<<(std::ostream &os, const Point&m); //用于cout或其他输出
};//Point.cpp 包含了Point类的成员函数以及友元函数定义
#include<iostream>
#include<cmath>
#include<fstream>
#include"Point.h"
void Point::setxy() //设置x、y坐标(根据极坐标),函数内容是拿以前的改改
{const double jiao_to_angle = 57.29577951; //角度/弧度的值大概是这个数double hudu = jiaodu / jiao_to_angle;   //角度除以这个值得到弧度y = sin(hudu)*changdu;    //对边(y) = sin弧度*斜边。x = cos(hudu)*changdu; //临边(x) = sin弧度*斜边
}void Point::setji()    //设置极坐标(根据x,y坐标)
{const double jiao_to_angle = 57.29577951; //角度/弧度的值大概是这个数changdu = sqrt(x*x + y*y); //利用坐标求距离jiaodu = atan2(y, x)*jiao_to_angle;   //利用坐标求角度
}Point::Point(double a, double b, int c)
{if (c == xy){x = a;y = b;mode = c;setji();}else if (c == ji){jiaodu = a;changdu = b;mode = c;setxy();}else std::cout << "参数输入错误。如果你需要输入x、y坐标,请第三个参数输出0或者不输入第三个参数;如果你需要输入极坐标,请第三个参数输入1。" << std::endl;
}
void Point::reset(double a, double b, int c)
{if (c == xy){x = a;y = b;setji();}else if (c == ji){jiaodu = a;changdu = b;setxy();}else std::cout << "参数输入错误。如果你需要输入x、y坐标,请第三个参数输出0或者不输入第三个参数;如果你需要输入极坐标,请第三个参数输入1。" << std::endl;
}
void Point::show(std::ostream &os)const //输出坐标
{os << "现在报告x,y坐标:" << std::endl;os << "x:" << x << ",y:" << y << std::endl;os << "现在报告极坐标:" << std::endl;os << "长度为:" << changdu << ",角度为:" << jiaodu << "度" << std::endl;
}
Point Point::operator+(const Point& m)const    //运算符+重载,不用引用是因为要创建一个新对象,然后返回这个对象
{Point q;q.x = x + m.x;q.y = y + m.y;q.mode = mode;    //默认模式为运算符前面的对象的显示模式q.setji();return q;
}
std::ostream& operator<<(std::ostream &os, const Point&m)
{os << " (x,y) = (" << m.x << ", " << m.y << ")";return os;
}//1.cpp main()函数所在,用于使用和验证类
#include<iostream>
#include<fstream>
#include"Point.h"
#include<ctime>int main()
{using namespace std;srand(time(0));ofstream q; //创建osftream对象q.open("111.txt");  //打开111.txt文件int distance;cout << "请输入距离:";cin >> distance;int step_size;cout << "请输入每一步的距离:";cin >> step_size;Point m(0, 0);q << "0:" << m << endl;int steps;for (steps = 0;m.get_changdu() < distance;steps++){double d = rand() % 360;Point newone(d, step_size, 1);m = m + newone;q << steps+1 << ":" << m << endl;}q << "After " << steps << " steps, the subject has the following location:" << endl;m.show(q);q << "Average outward distance per step = " << m.get_changdu() / steps << endl;system("pause");return 0;
}

文本文件结果:

0: (x,y) = (0, 0)
1: (x,y) = (-8.45237, 18.1262)
2: (x,y) = (7.09055, 30.7126)
3: (x,y) = (24.0515, 20.1142)
4: (x,y) = (4.05152, 20.1142)
5: (x,y) = (-0.447505, 0.626777)
6: (x,y) = (-5.62389, 19.9453)
7: (x,y) = (7.75873, 34.8082)
8: (x,y) = (27.0772, 39.9846)
9: (x,y) = (24.6399, 59.8355)
10: (x,y) = (33.4073, 77.8114)
11: (x,y) = (17.4346, 65.7751)
12: (x,y) = (15.6915, 45.8512)
13: (x,y) = (25.6915, 63.1717)
14: (x,y) = (6.20405, 67.6707)
15: (x,y) = (19.0598, 82.9916)
16: (x,y) = (38.0809, 76.8113)
17: (x,y) = (23.6941, 62.9181)
18: (x,y) = (43.1001, 67.7565)
19: (x,y) = (53.9928, 50.9831)
20: (x,y) = (73.3987, 55.8216)
21: (x,y) = (67.5513, 74.9477)
After 21 steps, the subject has the following location:
现在报告x,y坐标:
x:67.5513,y:74.9477
现在报告极坐标:
长度为:100.898,角度为:47.9712度
Average outward distance per step = 4.80465

2.对于Vector类的头文件(程序清单11.13)和实现文件(程序清单11.14)进行修改,使其不再储存矢量的长度和角度,而是在magval()和angval()被调用时计算它们。

应保留公有接口不变(公有方法及其参数不变),但对私有部分(包括一些私有方法)和方法实现进行修改。然后,使用程序清单11.15对修改后的版本进行测试,结果应该与以前相同,因为Vecotr类的公有接口与原来相同。

答:

//vect.cpp
#include<cmath>
#include"vect.h"
using std::sqrt;
using std::sin;
using std::atan;
using std::atan2;
using std::cout;namespace VECTOR
{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.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;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;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() << ")";}elseos << "Vector object mode is invalid";return os;}
}//vect.cpp
#include<cmath>
#include"vect.h"
using std::sqrt;
using std::sin;
using std::atan;
using std::atan2;
using std::cout;namespace VECTOR
{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.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;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;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() << ")";}elseos << "Vector object mode is invalid";return os;}
}//randwalk.cpp
#include<iostream>
#include<cstdlib>
#include<ctime>
#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;while (result.magval()<target){direction = rand() % 360;step.reset(dstep, direction, Vector::POL);result = result + step;steps++;}cout << "After " << steps << " steps, the subjct 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;
}

3.修改程序清单11.15,使之报告N次测试中的最高、最低和平均步数(其中N是用户输入的整数),而不是报告每次测试的结果。

答:

为了方便,我在编程练习2的基础上修改。

//randwalk.cpp
#include<iostream>
#include<cstdlib>
#include<ctime>
#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 << "输入你想测试的次数:";int N;cin >> N;if (N == 0)break;while (result.magval()<target){direction = rand() % 360;step.reset(dstep, direction, Vector::POL);result = result + step;steps++;}double Max, Min, Average;Max = Min = Average = steps;for (int i = 1;i < N;i++){steps = 0;result.reset(0.0, 0.0);while (result.magval()<target){direction = rand() % 360;step.reset(dstep, direction, Vector::POL);result = result + step;steps++;}if (Max < steps)Max = steps;if (Min > steps)Min = steps;Average = Average + steps;}Average = Average / N;cout << "次数:" << N << endl;cout << "最大步数:" << Max << endl;cout << "最小步数:" << Min << endl;cout << "平均步数:" << Average << endl;cout << "Enter target distance (q to quit):";}cout << "Bye!\n";cin.clear();cin.sync();system("pause");return 0;
}

显示:

Enter target distance (q to quit):50
Enter step length: 2
输入你想测试的次数:1000
次数:1000
最大步数:3083
最小步数:72
平均步数:646.003
Enter target distance (q to quit):50
Enter step length: 2
输入你想测试的次数:1000
次数:1000
最大步数:3034
最小步数:67
平均步数:646.13
Enter target distance (q to quit):q
Bye!
请按任意键继续. . .

4.重新编写最后的Time了示例(程序清单11.10、程序清单11.11和程序清单11.12),使用友元函数来实现所有的重载运算符。

答:

//mytime3.h
#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 n);friend Time operator*(double n, const Time & t1);friend std::ostream & operator<<(std::ostream & os, const Time & t);
};
#endif//mytime3.cpp
#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)
{int minutes, hours;minutes = t1.minutes + t2.minutes;hours = t1.hours + t2.hours + minutes / 60;minutes %= 60;Time sum(hours, minutes);return sum;
}Time operator-(const Time & t1, const Time & t2)
{int tot1, tot2;int minutes, hours;tot1 = t1.minutes + 60 * t1.hours;tot2 = t2.minutes + 60 * t2.hours;minutes = (tot1 - tot2) % 60;hours = (tot1 - tot2) / 60;Time diff(hours, minutes);return diff;
}
Time operator*(const Time & t1, double n)
{int h, m;long totalminutes = t1.hours*n * 60 + t1.minutes*n;h = totalminutes / 60;m = totalminutes % 60;Time result(h, m);return result;
}
Time operator*(double n, const Time & t1)
{return t1*n;
}
std::ostream & operator<<(std::ostream & os, const Time & t)
{os << t.hours << " hours, " << t.minutes << " minutes";return os;
}//usetime3.cpp
#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;cout << "Aida + Tosca: " << temp << endl;temp = aida*1.17;cout << "Aida*1.17:" << temp << endl;cout << "10.0*Tosca: " << 10.0*tosca << endl;system("pause");return 0;
}

5.重新编写Stonewt类(程序清单11.16和程序清单11.17),使它有一个状态成员,由该状态成员控制对象应转换为英石格式、整数磅格式还是浮点磅格式。重载<<运算符,使用它来替换show_stn()和show_lbs()方法。重载加法、减法和重发运算符,以便可以对Stonewt值进行加、减、乘运算。编写一个使用所有类方法和友元的小程序,来测试这个类。

答:

// stonewt.h
#ifndef STONEWT_H_
#define STONEWT_H_
class Stonewt
{
private:enum { Lbs_per_stn = 14 };enum MODE{STONE,INTPOUNDS,FLOATPOUNDS};int stone;double pds_left;double pounds;int Mode;
public:Stonewt(double lbs);Stonewt(int stn, double lbs);Stonewt();~Stonewt();void setmode();friend std::ostream& operator<<(std::ostream& os, Stonewt &m);Stonewt operator+(Stonewt&m);Stonewt operator-(Stonewt&m);Stonewt operator*(double m);
};
#endif//stonewt.cpp
#include<iostream>
using std::cout;
using std::endl;
#include "stonewt.h"
enum MODE { STONE, INTPOUNDS, FLOATPOUNDS };
Stonewt::Stonewt(double lbs)
{stone = int(lbs) / Lbs_per_stn;pds_left = int(lbs) % Lbs_per_stn + lbs - int(lbs);pounds = lbs;Mode = INTPOUNDS;
}
Stonewt::Stonewt(int stn, double lbs)
{stone = stn;pds_left = lbs;pounds = stn*Lbs_per_stn + lbs;Mode = STONE;
}
Stonewt::Stonewt()
{stone = pounds = pds_left = 0;
}
Stonewt::~Stonewt()
{
}
void Stonewt::setmode()
{cout << "你想要以何种方式输出数据:" << endl;cout << "a.x英石y磅\tb.xx磅\tc.xx.yy磅" << endl;char a;std::cin >> a;while(!isalpha(a)){cout << "输入错误,请重新输入:";std::cin.clear();std::cin.sync();std::cin >> a;}switch (a){case'A':case'a':Mode = STONE;break;case'B':case'b':Mode = INTPOUNDS;break;case'C':case'c':Mode = FLOATPOUNDS;break;default:cout << "输入错误,默认以x英石y磅格式输出。" << endl;Mode = STONE;break;}
}
std::ostream& operator<<(std::ostream& os,Stonewt &m)
{if (m.Mode == STONE){os << m.stone << "英石" << m.pds_left << "磅";}else if (m.Mode == INTPOUNDS){os << int(m.pounds) << "磅";}else if (m.Mode == FLOATPOUNDS){os << m.pounds << "磅";}else{os << "出错,无法输出。";}return os;
}
Stonewt Stonewt::operator+(Stonewt&m)
{Stonewt q;q.pounds = pounds + m.pounds;q.stone = int(q.pounds) / Lbs_per_stn;q.pds_left = int(q.pounds) % Lbs_per_stn + q.pounds - int(q.pounds);q.Mode = STONE;return q;
}
Stonewt Stonewt::operator-(Stonewt&m)
{Stonewt q;q.pounds = pounds - m.pounds;q.stone = int(q.pounds) / Lbs_per_stn;q.pds_left = int(q.pounds) % Lbs_per_stn + q.pounds - int(q.pounds);q.Mode = STONE;return q;
}
Stonewt Stonewt::operator*(double m)
{Stonewt q;q.pounds = pounds*m;q.stone = int(q.pounds) / Lbs_per_stn;q.pds_left = int(q.pounds) % Lbs_per_stn + q.pounds - int(q.pounds);q.Mode = STONE;return q;
}#include<iostream>
#include"stonewt.h"int main()
{using namespace std;Stonewt a(100);Stonewt b(50, 10.3);cout << "a=" << a << endl;cout << "b=" << b << endl;Stonewt c = a + b;cout << "c=a+b=" << c << endl;c.setmode();cout << "c=" << c << endl;Stonewt d = b - a;cout << "d=b-a=" << d << endl;Stonewt e = a*3.3;cout << "e=a*3.3=" << e << endl;system("pause");return 0;
}

显示:

a=100磅
b=50英石10.3磅
c=a+b=57英石12.3磅
你想要以何种方式输出数据:
a.x英石y磅      b.xx磅  c.xx.yy磅
c
c=810.3磅
d=b-a=43英石8.3磅
e=a*3.3=23英石8磅
请按任意键继续. . .

6.重新编写Stonewt类(程序清单11.16和程序清单11.17),重载全部6个关系运算符。运算符对pounds成员进行比较,并返回一个bool值。编写一个程序,它声明一个包含6个Stonewt对象的数组,并在数组声明中初始化前3个对象。然后使用循环来读取用于设置剩余3个数组元素的值。接着报告最小的元素、最大的元素以及大于或等于11英石的元素的数量(最简单的方法是创建一个Stonewt对象,并将其初始化为11英石,然后将其同其他对象进行比较)。

答:

// stonewt.h
#ifndef STONEWT_H_
#define STONEWT_H_
class Stonewt
{
private:enum { Lbs_per_stn = 14 };enum MODE{STONE,INTPOUNDS,FLOATPOUNDS};int stone;double pds_left;double pounds;int Mode;
public:Stonewt(double lbs);Stonewt(int stn, double lbs);Stonewt();~Stonewt();void setmode();friend std::ostream& operator<<(std::ostream& os, Stonewt &m);Stonewt operator+(Stonewt&m);Stonewt operator-(Stonewt&m);Stonewt operator*(double m);bool operator<(Stonewt&m);bool operator<=(Stonewt&m);bool operator>(Stonewt&m);bool operator>=(Stonewt&m);bool operator==(Stonewt&m);bool operator!=(Stonewt&m);
};
#endif//stonewt.cpp
#include<iostream>
using std::cout;
using std::endl;
#include "stonewt.h"
enum MODE { STONE, INTPOUNDS, FLOATPOUNDS };
Stonewt::Stonewt(double lbs)
{stone = int(lbs) / Lbs_per_stn;pds_left = int(lbs) % Lbs_per_stn + lbs - int(lbs);pounds = lbs;Mode = INTPOUNDS;
}
Stonewt::Stonewt(int stn, double lbs)
{stone = stn;pds_left = lbs;pounds = stn*Lbs_per_stn + lbs;Mode = STONE;
}
Stonewt::Stonewt()
{stone = pounds = pds_left = 0;
}
Stonewt::~Stonewt()
{
}
void Stonewt::setmode()
{cout << "你想要以何种方式输出数据:" << endl;cout << "a.x英石y磅\tb.xx磅\tc.xx.yy磅" << endl;char a;std::cin >> a;while(!isalpha(a)){cout << "输入错误,请重新输入:";std::cin.clear();std::cin.sync();std::cin >> a;}switch (a){case'A':case'a':Mode = STONE;break;case'B':case'b':Mode = INTPOUNDS;break;case'C':case'c':Mode = FLOATPOUNDS;break;default:cout << "输入错误,默认以x英石y磅格式输出。" << endl;Mode = STONE;break;}
}
std::ostream& operator<<(std::ostream& os,Stonewt &m)
{if (m.Mode == STONE){os << m.stone << "英石" << m.pds_left << "磅";}else if (m.Mode == INTPOUNDS){os << int(m.pounds) << "磅";}else if (m.Mode == FLOATPOUNDS){os << m.pounds << "磅";}else{os << "出错,无法输出。";}return os;
}
Stonewt Stonewt::operator+(Stonewt&m)
{Stonewt q;q.pounds = pounds + m.pounds;q.stone = int(q.pounds) / Lbs_per_stn;q.pds_left = int(q.pounds) % Lbs_per_stn + q.pounds - int(q.pounds);q.Mode = STONE;return q;
}
Stonewt Stonewt::operator-(Stonewt&m)
{Stonewt q;q.pounds = pounds - m.pounds;q.stone = int(q.pounds) / Lbs_per_stn;q.pds_left = int(q.pounds) % Lbs_per_stn + q.pounds - int(q.pounds);q.Mode = STONE;return q;
}
Stonewt Stonewt::operator*(double m)
{Stonewt q;q.pounds = pounds*m;q.stone = int(q.pounds) / Lbs_per_stn;q.pds_left = int(q.pounds) % Lbs_per_stn + q.pounds - int(q.pounds);q.Mode = STONE;return q;
}
bool Stonewt::operator<(Stonewt&m)
{if (pounds < m.pounds)return true;else return false;
}
bool Stonewt::operator<=(Stonewt&m)
{if (pounds <= m.pounds)return true;else return false;
}
bool Stonewt::operator>(Stonewt&m)
{if (pounds > m.pounds)return true;else return false;
}
bool Stonewt::operator>=(Stonewt&m)
{if (pounds >= m.pounds)return true;else return false;
}
bool Stonewt::operator==(Stonewt&m)
{if (pounds == m.pounds)return true;else return false;
}
bool Stonewt::operator!=(Stonewt&m)
{if (pounds != m.pounds)return true;else return false;
}#include<iostream>
#include"stonewt.h"int main()
{using namespace std;Stonewt m[6] = { 100,150,170.4 };for (int i = 3;i < 6;i++){double q;cout << "请输入第:" << i + 1 << "个成员的重量(单位:磅):";cin >> q;m[i] = q;}Stonewt over = 11 * 14;    //标准11英石对象Stonewt max, min;max = min = m[0];int ma, mi, ov=0;for (int i = 1;i < 6;i++){if (max < m[i]){max = m[i];ma = i + 1;}if (min > m[i]){min = m[i];mi = i + 1;}if (m[i] >= over)ov++;}cout << "最大是的第" << ma << "个,重量为:" << max << endl;cout << "最小是的第" << mi << "个,重量为:" << min << endl;cout << "超过" << ov << "个大于等于11英石。" << endl;system("pause");return 0;
}

显示:

请输入第:4个成员的重量(单位:磅):400
请输入第:5个成员的重量(单位:磅):70
请输入第:6个成员的重量(单位:磅):190
最大是的第4个,重量为:400磅
最小是的第5个,重量为:70磅
超过3个大于等于11英石。
请按任意键继续. . .

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 = (A*C-B*D, (A*D + B*C )i)

④乘法::x * c = (x * C, x * Di),其中x为实数

⑤共轭: ~a = (A, -Bi)

请定义一个复数类,以便下面的程序可以使用它来获得正确的结果。

#include<iostream>

using namespace std;

#include "complex0.h" //to avoid confusion with complex.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;

}

注意,必须重载运算符<<和>>。标准C++使用头文件complex提供了比这个示例更广泛的复数支持,因此应将自定义的头文件命名为complex0.h,以免发生冲突。应尽可能使用const。

下面是该程序的运行情况。

Enter a complex number (q to quit):

real: 10

imaginary: 12

c is (10,12i)

complex conjugate is (10, -12i)

a is (3,4f)

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):

real: q

Done!

请注意,经过重载后,cin>>c将提示用户输入实数和虚数部分。

答:

//complex0.h
#ifndef COMPLEX0_
#define COMPLEX0_
#include<iostream>
class complex
{double a;double b;
public:complex() {};complex(double A, double B) { a = A;b = B; }complex operator+(complex& m)const;complex operator-(complex& m)const;complex operator*(complex& m)const;complex operator~();friend complex operator*(double a, complex& m);friend std::ostream& operator<<(std::ostream&os, const complex &m);friend std::istream& operator>>(std::istream&is, complex&m);
};#endif // !COMPLEX0//complex0.cpp
#include<iostream>
#include"complex0.h"complex complex::operator+(complex& m)const
{complex q;q.a = a + m.a;q.b = b + m.b;return q;
}
complex complex::operator-(complex& m)const
{complex q;q.a = a - m.a;q.b = b - m.b;return q;
}
complex complex::operator*(complex& m)const
{complex q;q.a = a*m.a - b*m.b;q.b = a*m.b + b*m.a;return q;
}
complex complex::operator~()
{complex q;q.a = a;q.b = -b;return q;
}
complex operator*(double a, complex& m)
{complex q;q.a = m.a*a;q.b = m.b*a;return q;
}
std::ostream& operator<<(std::ostream&os, const complex &m)
{os << "(" << m.a << "," << m.b << "i)";return os;
}
std::istream& operator>>(std::istream& is, complex&m)
{double x, y;std::cout << "real: ";is >> x;if (!is)return is;std::cout << "imagiary: ";is >> y;m.a = x;m.b = y;return is;
}//1.cpp main函数,用于测试
#include<iostream>
using namespace std;
#include "complex0.h"     //to avoid confusion with complex.hint 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";system("pause");   //加上这段,否则我的编译器上窗口会一闪而过return 0;
}

显示结果和要求完全相同。

(一二六)第十一章编程练习相关推荐

  1. 速学堂(java)第十一章编程题答案(自写)

    速学堂(java)第十一章编程题答案(自写) 1.设计一个多线程的程序如下:设计一个火车售票模拟程序.假如火车站要有100张火车票要卖出,现在有5个售票点同时售票,用5个线程模拟这5个售票点的售票情况 ...

  2. C++PrimerPlus学习——第十一章编程练习

    11-1 应该是修改list11.15,当当官方店买的,难道是盗版书吗... 打开file之后,操作跟cout类似 vect.h #ifndef VECT_h_ #define VECT_h_ #in ...

  3. 记录——《C Primer Plus (第五版)》第十一章编程练习第5-12题

    5.编写一个函数is_within(),它接受两个参数,一个是字符,另一个是字符串指针.其功能是如果字符在字符串中,就返回一个非0值(真):如果字符不在字符串中,就返回0值(假).在一个使用循环语句为 ...

  4. 记录——《C Primer Plus (第五版)》第十一章编程练习第四题

    4.设计并测试一个函数,其功能是搜索由函数的第一个参数指定的字符串,在其中查找由函数的第二 个参数指定的字符的第一次出现的位置.如果找到,返回指向这个字符的指针,如果没有找到,返回 空字符(这种方式和 ...

  5. 记录——《C Primer Plus (第五版)》第十一章编程练习第三题

    3.设计并测试一个函数,其功能是读取输入行里的第一个单词到数组,并丢掉该行中其他 字符,一个单词的定义是一串字符,基中不含空格,制表及换行符. #include <stdio.h> cha ...

  6. 记录——《C Primer Plus (第五版)》第十一章编程练习第二题

    2.修改练习1,使得可以在n个字符后,或第一个空格.制表符,换行符后停止读取输入,由上述情况 最先被满足的那个终止读取,不能使用scanf()函数. # include <stdio.h> ...

  7. 记录——《C Primer Plus (第五版)》第十一章编程练习第一题

    1.设计并测试一个函数,可以从输入读取n个字符(包括空格.制表符和换行符),把结果存储在 一个数组中,这个数组的地址通过参数来传递. # include <stdio.h> # defin ...

  8. C ++ Primer Plus 第六版 第九章编程练习答案

    2.修改程序清单9.9,用string对象代替字符数组.这样,该程序将不再需要检查输入的字符串是否过长,同时可以将输入字符串同字符串""进行比较,以判断是否为空行. #includ ...

  9. C++ Primer Plus 第六版第二章编程练习答案

    1.编写一个c++程序,它显示您的姓名和地址. #include<iostream> int main() {using namespace std;cout << " ...

最新文章

  1. 取代MybatisPlus?阿里推出了新 ORM 框架!(两者对比参考)
  2. 【Java】剑指 Offer 52. 两个链表的第一个公共节点
  3. golang枚举类型 - iota用法拾遗
  4. 常用HiveQL总结
  5. (运维)VMware-vCenter-Server-update Management
  6. 图像的灰度级数越多越好_MATLAB-数字图像处理 图像直方图归一化
  7. linux shell 获取本机ip 写入文件
  8. Library not found for -lPods-Unity-iPhone 的解决方法
  9. (转)Arcgis for JS之Cluster聚类分析的实现
  10. sklearn-python简介
  11. SQL进阶六:字符串函数
  12. 机器学习初探(手写数字识别)matlab读取数据集
  13. 企业互联网+转型实战:如何进行PB级别数据的架构变迁
  14. 【CVRP】基于matlab遗传算法求解带容量的车辆路径规划问题【含Matlab源码 1280期】
  15. 启动Kettle时报错找不到文件javaw.exe
  16. 深度学习模型提升性能的策略
  17. 面向对象进阶 三大特性
  18. 学历、长相、家境普通的人,未来的发展方向是什么?00后的职业规划都已经整得明明白白
  19. Python 淘宝商品价格爬取(requests库+正则表达式)
  20. 大屏监控 Metabase 集成到 Java 项目

热门文章

  1. java获取panel面板画笔_java - paintComponent()与paint()和JPanel vs Canvas在画笔类型的GUI中 - 堆栈内存溢出...
  2. 九年级计算机上册教学总结,九年级信息技术教学工作总结
  3. redhat linux 9.0 拷贝u盘的文件,肿么用U盘安装Linux,安装的是red hat 9.0…用Ubuntu很方便,redhat可以吗?...
  4. qdir 类似工具_qdir 类似工具_支持 Win8.1,全能资源管理器 Q-Dir 5.74 发布
  5. RabbitMQ exchange交换机机制
  6. 互联网晚报 | 9月2日 星期四 | 小米汽车有限公司正式成立;唯品富邦消费金融获批开业;恒大举行保交楼军令状签署大会...
  7. 你的实力就等于别人对待你的态度
  8. NETGEAR拒绝连接请求_破案:Kubernetes/Docker 上无法解释的连接超时
  9. 作者:​邵蓥侠(1988-),男,博士,北京明略软件系统有限公司技术经理。...
  10. 作者:王小兵,男,农业部市场与经济信息司副司长。