1 void ch8_1_print(const std::string & str, int n = 0 ) {
2 using namespace std;
3 static int flag = 0;
4 ++ flag;
5 if (!n)
6 cout << str << endl;
7 else {
8 for (int i = flag; i; -- i)
9 cout << str << endl;
10 }
11 }
12 void ch8_1() {
13 using namespace std;
14 string str{"ch8_1"};
15 ch8_1_print(str);
16 ch8_1_print(str, 100);
17 }
18
19 void ch8_2_setvalue(CandyBar & bar, const char * bd = "Millennium Munch", double w = 2.85, unsigned long k = 350) {
20 strcpy(bar.brand, bd);
21 bar.weight = w;
22 bar.kll = k;
23 }
24
25 void ch8_2_show(const CandyBar & bar) {
26 using namespace std;
27 cout << "brand: " << bar.brand << endl;
28 cout << "weight: " << bar.weight << endl;
29 cout << "energy: " << bar.kll << endl;
30 }
31
32 void ch8_2() {
33 using namespace std;
34 CandyBar bar;
35 cout << "default value: " << endl;
36 ch8_2_setvalue(bar);
37 ch8_2_show(bar);
38 char s[] = "nnn";
39 double w = 100.123;
40 unsigned long k = 1234;
41 cout << "reset value: " << endl;
42 ch8_2_setvalue(bar,s,w,k);
43 ch8_2_show(bar);
44 }
45 void ch8_3_str2upper(std::string & str) {
46 for (int i = 0; i < str.size(); ++ i)
47 str[i] = toupper(str[i]);
48 }
49 void ch8_3() {
50 using namespace std;
51 string s;
52 while (true) {
53 cout << "enter string(q to quit): ";
54 getline(cin, s);
55 if (s == "q")
56 break;
57 ch8_3_str2upper(s);
58 cout << "upper: " << s << endl;
59 }
60 }
61
62 void ch8_4_set(stringy & sy, const char * str) {
63 sy.str = new char[strlen(str)];
64 strcpy(sy.str, str);
65 sy.ct = strlen(str);
66 }
67
68 void ch8_4_show(const stringy & sy, int n = 0) {
69 using namespace std;
70 if (!n)
71 cout << sy.str << endl;
72 else {
73 for (int i = 0; i < n; ++ i)
74 cout << sy.str[i];
75 cout << endl;
76 }
77 }
78
79 void ch8_4_show(const char * str, int n = 0) {
80 using namespace std;
81 if (!n)
82 cout << str << endl;
83 else {
84 for (int i = 0; i < n; ++ i)
85 cout << str[i];
86 cout << endl;
87 }
88 }
89
90 void ch8_4() {
91 using namespace std;
92 stringy beany;
93 char testing[] = "Reality isn't what it used to be.";
94 ch8_4_set(beany, testing);
95 ch8_4_show(beany);
96 ch8_4_show(beany, 2);
97 testing[0] = 'D';
98 testing[1] = 'u';
99 ch8_4_show(testing);
100 ch8_4_show(testing, 3);
101 ch8_4_show("Done!");
102 }
103
104 template <typename T>
105 unsigned int ch8_5_max(const T * arr) {
106 unsigned int max_index = 0;
107 for (int i = 1; i < 5; ++ i)
108 if (arr[i] > arr[max_index])
109 max_index = i;
110 return max_index;
111 }
112
113 void ch8_5() {
114 using namespace std;
115 int arr_int[5]{1,3,5,2,4};
116 double arr_dou[5]{3.14,2.14,5.12,8.12,10.30};
117 cout << "max int: " << arr_int[ch8_5_max(arr_int)] << endl;
118 cout << "max dou: " << arr_dou[ch8_5_max(arr_dou)] << endl;
119 }
120
121 template <typename T>
122 T ch8_6_maxn(T * arr, unsigned int n) {
123 int max_index = 0;
124 for (int i = 1; i < n; ++ i)
125 if (arr[i] > arr[max_index])
126 max_index = i;
127 return arr[max_index];
128 }
129
130 template <> char * ch8_6_maxn<char *>(char * arr[], unsigned int n) {
131 int max_index = 0;
132 for (int i = 1; i < n; ++ i)
133 if (strlen(arr[i]) - strlen(arr[max_index]) > 0)
134 max_index = i;
135 return arr[max_index];
136 }
137
138 void ch8_6() {
139 using namespace std;
140 int arr_int[6]{1,2,3,4,5,6};
141 double arr_dou[4]{1.1,2.2,3.3,4.4};
142 char * arr_str[5]{
143 "h", "he", "hel", "hell", "hello"
144 };
145 cout << "max double: " << ch8_6_maxn(arr_int, 6) << endl;
146 cout << "max int : " << ch8_6_maxn(arr_dou, 4) << endl;
147 cout << "max str : " << ch8_6_maxn(arr_str, 5) << endl;
148 }
149
150 template <typename T>
151 T ch8_7_sumarray(T * arr, int n) {
152 T sum{0};
153 for (int i = 0; i < n; ++ i)
154 sum += arr[i];
155 return sum;
156 }
157
158 template <typename T>
159 T ch8_7_sumarray(T * arr[], int n) {
160 T sum{0};
161 for (int i = 0; i < n; ++ i)
162 sum += * arr[i];
163 return sum;
164 }
165
166 void ch8_7() {
167 using namespace std;
168 int things[6]{13,31,103,301,310,130};
169 debts mr_E[3]{
170 {"Ima Wolfe", 2400.0},
171 {"Ura Foxe", 1300.0},
172 {"Iby Stout", 1800.0}
173 };
174 double * pd[3];
175 for (int i = 0; i < 3; ++ i)
176 pd[i] = &mr_E[i].amount;
177 cout << "sum of things: " << ch8_7_sumarray(things, 6) << endl;
178 cout << "sum of debts: " << ch8_7_sumarray(pd, 3) << endl;
179 }

