我是靠谱客的博主 羞涩豆芽,最近开发中收集的这篇文章主要介绍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 An";
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 Bn";
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;
}

最后

以上就是羞涩豆芽为你收集整理的c++ primer plus第八章习题答案的全部内容,希望文章能够帮你解决c++ primer plus第八章习题答案所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(59)

评论列表共有 0 条评论

立即
投稿
返回
顶部