c++.primer.plus第五版第八章编程练习答案
2015-07-16 18:19
411 查看
// 1 #include <iostream> using namespace std; void print(const char *str,int n = 1); int main() { const char *str = "My English is very good + I can speak English Well!\n"; cout << "=====================================================================\n"; print(str); cout << "=====================================================================\n"; print(str,10); cout << "=====================================================================\n"; return 0; } void print(const char * str,int n) { if(n <= 0) n = 1; for(int i = 0;i < n;i++) cout << str ; } // 2 #include <iostream> #include <cstring> using namespace std; struct CandyBar { char name[50]; double weight; int hot; }; void fill_candybar(CandyBar &t,char * str,double w,int h); void show_candybar(const CandyBar &t); int main() { CandyBar t; char str[50] = "Millennium Munch"; double w = 2.85; int h = 350; fill_candybar(t,str,w,h); show_candybar(t); return 0; } void fill_candybar(CandyBar &t,char * str,double w,int h) { strcpy(t.name,str); t.weight = w; t.hot = h; } void show_candybar(const CandyBar &t) { cout << "CandyBar.name = \t" << t.name << endl; cout << "CandyBar.weight = \t" << t.weight << endl; cout << "CandyBar.hot = \t\t" << t.hot << endl; } // 3 // 注意对string对象的判断 #include <iostream> using namespace std; void string_to_toupper(string & str); void string_show(const string & str); int main() { string str; while(1) { cout << "Enter a string (q to quit): "; getline(cin,str); if(str == "q") break; string_to_toupper(str); string_show(str); } return 0; } void string_to_toupper(string & str) { for(int i = 0;str[i] != '\0';i++) str[i] = toupper(str[i]); } void string_show(const string & str) { cout << str << endl; } // 4 #include <iostream> #include <cstring> // for strlen(),strcpy() using namespace std; struct stringy{ char * str; // points to a string int ct; // length of string(not counting '\0') }; // prototypes for set(),show(),and show() go here void set(stringy &beany,char arr[]); void show(const stringy sty,int n = 1); void show(const char str[],int n = 1); int main() { stringy beany; char testing[] = "Reality isn't what it used to be. "; set(beany,testing); // first argument is a reference. // allocates space to hold copy of testing, // sets str member of beany to point to the // new block,copies testing to new block, // and set ct member of beany show(beany); // prints member string once show(beany,2); // prints member string twice testing[0] = 'D'; testing[1] = 'u'; show(testing); // prints testing string once show(testing,3); // prints testing string thrice show("Done!" ); return 0; } void set(stringy &beany,char arr[]) { int len = strlen(arr) + 1; beany.str = new char[len]; strcpy(beany.str,arr); beany.str[len] = '\0'; beany.ct = len - 1; } void show(const stringy sty,int n) { if(n <= 0) n = 1; for(int i = 0;i < n;i++) { cout << "stringy.str = \t" << sty.str << endl; cout << "stringy.ct = \t" << sty.ct << endl; } } void show(const char str[],int n) { if(n <= 0) n = 1; for(int i = 0;i < n;i++) cout << str << endl; } // 5 #include <iostream> using namespace std; const int len = 5; template <class T> T max5(const T ar[]); int main() { int ari[] = {1,5,2,4,3}; double ard[] = {1.0,5.0,2.0,4.0,3.0}; cout << "ari-max = \t" << max5(ari) << endl; cout << "ard-max = \t" << max5(ard) << endl; return 0; } template <class T> T max5(const T ar[]) { T temp = ar[0]; for(int i = 1;i < len;i++) if(temp < ar[i]) temp = ar[i]; return temp; } // 6 待验证 #include <iostream> #include <cstring> using namespace std; template <class T> T maxn(const T ar[],int n); const char * maxchlen(const char *str[],int n); const char ** maxchlenaddr(const char *str[],int n); int main() { int ari[] = {1,5,2,4,3}; double ard[] = {1.0,2.0,4.0,3.0}; const char *arst[5] = { "aaaaaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbbbbbbb", "ccccccccccccccccccccccccccc", "ddddddddddddddddddddddddddd", "eeeeeeeeeeeeeeeeeeeeeeeeeee" }; cout << "ari-max = \t" << maxn(ari,5) << endl; cout << "ard-max = \t" << maxn(ard,4) << endl; cout << "the theory address is\t" << &arst[2] << endl; cout << "the real address is\t" << maxchlenaddr(arst,5) << endl; cout << "the real address is\t" << maxchlen(arst,5) << endl; return 0; } template <class T> T maxn(const T ar[],int n) { T temp = ar[0]; for(int i = 1;i < n;i++) if(temp < ar[i]) temp = ar[i]; return temp; } const char * maxchlen(const char *str[],int n) { unsigned temp = strlen(str[0]); const char *add = str[0]; for(int i = 1;i < n;i++) if(temp < strlen(str[i])) { temp = strlen(str[i]); add = str[i]; } return add; } const char ** maxchlenaddr(const char *str[],int n) { unsigned temp = strlen(str[0]); const char **add = &str[0]; for(int i = 1;i < n;i++) if(temp < strlen(str[i])) { temp = strlen(str[i]); add = &str[i]; } return add; } // 8.14 temptempover.cpp // temptempover.cpp -- template overloading #include <iostream> template <typename T> void ShowArray(T arr[],int n); // template A template <typename T> // template B void ShowArray(T *arr[],int n); struct debts { char name[50]; double amount; }; int main(void) { using namespace std; 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]; // set pointers to the amount members of the structures in the arr mr_E for(int i = 0;i < 3;i++) pd[i] = &mr_E[i].amount; cout << "Listing Mr.E's counts of things:\n"; // things is an array of int ShowArray(things,6); // uses template A cout << "Listing Mr. E's debts: \n"; // pd is an array of pointers to double ShowArray(pd,3); // uses template B(more specialized) return 0; } template <typename T> void ShowArray(T arr[],int n) { using namespace std; cout << "template A\n"; for(int i = 0;i < n;i++) cout << arr[i] << ' '; cout << endl; } template <typename T> void ShowArray(T * arr[],int n) { using namespace std; cout << "template B\n"; for(int i = 0;i < n;i++) cout << *arr[i] << ' '; cout << endl; } // 7 #include <iostream> template <typename T> long ShowArray(T arr[],int n); // template A template <typename T> // template B double ShowArray(T *arr[],int n); struct debts { char name[50]; double amount; }; int main(void) { using namespace std; int things[6] = {13,31,103,301,310,130}; long things_sum = 0; struct debts mr_E[3] = { {"Ima Wolfe",2400.0}, {"Ura Foxe ",1300.0}, {"Iby Stout",1800.0} }; double * pd[3]; double pd_sum = 0.0; // set pointers to the amount members of the structures in the arr mr_E for(int i = 0;i < 3;i++) pd[i] = &mr_E[i].amount; // things is an array of int things_sum = ShowArray(things,6); // uses template A cout << "thing_sum = " << things_sum << endl; // pd is an array of pointers to double pd_sum = ShowArray(pd,3); // uses template B(more specialized) cout << "pd_sum = " << pd_sum << endl; return 0; } template <typename T> long ShowArray(T arr[],int n) { long arr_sum = 0; using namespace std; cout << "template A\n"; for(int i = 0;i < n;i++) arr_sum = arr_sum + arr[i]; return arr_sum; } template <typename T> double ShowArray(T * arr[],int n) { using namespace std; double arr_sum; cout << "template B\n"; for(int i = 0;i < n;i++) arr_sum = arr_sum + *arr[i]; return arr_sum; }
相关文章推荐
- c++.primer.plus第五版第七章编程练习答案
- C++读取配置文件内中的数组
- C语言获取文件大小
- C++中继承方式对访控属性的影响
- C语言实现修改文本文件中特定行的实现代码
- C语言实现文件复制或者拼接
- C++ STL
- 【C++】基类析构函数为什么要为虚函数
- c语言链表《学习记录》
- 学习C++和编程的50个观点
- 数的排列的递归实现-C++ STL实现
- C++ Primer学习笔记与思考_1----变量和基本类型
- iOS C语言4_一维数组
- 有继承的C++析构函数一定要用virtual
- ★C++课程实训:银行储蓄系统(多文件组织)
- C++ static 全局变量使用注意
- 字典树的应用 单词意义查找-C语言实现
- C语言实现串,和简单的主函数测试
- C++ 优先队列应用方法 浅析
- KMP算法-C语言程序实现