概述
8.1
#include <iostream>
using namespace std;
void p1(const string &str, const int times=0)
{
static int called = 0;
int repeat = times>0 ? called : 0;
do
{
cout << str << endl;
} while (repeat--);
called++;
return;
}
8.2
#include <iostream>
using namespace std;
struct CandyBar
{
string brand;
double weight;
int calorie;
};
void p2_construct(CandyBar& candybar, char* brand = "Millennium Munch", const double weight= 2.85, const int calorie = 350)
{
candybar.brand = brand;
candybar.weight = weight;
candybar.calorie = calorie;
}
void p2_print(const CandyBar& candybar)
{
cout << "CandyBar brand: " << candybar.brand << endl;
cout << "CandyBar weight: " << candybar.weight << endl;
cout << "CandyBar calorie: " << candybar.calorie << endl;
}
8.3
#include <iostream>
using namespace std;
void p3_Upper_String()
{
cout << "Enter a string (q to quit): ";
char ch;
cin.get(ch);
string output;
while(ch != 'q')
{
ch = toupper(ch);
output.push_back(ch);
cin.get(ch);
if(ch == 'n')
{
cout << output << endl;
cout << "Next string (q to quit): ";
output.clear();
cin.get(ch);
}
}
cout << "Bye." << endl;
return;
}
8.4
#include <iostream>
#include <cstring>
using namespace std;
struct stringy
{
char* str;
int ct;
};
void show(const stringy beany, int times=0)
{
do
{
cout << beany.str << endl;
}
while(times--);
}
void show(const char* testing, int times=0)
{
do
{
cout << testing << endl;
}
while(times--);
}
void set(stringy& beany, const char* testing)
{
int size = strlen(testing);
beany.ct = size;
beany.str = new char[size+1];
strcpy(beany.str, testing);
}
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;
}
8.5
#include <iostream>
using namespace std;
template <typename T>
T max5(const T arr[])
{
T max_T = arr[0];
for(int i=0; i<5; i++)
max_T = arr[i] > max_T ? arr[i] : max_T;
return max_T;
}
int main()
{
int int_arr[5] = {1,2,3,4,5};
double dou_arr[5] = {1.1,2.2,3.3,4.4,5.5};
cout << max5(dou_arr) << endl;
return 0;
}
8.6
#include <iostream>
using namespace std;
template <typename T>
T maxn(T arr[], int size);
template <> char* maxn(char* arr[], int size);
int main()
{
int int_arr[6] = {1,2,3,4,5,6};
double dou_arr[4] = {1.1,2.2,3.3,4.4};
cout << maxn(int_arr, 6) << endl;
cout << maxn(dou_arr, 4) << endl;
char* arr[] = {"Testing maxn specialization for string", "Testing maxn specialization", "Testing maxn", "Testing", "test"};
cout << maxn(arr, 5) << endl;
return 0;
}
template <typename T>
T maxn(T arr[], int size)
{
T max_T = arr[0];
for(int i=0; i<size; i++)
max_T = arr[i] > max_T ? arr[i] : max_T;
return max_T;
}
template <> char* maxn(char* arr[], int size)
{
if(size<=0)
return 0;
int max_len = strlen(arr[0]);
char* max_address = arr[0];
for(int i=0; i<size; ++i)
{
int size = strlen(arr[i]);
if(size > max_len)
{
max_len = size;
max_address = arr[i];
}
}
return max_address;
}
8.7
#include <iostream>
using namespace std;
template <typename T>
T SumArray(T arr[], int size);
template <typename T>
T SumArray(T* arr[], int size);
struct debts
{
char name[50];
double amount;
};
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 << "Listing Mr.E's sum of things:" <<
SumArray(things, 6) << endl;
cout << "Listing Mr.E's sum of debts:" << SumArray(pd, 3) << endl;
return 0;
}
template <typename T>
T SumArray(T arr[], int size)
{
int sum=0;
for(int i=0; i<size; ++i)
sum += arr[i];
return sum;
}
template <typename T>
T SumArray(T * arr[], int size)
{
double sum=0;
for(int i=0; i<size; ++i)
sum += *arr[i];
return sum;
}
最后
以上就是稳重秀发为你收集整理的C++ Primer Plus 第六版课后习题答案(第八章)的全部内容,希望文章能够帮你解决C++ Primer Plus 第六版课后习题答案(第八章)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复