第八章4-7题

    xiaoxiao2021-03-25  79

    第四题

    #include<iostream>#include<cstring>using namespace std;struct stringy{ char* str; int Len;};void set(stringy& st , const char* body);void showInfo(const stringy& st , int times = 1); //默认输出1次void showInfo(const char* ch , int times = 1);int main(){ stringy beany; char testing[] = "Reality isn't what it used ti be."; set(beany,testing); showInfo(beany); showInfo(beany,2); testing[0] = 'D'; testing[1] = 'u'; showInfo(testing); showInfo(testing,3); showInfo("Done!"); delete [] beany.str; //set()函数动态分配的内存 return 0;}void set(stringy& st , const char* body){ int lenght = strlen(body); st.Len = lenght; st.str = new char[lenght+1]; strcpy(st.str,body);}void showInfo(const stringy& st , int times){ cout<<"输出"<<times<<"次"<<endl; for(int i = 0 ; i < times ; ++i) { cout<<"结构字符:"<<st.str<<endl; cout<<"结构字符长度:"<<st.Len<<endl; }}void showInfo(const char* ch , int times){ cout<<"输出"<<times<<"次"<<endl; for(int i = 0 ; i < times ; ++i) cout<<ch<<endl;}

    第五题

    #include<iostream>const int Len = 5;template<typename T>T maxArray(const T* arr);int main(){ using namespace std; int Ints[Len] = {1,2,3,5,4}; double Doubles[Len] = {1.1,4.3,6.5,3.4,1.1}; cout<<"最大的int为"<<maxArray(Ints)<<endl; cout<<"最大的double为"<<maxArray(Doubles)<<endl; return 0;}template<typename T>T maxArray(const T* arr){ T maxEle = *arr; for(int i = 0 ; i < Len ; ++i) { if(maxEle < *(arr+i)) maxEle = *(arr+i); } return maxEle;}

    第六题

    #include<iostream>#include<cstring>using namespace std;template<typename T>const T maxn(const T* arr , int lenght);template<> const char* maxn(const char** names , int lenght); //char*具体化int main(){ int Ints[6] = {1,2,3,4,5,6}; double Doubles[4] = {1.1,2.2,3.4,5.0}; int maxInt = maxn(Ints,6); double maxDouble = maxn(Doubles,4); cout<<"最大的int值为"<<maxInt<<endl; cout<<"最大的double值为"<<maxDouble<<endl; const char* names[5] = {"a","b","ccc","e","f"}; const char* maxName = maxn(names,5); cout<<"最长的char值为"<<maxName<<endl; return 0;} template<typename T>T maxn(const T* arr , int lenght){ T max_arr = *arr; for(int i = 0 ; i < lenght ; ++i) { if(max_arr < *(arr+i)) max_arr = *(arr+i); } return max_arr;} template<> const char* maxn(const char** names , int lenght){ int maxLen = strlen(names[0]); int maxEle = 0; for(int i = 0 ; i < lenght ; ++i) { if(maxLen < strlen(*(names+i))) { int mid = strlen(*(names+i)); cout<<mid<<endl; maxLen = strlen(*(names+i)); maxEle = i; } } return *(names + maxEle);}

    第七题

    #include<iostream>template<typename T>T sumArray(const T arr[] , int lenght); //模板Atemplate<typename T>T sumArray(const T* arr[] , int lenght); //模板Bstruct debts{ char name[50]; double amount;};int main(){ using namespace std; 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 sumOfThings = sumArray(things,6); //调用模板A double sumOfpd = sumArray(pd,3); //调用模板B cout<<"things总和为"<<sumOfThings<<endl; cout<<"pd总和为"<<sumOfpd<<endl; return 0;}template<typename T>T sumArray(const T arr[] , int lenght){ T sum = arr[0]; for(int i = 1 ; i < lenght ; ++i) sum += arr[i]; return sum;}template<typename T>T sumArray(const T* arr[] , int lenght){ T sum = *(arr[0]); for(int i = 1 ; i < lenght ; ++i) sum += *(arr[i]); return sum;}

    转载请注明原文地址: https://ju.6miu.com/read-15140.html

    最新回复(0)