C++ Primer Plus(第6版)Chapter 8 编程题答案
第1题:
// task 1
#include <iostream>
#include <cstring>
#include <cctype>
#include <string>
using namespace std;
void print(const char*, int n = 0);
int main()
{
char* pstr = "Hello";
print(pstr);
int num;
cout << "Enter a num: ";
cin >> num;
print(pstr, num);
print(pstr, 4);
cin.get();
cin.get();
return 0;
}
void print(const char* ps, int n)
{
static int count; // count记录调用次数
count++;
if (n == 0)
cout << ps << endl;
else
{
int i = count;
while (i--)
cout << ps << endl;
}
}
第2题:
// task 2
struct CandyBar
{
char band[20];
double weight;
int calorie;
};
void set_candy(CandyBar& cb, const char* band = "Millennium Munch", double weight = 2.85, int calorie = 350);
void show_candy(const CandyBar&);
int main()
{
CandyBar cb1, cb2;
set_candy(cb1);
cout << "Default: " << endl;
show_candy(cb1);
set_candy(cb2, "Whatever", 4.4, 360);
cout << "Diy: " << endl;
show_candy(cb2);
cin.get();
cin.get();
return 0;
}
void set_candy(CandyBar& cb, const char* band, double weight, int calorie)
{
strcpy_s(cb.band, band);
cb.weight = weight;
cb.calorie = calorie;
}
void show_candy(const CandyBar& cb)
{
cout << "Band : " << cb.band << endl;
cout << "Weight: " << cb.weight << endl;
cout << "Calorie: " << cb.calorie << endl;
}
第3题:
// task 3
void UpperString(string&);
int main()
{
string temp;
cout << "Enter a string (q to quit):";
while (getline(cin, temp) && temp != "q" && temp != "Q")
{
UpperString(temp);
cout << temp << endl;
cout << "Next string (q to quit): ";
}
cout << "Bye.";
cin.get();
cin.get();
return 0;
}
void UpperString(string& rs)
{
int size = rs.size();
for (int i = 0; i < size; i++)
rs[i] = toupper(rs[i]);
}
第4题:
// task 4
struct stringy
{
char * str;
int ct;
};
void set(stringy&, char*);
void show(const stringy& rs, int n = 1);
void show(const char* ps, 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!");
delete[] beany.str; //释放内存
cin.get();
cin.get();
return 0;
}
void set(stringy& rs, char* s)
{
rs.ct = strlen(s);
char* pt = new char[rs.ct + 1];
strcpy_s(pt, rs.ct + 1, s); // 注意第2个参数!!!
rs.str = pt;
}
void show(const stringy& rs, int n)
{
while (n-- > 0)
cout << rs.str << endl;
}
void show(const char* ps, int n)
{
while (n-- > 0)
cout << ps << endl;
}
第5题:
// task 5
const int SIZE = 5;
template<typename T>
T max(T arr[]);
int main()
{
int arr_int[SIZE] = { 4, 3, 9, 6, 2 };
double arr_double[SIZE] = { 3.39, 6.324, 4.11, 2.58, 4.4 };
cout << "Test int: " << max(arr_int) << endl;
cout << "Test double: " << max(arr_double) << endl;
cin.get();
cin.get();
return 0;
}
template<typename T>
T max(T arr[])
{
T max = arr[0];
for (int i = 1; i < SIZE; i++)
{
if(arr[i] > max)
max = arr[i];
}
return max;
}
第6题:
// task 6
template<class T>
T maxn(T arr[], int n);
template<> const char* maxn<const char*>(const char* arr_char[], int n); //显式具体化
int main()
{
int arr_int[6] = { 3, 3, 9, 2, 4, 6 };
double arr_double[4] = { 3.1, 9.8, 2.4, 7.7 };
const char* names[5] = { "My love", "Lenom treeeeeeeee", "Think Out loud", "Shape of you", "Heroes" };
cout << "Test int: " << maxn(arr_int, 6) << endl;
cout << "Test double: " << maxn(arr_double, 4) << endl;
cout << "Test char: " << maxn<const char*>(names, 5) << endl;
cin.get();
cin.get();
return 0;
}
template<class T>
T maxn(T arr[], int n)
{
T temp = arr[0];
for (int i = 1; i < n; i++)
{
if (arr[i]>temp)
temp = arr[i];
}
return temp;
}
template<> const char* maxn<const char*>(const char* arr_char[], int n)
{
int temp = 0;
for (int i = 1; i < n; i++)
{
if (strlen(arr_char[i]) > strlen(arr_char[temp]))
temp = i;
}
return arr_char[temp];
}
第7题:
// task 7
template <class T>
T SumArray(T arr[], int n); //模板函数重载
template <class T>
T SumArray(T* p_arr[], int n);
struct debts
{
char name[50];
double amount;
};
int main()
{
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] = { &mr_E[0].amount, &mr_E[1].amount, &mr_E[2].amount };
cout << SumArray(things, 6) << endl;
cout << SumArray(pd, 3) << endl;
cin.get();
cin.get();
return 0;
}
template <class T>
T SumArray(T arr[], int n)
{
T sum = 0;
for (int i = 0; i < n; i++)
{
sum += arr[i];
}
return sum;
}
template <class T>
T SumArray(T* p_arr[], int n)
{
T sum = 0;
for (int i = 0; i < n; i++)
{
sum += *p_arr[i];
}
return sum;
}