淘宝网toscreen是什么型号手机以及toscaso

本篇文章给大家谈谈toscreen是什么型号手机,以及toscaso的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

文章详情介绍:

90Hz的畅快谁用谁知道 外媒都这样评价一加7 Pro

 

全速旗舰一加7 Pro上市已经有一段时间了,其用户好评最集中的亮点就要数那块90Hz刷新率、2K+超清分辨率的屏幕了,的确,这种清晰又畅快的体验真的是用了就回不去的。那么,大家现在一定都好奇,一直在国外市场做得风生水起的一加,这款全新旗舰在国外媒体眼中究竟是什么样的?我们一起来看看吧!

全球权威媒体:《时代周刊》

评价:The OnePlus 7 Pro’s display is jaw-dropping.

意为:OnePlus 7 Pro 的屏幕显示效果着实令人惊叹。

知名科技媒体:PhoneArena

评价:You don’t need to be an expert to feel the difference, and once you get used to this new standard of speed in every interaction, it’s hard to go back to a traditional display.

意为:一旦你在每次滑动交互中习惯了(90Hz)这种新的速度标准,就很难回到传统的(60Hz)屏幕上,这个差异即便你不是专业人士也能感受到。

知名酷玩科技媒体:Stuff

评价:It makes the screen appear to scroll more smoothly.

意为:它(90Hz)让屏幕滑动看起来更加流畅。

知名科技媒体:DigitalTrends

评价:There’s a truly stunning 6.67-inch AMOLED screen with an impressive 3,140 x 1,440-pixel resolution, a buttery smooth 90Hz refresh rate, and support for HDR10+.

意为:这是一块真正令人惊叹的 6.67 英寸 AMOLED 屏幕,令人印象深刻的3,140 x 1,440 超高像素分辨率,像抹了黄油一样顺滑的 90Hz 刷新率,并支持 HDR10 +。

权威科技媒体:The Verge

评价:by giving this screen a higher refresh rate: 90Hz instead of 60Hz. It makes everything from scrolling to animations look much smoother… It means you can read as you scroll, and touch responses feel much more in tune with your finger’s placement.

意为:通过更高的屏幕刷新率:90Hz 而非 60 Hz,从滑动到动效的所有内容更加顺畅,这意味着你可以在阅读时滑动更流畅,触摸响应更迅速。

知名科技媒体:Gsmarena

评价:The OnePlus 7 Pro has a huge potential to reshape the smartphone industry with its 90Hz display.

意为:OnePlus 7 Pro 真是潜力巨大,可通过其 90Hz 显示屏重塑智能手机行业。

 

「C++ Primer plus 习题」第十一章编程题

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

Target Distance: 100, Step Size:20 0: (x,y) = {0,0} 1: (x,y) = {-11.4715, 16.383} 2: (x,y) = {-8.68807, -3.42232} … 26: (x,y) = {45.2919,-78.259} 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 disatance per step = 3.97081

答案:

//vector.h #ifndef VECTOR_H_ #define VECTOR_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; } // repot values 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 //vector.cpp #include “vector.h” #include <cmath> using std::sqrt; using std::sin; using std::cos; using std::atan; using std::atan2; using std::cout; 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 Vector() — “; 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 Vector() — “; cout << “vector set to 0\n”; x = y = mag = ang = 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.mag << “, ” << v.ang * Rad_to_deg << “)”; } else os << “Vector object mode is invalid”; return os; } } //main.cpp #include <iostream> #include <cstdlib> #include <ctime> #include “vector.h” #include <fstream> 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; ofstream fout; fout.open(“RandWalk.txt”); cout << “Enter target distance (q to quit): “; while (cin >> target) { cout << “Enetr step length: “; if (!(cin >> dstep)) { break; } else { fout << “Target Distance: ” << target << “, Step Size: ” << dstep << endl; } int i = 0; while (result.magval() < target) { direction = rand() % 360; step.reset(dstep, direction, Vector::POL); result = result + step; steps++; fout << i << “: (x,y) = (” << result.xval() << “, ” << result.yval() << “)\n”; i++; } cout << “After ” << steps << ” steps, the subject has the following location:\n”; cout << result << endl; fout << “After ” << steps << ” steps, the subject has the following location:\n”; fout << result << endl; result.polar_mode(); cout << ” or\n” << result << endl; cout << “Average outward distance per step = ” << result.magval() / steps << endl; fout << ” or\n” << result << 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”; fout << “Bye!\n”; cin.clear(); fout.close(); while (cin.get() != ‘\n’) continue; return 0; }

