C++ primer plus 第八章答案
2017-08-17 16:36
357 查看
万恶的暑假(T_T)
1.
2.
3.
6.
1.
#include <iostream> #include <string> using namespace std; void Print(string *name, int time= 0); int time = 0; int main() { string name = "Hu Zeyu"; Print(&name,time); Print(&name,time); return 0; } void Print(string *name, int time) { for(int i = 0; i <= time; i++) { cout << *name << endl; } time++; }
2.
#include <iostream> using namespace std; struct Candy { char* brandname; double weight; int calories; }; void Set(Candy &candybar, char* brandname = "Millenium Munch", double weight = 2.85, int calories = 350); void Show(const Candy &candybar); int main() { Candy test; Set(test); Show(test); return 0; } void Set(Candy & candybar, char * brandname, double weight, int calories) { candybar.brandname = brandname; candybar.weight = weight; candybar.calories = calories; } void Show(const Candy & candybar) { cout << candybar.brandname << endl; cout << candybar.weight << endl; cout << candybar.calories << endl; }
3.
#include <iostream> #include <string> using namespace std; void mytoupper(string & words); int main() { string test; cout << "Enter a string (q to quit): "; while(getline(cin, test) && test != "q") { mytoupper(test); cout << test << endl; cout << "Next string(q to quit): "; } cout << "Bye."; return 0; } void mytoupper(string & words) { int length = words.size(); for(int i = 0; i < length; i++) { words[i] = toupper(words[i]); } }4.
#include <iostream> using namespace std; #include <cstring> // for strlen(),strcpy() struct stringy{ char * str; int ct; }; void set(stringy &beany, char []); void show(const char [],int = 1); void show(const stringy &, int = 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!"); return 0; } void set(stringy &beany,char testing[]) { beany.ct = strlen(testing); beany.str = new char[beany.ct + 1]; strcpy(beany.str, testing); } void show(const char testing[],int length) { for(int i = 0; i < length; i++) { cout << testing << endl; } } void show(const stringy &beany,int length) { for(int i = 0; i < length; i++) { cout << beany.str << endl; } }5.
#include <iostream> using namespace std; template <typename T> T max5(T a[]); int main() { int array1[] = {1,2,3,4,5}; double array2[] = {2.2,3.4,4.5,5.6,3.2}; cout << max5(array1) << endl; cout << max5(array2) << endl; return 0; } template <typename T> T max5(T a[]) { T max = a[0]; for(int i = 0; i < 5; i++) { max = max >= a[i] ? max : a[i]; } return max; }
6.
#include <iostream> #include <cstring> using namespace std; template <typename T> T maxn(T num[], int length); template <> char *maxn<char *>(char *num[], int length); int main() { int a[6] = {1, 2, 3, 4, 5, 6}; double b[4] = {1.1, 2.2, 3.3, 4.4}; char *c[5] ={"apple","banana","egg","seal","elephant"}; std::cout << maxn(a, 6) << std::endl; std::cout << maxn(b, 4) << std::endl; std::cout << maxn(c, 5) << std::endl; return 0; } template <typename T> T maxn(T num[], int length) { T max = num[0]; for(int i = 0; i < length; i++) { max = max >= num[i] ? max : num[i]; } return max; } template <> char *maxn<char *>(char *num[], int length) { int max = strlen(num[0]); for(int i = 0; i < length; i++) { max = max >= (int)strlen(num[i]) ? max : strlen(num[i]); } for(int i = 0; i < length; i++) { if((int)strlen(num[i]) == max) return num[i]; } }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.0}, {"Ura Foxe", 1300.0}, {"Iby Stout", 1800.0} }; double *pd[3]; for(int i = 0; i < 3; i++) pd[i] = &mr_E[i].amount; cout << SumArray(things, 6) << endl; cout << "Listing Mr.E's debts:\n"; cout << SumArray(pd, 3) << endl; return 0; } template <typename T> T SumArray(T arr[], int n) { int sum = 0; cout << "template A\n"; 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"; int sum = 0; for(int i = 0;i < n; i++) sum += *arr[i]; return sum; }
相关文章推荐
- 《C++ Primer Plus(第六版)》(12)(第八章 函数探幽 编程题答案)
- C++ Primer Plus第六版 第八章 编程练习答案
- c++ primer plus(第6版)中文版 第八章编程练习答案
- C++ Primer Plus第五版 第八章 编程练习答案
- C++ primer plus(sixth edition) 编程练习答案(answers for programing exercises)第八章(chapter 8) 1-4
- c++ primer plus第八章习题答案
- c++primer plus 第八章习题答案(自己写的)
- c++ primer plus(文章6版本)中国版 编程练习答案第八章
- 《C++ Primer Plus(第六版)》(11)(第八章 函数探幽 复习题答案)
- C++ primer plus 第六版 课后答案 8.4
- C++ primer plus(sixth edition) 编程练习答案(answers for programing exercises)第五章(chapter 5) 6-10
- 关于《C++ primer plus 第六版》中文版的答案说明
- C++primer plus第六版课后编程题答案8.4
- C++primer plus第六版课后编程题答案8.8
- C++primer plus第六版课后编程题答案9.3
- C++primer plus第六版课后编程题答案10.1
- 【8.5】c++ primer plus 课后编程答案
- C++primer plus第六版课后编程题答案11.5
- 【12.1】c++ primer plus 课后编程答案
- C++primer plus第六版课后编程题答案12.2