C++ Primer Plus 第六版(中文版)课后编程题----第八章
2016-01-13 11:08
751 查看
8.1
一开始并不太理解题目中“打印该字符串的字数为该函数被调用的次数”,于是参考了一篇博文:http://blog.csdn.net/qq844352155/article/details/23349027#include <iostream> using namespace std; void print(const char *str, int n = 0); int num = 0; int main() { print("There is no second parameter!"); print("The second parameter is zero!", 0); print("The second parameter is not zero", 1); print("Done!", 100000); system("pause"); return 0; } void print(const char *str, int n) { if (num == 0) cout << str << endl; else { for (int i = 0; i < num; ++i) cout << str << endl; } ++num; }
8.2
#include <iostream> #include <string> using namespace std; struct CandyBar { char brand[50]; double weight; int calories; }; void assignin(CandyBar & candy, const char *str = "Millenmum Munch", const double d = 2.85, const int n = 350); void display(const CandyBar & candy); int main() { CandyBar bar; assignin(bar); cout << "The original information of Candy Bar is: " << endl; display(bar); assignin(bar, "Candy Bar", 3.69, 920); cout << "The latest information of Candy Bar is: " << endl; display(bar); system("pause"); return 0; } void assignin(CandyBar & candy, const char *str, const double d, const int n) { strcpy_s(candy.brand,str); candy.weight = d; candy.calories = n; } void display(const CandyBar & candy) { cout << "Brand is: " << candy.brand << endl; cout << "Weight is: " << candy.weight << endl; cout << "Calories is: " << candy.calories << endl; }
8.3
#include <iostream> #include <string> #include <cctype> using namespace std; void Transfor(string & str); int main() { string str; cout << "Please enter a string(q to quit): "; while ((getline(cin,str)) && str != "q") { Transfor(str); cout << str << endl; cout << "Next string(q to quit): "; } cout << "Bye." << endl; system("pause"); return 0; } void Transfor(string & str) { for (int i = 0; i < str.size(); ++i) str[i] = toupper(str[i]); }
8.4
set 函数中不能使用 const char * ch 作为第二个形参,否则在函数定义中无法直接对 string 进行赋值。#include <iostream> #include <string> using namespace std; struct stringy { char * st; int ct; }; void set(stringy &str, char *ch); void show(const char *str, const int n = 1); void show(const stringy &str, const int n = 1); int main() { stringy beany; char testing[] = "Reality 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!"); system("pause"); return 0; } void set(stringy &str, char * ch) { str.ct = strlen(ch); str.st = ch; } void show(const stringy &str, const int n) { for (int i = 0; i < n; ++i) cout << str.st << endl; } void show(const char * str, const int n) { for (int i = 0; i < n; ++i) cout << str << endl; }
8.5
题中说的是固定长度为 5,此处我仍旧设置了一个可变的长度,如果只需固定长度,那 max5 函数中的第二个参数可省略。(后面才知道第六题中是可变长度,据此可以修改)#include <iostream> using namespace std; const int num = 5; template <typename T> T max5(T * arr, const int n = num);<span style="white-space:pre"> </span>//固定长度的话可以修改为 <span style="font-family: Arial, Helvetica, sans-serif;">T max5(T * arr);</span> int main() { int arr_i[num] = { 1, 5, 2, 3, 6 }; double arr_d[num] = { 1.6, 2.5, 9.8, 6.3, 2.3 }; cout << "The max of five int numbers is: " << max5(arr_i) << endl; cout << "The max of five double number is: " << max5(arr_d) << endl; system("pause"); return 0; } template <typename T> T max5(T * arr, const int n)<span style="white-space:pre"> </span>//固定长度此处修改为:<span style="font-family: Arial, Helvetica, sans-serif;">T max5(T * arr);</span> { if (n == 0)<span style="white-space:pre"> </span>//固定长度中这个判断可以省略,也没有 n 的存在了。 return 0; T max_num = arr[0]; for (int i = 1; i < n; ++i)<span style="white-space:pre"> </span>//固定长度此处修改为 <span style="font-family: Arial, Helvetica, sans-serif;">for (int i = 1; i < num; ++i)</span> max_num = max_num>arr[i] ? max_num : arr[i]; return max_num; }
8.6
注意:具体化的模板和非具体化的模板格式必须完全一致。下面两种情况都是不可行的。情况一:
//非具体化模板 template <typename T> T maxn(const T * arr, const int n); //具体化模板 template <> char *maxn<char *>(char *p[], const int n);
情况二:
//非具体化模板 template <typename T> T maxn(const T * arr, const int n); //具体化模板 template <> const char *maxn<char *>(const char *p[], const int n);
正确的应该是:
#include <iostream> using namespace std; template <typename T> T maxn(T * arr, const int n); template <> char *maxn<char *>(char *p[], const int n); int main() { int arr_i[6] = { 1, 5, 2, 3, 6, 9 }; double arr_d[4] = { 1.6, 2.5, 9.8, 6.3 }; char *p[5] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" }; cout << "The max of five int numbers is: " << maxn(arr_i, 6) << endl; cout << "The max of five double number is: " << maxn(arr_d, 4) << endl; cout << "The max length of the string is: " << maxn(p, 5) << endl; system("pause"); return 0; } template <typename T> T maxn(T * arr, const int n) { if (n == 0) return 0; T max_num = arr[0]; for (int i = 1; i < n; ++i) max_num = max_num>arr[i] ? max_num : arr[i]; return max_num; } template <> char *maxn<char *>(char *p[], const int n) { if (n == 0) return nullptr; char *pt = p[0]; for (int i = 1; i < n; ++i) pt = strlen(pt)>strlen(p[i]) ? pt : p[i]; return pt; }
8.7
#include <iostream> using namespace std; template <typename T> T SumArray(T arr[], int n); template <typename T> T SumArray(T *arr[], int n); struct debts { char name[50]; double amount; }; int main() { int things[6] = { 13, 31, 103, 301, 310, 130 }; struct debts mr_E[3]= { { "Ima Wolfe", 2400.5 }, { "Ura Foxe", 1300.3 }, { "Iby Stout",1800.4 } }; double *pd[3]; for (int i = 0; i < 3; ++i) pd[i] = &mr_E[i].amount; cout << "Total Mr. E's counts of things is: " << SumArray(things, 6) << endl; cout << "Total Mr. E's debts is: " << SumArray(pd, 3) << endl; system("pause"); return 0; } template <typename T> T SumArray(T arr[], int n) { cout << "template A\n"; T sum = T(0); for (int i = 0; i < n; ++i) sum += arr[i]; return sum; } template <typename T> T SumArray(T *arr[], int n) { cout << "template B\n"; T sum = T(0); for (int i = 0; i < n; ++i) sum += *arr[i]; return sum; }