c++ primer(第五版)习题答案代码版(第六章)函数

本文为转载,原博客地址:http://blog.csdn.net/refuil/article/details/51346844

习题答案至于一个.cc 中,编译需要包含Chapter6.h头文件。 需要演示某一题直接修改 #define NUM***, 如运行6.23题为#define NUM623;

  1. #ifndef CHAPTER6_H  
  2. #define CHAPTER6_H  
  3. int fact(int val);  
  4.   
  5. int func();  
  6.   
  7. template <typename T> T abs(T val){  
  8.     return val >= 0 ? val : -val;  
  9. }  
  10.   
  11. #endif   
[cpp]  view plain  copy
  1. #include <iostream>  
  2. #include "Chapter6.h"  
  3. using namespace std;  
  4.   
  5. int main(){  
  6.     cout << "5! is "<<fact(5)<<endl;  
  7.     cout << func() <<endl;  
  8.     cout << abs(-2.34) <<endl;  
  9. }  
[cpp]  view plain  copy
  1. #include <iostream>  
  2. #include "Chapter6.h"  
  3. using namespace std;  
  4.   
  5. int fact(int val){  
  6.     if(val == 0 || val ==1)  
  7.         return 1;  
  8.     else   
  9.         return val * fact(val-1);  
  10. }  
  11.   
  12. int func(){  
  13.     int n, ret =1;  
  14.     cout<< "Enter a number: "<<endl;  
  15.     cin >> n;  
  16.     while(n > 1)  
  17.         ret *= n--;  
  18.     return ret;  
  19. }  

