c++primer 第八章習題
阿新 • • 發佈:2018-12-17
#include <iostream> #include <string> #include <cctype> #include <cstring> using namespace std; //practice 1 void fun_of_p8_1(char *str, int print_times=0){ cout<<"string: "<<str<<", print times: "<<print_times<<endl; if (print_times>1){ print_times--; fun_of_p8_1(str, print_times); } } void p8_1(void){ char str[128]; int print_times=0; cout<<"Enter what string you want to print: "; cin.getline(str, 128); cout<<"Enter the time you want to print: "; cin>>print_times; cout<<"two paramters: "<<endl; fun_of_p8_1(str, print_times); cout<<"one parameters: "<<endl; fun_of_p8_1(str); } //practice 2 struct CandyBar{ char company[128]; double weight; int heat; }; void print_CandyBar(const CandyBar &candybar){ cout<<"candy bar company: "<<candybar.company<<endl; cout<<"candy bar weight: "<<candybar.weight<<endl; cout<<"candy bar heat: "<<candybar.heat<<endl; } void fill_CandyBar(struct CandyBar &candybar, char *company, double weight, int heat){ strcpy(candybar.company, company); candybar.weight=weight; candybar.heat=heat; } void p8_2(void){ struct CandyBar candybar={""}; char company[128]; double weight=0.0; int heat=0; cout<<"enter the company of candybar: "; cin.getline(company, 128); cout<<"enter the weight of candybar: "; cin>>weight; cout<<"enter the heat of candybar: "; cin>>heat; fill_CandyBar(candybar, company, weight, heat); print_CandyBar(candybar); return ; } //practice 3 void p8_3(void){ string input; cout<<"enter a string (q to quit): "; cin>>input; while (input[0]!='q') { for(size_t i=0;i<input.length();i++){ input[i]=toupper(input[i]); } cout<<input<<endl; cout<<"next string (q to quit): "; cin>>input; } } //practice 4 struct stringy{ char *str; int ct; }; void set(struct stringy &in_stringy, char *in_string){ int string_length=strlen(in_string); in_stringy.str=new char(string_length+1); strcpy(in_stringy.str, in_string); in_stringy.ct=string_length; } void show(const struct stringy &in_string, int print_time=1){ for (int i=0; i<print_time; i++) { cout<<"member string of struct stringy: "<<in_string.str<<endl; } } void show(const char *str, int print_time=1){ for(int i=0;i<print_time;i++){ cout<<"print char string: "<<str<<endl; } } int p8_4(void){ stringy beany; char testing[]="reality is not 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; } //main int main(int argc, char**argv){ p8_4(); while (cin.get()); } //practice 5 template<typename T> T max5(T in_array[]) { T max = in_array[0]; for (size_t i=0; i<5; i++) { if(max<in_array[i]){ max=in_array[i]; } } return max; } void p8_5(void){ int int_array[5]={1,2,3,4,5}; double double_array[5]={1.1,2.2,3.3,4.4,5.5}; int int_array_max=max5(int_array); double double_array_max=max5(double_array); cout<<int_array_max<<endl; cout<<double_array_max<<endl; } //practice 6 template<typename T> T maxn(T in_array[], int array_size){ T max=in_array[0]; for (int i=0; i<array_size; i++) { if (max<in_array[i]) { max=in_array[i]; } } return max; } template<>const char * maxn(const char *int_str[], int n){ const char * str=int_str[0]; for (int i=0;i<n; i++) { if (strlen(str)<strlen(int_str[i])) { str=int_str[i]; } } return str; } void p8_6(void){ int int_array[6] = { 43, 235, 54, 232, 123, 65 }; double double_array[4] = { 32.1, 453.2, 53.3, 67.4 }; const char * str_array[5] = { "Hello Jimmy!", "Hello World!", "ABCDEFG,HIJKLMN", "Today is a goood day!", "C++ Primer Plus!" }; int int_array_max=maxn(int_array, 6); double double_array_max=maxn(double_array, 4); const char * length_max_str=maxn(str_array, 5); cout<<int_array_max<<endl; cout<<double_array_max<<endl; cout<<length_max_str<<endl; } //practice 7 template <typename T> T SumArray(T in_arr[], int n); template<typename T> T SumArray(T *arr[], int n); struct debts{ char name[20]; double amount; }; int p8_7(void) { int thing[6] = { 13, 31, 103, 301, 310, 130 }; int int_sum = 0; struct debts mr_E[3] = { {"Ima Wolfe", 2400.0}, {"Ura Foxe", 1300.0}, {"Iby Stout", 1800.0} }; double *pd[3]; double double_sum = 0.0; for (size_t i = 0; i < 3; i++) { pd[i] = &mr_E[i].amount; } int_sum = SumArray(thing, 6); double_sum = SumArray(pd, 3); cout << "Sum of int array: " << int_sum << endl; cout << "Sum of double* array: " << double_sum << endl; return 0; }