C++ Primer Plus 第十六章 (16章) 编程题答案

16.10.1

 

  1. // 16.10.1.cpp: 定义控制台应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include <string>
  5. #include <iostream>
  6. using namespace std;
  7. bool reversed_same(string & st);
  8. int main()
  9. {
  10.     string str;
  11.     cout << "Enter a sentence or words.\n"
  12.         <<"type \"quit\" to quit\n";
  13.     getline(cin, str);
  14.     while (str != "quit")
  15.     {
  16.         if (reversed_same(str))
  17.             cout << "It's the same after reversing.\n";
  18.         else cout << "It's not the same after reversing.\n";
  19.         getline(cin, str);
  20.     }
  21.     return 0;
  22. }
  23. bool reversed_same(string & st)
  24. {
  25.     string temp = st;
  26.     reverse(temp.begin(), temp.end());
  27.     if (temp == st)
  28.         return true;
  29.     else return false;    
  30. }

16.10.2

  1. // 16.10.2.cpp: 定义控制台应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include <string>
  5. #include <iostream>
  6. #include <cctype>
  7. using namespace std;
  8. bool reversed_same(string & st);
  9. int main()
  10. {
  11.     string str;
  12.     cout << "Enter a sentence or words.\n"
  13.         << "type \"quit\" to quit\n";
  14.     getline(cin, str);
  15.     while (str != "quit")
  16.     {
  17.         if (reversed_same(str))
  18.             cout << "It's the same after reversing.\n";
  19.         else cout << "It's not the same after reversing.\n";
  20.         getline(cin, str);
  21.     }
  22.     return 0;
  23. }
  24. bool reversed_same(string & st)
  25. {
  26.     string only_alpha;
  27.     int limit = st.size();
  28.     for (int i = 0; i < limit; i++)
  29.         if (isalpha(st[i]))
  30.             only_alpha += tolower(st[i]);
  31.     string temp = only_alpha;
  32.     reverse(temp.begin(), temp.end());
  33.     if (temp == only_alpha)
  34.         return true;
  35.     else return false;
  36. }

 16.10.3

  1. // 16.10.3.cpp: 定义控制台应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include <iostream>
  5. #include <fstream>
  6. #include <vector>
  7. #include <string>
  8. #include <algorithm>
  9. using namespace std;
  10. void Show(string & s) { cout << s; }
  11.  
  12. int main()
  13. {
  14.     ifstream fin;
  15.     fin.open("16103words.txt");
  16.     if (fin.is_open() == false)
  17.     {
  18.         cerr << "Can't open file. Bye \n";
  19.         exit(EXIT_FAILURE);
  20.     }
  21.     vector <string> vec;
  22.     string temp;
  23.     fin >> temp;
  24.     while (fin)
  25.     {
  26.         vec.push_back(temp);
  27.         fin >> temp;
  28.     }
  29.     for_each(vec.begin(), vec.end(), Show);
  30.     fin.close();
  31.     
  32.     return 0;
  33. }
  34.  

16.10.4

  1. // 16.10.4.cpp: 定义控制台应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include <list>
  5. #include <algorithm>
  6. #include <cstdlib>
  7. #include <ctime>
  8. #include <iostream>
  9. using namespace std;
  10. int reduce(long ar[], int n);
  11. int main()
  12. {
  13.     srand(time(0));    
  14.     long data[50];
  15.     for (int i =0 ; i <50; i++)
  16.     {
  17.         data[i] = rand() % 20;
  18.     }
  19.     
  20.     cout << "before reducing : 50\n" << "after reducing: ";
  21.     cout << reduce(data, 50);
  22.     return 0;
  23. }
  24. int reduce(long ar[], int n)
  25. {
  26.     list <long> lis;    
  27.     for (int i = 0; i < n; i++)
  28.         lis.push_back(ar[i]);
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值