编译命令:g++ main.cpp vector.cpp -o main -std=c++11

输出(同时在本地会产生一个文件RandWalk.txt):

Enetr step length: 20 After 43 steps, the subject has the following location: (x,y) = (15.8579, 129.011) or (m,a) = (129.982, 82.9924) Average outward distance per step = 3.02283 Enter target distance (q to quit): q Bye! 2. 对Vector类的头文件(程序清单11.13)和实现文件(程序清单11.14)进行修改,使其不再存储矢量的长度和角度,而是在magval()和angval()被调用时计算它们。

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

分析:去除题1中私有的mag 和ang 成员变量,其他所有调用的地方都以临时变量 或者传入参数的方式进行使用

答案:

//vector.h #ifndef VECTOR_H_ #define VECTOR_H_ #include <cmath> #include <iostream> namespace VECTOR { class Vector { public: enum Mode { RECT, POL }; private: double x; double y; Mode mode; double set_mag(); double set_ang(); void set_x(double mag, double ang); void set_y(double mag, double ang); public: Vector(); Vector(double n1, double n2, Mode form = RECT); void reset(double n1, double n2, Mode form = RECT); ~Vector(); double xval() const { return x; } // repot values double yval() const { return y; } double magval() const { double mag; mag = sqrt(x * x + y * y); return mag; } double angval() const { double ang; if (x == 0.0 && y == 0.0) ang = 0.0; else ang = atan2(y, x); 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 //vector.cpp #include “vector.h” #include <cmath> using std::sqrt; using std::sin; using std::cos; using std::atan; using std::atan2; using std::cout; namespace VECTOR { const double Rad_to_deg = 45.0 / atan(1.0); double Vector::set_mag() { double mag; 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(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; set_mag(); set_ang(); } else if (form == POL) { double mag, ang; mag = n1; ang = n2 / Rad_to_deg; set_x(mag, ang); set_y(mag, ang); } 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; set_mag(); set_ang(); } else if (form == POL) { double mag, ang; mag = n1; ang = n2 / Rad_to_deg; set_x(mag, ang); set_y(mag, ang); } 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; } } //main.cpp #include <iostream> #include <cstdlib> #include <ctime> #include “vector.h” #include <fstream> 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 << “Enetr step length: “; if (!(cin >> dstep)) { break; } while (result.magval() < target) { direction = rand() % 360; step.reset(dstep, direction, Vector::POL); result = result + step; steps++; } cout << “After ” << steps << ” steps, the subject has the following 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; }

编译命令:g++ main.cpp vector.cpp -o main -std=c++11

输出:

Enter target distance (q to quit): 120 Enetr step length: 20 After 25 steps, the subject has the following location: (x,y) = (-57.4544, 117.223) or (m,a) = (130.546, 116.111) Average outward distance per step = 5.22184 Enter target distance (q to quit): q Bye! 3. 修改程序清单11.15,使之报告N次测试中的最高、最低和平均步数(其中N是用户输入的整数),而不是报告每次测试的结果。

分析:vector.h 和vector.cpp 和题1一样,主要在实现部分

首先,我们要定义一个表示测试次数的变量N,然后定义表示最高、最低和总步数的int类型变量为maxsteps,minsteps,sumsteps,分别初始化为0,65535,0,因为我们是把每一次的结果和已有的maxsteps和minsteps进行比较,如果大于maxsteps就更新maxsteps,小于minsteps就更新minsteps,所以它们的初值一定要设为int类型的最小值和最大值才最方便;而对于总步数的sumsteps,当然是从0开始走出来的。接下来再定义一个avesteps,double类型,因为将sumsteps除以N之后有可能是小数。

在这里必须明确一共测试多少次,所以在提示用户输入目标和步长之前,应提示用户输入测试次数。

