#include<iostream>#include<string>#include<cctype>usingnamespace std;
string ToUpper(string &st);//string为内置类型,无需返回引用intmain(){
string st;
cout <<"Enter a string (q to quit): ";getline(cin, st);while(st !="q"){
st =ToUpper(st);
cout << st << endl;
cout <<"Enter a string (q to quit): ";getline(cin, st);}
cout <<"Bye."<< endl;return0;}
string ToUpper(string &st){int len = st.length();for(int i =0; i < len; i++){if(isalpha(st[i]))//是字母才转大写{
st[i]=toupper(st[i]);}}return st;}
4. 第4题
#include<iostream>#include<cstring>usingnamespace std;struct stringy
{char* str;int ct;};voidset(stringy & sy,constchar* ch);voidshow(const stringy sy,int n =1);voidshow(constchar* ch,int n =1);intmain(){
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!");return0;}voidset(stringy & sy,constchar* ch){
sy.ct =strlen(ch)+1;//首先确定字符串长度
sy.str =newchar[sy.ct];//分配相应长度的内存空间strcpy_s(sy.str, sy.ct, ch);//将字符串拷贝}voidshow(const stringy sy,int n){for(int i =0; i < n; i++){
cout <<"#"<< i +1<<": "<< endl;
cout <<"str: "<< sy.str << endl;}
cout << endl;}voidshow(constchar* ch,int n){for(int i =0; i < n; i++){
cout << ch << endl;}
cout << endl;}
5. 第5题
#include<iostream>usingnamespace std;template<typename T>
T max5(T arr[5]);intmain(){int arr1[5]={1,2,3,4,5};double arr2[5]={10.0,21.0,55.0,9.5,5.5};int MAX1 =max5(arr1);int MAX2 =max5(arr2);
cout <<"int型数组中最大的元素为:"<< MAX1 << endl;
cout <<"double型数组中最大的元素为:"<< MAX2 << endl;return0;}template<typename T>
T max5(T arr[5]){
T temp = arr[0];for(int i =0; i <5; i++){if(temp < arr[i]){
temp = arr[i];}}return temp;}
6. 第6题
#include<iostream>#include<cstring>usingnamespace std;template<typename T>
T maxn(T * arr,int n);template<>char*maxn(char* arr[],int n);//显式具体化的声明intmain(){int arr1[6]={1,2,3,4,5,6};double arr2[4]={10.0,21.0,55.0,9.5};constchar* arr3[3]={"abc","defg","hl"};int MAX1 =maxn(arr1,6);int MAX2 =maxn(arr2,4);constchar* MAX3 =maxn(arr3,3);//MAX3为最长的字符串
cout <<"int型数组中最大的元素为:"<< MAX1 << endl;
cout <<"double型数组中最大的元素为:"<< MAX2 << endl;
cout <<"char指针数组中最长字符串的地址为:"<<&MAX3 << endl;//取地址运算符return0;}template<typename T>
T maxn(T * arr,int n){
T temp = arr[0];for(int i =0; i < n; i++){if(temp < arr[i]){
temp = arr[i];}}return temp;}//显式具体化的定义template<>char*maxn(char* arr[],int n){int len =strlen(arr[0]);char*p = arr[0];for(int i =1; i < n; i++){if(len <strlen(arr[i])){
len =strlen(arr[i]);
p = arr[i];//p为最长的字符串}}return p;}
7. 第7题
#include<iostream>usingnamespace std;template<typename T>
T SumArray(T arr[],int n);template<typename T>
T SumArray(T * arr[],int n);struct debts
{char name[50];double amount;};intmain(){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;}int sum1 =SumArray(things,6);int sum2 =SumArray(pd,3);
cout <<"things数组元素之和为:"<< sum1 << endl;
cout <<"pd指针数组所指的元素之和为:"<< sum2 << endl;return0;}template<typename T>
T SumArray(T arr[],int n){
T total =0;for(int i =0; i < n; i++){
total = total + arr[i];}return total;}template<typename T>
T SumArray(T * arr[],int n){
T total =0;for(int i =0; i < n; i++){
total = total +*arr[i];}return total;}