c++ primer plus第八章習題答案

這章講引用、模版和重載


第一題

/*
這道題什麼意思?
第二個值爲零或空,只打印一次;
第二個值非零時,調用第一次,打印一次字符串;調用第二次,打印兩次字符串
以此類推
*/
#include <iostream>
using namespace std;

void Print(const char * pt, int n = 0);

int main()
{
char words[] = {"Hello World!"};

Print(words);

Print(words, 1);

Print(words, 0);

Print(words, 1);

return 0;
}

void Print(const char * pt, int n)
{
static int count_sum = 0;
static int count_time = 0;

count_sum++;

cout << "The " << count_sum << " time: " << endl;
if(n == 0)
cout << pt << endl;
else
{
count_time++;
cout << "The " << count_time << " time n != 0: " << endl;
for(int i = 0; i < count_sum; i++)
{
cout << pt << endl;
}
}
}


第二題

#include <iostream>

typedef struct
{
char* brand;
double mass;
int heat;
}CandyBar;

void Init(CandyBar & candy, char * brand = "Millennium Munch", double mass = 2.85, int heat = 350);
void Print(const CandyBar can);

int main(void)
{
CandyBar mycandy1, mycandy2;
Init(mycandy1);
Init(mycandy2, "Chocolate", 3.1415, 100);

Print(mycandy1);
Print(mycandy2);

return 0;
}

void Init(CandyBar & candy, char * brand, double mass, int heat)
{
candy.brand = brand;
candy.mass = mass;
candy.heat = heat;
}

void Print(const CandyBar can)
{
using namespace std;
cout << can.brand << ": mass = " << can.mass << ", heat = " << can.heat << endl;
}


第三題

#include <iostream>
#include <string>
#include <cctype>
using namespace std;

//*******************************************************************//
//***********標註部分是另一種實現方式,基於字符類型******************//
/*char convert(char character)
{
if(character >= 97 && character <= 122)
return(toupper(character));
else
return(character);
}*/
//*******************************************************************//

void conversion(string & str)//網上抄的,詳見http://blog.sina.com.cn/s/articlelist_1238143313_0_1.html
{
int limit = str.size();
char temp = 0;

for(int i = 0; i < limit; i++)
{
if(isalpha(str[i]))
str[i] = toupper(str[i]);
}
}

int main(void)
{

//*******************************************************************//
/*char phrase,output;

cout << "Enter a string (q to quit): ";
while(cin.get(phrase) && phrase != 'q')
{
if(phrase == 10)// ASCII碼10, 換行符
cout << endl << "Next string (q to quit): ";
else
cout << convert(phrase);
}
cout << "Bye." << endl;*/
//*******************************************************************//

string words;

cout << "Enter a string (q to quit): ";
while(getline(cin, words) && words != "q")
{
conversion(words);
cout << words << endl
<< "Next string (q to quit): ";
}
cout << "Bye." << endl;

return 0;
}


第四題

#include <iostream>
using namespace std;
#include <cstring>
struct stringy {
char * str;
int ct;
};

void set(stringy & bean, char test[]);
void show(const stringy & bean, int n = 1);
void show(const char * pt, int m = 1);

// prototypes for set(), show(), and show() go here
int main()
{
stringy beany;
char testing[] = "Reality isn't what it used to be.";

set(beany, testing);
show(beany);
show(beany,2);
delete [] beany.str;

testing[0] = 'D';
testing[1] = 'u';
show(testing);
show(testing, 3);
show("Done!");
return 0;
}

void set(stringy & bean, char test[])
{
bean.str = new char [strlen(test)+1];
strcpy(bean.str, test);
}

void show(const stringy & bean, int n)
{
for(int i = 0; i < n; i++)
{
cout << bean.str << endl;
}
cout << endl;
}

void show(const char * pt, int m)
{
for(int i = 0; i < m; i++)
{
cout << pt << endl;
}
cout << endl;
}


第五題

#include <iostream>
using namespace std;

template <typename T>
T max5(const T num[]);//傳遞的值爲地址,是指針類型,不需要引用。

int main(void)
{
int a[5] = {1,5,3,6,2};
double b[5] = {1.1,1.5,1.3,1.6,1.2};
int aMax;
double bMax;

aMax = max5(a);
bMax = max5(b);

cout << "Max num of int type is " << aMax << endl;
cout << "Max num of double type is " << bMax << endl;
}

template <typename T>
T max5(const T num[])
{
T temp = num[0];
for(int i = 0; i < 5; i++)
{
if(temp < num[i])
temp = num[i];
//num[5] = 10;
}
return temp;
}


第六題

#include <iostream>
using namespace std;

template <typename T>
T maxn(T num[], int n);

template <> char * maxn<char *>(char * num[], int n);

int main(void)
{
int aMax, a[] = {1,5,2,3,7,4};
double bMax, b[] = {1,5,2,3};
char * c[] = {
"Hello World",
"I'm glad to meet you",
"Little Apple",
"Dominate"
};
char * cAddr;

aMax = maxn(a, 6);
bMax = maxn(b, 4);
cAddr = maxn(c, 4);

cout << "aMax = " << aMax << endl;
cout << "bMax = " << bMax << endl;
cout << "cAddr = 0x" << static_cast<const void *>(cAddr) << " as \"" << cAddr << "\"" << endl;//轉換成無類型指針輸出字符串地址

return 0;
}

template <typename T>
T maxn(T num[], int n)
{
T temp;
temp = num[0];
for(int i = 0; i < n; i++)
{
if(temp < num[i])
temp = num[i];
}
return temp;
}

template <> char * maxn<char *>(char * num[], int n)
{
char * temp = num[0];
for(int i = 0; i < n; i++)
{
if(strlen(temp) < strlen(num[i]))
temp = num[i];
}
return temp;
}


第七題

#include <iostream>

struct debts
{
char name[50];
double amount;
};

template <typename T>
void ShowArray(T arr[], int n);

template <typename T>
void ShowArray(T * arr[], int n);

template <typename T>
T SumArray(T arr[], int n);

template <> debts SumArray<debts>(debts arr[], int n);

int main()
{
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];

for (int i = 0; i < 3; i++)
pd[i] = &mr_E[i].amount;

cout << "Listing Mr. E's counts of things:\n";
ShowArray(things, 6);

cout << "Listing Mr. E's debts:\n";
ShowArray(pd, 3);

cout << "Listing Mr. E's things amount:\n";
cout << SumArray(things, 6) << endl;

cout << "Listing Mr. E's debts amount:\n";
cout << SumArray(mr_E, 3).amount << endl;
return 0;
}

template <typename T>
void ShowArray(T arr[], int n)
{
using namespace std;
cout << "template A\n";
for (int i = 0; i < n; i++)
cout << arr[i] << ' ';
cout << endl;
}

template <typename T>
void ShowArray(T * arr[], int n)
{
using namespace std;
cout << "template B\n";
for (int i = 0; i < n; i++)
cout << *arr[i] << ' ';
cout << endl;
}

template <typename T>
T SumArray(T arr[], int n)
{
T temp = 0;
for(int i = 0; i < n; i++)
temp = temp + arr[i];
return temp;
}

template <> debts SumArray<debts>(debts arr[], int n)
{
debts temp = {"NONE", 0};
for (int i = 0; i < n; i++)
{
temp.amount = temp.amount + arr[i].amount;
}
return temp;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章