在这里,对于每一次随机的漫步,我们都需要把内部获得的steps数据同maxsteps和minsteps进行比较和更新,然后把steps累加进sumsteps里。当N次测试循环结束之后,我们再来计算avesteps,并将结果进行输出。

在这里由于测试次数一定,我们不再需要原来程序清单11.15里的最外边的while循环,因为我们只需要一个target和一个dstep,只是在此基础上去循环测试,所以我们此时的循环应该是for循环,i从0到N。

答案:

//vector.h #ifndef VECTOR_H_ #define VECTOR_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; } // repot values 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 //vector.cpp #include “vector.h” #include <cmath> using std::sqrt; using std::sin; using std::cos; using std::atan; using std::atan2; using std::cout; 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 Vector() — “; 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 Vector() — “; cout << “vector set to 0\n”; x = y = mag = ang = 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.mag << “, ” << v.ang * Rad_to_deg << “)”; } else os << “Vector object mode is invalid”; return os; } } //main.cpp #include <iostream> #include <cstdlib> #include <ctime> #include “vector.h” #include <fstream> 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 maxsteps, minsteps, sumsteps; maxsteps = 0; minsteps = 65535; sumsteps = 0; double avesteps; cout << “How many times of test would you want: “; cin >> N; cout << “\nEnter target distance: “; cin >> target; cout << “Enetr step length: “; cin >> dstep; cout << endl; for (int i = 0; i < N; i++) { while (result.magval() < target) { direction = rand() % 360; step.reset(dstep, direction, Vector::POL); result = result + step; steps++; } sumsteps += steps; if (steps > maxsteps) { maxsteps = steps; } if (steps < minsteps) { minsteps = steps; } steps = 0; result.reset(0.0, 0.0); } avesteps = sumsteps / N; cout << “Walk finished!\nAfter ” << N << ” times of test, the result is following:\n”; cout << “The maxmum steps is ” << maxsteps << “, the minimum steps is ” << minsteps << “, and the average steps is ” << avesteps << “.\n”; cout << “Bye!\n”; cin.clear(); while (cin.get() != ‘\n’) continue; return 0; }

输出:

How many times of test would you want: 8 Enter target distance: 120 Enetr step length: 20 Walk finished! After 8 times of test, the result is following: The maxmum steps is 78, the minimum steps is 14, and the average steps is 36. Bye! 4. 重新编写最后的Time类示例(程序清单11.10、程序清单11.11和程序清单11.12),使用友元函数来实现所有的重载运算符。

分析:其实将公方法改成友元函数非常简单,首先在头文件的公方法前面加上friend,其次在实现文件中定义函数时,去掉Time::作用域,因为此时友元函数并不属于该类,所以就当做普通函数来定义即可。

答案:

//mytime3.h // 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 & t, double n); friend Time operator*(double m, const Time & t); friend std::ostream & operator<<(std::ostream & os, const Time & t); }; #endif //mytime3.cpp // 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 + t2.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 & t, double mult) { Time result; long totalminutes = t.hours * mult * 60 + t.minutes * mult; result.minutes = totalminutes % 60; result.hours = totalminutes / 60; return result; } Time operator*(double mult, const Time & t) { Time result; long totalminutes = t.hours * mult * 60 + t.minutes * mult; result.minutes = totalminutes % 60; result.hours = totalminutes / 60; return result; } std::ostream & operator<<(std::ostream & os, const Time & t) { os << t.hours << ” hours, ” << t.minutes << ” minutes”; return os; } //main.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; }

编译方法:g++ main.cpp mytime3.cpp -o main -std=c++11

输出:

Aida and Tosca: 3 hours, 35 minutes; 2 hours, 48 minutes Aida + Tosca: 6 hours, 23 minutes Aida * 1.17: 4 hours, 11 minutes 10.0 * Tosca: 28 hours, 0 minutes 5. 重新编写Stonewt类(程序清单11.16和程序清单11.17),使它有一个状态成员,由该成员控制对象应转换为英石格式、整数磅格式还是浮点磅格式。重载<<运算符,使用它来替换show_stn()和show_lbs()方法。重载加法、减法和乘法运算符,以便可以对Stonewt值进行加、减、乘运算。编写一个使用所有类方法和友元的小程序,来测试这个类。

答案:

