(一二六)第十一章编程练习
這是我創(chuàng)建的專輯,所有學(xué)習(xí)筆記都以word格式上傳,放在里面,相對(duì)更美觀和直接。歡迎下載瀏覽。
http://download.csdn.net/album/detail/2971
1.修改程序清單11.5,使之將一系列連續(xù)的隨機(jī)漫步者位置寫入到文件中。對(duì)于每個(gè)位置,用步號(hào)進(jìn)行標(biāo)志。另外,讓該程序?qū)⒊跏紬l件(目標(biāo)距離和步長(zhǎng))以及結(jié)果小結(jié)寫入到該文件中。該文件的內(nèi)容與下面類似:
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
答:
注:這里我不修改源文件了,在自己的基礎(chǔ)上,重新寫一個(gè)
//Point.h 包含類定義 #pragma once class Point { public:enum mode { xy = 0, ji = 1 }; //常量 private:double x; //x,y坐標(biāo)double y;double jiaodu; //極坐標(biāo):角度和長(zhǎng)度double changdu;int mode; //模式,根據(jù)模式來決定坐標(biāo)輸出時(shí)的形式void setxy(); //設(shè)置x、y坐標(biāo)(根據(jù)極坐標(biāo))void setji(); //設(shè)置極坐標(biāo)(根據(jù)x,y坐標(biāo)) public:Point(double a = 0, double b = 0, int c = xy); //默認(rèn)構(gòu)造函數(shù)。默認(rèn)為x,y坐標(biāo)賦值void reset(double a = 0, double b = 0, int c = xy); //重置坐標(biāo)void show(std::ostream &os)const; //輸出坐標(biāo)double get_changdu() { return changdu; } //返回極坐標(biāo)的長(zhǎng)度Point operator+(const Point& m)const; //運(yùn)算符+重載,不用引用是因?yàn)橐獎(jiǎng)?chuàng)建一個(gè)新對(duì)象,然后返回這個(gè)對(duì)象friend std::ostream& operator<<(std::ostream &os, const Point&m); //用于cout或其他輸出 };//Point.cpp 包含了Point類的成員函數(shù)以及友元函數(shù)定義 #include<iostream> #include<cmath> #include<fstream> #include"Point.h" void Point::setxy() //設(shè)置x、y坐標(biāo)(根據(jù)極坐標(biāo)),函數(shù)內(nèi)容是拿以前的改改 {const double jiao_to_angle = 57.29577951; //角度/弧度的值大概是這個(gè)數(shù)double hudu = jiaodu / jiao_to_angle; //角度除以這個(gè)值得到弧度y = sin(hudu)*changdu; //對(duì)邊(y) = sin弧度*斜邊。x = cos(hudu)*changdu; //臨邊(x) = sin弧度*斜邊 }void Point::setji() //設(shè)置極坐標(biāo)(根據(jù)x,y坐標(biāo)) {const double jiao_to_angle = 57.29577951; //角度/弧度的值大概是這個(gè)數(shù)changdu = sqrt(x*x + y*y); //利用坐標(biāo)求距離jiaodu = atan2(y, x)*jiao_to_angle; //利用坐標(biāo)求角度 }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 << "參數(shù)輸入錯(cuò)誤。如果你需要輸入x、y坐標(biāo),請(qǐng)第三個(gè)參數(shù)輸出0或者不輸入第三個(gè)參數(shù);如果你需要輸入極坐標(biāo),請(qǐng)第三個(gè)參數(shù)輸入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 << "參數(shù)輸入錯(cuò)誤。如果你需要輸入x、y坐標(biāo),請(qǐng)第三個(gè)參數(shù)輸出0或者不輸入第三個(gè)參數(shù);如果你需要輸入極坐標(biāo),請(qǐng)第三個(gè)參數(shù)輸入1。" << std::endl; } void Point::show(std::ostream &os)const //輸出坐標(biāo) {os << "現(xiàn)在報(bào)告x,y坐標(biāo):" << std::endl;os << "x:" << x << ",y:" << y << std::endl;os << "現(xiàn)在報(bào)告極坐標(biāo):" << std::endl;os << "長(zhǎng)度為:" << changdu << ",角度為:" << jiaodu << "度" << std::endl; } Point Point::operator+(const Point& m)const //運(yùn)算符+重載,不用引用是因?yàn)橐獎(jiǎng)?chuàng)建一個(gè)新對(duì)象,然后返回這個(gè)對(duì)象 {Point q;q.x = x + m.x;q.y = y + m.y;q.mode = mode; //默認(rèn)模式為運(yùn)算符前面的對(duì)象的顯示模式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()函數(shù)所在,用于使用和驗(yàn)證類 #include<iostream> #include<fstream> #include"Point.h" #include<ctime>int main() {using namespace std;srand(time(0));ofstream q; //創(chuàng)建osftream對(duì)象q.open("111.txt"); //打開111.txt文件int distance;cout << "請(qǐng)輸入距離:";cin >> distance;int step_size;cout << "請(qǐng)輸入每一步的距離:";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; }文本文件結(jié)果:
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: 現(xiàn)在報(bào)告x,y坐標(biāo): x:67.5513,y:74.9477 現(xiàn)在報(bào)告極坐標(biāo): 長(zhǎng)度為:100.898,角度為:47.9712度 Average outward distance per step = 4.80465
?
?
?
?
?
2.對(duì)于Vector類的頭文件(程序清單11.13)和實(shí)現(xiàn)文件(程序清單11.14)進(jìn)行修改,使其不再儲(chǔ)存矢量的長(zhǎng)度和角度,而是在magval()和angval()被調(diào)用時(shí)計(jì)算它們。
應(yīng)保留公有接口不變(公有方法及其參數(shù)不變),但對(duì)私有部分(包括一些私有方法)和方法實(shí)現(xiàn)進(jìn)行修改。然后,使用程序清單11.15對(duì)修改后的版本進(jìn)行測(cè)試,結(jié)果應(yīng)該與以前相同,因?yàn)?/span>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,使之報(bào)告N次測(cè)試中的最高、最低和平均步數(shù)(其中N是用戶輸入的整數(shù)),而不是報(bào)告每次測(cè)試的結(jié)果。
答:
為了方便,我在編程練習(xí)2的基礎(chǔ)上修改。
//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 << "輸入你想測(cè)試的次數(shù):";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 << "次數(shù):" << N << endl;cout << "最大步數(shù):" << Max << endl;cout << "最小步數(shù):" << Min << endl;cout << "平均步數(shù):" << 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 輸入你想測(cè)試的次數(shù):1000 次數(shù):1000 最大步數(shù):3083 最小步數(shù):72 平均步數(shù):646.003 Enter target distance (q to quit):50 Enter step length: 2 輸入你想測(cè)試的次數(shù):1000 次數(shù):1000 最大步數(shù):3034 最小步數(shù):67 平均步數(shù):646.13 Enter target distance (q to quit):q Bye! 請(qǐng)按任意鍵繼續(xù). . .?
?
?
?
?
?
?
?
4.重新編寫最后的Time了示例(程序清單11.10、程序清單11.11和程序清單11.12),使用友元函數(shù)來實(shí)現(xiàn)所有的重載運(yùn)算符。
答:
//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),使它有一個(gè)狀態(tài)成員,由該狀態(tài)成員控制對(duì)象應(yīng)轉(zhuǎn)換為英石格式、整數(shù)磅格式還是浮點(diǎn)磅格式。重載<<運(yùn)算符,使用它來替換show_stn()和show_lbs()方法。重載加法、減法和重發(fā)運(yùn)算符,以便可以對(duì)Stonewt值進(jìn)行加、減、乘運(yùn)算。編寫一個(gè)使用所有類方法和友元的小程序,來測(cè)試這個(gè)類。
答:
// 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 << "你想要以何種方式輸出數(shù)據(jù):" << endl;cout << "a.x英石y磅\tb.xx磅\tc.xx.yy磅" << endl;char a;std::cin >> a;while(!isalpha(a)){cout << "輸入錯(cuò)誤,請(qǐng)重新輸入:";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 << "輸入錯(cuò)誤,默認(rèn)以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 << "出錯(cuò),無法輸出。";}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磅 你想要以何種方式輸出數(shù)據(jù): a.x英石y磅 b.xx磅 c.xx.yy磅 c c=810.3磅 d=b-a=43英石8.3磅 e=a*3.3=23英石8磅 請(qǐng)按任意鍵繼續(xù). . .
?
?
?
?
?
?
?
6.重新編寫Stonewt類(程序清單11.16和程序清單11.17),重載全部6個(gè)關(guān)系運(yùn)算符。運(yùn)算符對(duì)pounds成員進(jìn)行比較,并返回一個(gè)bool值。編寫一個(gè)程序,它聲明一個(gè)包含6個(gè)Stonewt對(duì)象的數(shù)組,并在數(shù)組聲明中初始化前3個(gè)對(duì)象。然后使用循環(huán)來讀取用于設(shè)置剩余3個(gè)數(shù)組元素的值。接著報(bào)告最小的元素、最大的元素以及大于或等于11英石的元素的數(shù)量(最簡(jiǎn)單的方法是創(chuàng)建一個(gè)Stonewt對(duì)象,并將其初始化為11英石,然后將其同其他對(duì)象進(jìn)行比較)。
答:
// 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 << "你想要以何種方式輸出數(shù)據(jù):" << endl;cout << "a.x英石y磅\tb.xx磅\tc.xx.yy磅" << endl;char a;std::cin >> a;while(!isalpha(a)){cout << "輸入錯(cuò)誤,請(qǐng)重新輸入:";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 << "輸入錯(cuò)誤,默認(rèn)以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 << "出錯(cuò),無法輸出。";}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 << "請(qǐng)輸入第:" << i + 1 << "個(gè)成員的重量(單位:磅):";cin >> q;m[i] = q;}Stonewt over = 11 * 14; //標(biāo)準(zhǔn)11英石對(duì)象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 << "個(gè),重量為:" << max << endl;cout << "最小是的第" << mi << "個(gè),重量為:" << min << endl;cout << "超過" << ov << "個(gè)大于等于11英石。" << endl;system("pause");return 0; }顯示:
請(qǐng)輸入第:4個(gè)成員的重量(單位:磅):400 請(qǐng)輸入第:5個(gè)成員的重量(單位:磅):70 請(qǐng)輸入第:6個(gè)成員的重量(單位:磅):190 最大是的第4個(gè),重量為:400磅 最小是的第5個(gè),重量為:70磅 超過3個(gè)大于等于11英石。 請(qǐng)按任意鍵繼續(xù). . .
?
?
?
?
?
7.復(fù)數(shù)有兩個(gè)部分組成:實(shí)數(shù)部分和虛數(shù)部分。復(fù)數(shù)的一種書寫方式是:(3.0,4.0),其中,3.0是實(shí)數(shù)部分,4.0是虛數(shù)部分。假設(shè)a=(A,?Bi),?c=?(C,?Di),則下面是一些復(fù)數(shù)運(yùn)算。
①加法: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為實(shí)數(shù)
⑤共軛:?~a?=?(A,?-Bi)
請(qǐng)定義一個(gè)復(fù)數(shù)類,以便下面的程序可以使用它來獲得正確的結(jié)果。
#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;
}
注意,必須重載運(yùn)算符<<和>>。標(biāo)準(zhǔn)C++使用頭文件complex提供了比這個(gè)示例更廣泛的復(fù)數(shù)支持,因此應(yīng)將自定義的頭文件命名為complex0.h,以免發(fā)生沖突。應(yīng)盡可能使用const。
下面是該程序的運(yùn)行情況。
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!
請(qǐng)注意,經(jīng)過重載后,cin>>c將提示用戶輸入實(shí)數(shù)和虛數(shù)部分。
答:
//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函數(shù),用于測(cè)試 #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"); //加上這段,否則我的編譯器上窗口會(huì)一閃而過return 0; }顯示結(jié)果和要求完全相同。
總結(jié)
以上是生活随笔為你收集整理的(一二六)第十一章编程练习的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【BZOJ3524】 [Poi2014]
- 下一篇: IntelliJ IDEA 14.x 创