第三题
#include<cctype>#include<iostream>#include<string>usingnamespace std;voidStr_To_Upper(string &str);intmain(){
cout <<"Enter a string (q to quit):";
string str;getline(cin, str);while(str[0]!='q'){Str_To_Upper(str);
cout << str << endl;
cout <<"Next string (q to quit):";getline(cin, str);}
cout <<"Bye!!!"<< endl;return0;}voidStr_To_Upper(string &str){int i =0;while(str[i]!='\0'){
str[i]=toupper(str[i]);++i;}}
第四题
#include<iostream>usingnamespace std;#include<cstring>struct stringy
{char*str;int ct;};voidset(stringy &strref,char*str);voidshow(const stringy &strref,int n =1);voidshow(constchar*str,int n =1);intmain(){
stringy beany;char testing[]="Reality isn't what it used to be.";// 分配空间保存testing、设置stringy结构的成员str指向new的空间// 并将testing拷贝到分配的空间,并且设置stringy的成员ct的值set(beany, testing);show(beany);show(beany,2);
testing[0]='D';
testing[1]='u';show(testing);show(testing,3);show("Done!");return0;}voidset(stringy &strref,char*str){
strref.str =newchar[strlen(str)+1];strcpy(strref.str, str);
strref.ct =strlen(str);}voidshow(const stringy &strref,int n){for(int i =0; i < n; i++){
cout << strref.str <<"----->"<< strref.ct << endl;}}voidshow(constchar*str,int n){for(int i =0; i < n; i++){
cout << str << endl;}}
第五题
#include<iostream>template<typename T>
T max5(T *arrT);intmain(){usingnamespace std;int arri[5]={1,45,676,34,444};double arrd[5]={23.4,44.5,66.4,34.5,12.4};
cout <<"Max of double array: "<<max5(arrd)<< endl;
cout <<"Max of int array: "<<max5(arri)<< endl;return0;}template<typename T>
T max5(T *arrT){
T max = arrT[0];for(int i =1; i <5; i++){if(max < arrT[i]){
max = arrT[i];}}return max;}
第6题
#include<cstring>#include<iostream>usingnamespace std;template<typename T>
T maxn(T *arrT,int n);template<>char*maxn<char*>(char**arrp,int n);intmain(){int arri[5]={1,45,676,34,444};double arrd[5]={23.4,44.5,66.4,34.5,12.4};char*arr[5]={"Hello World","Hi,myfriend","Dong zhao cheng","Chen li ang","Huang Yu xiong"};//数组,元素是5个char指针(char *),分别指向5个字符串
cout <<"Max of double array: "<<maxn(arrd,5)<< endl;
cout <<"Max of int array: "<<maxn(arri,5)<< endl;
cout <<">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n";for(int i =0; i <5; i++){//arr[i]是char*代表是字符串,实质是指向字符串首元素的地址,打印的话结果是字符串//假如想打印这个地址,则需要(void*)arr[i]//那 &arr[i]是什么?----arr[i]是字符串的地址,加了取地址符号后,就是存储这个地址的内存地址
cout <<"Address of "<< arr[i]<<" = "<<(void*)arr[i]<< endl;}//测试显式具体化
cout << hex <<"The Adress of max length string in the array is "<<(void*)(maxn(arr,5))<< endl;return0;}// 模板函数template<typename T>
T maxn(T *arrT,int n){
T max = arrT[0];for(int i =1; i < n; i++){if(max < arrT[i])//长度相等并不会改变地址,所以即使出现同一长度的字符串,也只会返回首次出现的那个{
max = arrT[i];}}return max;}template<>char*maxn<char*>(char*arrp[],int n){char*MaxAddress = arrp[0];for(int i =1; i < n; i++){if(strlen(MaxAddress)<strlen(arrp[i])){
MaxAddress = arrp[i];}}return MaxAddress;}
第7题
#include<iostream>struct debts
{char name[50];double amount;};template<typename T>intSumArray(T arr[],int n);template<typename T>doubleSumArray(T *arr[],int n);intmain(){usingnamespace 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];for(int i =0; i <3; i++){
pd[i]=&mr_E[i].amount;}
cout <<"Sum of thing is "<<SumArray(things,6)<< endl;
cout <<"Sum of debts is "<<SumArray(pd,3)<< endl;return0;}template<typename T>intSumArray(T arr[],int n){int sum =0;for(int i =0; i < n; i++){
sum += arr[i];}return sum;}template<typename T>doubleSumArray(T *arr[],int n){double sum =0;for(int i =0; i < n; i++){
sum = sum +*(arr[i]);}return sum;}