C.Primer.Plus(第六版)第八章 编程练习
来源:互联网 发布:5位电话 知乎 编辑:程序博客网 时间:2024/04/30 02:40
#include<iostream>#include<string>using namespace std;int pro_8_1_number=0;void show_str(const char* name,int i=0);struct CandyBar{ char* name; float weight; int calories;};struct stringy{ char* str; int ct;};void set_candybar(CandyBar &candybar,char* ps_char="Millennum Munch",double weight=2.85,int calories=350);void show_candybar(const CandyBar &candybar);void show_pro_8_3(string &jerr_str);void show_8_4(const stringy str1,int x=0);void show_8_4(const char str2[],int x=0);char* set_8_4(stringy &str3 ,char test[]);template <typename T>//8.5T Max(T arr[]){ int key_max=arr[0]; for(int i=1;i<5;i++) { if(arr[i]>key_max) key_max=arr[i]; } return key_max;} template <typename T>//8.6T maxn(T array[],int num){ int key_max=array[0]; for(int i=0;i<num;i++) { if(array[i]>key_max) key_max=array[i]; } return key_max;}template<> char* maxn<char*>(char* arr[],int number)//具体化{ int max=0; for(int i=1;i<number;i++) { if(strlen(arr[i])>strlen(arr[0])) max=i; } return arr[max];}struct debts{ char name[10]; double amount;};template <typename T>T ShowArray_8_7(T *arr,int n){ int sum=0; for(int i=0;i<n;i++) sum+=arr[i]; return sum;}template <typename T>T ShowArray_8_7(T* *arr,int n)//在vc6.0中该定义存在二义性,函数参数可解释为(T*)*/(T)**,其中括号表示返回值类型,前者与上一个函数等价,后者调用含指针参数的函数,vc6.0不会进行最优选择而是报错提示。{ double sum=0; for(int i=0;i<n;i++) sum+=(*arr[i]); return sum;}int main(){/* //8.1 const char arr[20]="jerry peng"; show_str(arr); cout<<endl; show_str(arr,1);*//* //8.2 CandyBar jerr_box; set_candybar(jerr_box); show_candybar(jerr_box); set_candybar(jerr_box,"jerry_peng",22.2,2); show_candybar(jerr_box);*//* //8.3 string jerr_str; show_pro_8_3(jerr_str); */ /* //8.4 stringy beany; char testing[]="Reality isnt what is used to be."; char* ps=set_8_4(beany,testing);//在函数中使用动态分配数组,将指向的地址赋给另一个指针用于释放内存。 show_8_4(beany); show_8_4(beany,2); testing[0]='D'; testing[2]='U'; show_8_4(testing); show_8_4(testing,2); show_8_4("Dn=one!"); delete [] ps;*//* //8.5 int arr_8_5[5]={1,233,232,214,4}; double doulbe_8_5[5]={1231,32442,23532,21,35363}; cout<<Max(arr_8_5)<<endl; cout<<Max(doulbe_8_5)<<endl;*//* //8.6 int arr_8_6[6]={23,43,234,53,54,5}; double darr_8_6[4]={239,345,22,325}; cout<<maxn(arr_8_6,6)<<endl; cout<<maxn(darr_8_6,4)<<endl; char* str_8_6[5]={"sdfsfs sdfs","sdfgdxf dfjsdkd","i miss you","what happened?","are you kidding me?"}; cout<<maxn<char*>(str_8_6,5)<<endl; cout<<int(maxn<char*>(str_8_6,5))<<endl;*//* //8.7 int things[6]={1,2,34,5,6,77}; struct debts pro_str[3]= { {"xx",2400.0}, {"yy",1200.0}, {"zz",2999.0} }; double *pd[3]; for(int i=0;i<3;i++) pd[i]=&pro_str[i].amount; cout<<ShowArray_8_7(things,6)<<endl;// ShowArray_8_7(pd,1); 二义性*/ return 0;}/* //8.1void show_str(const char* name,int i){ pro_8_1_number++; if(i==0) cout<<name<<endl; else { for(;pro_8_1_number>0;pro_8_1_number--) cout<<name<<endl; }}*//*void set_candybar(CandyBar &candybar,char* ps_char,double weight,int calories){ candybar.calories=calories; candybar.name=ps_char; candybar.weight=weight;}void show_candybar(const CandyBar &candybar){ cout<<candybar.calories<<"\t"<<candybar.name<<"\t"<<candybar.weight<<endl;}*//*void show_pro_8_3(string &jerr_str){ cout<<"Please enter a string (q to quit):"; while(getline(cin,jerr_str)&&jerr_str!="q") { cin.get(); for(int i=jerr_str.size();i>0;i--) { jerr_str[i-1]=toupper(jerr_str[i-1]); } cout<<jerr_str; cout<<"\nPlease enter next string (q to quit):"; } cout<<"Bye!";}*//* //8.4void show_8_4(const stringy str1,int x){ for(int j=strlen(str1.str);x<j;x++) cout<<str1.str[x]; cout<<endl;}void show_8_4(const char str2[],int x){ for(int k=strlen(str2);x<k;x++) cout<<str2[x]; cout<<endl;}char* set_8_4(stringy &str3 ,char test[]){ str3.str=new char[strlen(test)+1]; strcpy(str3.str,test); return str3.str;}*/
阅读全文
0 0
- C.Primer.Plus(第六版)第八章 编程练习
- C ++ Primer Plus 第六版 第八章编程练习答案
- C++ Primer Plus第六版 第八章 编程练习答案
- 记录——《C Primer Plus (第五版)》第八章编程练习第六题
- 【学习C++】C++ Primer Plus (第六版)第八章编程练习1-7
- C.Primer.Plus(第六版)第六章编程练习
- C ++ Primer Plus 第六版 第六章编程练习答案
- C++primer plus第六章编程练习
- C primer plus(第五版)编程练习第八章
- c++.primer.plus第五版第八章编程练习答案
- C primer plus(第五版)编程练习第六章
- c++.primer.plus第五版第六章编程练习答案
- C.Primer.Plus(第六版)第五章编程练习
- C.Primer.Plus(第六版)第七章 编程练习
- C.Primer.Plus(第六版)第九章 编程练习
- C.Primer.Plus(第六版)第10章 编程练习
- C ++ Primer Plus 第六版 第七章编程练习答案
- C.Primer.Plus(第六版)第11章 编程练习
- 一个关于list的有趣的问题
- jQuery之动画效果(动画基础隐藏和显示,上卷下拉效果 ,淡入淡出效果 ,动画切换的比较,自定义动画,jQuery核心)
- 排序算法——二元选择排序
- 二叉查找树总结
- android手机裸眼3D技术原理和编程实现
- C.Primer.Plus(第六版)第八章 编程练习
- Android实现RadioGroup中的RadioButton再次点击取消选中
- 出现:Error creating bean with name 'xxxxxController': Injection of autowired dependencies failed;
- jQuery操作DOM对象
- 对照Startup.s学习ARM汇编指令
- 图片的懒加载,预加载,色彩预制加载
- Sequential Match Network: A New Architecture for Multi-turn Response Selection in Retrieval-based Ch
- 第十三周:( Sicily1402) Panic Room(c++)
- 欢迎使用CSDN-markdown编辑器