//stonewt.h #ifndef STONEWT_H_ #define STONEWT_H_ #include <iostream> class Stonewt { private: enum { Lbs_per_stn = 14 }; int state; int stone; double pds_left; double pounds; public: Stonewt(double lbs); Stonewt(int stn, double lbs); Stonewt(); ~Stonewt(); void setstate(int x); friend Stonewt operator+(const Stonewt & s1, const Stonewt & s2); friend Stonewt operator-(const Stonewt & s1, const Stonewt & s2); friend Stonewt operator*(const Stonewt & s, double n); friend Stonewt operator*(double m, const Stonewt & s); friend std::ostream & operator<<(std::ostream & os, const Stonewt & s); }; #endif //stonewt.cpp #include <iostream> #include “stonewt.h” 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 = pounds = pds_left = 0; state = 3; } Stonewt::~Stonewt() { } void Stonewt::setstate(int x) { state = x; } Stonewt operator+(const Stonewt & s1, const Stonewt & s2) { Stonewt sum; sum.pounds = s1.pounds + s2.pounds; sum.stone = int(sum.pounds) / 14; sum.pds_left = int(sum.pounds) % 14 + sum.pounds – int(sum.pounds); return sum; } Stonewt operator-(const Stonewt & s1, const Stonewt & s2) { double pounds; pounds = s1.pounds – s2.pounds; Stonewt diff(pounds); return diff; } Stonewt operator*(const Stonewt & s, double n) { double pounds; pounds = s.pounds * n; Stonewt mult(pounds); return mult; } Stonewt operator*(double m, const Stonewt & s) { double pounds; pounds = s.pounds * m; Stonewt mult(pounds); return mult; } std::ostream & operator<<(std::ostream & os, const Stonewt & s) { if (s.state == 1) { os << s.stone << ” stone, ” << s.pds_left << ” pounds\n”; } if (s.state == 2) { os << int(s.pounds) << ” pounds\n”; } if (s.state == 3) { os << s.pounds << ” pounds\n”; } return os; } //main.cpp #include <iostream> #include “stonewt.h” using namespace std; int main() { Stonewt s[3]; Stonewt s1(275); Stonewt s2(285.7); Stonewt s3(21, 8); s[0] = s1; s[1] = s2; s[2] = s3; int state; for (int i = 0; i < 3; i++) { cout << “#” << i + 1 << “: \n”; cout << “Choose your style to display( ‘1’ for stones, ‘2’ for integer pounds, ‘3’ for float pounds, others to quit): “; cin >> state; if (state != 1 && state != 2 && state != 3) { cout << “Warning: style can’t satisfied!” << endl; cout << “Bye!\n”; system(“pause”); return 0; } s[i].setstate(state); cout << “s” << i + 1 << “: ” << s[i] << endl; } int st; cout << “Choose your style to display the result( ‘1’ for stones, ‘2’ for integer pounds, ‘3’ for float pounds, others to quit): “; cin >> st; Stonewt sum; sum = s1 + s2; sum.setstate(st); cout << “s1 + s2 = ” << sum << endl; Stonewt diff; diff = s3 – s2; diff.setstate(st); cout << “s3 – s2 = ” << diff << endl; Stonewt mult1, mult2; mult1 = 10 * s1; mult2 = s3 * 1.5; mult1.setstate(st); mult2.setstate(st); cout << “10 * s1 = ” << mult1 << endl; cout << “s3 * 1.5 = ” << mult2 << endl; return 0; }

编译命令:g++ main.cpp stonewt.cpp -o main -std=c++11

输出:

#1: Choose your style to display( ‘1’ for stones, ‘2’ for integer pounds, ‘3’ for float pounds, others to quit): 1 s1: 19 stone, 9 pounds #2: Choose your style to display( ‘1’ for stones, ‘2’ for integer pounds, ‘3’ for float pounds, others to quit): 2 s2: 285 pounds #3: Choose your style to display( ‘1’ for stones, ‘2’ for integer pounds, ‘3’ for float pounds, others to quit): 3 s3: 302 pounds Choose your style to display the result( ‘1’ for stones, ‘2’ for integer pounds, ‘3’ for float pounds, others to quit): 1 s1 + s2 = 40 stone, 0.7 pounds s3 – s2 = 1 stone, 2.3 pounds 10 * s1 = 196 stone, 6 pounds s3 * 1.5 = 32 stone, 5 pounds 6. 重新编写Stonewt类(程序清单11.16和程序清单11.17),重载全部6个关系运算符。运算符对pounds成员进行比较,并返回一个bool值。编写一个程序,它声明一个包含6个Stonewt对象的数组,并在数组声明初始化前3个对象。然后使用循环来读取用于设置剩余3个数组元素的值。接着报告最小的元素、最大的元素以及大于或等于11英石的元素的数量(最简单的方法是创建一个Stonewt对象,并将其初始化为11英石,然后将其同其他对象进行比较)。

