《C++ Primer Plus》——编程练习答案(7)
来源:互联网 发布:韩国女主播软件 编辑:程序博客网 时间:2024/06/06 06:50
第八章 函数探幽
8.8.1 有点没看懂题目表达什么,由n决定输出的次数(函数调用的次数),那么就递归喽。
#include <iostream>#include <string>using namespace std;void printf_m(string s, int n = 0);int main(){ string sInput = "I am a student."; printf_m(sInput); int Time; cout << "Enter times: "; cin >> Time; printf_m(sInput, Time); cin.sync(); cin.get(); return 0;}void printf_m(string s, int n){ cout << s << endl; if (n==0||n==1) { return; } else { printf_m(s, n - 1); }}
8.8.2
#include <iostream>const int SIZE_CB = 20;using namespace std;struct CandyBar{ char CBname[SIZE_CB]; double CBweight; int CBheat;};void SetCandyBar(CandyBar &x, char *p = "Millennium Munch", double w = 2.85, int h = 350);int main(){ CandyBar T; SetCandyBar(T); SetCandyBar(T,"Arthur Yong",155.25,78); cin.get(); return 0;}void SetCandyBar(CandyBar &x, char *p, double w, int h){ void ShowCandyBar(const CandyBar &c); int i = 0; while (p[i]!='\0') { x.CBname[i] = p[i]; i++; } x.CBname[i] = '\0'; x.CBweight = w; x.CBheat = h; ShowCandyBar(x);}void ShowCandyBar(const CandyBar &c){ cout <<"CandyBar name: "<< c.CBname << endl; cout << "CandyBar weight: " << c.CBweight<< endl; cout << "CandyBar heat: " << c.CBheat<< endl;}
8.8.3
这里要说一下 书上说对string类使用toupper()完成大小写转换,可是在std标准库中,cctype中的函数只能用于char型,因此必须借助STL的transform才能完成上述要求。
#include <iostream>#include <string>#include <cctype>#include <algorithm> //transformusing namespace std; void TOUPPER(string &temp);int main(){ string T; cout << "Enter a string (q to quit): "; getline(cin, T); while (T!="q") { TOUPPER(T); cout << T << endl; cout << "Next string (q to quit): "; getline(cin, T); } cout << "Bye!" << endl; cin.get(); return 0;}void TOUPPER(string &temp){ transform(temp.begin(), temp.end(), temp.begin(), ::toupper);
8.8.4
分别使用了函数的重载和默认参数完成
#include <iostream>using namespace std;#include <cstring>struct stringy{ char *str; int ct;};void set(stringy &x, char *s);void show(stringy const &x);void show(stringy const &x, int n);void show(char *s, int n=0);int main(){ stringy beany; char testing[] = "Really isn't what it used to be."; set(beany, testing); show(beany); show(beany, 2); testing[0] = 'D'; testing[1] = 'u'; show(testing); show(testing,3); show("Done!"); cin.get(); return 0;}void set(stringy &x, char *s){ x.str = s; int len = 0; while (*s!='\0') { len++; s++; } x.ct = len;}void show(stringy const &x){ cout <<"str: "<< x.str<<endl; cout <<"ct: "<< x.ct<<endl;}void show(stringy const &x, int n){ for (int i = 0; i < n; i++) { cout << "str: " << x.str << endl; cout << "ct: " << x.ct << endl; }}void show(char *s, int n){ cout << s << endl; if (n==0||n==1) { return; } else { show(s, n - 1); }}
8.8.5
#include <iostream>using namespace std;const int SIZE = 5;template<typename T>T Max_(T x[SIZE]);int main(){ int a[SIZE] = { 32, 65, 32, 21, 66 }; double b[SIZE] = { 54.32, 54.23, 34.67, 87.4, 23.6 }; cout << "a#: " << Max_<int>(a) << endl; cout << "b#: " << Max_<double>(b) << endl; cin.get(); return 0;}template<typename T>T Max_(T x[SIZE]){ T temp = x[0]; for (int i = 1; i < SIZE; i++) { if (x[i]>temp) { temp = x[i]; } } return temp;}
8.8.6
#include <iostream>using namespace std;template<typename T>T Max(T x[], int n);//////////template<> char * Max<char *>(char* x[], int n);int main(){ int a[6] = { 56, 32, 43, 54, 2, 45 }; double b[4] = { 54.32, 34.6, 23.4, 12.34 }; char *c[3] = { "I am a student", "Avalon_Y", "Arthur Yong" }; cout << "a# : " << Max<int>(a, 6) << endl; cout << "b# : " << Max<double>(b, 4) << endl; cout << "c# : " << Max<char *>(c, 3) << endl; cin.get(); return 0;}template<typename T>T Max(T x[], int n){ if (x==NULL) { cerr << "error!" << endl; exit(1); } T temp=x[0]; for (int i = 1; i < n; i++) { if (x[i]>temp) { temp = x[i]; } } return temp;}template<> char * Max<char *>(char* x[], int n){ if (x == NULL) { cerr << "error!" << endl; exit(1); } char *temp =x[0]; for (int i = 1; i < n; i++) { if (strlen(x[i])>strlen(temp)) { temp =x[i]; } } return temp;}
8.8.7
#include <iostream>using namespace std;struct debts{ char name[50]; double amount;};template<typename T>void Sumarray(T *x, int n);template<> void Sumarray<debts>(debts* x, int n);int main(){ int things[6] = { 13, 31, 103, 301, 310, 130 }; debts mr_E[3] = { { "Ima Wolfe", 2400.0 }, { "Ura Foxe", 1300.0 }, { "Iby Stout", 1800.0 } }; Sumarray<int>(things, 6); Sumarray<debts>(mr_E, 3); cin.get(); return 0;}template<typename T>void Sumarray(T *x, int n){ T temp=0; for (int i = 0; i < n; i++) { temp += x[i]; } cout << "Sum: " << temp << endl;}template<> void Sumarray<debts>(debts* x, int n){ double temp = 0; for (int i = 0; i < n; i++) { temp += x[i].amount; } cout << "Sum: " << temp << endl;}
0 0
- 《C++ Primer Plus》——编程练习答案(7)
- 《C++ Primer Plus》——编程练习答案(1)
- 《C++ Primer Plus》——编程练习答案(2)
- 《C++ Primer Plus》——编程练习答案(3)
- 《C++ Primer Plus》——编程练习答案(4)
- 《C++ Primer Plus》——编程练习答案(5)
- 《C++ Primer Plus》——编程练习答案(6)
- 《C++ Primer Plus》——编程练习答案(8)
- 《C++ Primer Plus》——编程练习答案(9)
- 《C++ Primer Plus》——编程练习答案(10)
- 《C++ Primer Plus》——编程练习答案(11)
- C++.Primer.Plus第五版第五章编程练习答案
- c++.primer.plus第五版第六章编程练习答案
- c++.primer.plus第五版第七章编程练习答案
- c++.primer.plus第五版第八章编程练习答案
- c++.primer.plus第五版第九章编程练习答案
- c++.primer.plus第五版第十章编程练习答案
- C++primer plus第六版课后编程练习答案2.2
- mysql 主从复制 主主配置
- 详细理解context(context访问资源的唯一性分析)
- MySQL 查看约束,添加约束,删除约束 添加列,修改列,删除列
- Problem F: 删出多余的空格
- FTP协议学习总结
- 《C++ Primer Plus》——编程练习答案(7)
- 你该知道的json解析
- 【WebService开发】之编写天气预报的WebService客户端
- ARM架构 修改DMA大小
- 扒扒数据库长长知识(下载资源组合看day2)之08 (集合运算)
- case class与case object实战
- appcompat_v7/res/values-v21/themes_base.xml No resource found that matches the given name: androidXX
- 打印非负整数阶乘的函数
- 求字符串长度