C++ Primer Plus第五版 第八章 编程练习答案

来源:互联网 发布:淘宝流量分为哪几种 编辑:程序博客网 时间:2024/04/30 00:00
/*******************************************************************************************************************  Author : Cui mingyang Blog : cx_12586 Time : 2017/11/1 From : C++ Primer Plus第五版第8章编程练习 第1题  *******************************************************************************************************************/ #include<iostream>const int ArSize = 80;using namespace std;void Printf(const char *ch,int n=1);static int count=0;int main(){cout << "Enter a string: \n";char ch[80];cin.get(ch,ArSize);cout << "#1 " <<endl;  Printf(ch,20);  cout << "#2 " <<endl;  Printf(ch,40);   cout << "#3 " <<endl;  Printf(ch,10); ;  cout << "#4 " <<endl;  Printf(ch); system("pause");return 0;}void Printf(const char *ch,int n){++count;if (n){for (int i=0;i<count;i++)cout << ch <<endl;}elsecout << ch <<endl;}


/*******************************************************************************************************************  Author : Cui mingyang Blog : cx_12586 Time : 2017/11/1 From : C++ Primer Plus第五版第8章编程练习 第2题  *******************************************************************************************************************/ #include<iostream>const int ArSize = 80;using namespace std;struct CandyBar{char brand[ArSize];double weight;int heat;};void fill_it(CandyBar &a,const char *ch="Millennium",double weight=2.85,int heat=350);void show (const CandyBar &a);int main(){CandyBar candy1;cout << "Enter the brand of the candy: \n";char name[ArSize];cin.get(name,ArSize);double weight;int heat;cout << "Enter the weight of the candy: \n";cin >> weight;cout << "Enter the brand of the candy: \n";cin >> heat;fill_it(candy1,name,weight,heat);show(candy1);system("pause");return 0;}void fill_it(CandyBar &a,const char *ch,double weight,int heat){int i;for (i=0;ch[i]!='\0'&&i<ArSize;i++)a.brand[i]=ch[i];a.brand[i]='\0';a.heat=heat;a.weight=weight;}void show (const CandyBar &a){cout << a.brand <<endl;cout << a.weight <<endl;cout << a.heat <<endl;}

/*******************************************************************************************************************  Author : Cui mingyang Blog : cx_12586 Time : 2017/11/1 From : C++ Primer Plus第五版第8章编程练习 第3题  *******************************************************************************************************************/#include<iostream>#include<string>using namespace std;void to_upper(string &str);int main(){cout << "Enter a string (q to quit): ";string str;while(getline(cin,str) && str[0]!='q'){to_upper(str);cout << "Next string (q to quit): ";}system("pause");return 0;}void to_upper(string &str){int len=str.size();for(int i=0;i<len;i++)str[i]=toupper(str[i]);cout << str <<endl;}

/*******************************************************************************************************************  Author : Cui mingyang Blog : cx_12586 Time : 2017/11/1 From : C++ Primer Plus第五版第8章编程练习 第4题  *******************************************************************************************************************/#include <iostream>#include <cstring>    // for strlen(), strcpy()using namespace std;struct stringy {char * str;        // points to a stringint ct;        // length of string (not counting '\0')};void show(const char *str, int cnt =  1);void show(const stringy & bny, int cnt = 1);void set(stringy & bny, const char * str);int main(void){stringy beany;char testing[] = "Reality isn't what it used to be.";set(beany, testing);show(beany);        // prints member string onceshow(beany, 2);    // prints member string twicetesting[0] = 'D';testing[1] = 'u';show(testing);    // prints testing string onceshow(testing, 3);    // prints testing string thriceshow("Done!");system("pause");return 0;}void show(const char *str, int cnt){while(cnt-- > 0){cout << str << endl;}}void show(const stringy & bny, int cnt){while(cnt-- > 0){cout << bny.str << endl;}}void set(stringy & bny, const char * str){bny.ct = strlen(str);bny.str = new char[bny.ct+1];strcpy(bny.str, str);}

/*******************************************************************************************************************  Author : Cui mingyang Blog : cx_12586 Time : 2017/11/1 From : C++ Primer Plus第五版第8章编程练习 第5题  *******************************************************************************************************************/#include<iostream>template <typename Any>Any max5(Any *array);template <typename Any>void show (Any *array);using namespace std;int main(){double arr1[5]={4.5,6.8,11.2,55.9,35.2};int    arr2[5]={4,6,11,55,35};cout << "The max of " ;show(arr1);cout << " is " <<max5(arr1) <<endl;cout << "The max of ";show(arr2);cout << " is " <<max5(arr2) <<endl;system("pause");return 0;}template <typename Any>Any max5(Any *array){for (int i=1;i<5;i++)array[0]=array[i]>array[0]?array[i]:array[0];return array[0];}template <typename Any>void show (Any *array){for (int i=0;i<5;i++)cout << array[i] << ", ";}

/*******************************************************************************************************************  Author : Cui mingyang Blog : cx_12586 Time : 2017/11/1 From : C++ Primer Plus第五版第8章编程练习 第6题  *******************************************************************************************************************/#include<iostream>//重载模板函数template <typename Any>Any maxn(Any *array,int n);//具体化template <>char *maxn(char **d,int n);using namespace std;int main(){int    arr1[6]={4,6,11,55,35,52};double arr2[4]={4.5,6.8,11.2,55.9};cout << "The max of is " <<maxn(arr1,6) <<endl;cout << "The max of is " <<maxn(arr2,4) <<endl;char *g3[3]={"abc","abcd","abcde"};char *f=maxn(g3,3);cout << &(f) << endl;system("pause");return 0;}template <typename Any>Any maxn(Any *array, int n){for (int i=1;i<n;i++)array[0]=array[i]>array[0]?array[i]:array[0];return array[0];}template <>char *maxn(char **d,int n){for (int i=1;i<n;i++)d[0]=strlen(d[i])>strlen(d[0])?d[i]:d[0];return d[0];}

/*******************************************************************************************************************  Author : Cui mingyang Blog : cx_12586 Time : 2017/10/19 From : C++ Primer Plus第五版第8章编程练习 第7题  *******************************************************************************************************************/ // tempover.cpp --- template overloading#include <iostream>template <typename T>            // template Avoid ShowArray(T arr[], int n);template <typename T>            // template Bvoid 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_Efor (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 intShowArray(things, 6);  // uses template Acout << "Listing Mr. E's debts:\n";// pd is an array of pointers to doubleShowArray(pd, 3);      // uses template B (more specialized)system("pause");return 0;}template <typename T>void ShowArray(T arr[], int n){using namespace std;T temp=0;cout << "The sum of template A\n";for (int i = 0; i < n; i++)temp+=arr[i];cout << temp << endl;}template <typename T>void ShowArray(T * arr[], int n){using namespace std;T temp=0;cout << "The sum of template B\n";for (int i = 0; i < n; i++)temp+=*arr[i];cout << temp << endl; }


阅读全文
0 0
原创粉丝点击