【C++ Primer Plus】编程练习答案——第8章的更多相关文章

  1. 【C++ Primer Plus】编程练习答案——第12章

    1 // chapter12_1_cow.h 2 3 4 #ifndef LEARN_CPP_CHAPTER12_1_COW_H 5 #define LEARN_CPP_CHAPTER12_1_COW ...

  2. 【C++ Primer Plus】编程练习答案——第11章 (待更新)

    最近开学,事情较多,过两天更新...

  3. 【C++ Primer Plus】编程练习答案——第10章

    1 // chapter10_1_account.h 2 3 #ifndef LEARN_CPP_CHAPTER10_1_ACCOUNT_H 4 #define LEARN_CPP_CHAPTER10 ...

  4. 【C++ Primer Plus】编程练习答案——第9章

    1 // chapter09_golf.h 2 3 #ifndef LEARN_CPP_CHAPTER09_GOLF_H 4 #define LEARN_CPP_CHAPTER09_GOLF_H 5 ...

  5. 【C++ Primer Plus】编程练习答案——第7章

    1 double ch7_1_harmonicaverage(double a, double b) { 2 return 2 / (1 / a + 1 / b); 3 } 4 5 void ch7_ ...

  6. 【C++ Primer Plus】编程练习答案——第6章

    1 void ch6_1() { 2 using namespace std; 3 char ch; 4 while ((ch = cin.get()) != '@') { 5 if (isdigit ...

  7. 【C++ Primer Plus】编程练习答案——第5章

    1 void ch5_1() { 2 using namespace std; 3 int small, big, sum{0}; 4 cout << "enter small ...

  8. 【C++ Primer Plus】编程练习答案——第4章

    1 void ch4_1() { 2 using namespace std; 3 string fname, lname; 4 char grade; 5 unsigned int age; 6 c ...

  9. 【C++ Primer Plus】编程练习答案——第3章

    1 void ch3_1() { 2 using namespace std; 3 unsigned int factor = 12; 4 unsigned int inch, feet; 5 cou ...

随机推荐

  1. 十六:使用JDBC对数据库进行CRUD

    一.statement对象介绍 Jdbc中的statement对象用于向数据库发送SQL语句,想完成对数据库的增删改查,只需要通过这个对象向数据库发送增删改查语句即可. Statement对象的exe ...

  2. 【java虚拟机】jvm调优原则

    转自:https://www.cnblogs.com/xiaopaipai/p/10522794.html 合理规划jvm性能调优 JVM性能调优涉及到方方面面的取舍,往往是牵一发而动全身,需要全盘考 ...

  3. Int 2e 与 Sysenter区别

    参考:张银奎<软件调试>第八章 Int 2e: Windows将2e号向量专门用作系统调用,在启动早起初始化中断描述表时便注册好了适合的服务例程.因此当NtDll中的NtReadFile发 ...

  4. 通过PEB的Ldr枚举进程内所有已加载的模块

    一.几个重要的数据结构,可以通过windbg的dt命令查看其详细信息 _PEB._PEB_LDR_DATA._LDR_DATA_TABLE_ENTRY 二.技术原理 1.通过fs:[30h]获取当前进 ...

  5. JAVA 各种时间类型转换

    final Date date = new Date(); final Timestamp timestamp = new Timestamp(date.getTime()); final Calen ...

  6. Python中的多线程编程

    前言: 线程是操作系统能够进行运算调度的最小单位(程序执行流的最小单元) 它被包含在进程之中,是进程中的实际运作单位 一个进程中可以并发多个线程每条线程并行执行不同的任务 (线程是进程中的一个实体,是 ...

  7. 剑指 Offer 32 - I. 从上到下打印二叉树

    剑指 Offer 32 - I. 从上到下打印二叉树 从上到下打印出二叉树的每个节点,同一层的节点按照从左到右的顺序打印. 例如: 给定二叉树: [3,9,20,null,null,15,7], 3 ...

  8. 矩阵BFS

    leetcode 1091矩阵BFS 在一个 N × N 的方形网格中,每个单元格有两种状态:空(0)或者阻塞(1). 一条从左上角到右下角.长度为 k 的畅通路径,由满足下述条件的单元格 C_1, ...

  9. awk 命令-随笔

    awk语法: awk [option] 'pattern{action}' file ... awk [参数] '条件{动作}' 文件 ... 解析: 命令: awk 参数: -F "&qu ...

  10. IMAP协议笔记

    1. IMAP客户端接收流程简介 1) 遍历所有的邮箱,获取邮箱状态,然后遍历所有邮箱获取到更新的邮件头部信息(包含收件人.发件人.主题.以及时间信息) 2)获取最新邮件的完整信息,其中也包括在第一步 ...