C++Primerplus编程练习 第八章
第八章 函数探幽
第一题
#include <iostream>
void print(const char* c, int n=0){static int count=0;count++;if(n!=0){for(int i=0;i<count;i++)std::cout << c <<std::endl;}else{std::cout << c <<std::endl;}
}
int main()
{using std::cout;char str[]{"bueryi"}; cout << "调用三次\n";print(str);print(str);print(str);cout << "使用第二个参数\n";print(str, 1);cout << "-----------------------------\n";print(str, -1);return 0;
}
第二题
#include <iostream>
#include <cstring>
const int BRANDSIZE =50;
struct CandyBar{char brand[BRANDSIZE];double weight;int calorie;
};
void changeCandy(CandyBar& candy, const char*str="Millennium",double w = 2.85,int h=350){strcpy(candy.brand, str);candy.weight = w;candy.calorie = h;
}
void displayCandy(const CandyBar& candy){using std::cout;cout << "brand is " << candy.brand << " weight is " << candy.weight<< " calorie is " << candy.calorie << std::endl;
}
int main()
{CandyBar candy;displayCandy(candy);changeCandy(candy);displayCandy(candy); return 0;
}
第三题
#include <iostream>#include <cctype>#include <cstring>void strToUpper(std::string & str){
? for(int i=0;i<str.size();i++){str[i] = toupper(str[i]);}}int main(){using namespace std;string str;cout << "Enter a string (q to quit): ";getline(cin,str);while(str != "q"){strToUpper(str); cout << str << endl;cout << "Next string (q to quit): ";getline(cin,str);}cout << "Bye.\n";return 0;}
第四题
#include <iostream>
using namespace std;
#include <cstring>
struct stringy{char * str;int ct;
};
void set(stringy& s, const char* c);
void show(const stringy& s, int n=1);
void show(const char* c,int n=1);
int main()
{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!");return 0;
}
void set(stringy& s, const char* c){s.ct = strlen(c);s.str = new char[s.ct+1];strcpy(s.str, c);
}
void show(const stringy& s, int n){for(int i=0;i<n;i++){cout << s.str<< endl;}
}
void show(const char* c, int n){for(int i=0;i<n;i++){cout << c << endl;}
}
第五题
#include <iostream>template <typename T>
T max5(T a[]){T t = a[0];for(int i=1;i<5;i++){if(t<a[i]){t = a[i];}}return t;
}
int main()
{int a[5]{1,214,15,5647,8};double b[5]{-1.54,4.26,3.23,23.7,21};using std::cout;cout << max5(a) << std::endl;cout << max5(b) << std::endl;return 0;
}
第六题
#include <iostream>#include <cstring>template <typename T>T maxn(T a[], int n){T t = a[0];for(int i=1;i<n;i++){if(t<a[i]){t = a[i];}}return t;}template <> const char* maxn(const char* a[],int n){const char* p = a[0];int m = strlen(a[0]);for(int i=1;i<n;i++){
? if(m < strlen(a[i])){m = strlen(a[i]);p = a[i];}}return p;}int main(){int a[6]{1,214,15,56,47,8};double b[4]{-1.54,4.26,3.23,21};const char* str[5]{"hello", "bueryi","WXY","next","template" };using std::cout;cout << maxn(a,6) << std::endl;cout << maxn(b,4) << std::endl;cout << maxn(str,5) << std::endl;return 0;}
第七题
程序清单8.14
#include <iostream>
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;
};
int main()
{using namespace 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 Mr. E's counts of things: " << SumArray(things, 6) <<endl;cout << "sum of Mr. E's debts: " <<SumArray(pd,3)<<endl; return 0;
}
template <typename T>
T SumArray(T arr[], int n){T t=0;for(int i=0;i<n;i++){t+= arr[i]; }return t;
}
template <typename T>
T SumArray(T* arr[], int n){T t=0;for(int i=0;i<n;i++){t+= *arr[i];}return t;
}