答案:

//stonewt.h #ifndef STONEWT_H_ #define STONEWT_H_ #include <iostream> class Stonewt { private: enum { Lbs_per_stn = 14 }; int state; int stone; double pds_left; double pounds; public: Stonewt(double lbs); Stonewt(int stn, double lbs); Stonewt(); ~Stonewt(); void setstate(int x); friend bool operator<(const Stonewt & s1, const Stonewt & s2); friend bool operator>(const Stonewt & s1, const Stonewt & s2); friend bool operator==(const Stonewt & s1, const Stonewt & s2); friend bool operator<=(const Stonewt & s1, const Stonewt & s2); friend bool operator>=(const Stonewt & s1, const Stonewt & s2); friend bool operator!=(const Stonewt & s1, const Stonewt & s2); friend std::ostream & operator<<(std::ostream & os, const Stonewt & s); }; #endif //stonewt.cpp #include <iostream> #include “stonewt.h” 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; state = 1; } Stonewt::Stonewt(int stn, double lbs) { stone = stn; pds_left = lbs; pounds = stn * Lbs_per_stn + lbs; state = 1; } Stonewt::Stonewt() { stone = pounds = pds_left = 0; state = 1; } Stonewt::~Stonewt() { } void Stonewt::setstate(int x) { state = x; } bool operator<(const Stonewt & s1, const Stonewt & s2) { if (s1.pounds < s2.pounds) return true; else return false; } bool operator>(const Stonewt & s1, const Stonewt & s2) { if (s1.pounds > s2.pounds) return true; else return false; } bool operator==(const Stonewt & s1, const Stonewt & s2) { if (s1.pounds == s2.pounds) return true; else return false; } bool operator<=(const Stonewt & s1, const Stonewt & s2) { if (s1.pounds <= s2.pounds) return true; else return false; } bool operator>=(const Stonewt & s1, const Stonewt & s2) { if (s1.pounds >= s2.pounds) return true; else return false; } bool operator!=(const Stonewt & s1, const Stonewt & s2) { if (s1.pounds != s2.pounds) return true; else return false; } std::ostream & operator<<(std::ostream & os, const Stonewt & s) { if (s.state == 1) { os << s.stone << ” stone, ” << s.pds_left << ” pounds”; } if (s.state == 2) { os << int(s.pounds) << ” pounds”; } if (s.state == 3) { os << s.pounds << ” pounds”; } return os; } //main.cpp #include <iostream> #include “stonewt.h” using namespace std; int main() { Stonewt s[6] = { {275}, {288.22}, {26, 7} }; for (int i = 3; i < 6; i++) { double pounds; cout << “Please enter the last 3 Stonewt class members(enter the entire pounds): \n”; cout << “#” << i+1 << “: “; cin >> pounds; s[i] = pounds; } cout << “Enter finished!\n\n”; cout << “So the whole 6 Stonewt class members are: \n”; cout << “#1: ” << s[0] << endl; cout << “#2: ” << s[1] << endl; cout << “#3: ” << s[2] << endl; cout << “#4: ” << s[3] << endl; cout << “#5: ” << s[4] << endl; cout << “#6: ” << s[5] << endl; Stonewt mins = 65535; Stonewt maxs = 0; int num = 0; Stonewt flag(11,0); for (int i = 0; i < 6; i++) { if (s[i] < mins) mins = s[i]; if (s[i] > maxs) maxs = s[i]; if (s[i] >= flag) num++; } cout << “After comparation:\n”; cout << “The maximum Stonewt class member is ” << maxs << endl; cout << “The minimum Stonewt class member is ” << mins << endl; cout << “There are ” << num << ” Stonewt class members bigger than 11 stones” << endl; cout << endl; cout << “Comparation finished!\nBye!\n”; return 0; } 编译命令:g++ main.cpp stonewt.cpp -o main -std=c++11