[cpp]  view plain  copy
  1. #include <iostream>  
  2. #include <vector>  
  3. #include <cstring>  
  4. #include <cassert>  
  5. #include "Chapter6.h"  
  6. #define NUM648  
  7. #define NDEBUG   
  8. using namespace std;  
  9.   
  10. /*6.3*/  
  11. int fact(int val){  
  12.     if(val == 0 || val ==1)   
  13.         return 1;  
  14.     else  
  15.         return val * fact(val-1);  
  16. }  
  17. /*6.4*/  
  18. int fact2(){  
  19.     int val;  
  20.     cout << "Enter a number: "<<endl;  
  21.     while(cin >> val){  
  22.         if( val == 0 || val < 0)  
  23.             cout<<"Please input a positive number. "<<endl;  
  24.     cout<< val;  
  25.     unsigned long long exp = 1;  
  26.     exp = fact(val);  
  27.     cout<< "! is: ";  
  28.     if(exp)   
  29.         cout << exp <<endl;  
  30.     else   
  31.         cout<< "too big. "<<endl;  
  32.     }     
  33. }  
  34. /*6.5*/  
  35. #ifdef NUM65  
  36. template <typename T>  
  37. T abs(T val){  
  38.     return val > 0 ? val : -val;  
  39. }  
  40. #endif  
  41. /*6.6*/  
  42. int temp(int val1){  
  43.     static int val2 = 0;  
  44.     val2 += val1;  
  45.     return val2;  
  46. }  
  47. /*6.7*/  
  48. int func(){  
  49.     static int flag = 0;  
  50.     return flag++;  
  51. }  
  52. /*6.10*/  
  53. void  swap(int *val1, int *val2){  
  54.     int temp;  
  55.     temp = *val1;  
  56.     *val1 = *val2;  
  57.     *val2 = temp;  
  58.       
  59. }  
  60. /*6.11*/  
  61. void reset(int &val){  
  62.     val  = 0;  
  63. }  
  64. /*6.12*/  
  65. void swap2(int &val1, int &val2){  
  66.     int temp;  
  67.     temp = val1;  
  68.     val1 = val2;  
  69.     val2 = temp;  
  70. }  
  71. /*6.17*/  
  72. bool hasUpper(string &s){  
  73.     for(string::iterator it = s.begin(); it != s.end(); ++it){  
  74.         if(isupper(*it))  
  75.             return true;  
  76.         else   
  77.             return false;  
  78.     }  
  79. }  
  80. void changTolower(string& s){  
  81.     for(size_t it = 0; it != s.size()-1; ++it){  
  82.         if(isupper(s[it])){  
  83.             s[it] = tolower(s[it]);  
  84.         }  
  85.     }  
  86. }  
  87. /*6.18*/  
  88. class matrix;  
  89. bool compare(matrix &ma1, matrix &ma2);  
  90. vector<int>::iterator change_val(int, vector<int>::iterator);  
  91. /*6.21*/  
  92. int contrast(const int val1, const int *p){  
  93.     return val1 > *p ? val1 : *p;   
  94. }  
  95. /*6.22*/  
  96. void swap3(int*& val1, int*& val2){  
  97.     int* temp;  
  98.     temp = val1;  
  99.     val1 = val2;  
  100.     val2 = temp;  
  101. }  
  102. /*6.23*/  
  103. void print(const int *p){  
  104.     if(p)  
  105.         cout<< *p <<endl;  
  106. }  
  107. void print(int size, int str[]){  
  108.     for(size_t it = 0; it!= size; ++it)  
  109.         cout<< str[it] << endl;  
  110. }  
  111. void print(const int* beg, const int* end){  
  112.     for(; beg!= end; )  
  113.         cout<< *beg++ <<endl;  
  114. }  
  115. void print(int(&arr)[2]){  
  116.     for(int i =0; i<= 1; ++i)  
  117.         cout<< arr[i]<<endl;  
  118. }  
  119. /*6.27*/  
  120. #ifdef NUM627  
  121. int sum(initializer_list<int> li){  
  122.     int sum(0);  
  123.     for(initializer_list<int>::iterator beg = li.begin(); beg!= li.end(); ++beg)  
  124.         sum += *beg;  
  125.     return sum;  
  126. }  
  127. #endif  
  128. /*6.30*/  
  129. #ifdef NUM630  
  130. bool str_subrange(const string &str1, const string &str2){  
  131.     if(str1.size() == str2.size())  
  132.         return str1 == str2;  
  133.     size_t size = str1.size() < str2.size() ? str1.size() : str2.size();  
  134.     for(size_t i =0; i!= size; ++i){  
  135.         if(str1[i] != str2[i])  
  136.             return;  
  137.     }  
  138. }  
  139. #endif  
  140. /*6.33*/  
  141. int &get(vector<int> &ia, int index){ return ia[index]; }  
  142. void print(vector<int>::iterator beg, vector<int>::iterator end){  
  143.     if(beg != end){  
  144.         cout << *beg <<" ";  
  145.         print(next(beg), end);  
  146.     }  
  147. }  
  148. /*6.35*/  
  149. int factorial(int val){  
  150.     if(val > 1)  
  151.         return factorial(val -1) * val;  
  152.     return 1;  
  153. }  
  154. /*6.36*/  
  155. string (&func2(string (&str)[10]))[10];  
  156. /*6.37*/  
  157. #ifdef NUM637  
  158. typedef string arrT[10];  
  159. arrT &func3(arrT& str);  
  160.   
  161. auto func4(arrT& str) -> string(&)[10];  
  162.   
  163. string arrS[10];  
  164. decltype(arrS)& func5(arrT& str);  
  165. #endif  
  166. /*6.38*/  
  167. int odd[] = {1,3,5,7,9};  
  168. int even[] = {0,2,4,6,8};  
  169. typedef int arrInt[5];  
  170. arrInt&  arrPtr(int i){  
  171.     return (i % 2) ? odd : even; //返回引用  
  172. }  
  173. /*6.42*/  
  174. string make_plural(size_t ctr, const string& word, const string &ending = "s"){  
  175.     return (ctr > 1) ? word + ending : word;  
  176. }  
  177. /*6.44*/  
  178. inline bool isShorter(const string &s1, const string &s2){  
  179.     return s1.size() < s2.size();  
  180. }  
  181. #ifdef NUM646  
  182. /*6.46*/  
  183. constexpr bool isShorter1(const string &s1, const string &s2){  
  184.     return s1.size() < s2.size();  
  185. }  
  186. #endif  
  187. /*6.47*/  
  188. #ifdef NUM647  
  189. void printVector(vector<int>& vec){  
  190. #ifdef NDEBUG  
  191.     cout << "vector size: " << vec.size() <<endl;  
  192. #endif  
  193.     if(!vec.empty()){  
  194.         auto temp = vec.back();  
  195.         vec.pop_back();  
  196.         printVector(vec);  
  197.         cout << temp <<" ";  
  198.     }  
  199. }  
  200. #endif  
  201. /*6.51*/  
  202. #ifdef NUM651  
  203. void f(){  
  204.     cout << "f()"<<endl;  
  205. }     
  206. void f(int){  
  207.     cout << "f(int)"<<endl;  
  208. }  
  209. void f(intint){  
  210.     cout << "f(int, int)"<<endl;  
  211. }  
  212. void f(doubledouble = 3.14){  
  213.     cout << "f(duble, double)"<<endl;  
  214. }  
  215. #endif  
  216. /*6.54*/  
  217. typedef int func6(intint);  
  218. vector<func6*> vec;  
  219. /*6.55*/  
  220. int add(int a, int b){  
  221.     return a + b;  
  222. }  
  223. int substact(int a, int b){  
  224.     return a - b;  
  225. }  
  226. int multiply(int a, int b){  
  227.     return a * b;  
  228. }  
  229. int divide(int a, int b){  
  230.     return b !=0 ? a/b : 0;  
  231. }  
  232.   
  233. int main(){  
  234. /*6.1*/  
  235. #ifdef NUM61  
  236.     cout<<"实参是形参的初始值,实参的类型必须与对应的形参类型匹配。"<<endl;   
  237. #endif  
  238. /*6.2*/  
  239. #ifdef NUM62  
  240.     cout<<"(a)返回类型不匹配,int型改成string;(b)没有定义函数的返回类型,可以用void代替; (c)缺少一个括号; (d)函数体缺少一对括号."<<endl;  
  241. #endif  
  242. /*6.3*/  
  243. #ifdef NUM63  
  244.     cout << "5!: " << fact(5) <<endl;  
  245. #endif  
  246. /*6.4*/  
  247. #ifdef NUM64  
  248.     fact2();  
  249. #endif  
  250. /*6.5*/  
  251. #ifdef NUM66  
  252.     cout << "The absolute of -2.34 is: "<< abs(-2.34)<<endl;  
  253. #endif  
  254. /*6.6*/  
  255. #ifdef NUM66  
  256.     cout<< "形参的生命周期是从函数开始到函数终止即被销毁,局部变量的生命周期是从其被创建到函数体结束。静态局部变量在被初始化开始,直到程序结束才会被销毁。"<<endl;  
  257.     for(int val3 =0; val3 !=10; ++val3)  
  258.         cout << "静态变量与局部变量的和是: "<< temp(val3) <<endl;  
  259. #endif  
  260. /*6.7*/  
  261. #ifdef NUM67  
  262.     for(int i =0; i!=4; ++i)  
  263.         cout<< func()<<endl;  
  264. #endif  
  265. /*6.8*/  
  266. #ifdef NUM68  
  267.     cout<<"见Chapter6.h" <<endl;  
  268. #endif  
  269. /*6.9*/  
  270. #ifdef NUM69  
  271.     cout<<"见fact.cc factMain.cc" <<endl;  
  272. #endif  
  273. /*6.10*/  
  274. #ifdef NUM610  
  275.     int val1 = 1, val2 = 2;  
  276.     swap(&val1, &val2);  
  277.     cout<< "val1: "<< val1 << " val2: " << val2<<endl;  
  278. #endif  
  279. /*6.11*/  
  280. #ifdef NUM611  
  281.     int val = 23;  
  282.     reset(val);  
  283.     cout << "val has been reset: "<< val <<endl;  
  284. #endif  
  285. /*6.12*/  
  286. #ifdef NUM612  
  287.     for(int val1(0), val2(0); cout<< "Enter two numbers: \n", cin >> val1 >> val2;){  
  288.         swap2(val1, val2);  
  289.         cout<< "val1: "<< val1 << " val2: " << val2<<endl;  
  290.     }  
  291. #endif  
  292. /*6.13*/  
  293. #ifdef NUM613  
  294.     cout<<"第一种是传值调用,调用过程中不会修改实参的值,第二种传地址引用调用,在调用过程中将会与实参的绑定。"<<endl;  
  295. #endif  
  296. /*6.14*/  
  297. #ifdef NUM614  
  298.     cout<< "6.11例子中引用类型可以避免拷贝,但是在实参不希望被改变时不能使用引用形参."<<endl;  
  299. #endif  
  300. /*6.15*/  
  301. #ifdef NUM615  
  302.     cout<<"首先,实参s不能够被改变,但是occur的最后值时通过函数计算的;c可能是一个临时变量,可以换成其他值;如果交换类型,s可以被改变,occur不能改变,=0,报错"<<endl;  
  303. #endif  
  304. /*6.16*/  
  305. #ifdef NUM616  
  306.     cout<< "应该设置为const引用,因为s不希望被改变,可以避免拷贝,直接使用字符串常量作为参数. "<<endl;  
  307. #endif  
  308. /*6.17*/  
  309. #ifdef NUM617  
  310.     string s = "C++ & Linux";  
  311.     if(hasUpper(s)) cout << "has upper letter. "<<endl;  
  312.     else            cout << "no upper letter. "<<endl;  
  313.     changTolower(s);  
  314.     cout << "After tolower: " << s <<endl;  
  315. #endif  
  316. /*6.18*/  
  317. #ifdef NUM618  
  318.     cout<<"见main函数外函数声明;"<<endl;  
  319. #endif  
  320. /*6.19*/  
  321. #ifdef NUM619  
  322.     cout<< "(a)不合法,calc只有一个参数." <<endl;  
  323. #endif  
  324. /*6.20*/  
  325. #ifdef NUM620  
  326.     cout<< "一般能用const都加上const,如果设为普通引用,可能在函数中会改变常量的值."<<endl;  
  327. #endif  
  328. /*6.21*/  
  329. #ifdef NUM621  
  330.     int val1(2), val2(3);  
  331.     cout << "return the larger: " << contrast(val1, &val2) << endl;  
  332. #endif  
  333. /*6.22*/  
  334. #ifdef NUM622  
  335.     int val1(2), val2(3);  
  336.     int* p1 = &val1; int* p2 = &val2;  
  337.     swap3( p1, p2);  
  338.     cout << "val1: " << *p1 << " val2: " << *p2 << endl;  
  339. #endif  
  340. /*6.23*/  
  341. #ifdef NUM623  
  342.     int i =0, j[2] = {0, 1};  
  343.     print(&i);  
  344.     print(2, j);  
  345.     print(begin(j), end(j));  
  346.     print(j);  
  347. #endif  
  348. /*6.24*/  
  349. #ifdef NUM624  
  350.     cout<<"没有什么问题,单数如果仅仅为了遍历数组,完全可以指针或引用的形式传递.如:"  
  351.     " void print(const int (&ia)[10]) "<<endl;  
  352. #endif  
  353. /*6.25*/  
  354. #ifdef NUM625  
  355.     cout<< "见main-6.25.cc"<<endl;  
  356. #endif  
  357. /*6.26*/  
  358. #ifdef NUM626  
  359.     cout<< "见main-6.26.cc"<<endl;  
  360. #endif  
  361. /*6.27*/  
  362. #ifdef NUM627  
  363.     cout << "Sum of 1-5: "<< sum( {1,2,3,4,5} ) <<endl;  
  364. #endif  
  365. /*6.28*/  
  366. #ifdef NUM628  
  367.     cout << "elem 的类型是const string&"<<endl;  
  368. #endif  
  369. /*6.29*/  
  370. #ifdef NUM629  
  371.     cout <<"因为initializer_list的元素总是 const类型,不能在函数内改变,应该声明为常量引用类型. "<<endl;  
  372. #endif  
  373. /*6.30*/  
  374. #ifdef NUM630  
  375.     cout<<"见main函数外函数声明;"<<endl;  
  376. #endif  
  377. /*6.31*/  
  378. #ifdef NUM631  
  379.     cout<< "返回局部变量的引用是无效的; 如果试图对返回常量引用类型赋值,也是无效的."<<endl;  
  380. #endif  
  381. /*6.32*/  
  382. #ifdef NUM632  
  383.     cout<< "合法,作用是将0-9赋值给ia数组"<<endl;  
  384. #endif  
  385. /*6.33*/  
  386. #ifdef NUM633  
  387.     vector<int> vec(10,0);   //需要初始化,否则传参时会出错  
  388.     for(int i =0; i != 10; ++i)  
  389.         get(vec, i) = i;  
  390.     print(vec.begin(), vec.end());  
  391.     cout <<endl;  
  392. #endif  
  393. /*6.34*/  
  394. #ifdef NUM634  
  395.     cout <<"如果val为负数,将发生堆栈溢出. "<<endl;  
  396. #endif  
  397. /*6.35*/  
  398. #ifdef NUM635  
  399.     cout << factorial(5)<<endl;  
  400.     cout <<" val--无法递归,报错 "<<endl;    
  401. #endif  
  402. /*6.36*/  
  403. #ifdef NUM636  
  404.     cout<<"见main函数外函数声明, 第一种声明清晰,修改和阅读比较方便. "<<endl;  
  405. #endif  
  406. /*6.37*/  
  407. #ifdef NUM637  
  408.     cout<<"见main函数外函数声明;"<<endl;  
  409. #endif  
  410. /*6.38*/  
  411. #ifdef NUM638  
  412.     cout<<"见main函数外函数声明;"<<endl;  
  413. #endif  
  414. /*6.39*/  
  415. #ifdef NUM639  
  416.     cout << "(a)非法,顶层const,重复声明. (b)非法,函数参数相同. (c)合法. "<<endl;  
  417. #endif  
  418. /*6.40*/  
  419. #ifdef NUM640  
  420.     cout<< "(b)非法,一旦某个形参被赋予了默认值,它后面的所有形参都必须有默认值. "<<endl;  
  421. #endif  
  422. /*6.41*/  
  423. #ifdef NUM641  
  424.     cout<< "(a)非法,没有给第一个形参传值. (b)合法. (c)合法,we被赋值为'*',但与意图不符. "<<endl;  
  425. #endif  
  426. /*6.42*/  
  427. #ifdef NUM642  
  428.     cout << "singual: " << make_plural(1, "success""es")<<" "<< make_plural(1, "failure")<<endl;  
  429.     cout << "plural: " << make_plural(2,  "success""es")<<" "<< make_plural(2, "failure")<<endl;  
  430. #endif  
  431. /*6.43*/  
  432. #ifdef NUM643  
  433.     cout <<"(a)内联函数放在头文件中. (b) 函数声明放在头文件中. "<<endl;  
  434. #endif  
  435. /*6.44*/  
  436. #ifdef NUM644  
  437.     cout << isShorter("c++""linux") <<endl;  
  438. #endif  
  439. /*6.45*/  
  440. #ifdef NUM645  
  441.     cout << "内联函数的声明适合那些代码短小,并且容易经常被调用的函数. "<<endl;  
  442. #endif  
  443. /*6.46*/  
  444. #ifdef NUM646  
  445.     cout << isShorter1("c++""linux") <<endl;  
  446. #endif  
  447. /*6.47*/  
  448. #ifdef NUM647  
  449.     vector<int> vec{1,2,3,4,5};  
  450.     printVector(vec);  
  451.     cout <<endl;  
  452. #endif  
  453. /*6.48*/  
  454. #ifdef NUM648  
  455.     string s, sought("no");  
  456.     while(cin >>s && s != sought){ }  
  457.         assert(cin);  
  458.     cout<< "不合理,因为cin输入总是有内容的,所以assert中的表达式总是为真,就不会执行assert. "<<endl;  
  459. #endif  
  460. /*6.49*/  
  461. #ifdef NUM649  
  462.     cout << "函数匹配的第一步是选定本次调用对应的重载函数集,集合中的函数称为候选函数. 第二步考察本次调用提供的实参,然后从候选函数中选出能被这组实参调用的函数,这些新选出的函数称为可行函数. "<<endl;  
  463. #endif  
  464. /*6.50*/  
  465. #ifdef NUM650  
  466.     cout << "(a) 2.56匹配double,但是42匹配int.(b)匹配void f(int). (c)匹配void f(int, int). (d)匹配void f(double, double = 3.14);"<<endl;  
  467. #endif  
  468. /*6.51*/  
  469. #ifdef NUM651  
  470.     f(2.56, 42);  
  471.     f(42);  
  472.     f(42, 0);  
  473.     f(2.56, 3.14);  
  474.       
  475. #endif  
  476. /*6.52*/  
  477. #ifdef NUM652  
  478.     cout << "(a)3, 通过类型提升实现的匹配. (b)4, 通过算术类型转换实现的匹配. "<<endl;  
  479. #endif  
  480. /*6.53*/  
  481. #ifdef NUM653  
  482.     cout <<"(a)没有影响,const是顶层实现,第二句实现了函数重载. (b)非法,重复声明."<<endl;  
  483. #endif  
  484. /*6.54*/  
  485. #ifdef NUM654  
  486.     cout <<"见main函数外声明. "<<endl;  
  487. #endif  
  488. /*6.55*/  
  489. #ifdef NUM655  
  490.     vec.push_back(add);  
  491.     vec.push_back(substact);  
  492.     vec.push_back(multiply);  
  493.     vec.push_back(divide);  
  494.     for(vector<func6*>::iterator it = vec.begin(); it != vec.end(); ++it)  
  495.         cout << (*it)(100, 50)<< " ";  //*it两端的括号必不可少, 否则函数返回vector类型的指针, 而非函数指针.  
  496.     cout <<endl;  
  497. #endif  
  498.     return 0;  
  499. }  

参考资料:

c++ primer中文版第五版,电子工业出版社。
c++ primer第四版习题解答,人民邮电出版社。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值