/***********************************
2017年10月24日15:56:55
Athor:xiyuan255
Course:C++
Contain:review8.cpp
Reference: C++ Primer plus
说明:C++ Primer plus第八章的练习题(选取部分)
【 P298 】
*************************************/
// review8.cpp -- C++ Primer plus 第八章练习题
#include <iostream>
#include <cstring> // exsampleNo6 field
#include <string> // exsampleNo6 field
#include <fstream>
using namespace std;
void exampleNo1(void);
void exampleNo2(void);
void exampleNo3(void);
void exampleNo4(void);
void exampleNo5(void);
void exampleNo6(void);
void exampleNo7(void);
void exampleNo8(void);
void exampleNo9(void);
void exampleNo10(void);
int main(void)
{
//exampleNo1(); // example 1 test
//exampleNo2(); // example 2 test
//exampleNo3(); // example 3 test
//exampleNo4(); // example 4 test
//exampleNo5(); // example 5 test
//exampleNo6(); // example 6 test
exampleNo7(); // example 7 test
//exampleNo8(); // example 8 test
//exampleNo9(); // example 9 test
//exampleNo10(); // example 10 test
return 0;
}
void PrintStr(const char *, int flag = 0);
void exampleNo1(void)
{
char str[] = "sucess sucess sucess";
std::cout << "#1 " << std::endl;
PrintStr(str);
std::cout << "#2 " << std::endl;
PrintStr(str, 1996);
std::cout << "#3 " << std::endl;
PrintStr(str, 1996);
std::cout << "#4 " << std::endl;
PrintStr(str);
}
void PrintStr(const char * str, int flag)
{
static int count = 0;
++count;
if (!flag)
std::cout << str << std::endl;
else
for (int i = 0; i < count; i++)
std::cout << str << std::endl;
}
/**
输出结果:
#1
sucess sucess sucess
#2
sucess sucess sucess
sucess sucess sucess
#3
sucess sucess sucess
sucess sucess sucess
sucess sucess sucess
#4
sucess sucess sucess
*/
struct CandyBar{
char name[40];
double weight;
int heats;
};
void setCandyBar(CandyBar & cb, const char * name = \
"Millennium Munch", double weight = 2.85, int heats = 350);
void showCandyBar(const CandyBar & cb);
void exampleNo2(void)
{
CandyBar cb;
setCandyBar(cb);
showCandyBar(cb);
char name[30];
double weight;
int heats;
cout << "Enter name: ";
std::cin.getline(name, 30);
cout << "Enter weight and heats: ";
std::cin >> weight >> heats;
setCandyBar(cb, name, weight, heats);
showCandyBar(cb);
}
void setCandyBar(CandyBar & cb, const char * name, double weight, int heats)
{
strcpy((char *)(cb.name), name);
cb.weight = weight;
cb.heats = heats;
}
void showCandyBar(const CandyBar & cb)
{
cout << "name = " << cb.name
<< ", weight = " << cb.weight
<< ", heats = " << cb.heats << endl;
}
/**
输出结果:
name = Millennium Munch, weight = 2.85, heats = 350
Enter name: zhang san
Enter weight and heats: 3.15 380
name = zhang san, weight = 3.15, heats = 380
*/
string & changeUpper(string & str);
void exampleNo3(void)
{
cout << "Enter a string (q to quit): ";
string str;
while ( getline(cin, str) && !((str.size() == 1) && str == "q") )
{
cout << changeUpper(str) << endl;
cout << "Next string (q to quit): ";
}
cout << "Done.\n";
}
string & changeUpper(string & str)
{
for (int i = 0; i < str.size(); i++)
str[i] = toupper(str[i]);
return str;
}
/**
输出结果:
Enter a string (q to quit): go away
GO AWAY
Next string (q to quit): good grief!
GOOD GRIEF!
Next string (q to quit): qsss
QSSS
Next string (q to quit): q
Done.
*/
#include <cstring> // for strlen(), strcpy()
struct stringy {
char * str; // points to a string
int ct; // length of string (not counting '\0')
};
void set(stringy & stry1, const char * stry2);
void show(const stringy & stry, const int count = 1);
void show(const char * str, const int count = 1);
// prototypes for set(). show(), and show() go here
void exampleNo4(void)
{
stringy beany;
char testing[] = "Reality isn't what it used to be. ";
set(beany, testing); // first argument is a reference,
// allocates space to hold copy of testing
// sets str member of beany to point to the
// new block, copies testing to new block,
// and sets ct member of beany
show(beany); // prints member string once
show(beany, 2); // prints member string twice
delete [] beany.str; // 释放new出的内存空间;
beany.ct = 0;
testing[0] = 'D';
testing[1] = 'u';
show(testing); // prints member string once
show(testing, 3); // prints member string thrice
show("Done!");
}
void set(stringy & stry1, const char * stry2)
{
stry1.str = new char[strlen(stry2) + 1]; // 要留一个字符存放'\0',所以需要
// 分配strlen(stry2) + 1个空间
strcpy(stry1.str, stry2);
stry1.str[strlen(stry2)] = '\0';
stry1.ct = strlen(stry2);
}
void show(const stringy & stry, const int count)
{
for (int i = 0; i < count; i++)
cout << "str = " << stry.str
<< ", ct = " << stry.ct << endl;
}
void show(const char * str, const int count)
{
for (int i = 0; i < count; i++)
cout << "str = " << str << endl;
}
/**
输出结果:
str = Reality isn't what it used to be. , ct = 34
str = Reality isn't what it used to be. , ct = 34
str = Reality isn't what it used to be. , ct = 34
str = Duality isn't what it used to be.
str = Duality isn't what it used to be.
str = Duality isn't what it used to be.
str = Duality isn't what it used to be.
str = Done!
*/
const int SIZE = 5;
template <typename T> // or <class T>
T max5(const T arr[SIZE]);
void exampleNo5(void)
{
int arr_int[SIZE] = { 1, 2, 3, 4, 5 };
double arr_dou[SIZE] = { 2.0, 4.0, 6.0, 8.0, 10.0 };
cout << "int array max value: " << max5(arr_int) << endl;
cout << "double array max value: " << max5(arr_dou) << endl;
}
template <typename T>
T max5(const T arr[SIZE])
{
T temp = arr[0];
for (int i = 1; i < SIZE; i++) {
if (temp < arr[i])
temp = arr[i];
}
return temp;
}
/**
输出结果:
int array max value: 5
double array max value: 10
*/
template <typename T>
T maxn(T arr[], int len); // 如果使用 T maxn(const T arr[], int len)
// 则该函数原型需要改成: template <> char * maxn(char* const arr[], int pcount)
template <> char *maxn(char *arr[], int pcount);
// 注意:模板中的const T代表的是char* const 而不是const char *。
void exampleNo6(void)
{
int arr_int[6] = { 1, 2, 3, 4, 5, 6 };
double arr_dou[4] = { 2.0, 4.0, 6.0, 8.0 };
char * ch[5] = { "suces", "sucess", "sucesssuc", "suncesssuce", "sucesssucess" };
cout << "int array max value: " << maxn(arr_int, 6) << endl;
cout << "double array max value: " << maxn(arr_dou, 4) << endl;
cout << "char* array max value: " << maxn(ch, 5) << endl;
}
template <typename T>
T maxn(T arr[], int len)
{
if (0 == len)
return 0;
else {
T temp = arr[0];
for (int i = 1; i < len; i++) {
if (temp < arr[i])
temp = arr[i];
}
return temp;
}
}
template <> char *maxn(char *arr[], int pcount)
{
char * point = NULL;
if (pcount == 0)
return NULL;
else {
int leng = strlen(arr[0]);
for (int i = 1; i < pcount; i++) {
if (leng < strlen(arr[i])) {
point = arr[i];
}
}
}
return point;
}
/**
输出结果:
int array max value: 6
double array max value: 8
char* array max value: sucesssucess
*/
template <typename T> // template A
void ShowArray(T arr[], int n);
template <typename T> // template B
void ShowArray(T * arr[], int n);
struct debts {
char name[50];
double amount;
};
void exampleNo7(void)
{
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];
// set pointers to the amount members of the structures in mr_E
for (int i = 0; i < 3; i++)
pd[i] = &mr_E[i].amount;
cout << "Listing Mr. E's counts of things:\n";
// things is an array of int
ShowArray(things, 6); // use template A
cout << "Listing Mr. E's debts:\n";
// pd is an array of pointers to double
ShowArray(pd, 3); // use template B(more specialized)
}
template <typename T>
void ShowArray(T arr[], int n)
{
using namespace std;
cout << "template A\n";
T sum = 0;
for (int i = 0; i < n; i++)
sum += arr[i];
cout << "sum = " << sum << endl;
}
template <typename T>
void ShowArray(T * arr[], int n)
{
using namespace std;
cout << "template B\n";
T sum = 0;
for (int i = 0; i < n; i++)
sum += *arr[i];
cout << "sum = " << sum << endl;
}
/**
输出结果:
Listing Mr. E's counts of things:
template A
sum = 888
Listing Mr. E's debts:
template B
sum = 5500
*/
void exampleNo8(void)
{
}
void exampleNo9(void)
{
}
void exampleNo10(void)
{
}
【第八章】C++ Primer plus 的编程练习题(选取部分)
最新推荐文章于 2022-01-05 10:00:00 发布