#include<iostream>#include<string>#include<cctype>usingnamespace std;voidto_upper(string& str);intmain(){
string str;
cout <<"Enter a string (q to quit): ";getline(cin, str);while(str !="q"){to_upper(str);
cout << str << endl;
cout <<"Next string (q to quit): ";getline(cin, str);}
cout <<"Bye."<< endl;return0;}voidto_upper(string& str){for(int i =0; i < str.size();++i){if(isalpha(str[i]))
str[i]=toupper(str[i]);}}
4.
#include<iostream>usingnamespace std;#include<cstring>struct stringy {char* str;int ct;};voidset(stringy&,char*);voidshow(const stringy&,int n =1);voidshow(constchar*,int n =1);intmain(){
stringy beauy;char testing[]="Reality isn't what it used to be.";set(beauy, testing);show(beauy);show(beauy,2);
testing[0]='D';
testing[1]='u';show(testing);show(testing,3);show("Done!");return0;}voidset(stringy& beauy,char* str){int len =strlen(str);
beauy.str =newchar[len];strcpy_s(beauy.str,strlen(str)+1, str);
beauy.ct = len;}voidshow(const stringy& beauy,int n){
cout <<"共"<< n <<"次循环\n";while(n >0){
cout << beauy.str << endl;
n--;}}voidshow(constchar* str,int n){
cout <<"共"<< n <<"次循环\n";while(n >0){
cout << str << endl;
n--;}}
5.
#include<iostream>constint Len =5;template<typename T>
T max5(T arr[Len]){
T max = arr[0];for(int i =0; i < Len;++i){if(arr[i]> max)
max = arr[i];}return max;}intmain(){usingnamespace std;int arr_int[Len]={1,2,3,4,5};double arr_flt[Len]={1.1,2.2,3.3,4.4,5.5};
cout <<"Max int = "<<max5(arr_int)<< endl;
cout <<"Max double = "<<max5(arr_flt)<< endl;return0;}
6.
#include<iostream>#include<cstring>usingnamespace std;template<typename T>voidshow(T arr[],int);template<>void show<char*>(char*[],int);template<typename T> T maxn(T arr[],int);template<>char* maxn<char*>(char*[],int);intmain(){int arr_int[6]={1,2,3,4,5,6};double arr_flt[4]={10.0,11.0,12.0,13.0};constchar* arr_ch[4]={"wo","jiushiyao","wan","yingxionglianmeng"};show(arr_int,6);
cout <<"Max int: "<<maxn(arr_int,6)<< endl;show(arr_flt,4);
cout <<"Max double: "<<maxn(arr_flt,4)<< endl;show(arr_ch,4);
cout <<"Max string: "<<maxn(arr_ch,4)<< endl;return0;}template<typename T>
T maxn(T arr[],int n){
T max = arr[0];for(int i =0; i < n;++i){if(arr[i]> max)
max = arr[i];}return max;}template<>char* maxn<char*>(char* arr[],int n){int max_len =strlen(arr[0]);int tmp_len;int max_idx =0;int i;for(i =0; i < n;++i){
tmp_len =strlen(arr[i]);if(tmp_len > max_len){
max_len = tmp_len;
max_idx = i;}}return arr[max_idx];}template<typename T>voidshow(T arr[],int n){for(int i =0; i < n;++i)
cout << arr[i]<<" ";
cout << endl;}template<>void show<char*>(char* arr[],int n){for(int i =0; i < n;++i)
cout << arr[i]<< endl;}
7.
#include<iostream>struct debts {char name[50];double amount;};template<classT>
T SumArray(T arr[],int n);template<typename T>
T SumArray(T* pd[],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 <<"amount of things: "<<SumArray(things,6)<< endl;
cout <<"amount of debt: "<<SumArray(pd,3)<< endl;return0;}template<classT>
T SumArray(T arr[],int n){
T total =0;for(int i =0; i < n;++i)
total += arr[i];return total;}template<typename T>
T SumArray(T* pd[],int n){
T sum =(T)0;for(int i =0; i < n;++i)
sum +=*pd[i];return sum;}