C++ primer plus中文编程练习答案第16章.docx
《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
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C+ primer plus中文编程练习答案第16章 plus 中文 编程 练习 答案 16
链接地址:https://www.31ppt.com/p-3060399.html