《C++ primer plus中文编程练习答案第16章.docx》由会员分享,可在线阅读,更多相关《C++ primer plus中文编程练习答案第16章.docx(12页珍藏版)》请在三一办公上搜索。
1、C+ primer plus中文编程练习答案第16章1、 /Reverse.cpp #include #include using namespace std; boolisReverse(const string &s1, const string &s2); int main string words; cout words&words != quit) stringrwords(words.rbegin, words.rend); cout words: words endl; if (isReverse(rwords, words) cout words is reverse word
2、s.n; else cout words is not reverse words.n; cout Enter a next words (quit to quit): n; system(pause); return 0; boolisReverse(const string &s1, const string &s2) if (pare(s2) return false; else return true; 2、 /Reverse.cpp #include #include #include using namespace std; boolisReverse(const string &
3、s1, const string &s2); voidToLower(string &s1); int main string words; cout Enter a string (quit to quit): n; getline(cin, words); while (words != quit) ToLower(words); stringrwords(words.rbegin, words.rend); cout words: words endl; if (isReverse(rwords, words) cout words is reverse words.n; else co
4、ut words is not reverse words.n; cout Enter a next words (quit to quit): n; getline(cin, words); system(pause); return 0; boolisReverse(const string &s1, const string &s2) if (pare(s2) return false; else return true; voidToLower(string &s1) unsignedinti = 0; while (i s1.length) if (isalpha(s1i) s1i
5、= tolower(s1i); i+; else s1.replace(i, s1.size, s1, i + 1, s1.size); 3、 /hangman.cpp #include #include #include #include #include #include #include using namespace std; vector wordlist; voidFillWord; int main srand(time(0); char play; cout Will you play a word game? ; cin play; play = tolower(play);
6、 FillWord; while (play = y) string target = wordlistrand % wordlist.size; int length = target.length; string attempt(length, -); stringbadchars; int guesses = 6; cout Guess my secret word. It has length letters, and you guessn one letter at a time. You get guesses wrong guesses.n; cout Your word: at
7、tempt 0 & attempt != target) char letter; cout letter; if (badchars.find(letter) != string:npos | attempt.find(letter) != string:npos) cout You already guessed that. Try again.n; continue; intloc = target.find(letter); if (loc = string:npos) cout Oh,bad guess!n; -guesses; badchars += letter; else co
8、ut Good guess!n; attemptloc = letter; loc - target.find(letter, loc + 1); while (loc != string:npos) attemptloc = letter; loc = target.find(letter, loc + 1); cout Your word: attempt 0) cout Bad choices: badcharsendl; cout guesses 0) cout Thats right!n; else cout Sorry, the word is target .n; cout Wi
9、ll you play another? ; cin play; play = tolower(play); cout Byen; system(pause); return 0; voidFillWord ifstream fin; string letter; fin.open(letters.txt); if (fin.is_open = false) cerr letter; wordlist.push_back(letter); fin.close; /letters.txt apiary beetle cereal danger ensign florid garage healt
10、h insult jackal keeper loaner manage nonce onset plaid quilt remote stolid train useful valid whence xenon yearn zippy 4、 /Reduce.cpp #include #include #include using namespace std; int reduce(long ar, int n); voidoutlong(int n) cout n ; int main long a10 = 1, 3, 2, 4, 7, 6, 3, 4, 8, 10 ; intnums =
11、reduce(a, 10); cout Numbers after reduce: numsendl; system(pause); return 0; int reduce(long ar, int n) listarr; arr.insert(arr.begin, ar, ar + n); arr.sort; arr.unique; for_each(arr.begin, arr.end, outlong); coutendl; returnarr.size; 5、 /Reduce.cpp #include #include #include #include using namespac
12、e std; template int reduce(T ar, int n); template voidoutlong(T n) cout n ; int main long a10 = 1, 3, 2, 4, 7, 6, 3, 4, 8, 10 ; int nums1 = reduce(a, 10); cout Numbers after reduce: nums1 endl; string b10 = qwe, qwe, ret, tyu, dfg, jkl, iok, kjl, ads, def ; int nums2 = reduce(b, 10); cout Numbers af
13、ter reduce: nums2 endl; system(pause); return 0; template int reduce(T ar, int n) listarr; arr.insert(arr.begin, ar, ar + n); arr.sort; arr.unique; for_each(arr.begin, arr.end, outlong); coutendl; returnarr.size; 6、 /bank.cpp #include #include #include #include using namespace std; class Customer pr
14、ivate: long arrive; intprocesstime; public: Customer arrive = processtime = 0; void set(long when) processtime = rand % 3 + 1; arrive = when; long whenconst return arrive; intptimeconst return processtime; ; typedef Customer Item; constint MIN_PER_HR = 60; boolnewcustomer(double x); int main srand(t
15、ime(0); cout Case Study: Bank of Heather Automatic Tellern; coutqs; queue line; cout hours; longcyclelimit = MIN_PER_HR*hours; coutperhour; doublemin_per_cust; min_per_cust = MIN_PER_HR / perhour; Item temp; longturnaways = 0; long customers = 0; long served = 0; longsum_line = 0; intwait_time = 0;
16、longline_wait = 0; for (int cycle = 0; cycle cyclelimit; cycle+) if (newcustomer(min_per_cust) if (line.size = qs) turnaways+; else customers+; temp.set(cycle); line.push(temp); if (wait_time 0) wait_time-; sum_line += line.size; if (customers 0) cout customers accepted: customers endl; cout custome
17、rs served: served endl; cout turnaways: turnawaysendl; cout average queue size: ; cout.precision(2); cout.setf(ios_base:fixed, ios_base:floatfield); cout (double)sum_line / cyclelimitendl; cout average wait time: (double)line_wait / served minutesn; else cout No customers!n; cout Done!n; system(paus
18、e); return 0; boolnewcustomer(double x) return (rand*x / RAND_MAX 1); 7、 /Lotto.cpp #include #include #include #include #include using namespace std; vector Lotto(int l1, int l2); void Show(int n); int main vector winners; winners = Lotto(51, 6); for_each(winners.begin, winners.end, Show); system(pa
19、use); return 0; vector Lotto(int l1, int l2) vectornums; vector nums2; srand(time(0); for (inti = 0; i l1; i+) nums.push_back(i); random_shuffle(nums.begin, nums.end); for (inti = 0; i l2; i+) nums2.push_back(numsrand % l1 + 1); return nums2; void Show(int n) cout The winner is n endl; 8、 /name.cpp
20、#include #include #include #include #include int main using namespace std; ostream_iteratorout(cout, ); string fname1; set A; cout Enter Mats friends(quit to quit): n; getline(cin, fname1); while (fname1 != quit) A.insert(fname1); getline(cin, fname1); cout Mats friends: n; copy(A.begin, A.end, out)
21、; coutendl; string fname2; set B; cout Enter Pats friends(quit to quit): n; getline(cin, fname1); while (fname1 != quit) B.insert(fname1); getline(cin, fname1); cout Pats friends: n; copy(B.begin, B.end, out); coutendl; cout Union of Mat and Pats friends: ; set_union(A.begin, A.end, B.begin, B.end,
22、out); coutendl; system(pause); return 0; 9、 /sorttime.cpp #include #include #include #include #include #include using namespace std; constint Size = 10000; int main clock_t start, end; vector vi0(Size); list li(Size); srand(time(0); for (inti = 0; i Size; i+) vi0i = rand % Size; copy(vi0.begin, vi0.
23、end, back_inserter(li); vector vi(vi0); start = clock; sort(vi.begin, vi.end); end = clock; cout Time of Sort Vector: (double)(end - start) / CLOCKS_PER_SEC endl; start = clock; li.sort; end = clock; cout Time of Sort List: (double)(end - start) / CLOCKS_PER_SEC endl; copy(vi0.begin, vi0.end, back_i
24、nserter(li); start = clock; copy(li.begin, li.end, back_inserter(vi); sort(vi.begin, vi.end); copy(vi.begin, vi.end, back_inserter(li); end = clock; cout Time of Sort List2: (double)(end - start) / CLOCKS_PER_SEC endl; system(pause); return 0; 10、 /vect.cpp #include #include #include #include #inclu
25、de #include using namespace std; struct Review string title; int rating; double price; ; bool operator(constshared_ptr&r1, constshared_ptr&r2); boolworseThan(constshared_ptr&r1, constshared_ptr&r2); boolbetterThan(constshared_ptr&r1, constshared_ptr&r2); boolworseThanP(constshared_ptr&r1, constshare
26、d_ptr&r2); boolbetterThanP(constshared_ptr&r1, constshared_ptr&r2); boolFillReview(shared_ptr&rr); shared_ptrmake_Review; voidShowReview(shared_ptr&rr); int main vectorshared_ptr books; shared_ptrtemp(new Review); while (FillReview(temp) books.push_back(temp); temp = make_Review; if (books.size 0) v
27、ectorshared_ptrsbook(books); cout Thank you. You entered the following:n books.size ratings:n RatingtBooktPricen; for_each(books.begin, books.end, ShowReview); charch; cout Enter measures of sort:no to old; coutch; while (tolower(ch) != q) switch (ch) caseo: cout Not Sort:nRatingtBooktPricen; for_ea
28、ch(books.begin, books.end, ShowReview); break; caset: sort(sbook.begin, sbook.end); cout Sorted by title:nRatingtBooktPricen; for_each(sbook.begin, sbook.end, ShowReview); break; caser: sort(sbook.begin, sbook.end, worseThan); cout Sorted by down rating:nRatingtBooktPricen; for_each(sbook.begin, sbo
29、ok.end, ShowReview); break; caseR: sort(sbook.begin, sbook.end, betterThan); cout Sorted by up rating:nRatingtBooktPricen; for_each(sbook.begin, sbook.end, ShowReview); break; casep: sort(sbook.begin, sbook.end, worseThanP); cout Sorted by down price:nRatingtBooktPricen; for_each(sbook.begin, sbook.
30、end, ShowReview); break; caseP: sort(sbook.begin, sbook.end, betterThanP); cout Sorted by up price:nRatingtBooktPricen; for_each(sbook.begin, sbook.end, ShowReview); break; caseF: casef: random_shuffle(sbook.begin, sbook.end); cout After shuffling:nRatingtBookn; for_each(sbook.begin, sbook.end, Show
31、Review); break; default: cout Error input!Input again!; break; cout Enter the next measures:no to old; coutch; else cout No entries. ; cout Bye.n; system(pause); return 0; bool operator(constshared_ptr&r1, constshared_ptr&r2) if (r1-title title) return true; else if (r1-title = r2-title&r1-rating ra
32、ting) return true; else return false; boolworseThan(constshared_ptr&r1, constshared_ptr&r2) if (r1-rating rating) return true; else return false; boolbetterThan(constshared_ptr&r1, constshared_ptr&r2) if (r1-rating r2-rating) return true; else return false; boolworseThanP(constshared_ptr&r1, constsh
33、ared_ptr&r2) if (r1-price price) return true; else return false; boolbetterThanP(constshared_ptr&r1, constshared_ptr&r2) if (r1-price r2-price) return true; else return false; shared_ptrmake_Review returnshared_ptr(new Review); boolFillReview(shared_ptr&rr) couttitle); if (rr-title = quit) return false; coutrr-rating; coutrr-price; if (!cin) return false; while (cin.get != n) continue; return true; voidShowReview(shared_ptr&rr) coutrating t title t price endl;