c++ Primer Plus(第六版)第八章习题,写代码之路

来源:互联网 发布:原油价格数据在哪里查 编辑:程序博客网 时间:2024/06/06 02:11

c++ Primer Plus(习题8.1)

/*题意看了好久才明白意思,即不依赖形参n,来输出字符串*//*个人见解,无*/#include<iostream>const int TIMES = 10;int count = 0;//用一个全局变量,记录函数调用次数void show(const char *p, int n = 0);int main(){using namespace std;const char str[] = "Output count by call me";show(str);for (int i = 0; i < TIMES; i++){cout << "Call time " << i + 1 << endl;show(str, i);cout << "\n";}return 0;}void show(const char *p, int n)           //默认变量只需在函数声明出指出{int calltimes;              //题目要求调用次数为输出次数,应建立全局变量或者静态变量if (n == 0)                 //说简单点就是不靠n值决定循环次数calltimes = 1;calltimes = ++count;         //前置递增,记录调用次数for (int i = 0; i < calltimes; i++)std::cout<<p<< "\n";}


c++ Primer Plus(习题8.2)

//结构的用法,再加上默认函数参数,还有引用结构#include<iostream>const int Len = 20;struct CandyBar {char name[Len];double weight;int cir;};void set(CandyBar &a, char *ptr = "Millennium Munch", double w = 2.85, int c = 350);void show(const CandyBar &b);int main(){using namespace std;CandyBar bar;CandyBar bar2;char temp[Len];double temw;int temc;cout << "Please enter candy bar name: ";cin.getline(temp, Len);cout << "And the weight: ";cin >> temw;cout << "Calorie: ";cin >> temc;set(bar,temp,temw,temc);set(bar2);show(bar);cout << "using default value:\n";//看看默认参数效果show(bar2);cout << "Bye!\n";return 0;}void set(CandyBar &a, char *ptr, double w, int c){std::strcpy(a.name, ptr);a.weight = w;a.cir = c;}void show(const CandyBar &b){using std::cout;using std::endl;cout << "Candy Bar name: " << b.name << endl<< "Weight: " << b.weight << endl<< "Calorie: " << b.cir << endl;}


c++ Primer Plus(习题8.3)

//使用string 接受字符,转换为大写#include<iostream>#include<string>using namespace std;string top(const string &s);int main(){string temp;cout << "Enter some words(q to exit): ";while (getline(cin,temp)&&temp!="q"){cout << top(temp)<<endl;cout << "Next string (q to exit): ";}cout << "Bye!\n";return  0;}string top(const string &s){string tem=s;//string对象的赋值,不初始化无法操作int a = s.size();for (int i = 0; i < a; i++){if (isalpha(s[i]))tem[i] = toupper(s[i]);elsetem[i] = s[i];}return tem;}


c++ Primer Plus(习题8.4)

//填空题,完成别人的框架#include<iostream>using namespace std;#include<cstring>struct Stringy{char *str;int ct;//用来保存字符的个数,不包括'\0'};void set(Stringy &s, const char a[]);void show(const Stringy &s, int times = 1);//重载void show(const char*a,int times=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!");delete []beany.str;return 0;}void set(Stringy &s, const char a[]){int size = strlen(a);s.str = new char[size+1];strcpy(s.str, a);s.ct = size;//这里不使用delete处理分配内存,因为还没利用完}void show(const Stringy &s, int times)//定义不用加默认参数了{for (int i = 0; i < times; i++)cout << "String: " << s.str << endl<< "Element count: " << s.ct << endl;}void show(const char*a, int times){for (int i = 0; i < times; i++)cout << a << endl;}


c++ Primer Plus(习题8.5)

//函数模板的使用,提供泛型编程的方便//编写模板找出5个元素中的最大值//明显降低了难度#include<iostream>const int Size = 5;template <typename T>//或者class TT Max5(const T arry[]);int main(){using namespace std;int a[5] = { 1,2,3,4,5 };double b[5] = { 1.0,2.0,3.0,44.580,5.0 };cout << "The int array max is : " << Max5(a) << endl;cout << "The double array max is : " << Max5(b) << endl;cout << "Bye!\n";return 0;}template <typename T>//或者class T,不能少的东东T Max5(const T arry[]){T max=arry[0];for (int i = 1; i < 5; i++){if (arry[i] > max)max = arry[i];}return max;}


c++ Primer Plus(习题8.6)

//函数模板的使用,上一题的升级版//返回最大值,接受字符指针,返回最大字符串的地址//很明显,要提供模板的具体化#include<iostream>#include<cstring>const int Size = 5;template <typename T>//或者class TT Max(T *arry,int n);//自作聪明在模板那里加了const导致出现很多错误template <> char *Max(char *arry[], int n);//具体化很重要int main(){using namespace std;int a[6] = { 1,2,3,4,5,6};double b[4] = {6.66,6.666,666.666,6666.666};char *c[5] = { "I am","a","C++","Student","."};cout << "The int array max is : " << Max(a,6) << endl;cout << "The double array max is : " << Max(b,4) << endl;cout << "The longest string is: " << Max(c,5) << endl;cout << "Bye!\n";return 0;}template <typename T>//或者class T,不能少的东东T Max(T arry[],int n){T max = arry[0];for (int i = 1; i < n; i++){if (arry[i] > max)max = arry[i];}return max;}template <> char *Max(char *arry[], int n){int len = 0;char *p = nullptr;for (int i = 0; i < n; i++){if (strlen(arry[i]) > len){len = strlen(arry[i]);p = arry[i];//记下最长字符串的地址}}return p;}


c++ Primer Plus(习题8.7)

//改写书上程序清单8.14,返回总和了,不再显示数据#include<iostream>template<class T>T SumArrnay(T arr[], int n );template<typename T>T SumArrnay(T *arr[], int n);struct debts{char name[50];double amount;};int main(){using namespace std;int things[6] = { 13,31,103,301,310,130 };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 << "Things total is: " << SumArrnay(things, 6) << endl;cout << "Debts total is: " << SumArrnay(pd, 3)<<endl;cout << "Bye!\n";}template<class T>T SumArrnay(T arr[], int n){T sum = 0;for (int i = 0; i < n; i++)sum += arr[i];return sum;}template<typename T>T SumArrnay(T *arr[], int n){T sum = 0;for (int i = 0; i < n; i++)sum += *arr[i];return sum;}


0 0
原创粉丝点击