C++ primer plus(第六版)第八章课后编程练习
来源:互联网 发布:给mac安装win7 编辑:程序博客网 时间:2024/06/09 20:59
第一题
#include <iostream>#include <string>using namespace std;void show_str(string &pf,int n = 0);int times = 0;int main(){string str = "I love you!";show_str(str);show_str(str,20);show_str(str,10);show_str(str,5);cin.get();cin.get();return 0;}void show_str(string &pf,int n){if(n == 0)cout << &pf << endl;else{times++;cout << "该函数被调用了" << times << "次" << endl;}}第二题
#include <iostream>#include <string>using namespace std;struct CandyBar{char barnd[80];double weight;int heat;};void set_CandyBar(CandyBar &pf,char *pt = "Millennium",double w = 2.85,int h = 350);void show_CandyBar(const CandyBar &pf);int main(){CandyBar candy_bar;set_CandyBar(candy_bar);show_CandyBar(candy_bar);char brand[80] = "ShiLiJia" ;set_CandyBar(candy_bar,brand,3.89,800);show_CandyBar(candy_bar);cin.get();cin.get();return 0;}void set_CandyBar(CandyBar &pf,char *pt,double w,int h){//在这里一开始我用的是这条语句:pf.barnd = pt,这样做的话最后会出现一个表达式必须是//可修改的左值,因此要改成strcpy(pf.brand,pt),这样才是正确的。strcpy(pf.barnd,pt);pf.weight = w;pf.heat = h;}void show_CandyBar(const CandyBar &pf){cout << "Brand: "<< pf.barnd;cout << ", Weight: " << pf.weight;cout << ", Heat; " << pf.heat << endl;}第三题
#include <iostream>#include <string>using namespace std;void str2upper(string &str);int main(){string str;cout << "Enter a string (q to quit):";getline(cin,str);while(str != "q"){str2upper(str);cout << str << endl;cout << "Next string (q to quit)";getline(cin,str);}cout << "Bye." << endl;cin.get();cin.get();return 0;}void str2upper(string &str){//一开始总是想不明白,这里传进来的str引用要怎么使用toupper函数//最后看了其他人的博客才明白原来传进来的str引用可以相当于一个数组//去使用。学到东西了,//参考博客地址为:http://blog.csdn.net/lmerissa/article/details/50509257for(int i = 0;i<str.size();i++)str[i] = toupper(str[i]);}
第四题
#include <iostream>#include <cstring>using namespace std;struct stringy{char *str;int ct;};void set(stringy &pf, char *str);//在这里不能使用const char* str声明第二个参数,如果这样做的话就会导致后面无法直接对pf.str进行赋值void show(const stringy &pf,const int n = 1);void show(const char*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!");cin.get();cin.get();return 0;}void set(stringy &pf, char *str){//strcpy(pf.str,str);pf.str = str;pf.ct = sizeof(str);}void show(const stringy &pf,const int n){for(int i = 0; i<n; ++i)cout << "show(beany)" << pf.str << endl;}void show(const char *str,const int n){for(int i = 0; i<n; ++i)cout << str << endl;}
第五题
#include <iostream>template <typename T>T max_element(T a[]);int main(){using namespace std;int a[5] = {1,2,3,4,5};double b[5] = {1.33,5.26,4.36,6.32,9.22};cout << "整型数组中最大的元素为" << max_element(a) << endl;cout << "double 型数组中最大的元素为:" << max_element(b) << endl;cin.get();cin.get();return 0;}template<typename T>T max_element(T a[]){T temp;temp = a[0];for(int i = 1;i<5;i++){if(temp<a[i])temp = a[i];}return temp;}
第六题
#include <iostream>template<typename T>T maxn(T *arr, const int n);template <> char *maxn<char *>(char *p[],const int n);int main() { using namespace std; int arr_i[6] = { 1, 2, 3, 4, 5, 6 }; double arr_d[4] = { 8.6, 15.6, 10.63, 20.3 }; char *p[5] = { "January", "March", "May", "September", "October" }; 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 sTtring is: " << maxn(p, 5) << endl; cin.get();cin.get(); 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) if(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) if(strlen(pt)<strlen(p[i])){pt = p[i];}return pt; }
阅读全文
0 0
- C++ primer plus(第六版)第八章课后编程练习
- C.Primer.Plus(第六版)第八章 编程练习
- C ++ Primer Plus 第六版 第八章编程练习答案
- C++primer plus第六版课后编程练习答案2.2
- C++primer plus第六版课后编程练习答案2.3
- C++primer plus第六版课后编程练习答案2.4
- C++primer plus第六版课后编程练习答案2.5
- C++primer plus第六版课后编程练习答案2.6
- C++primer plus第六版课后编程练习答案2.7
- C++primer plus第六版课后编程练习答案3.1
- C++primer plus第六版课后编程练习答案3.2
- C++primer plus第六版课后编程练习答案3.3
- C++primer plus第六版课后编程练习答案3.4
- C++primer plus第六版课后编程练习答案3.5
- C++primer plus第六版课后编程练习答案3.7
- C++primer plus第六版课后编程练习答案4.1
- C++primer plus第六版课后编程练习答案4.2
- C++primer plus第六版课后编程练习答案4.3
- GMIC 2017大会亮点提前看 明星科学家都在这里
- nginx反向代理配置
- 都在聊视频鉴黄,音频审核背后的技术你了解么 | 硬创公开课预告
- MySQL索引背后的数据结构及算法原理
- 剑指offer---和为S的连续正数序列
- C++ primer plus(第六版)第八章课后编程练习
- 一阶逻辑转换到Kripke结构
- APDU
- Simplex Noise (三)
- MVP新手尝试以及自动生成 View、Presenter
- [C/C++]遍历目录下指定(任意)文件
- Android智能POS应用开发学习清单
- 1、EditText 2、include、merge、ViewStub 3、DrawerLayout
- 1、全沉浸式自定义颜色