第八章編程練習答案
8.1編寫一個輸出字符串的函數,有一個默認參數表示輸出次數,默認為1.(原題太扯啦,題意基礎上小改動)
//8.1編寫一個輸出字符串的函數,有一個默認參數表示輸出次數,默認為1.(原題太扯啦,題意基礎上小改動)
#include
using namespace std;
void show (const char* str, int time=1)
{
unsigned n=(time>0)?time:-time;
for (unsigned i = 0; i < n; ++i)
cout << str << endl;
cout << "-----------------" << endl;
}
int main ()
{
const char* str = "hello word!";
show(str);
show(str,0);
show(str,1);
show(str,-1);
}
8.2CandyBar結構體有品牌,重量和熱量3個成員,編寫一個含有默認值的函數為成員賦值和一個顯示內容函數
//8.2CandyBar結構體有品牌,重量和熱量3個成員,編寫一個含有默認值的函數為成員賦值和一個顯示內容函數
//CandyBar& candbar, const char* strBrand = "Millennium Munch", double weight = 2.85, int calories = 350
#include
using namespace std;
struct CandyBar
{
string Brand;
double weight;
int calories;
};
void input (CandyBar& candbar, const char* Brand = "Millennium Munch", double weight = 2.85, int calories = 350)
{
candbar.Brand = Brand;
candbar.weight = weight;
candbar.calories = calories;
}
void show (const CandyBar& candbar)
{
cout << candbar.Brand << '\t' << candbar.weight << '\t' << candbar.calories << endl;
}
int main ()
{
CandyBar candbar1, candbar2;
input(candbar1);
show(candbar1);
input(candbar2, "world", 2,3 );
show(candbar2);
}
8.3編寫一個函數,接受string引用,並把string內容變大寫,之後利用循環測試
//8.3編寫一個函數,接受string引用,並把string內容變大寫,之後利用循環測試
#include
#include
using namespace std;
string& upper (string& str)
{
for (char& e : str)
e = (char)toupper(e);
return (str);
}
int main ()
{
while (true) {
cout << "Enter a string (q to quit): ";
string str;
getline(cin, str);
if (!cin || "q" == str || "Q" == str)
break;
cout << upper(str) << endl;
}
}
8.4按以下程序框架,完成函數
//8.4按以下程序框架,完成函數
#include
#include
using namespace std;
struct stringy {
char * str;
int ct;
};
void set (stringy& stry, const char* szTxt)
{
stry.ct = (int)strlen(szTxt);
stry.str = new char [stry.ct + 1];
strcpy(stry.str, szTxt);
}
void show (const char* szTxt, unsigned times = 1)
{
for (unsigned i = 0; i < times; ++i)
cout << szTxt << endl;
}
void show (const stringy& stry, unsigned times = 1)
{
for (unsigned i = 0; i < times; ++i)
cout << stry.str << endl;
}
void destroy (stringy& stry)
{
delete [] stry.str;
stry.str = NULL;
}
int main()
{
stringy beany;
char testing[] = "Reality isn't what it used to be.";
set(beany, testing);
show(beany);
show(beany, 2);
destroy(beany);
testing[0] = 'D';
testing[1] = 'u';
show(testing);
show(testing, 3);
show("Done!");
}
8.5利用一個模板函數max5(),返回給予的5個數的最大值(應用c++11的array)
//8.5利用一個模板函數max5(),返回給予的5個數的最大值(應用c++11的array)
#include
#include
using namespace std;
template
const T& max5 (const array& arr)
{
unsigned max = 0;
for (unsigned i = 0; i < 5; ++i)
if (arr[i] > arr[max])
max = i;
return (arr[max]);
}
int main ()
{
array iarray = {32, 20, 99, 256, 9};
for (const auto& e : iarray)
cout << e << ' ';
cout << " max: " << max5(iarray) << endl;
array darray = {1.2,2.3,4.5,32.3,4.3};
for (const auto& e : darray)
cout << e << ' ';
cout << "max: " << max5(darray) << endl;
}
8.6編寫模板函數maxn(),參數為一個數組和元素數目,返回數組最大值,然後具體化一個字符串為元素的字符串,返回最長字符串
//8.6編寫模板函數maxn(),參數為一個數組和元素數目,返回數組最大值,
//然後具體化一個字符串為元素的字符串,返回最長字符串
#include
#include
using namespace std;
template
const T& maxn (const T arr[], unsigned n)
{
unsigned max = 0;
for (unsigned i = 0; i < n; ++i)
if (arr[i] > arr[max])
max = i;
return (arr[max]);
}
char* maxn ( char* arr[], unsigned n)
{
unsigned max = 0;
for (unsigned i = 0; i < n; ++i)
if (strlen(arr[i]) > strlen(arr[max]))
max = i;
return (arr[max]);
}
int main ()
{
int iArray[] = {32, -1, 99, 0, 256, 9};
for (const auto& e : iArray)
cout << e << ' ';
cout << " ----max: " << maxn(iArray, sizeof(iArray)/sizeof(iArray[0])) << endl;
double dArray[] = {-3.2, 221.22, 9.9, 0, 1};
for (const auto& e : dArray)
cout << e << ' ';
cout << " ----max: " << maxn(dArray, sizeof(dArray)/sizeof(dArray[0])) << endl;
const char* szArray[] = {"aaaa","bbbbbb","cc","fffffffffff","kkkk"};
for (const auto& e : szArray)
cout << '\"' << e << '\"' << ' ';
cout << " ----max: " << '\"' << maxn(szArray, sizeof(szArray)/sizeof(szArray[0])) << '\"' << endl;
}
8.7對程序清單8.14,使用兩個SumArray()模板返回數組元素總和。程序返回thing以及debt的總和
//8.7對程序清單8.14,使用兩個SumArray()模板返回數組元素總和。程序返回thing以及debt的總和
#include
using namespace std;
struct debts
{
char name[50];
double amount;
};
template
double SumArray(T arr[], int n)
{
cout << "template A\n";
double sum = 0;
for (int i = 0; i < n; i++)
sum += arr[i];
return (sum);
}
template
double SumArray(T * arr[], int n)
{
cout << "template B\n";
double sum = 0;
for (int i = 0; i < n; i++)
sum += *arr[i];
return (sum);
}
int main()
{
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 << "the total number of Mr. E's things: " << SumArray(things, 6) << endl;
cout << "the sum of Mr. E's all debts: " << SumArray(pd, 3) << endl;
}