输出:

Please enter the last 3 Stonewt class members(enter the entire pounds): #4: 12.5 Please enter the last 3 Stonewt class members(enter the entire pounds): #5: 13.5 Please enter the last 3 Stonewt class members(enter the entire pounds): #6: 15.5 Enter finished! So the whole 6 Stonewt class members are: #1: 19 stone, 9 pounds #2: 20 stone, 8.22 pounds #3: 26 stone, 7 pounds #4: 0 stone, 12.5 pounds #5: 0 stone, 13.5 pounds #6: 1 stone, 1.5 pounds After comparation: The maximum Stonewt class member is 26 stone, 7 pounds The minimum Stonewt class member is 0 stone, 12.5 pounds There are 3 Stonewt class members bigger than 11 stones Comparation finished! Bye! 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” int main() { complex a(3.0,4.0); complex c; cout << “Enter a complex number (q to quit):\n”; while (cin >> c) { cout << “c is ” << c << ‘\n’; cout << “complex conjugate is ” << ~c << ‘\n’; cout << “a is ” << a << ‘\n’; cout << “a + c is ” << a + c << ‘\n’; cout << “a – c is ” << a – c << ‘\n’; cout << “a * c is ” << a * c << ‘\n’; cout << “2 * 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 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): real: quit Done!

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

答案:

//complex.h #ifndef COMPLEX_H_ #define COMPLEX_H_ #include <iostream> class complex { private: double real;//实数部分 double ima;//虚数部分 public: complex(); complex(double x, double y); ~complex(); friend complex operator+(const complex & c1, const complex & c2); friend complex operator-(const complex & c1, const complex & c2); friend complex operator*(double n, const complex & c); friend complex operator*(const complex & c1, const complex & c2); friend complex operator~(const complex & c); friend std::ostream & operator<<(std::ostream & os, const complex & c); friend std::istream & operator>>(std::istream & is, complex & c); }; #endif //complex.cpp #include <iostream> #include “complex.h” using namespace std; complex::complex() { real = ima = 0.0; } complex::complex(double x, double y) { real = x; ima = y; } complex::~complex() { } complex operator+(const complex & c1, const complex & c2) { complex sum; sum.real = c1.real + c2.real; sum.ima = c1.ima + c2.ima; return sum; } complex operator-(const complex & c1, const complex & c2) { complex diff; diff.real = c1.real – c2.real; diff.ima = c1.ima – c2.ima; return diff; } complex operator*(const complex & c1, const complex & c2) { complex mult; mult.real = c1.real * c2.real – c1.ima * c2.ima; mult.ima = c1.real * c2.ima + c1.ima * c2.real; return mult; } complex operator*(double n, const complex & c) { complex mult; mult.real = n * c.real; mult.ima = n * c.ima; return mult; } complex operator~(const complex & c) { complex conj; conj.real = c.real; conj.ima = -c.ima; return conj; } std::ostream & operator<<(std::ostream & os, const complex & c) { os << “(” << c.real << “,” << c.ima << “i)”; return os; } std::istream & operator>>(std::istream & is, complex & c) { cout << “real: “; is >> c.real; if (!is) return is; cout << “imaginary: “; is >> c.ima; return is; } //main.cpp #include <iostream> #include “complex.h” using namespace std; int main() { complex a(3.0, 4.0); complex c; cout << “Enter a complex number (q to quit):\n”; while (cin >> c) { cout << “c is ” << c << “\n”; cout << “complex conjugate is ” << ~c << “\n”; cout << “a is ” << a << “\n”; cout << “a + c is ” << a + c << “\n”; cout << “a – c is ” << a – c << “\n”; cout << “a * c is ” << a * c << “\n”; cout << “2 * c is ” << 2 * c << “\n”; cout << “Enter a complex number (q to quit):\n”; }; cout << “Done!\n”; return 0; }

编译命令:g++ main.cpp complex.cpp -o main -std=c++11

输出:

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): real: q Done!