C++ Primer Plus(第六版)第八章课后习题
8.8.1
#include
using namespace std;
inline void show(char *ch,int n)
{
for(int i=0;n>=1&&i<n;i++)
cout << ch <<endl;
}
int main()
{
char *ch=“Wanglin”;
int i;
cout << "Enter a number: ";
cin >> i;
show(ch,i);
return 0;
}
8.8.2
#include
using namespace std;
struct CandyBar
{
char *brand;
double weight;
int calorie;
};
void set(CandyBar &ch,char *a,double b,int c)
{
ch.brand=a;
ch.weight=b;
ch.calorie=c;
}
void show(const CandyBar &ch)
{
cout << "The name of candy company is " << ch.brand << endl;
cout << "The weight is " << ch.weight << endl;
cout << "The calorie is " << ch.calorie << endl;
}
int main()
{
CandyBar first;
set(first,“Millennium Munch”,2.85,350);
show(first);
return 0;
}
8.8.3
#include
#include
using namespace std;
void change(string &str)
{
for(int i=0;i<str.size();i++)
str[i]=toupper(str[i]);
}
int main()
{
string st;
cout << "Enter a string (q to quit): ";
getline(cin,st);
while(st!=“q”)
{
change(st);
cout << st << endl;
cout << “Next string (q to quit):” << endl;
getline(cin,st);
}
cout << “Bye.”;
return 0;
}
8.8.4
#include
using namespace std;
#include
struct stringy
{
char *str;
int ct;
};
void set(stringy &a,char *b);
void show(const char a[],int b=1);
void show(const stringy a,int b=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!”);
return 0;
}
void set(stringy &a,char *b)
{
a.str=new char(strlen(b)+1);
strcpy(a.str ,b);
a.ct=strlen(b);
}
void show(const char a[],int b)
{
for(int i=0;i<b;i++)
{
cout << a <<endl;
}
}
void show(const stringy a,int b)
{
for(int i=0;i<b;i++)
{
cout << a.str << endl;
}
}
8.8.5
#include
const int Arsize=5;
template
void change(T *a,T &Max)
{
Max=a[0];
for(int i=0;i<Arsize;i++)
if(Max<a[i])
Max=a[i];
}
int main()
{
using namespace std;
int a[Arsize]={1,45,78,45,86},a_max=0;
double b[Arsize]={45.7,8783.6,45.2,23.5,451.6},b_max=0;
change(a,a_max);
change(b,b_max);
cout << “a_max=” << a_max <<endl;
cout << “b_max=” << b_max <<endl;
return 0;
}
8.8.6
#include
template
anytype arr_max( anytype ch[],int limits)
{
anytype max=ch[0];
for(int i=0;i<limits;i++)
if(max<ch[i])
max=ch[i];
return max;
}
template <> char* arr_max(char ch[],int limits)
{
char max=ch[0];
for(int i=0;i<limits;i++)
if(strlen(max)<strlen(ch[i]))
max=ch[i];
return max;
}
int main()
{
using namespace std;
int a[6]={12,45,78,45,79,101};
double b[4]={45.2,78.6,66.4,101.5};
char *ch[5]={“dad”,“mother”,“brother”,“uncle”,“aunt”};
int a_max=arr_max(a,6);
double b_max=arr_max(b,4);
char *ch_max=arr_max(ch,5);
cout << "Int max is " << a_max<< endl;
cout << "Double max is " << b_max <<endl;
cout << "Char max is " << ch_max << endl;
return 0;
}
8.8.7
#include
using namespace std;
struct debts
{
char name[50];
double amount;
};
template T addarray(T arr[],int n)
{
cout << “template A\n”;
T sum=0;
for(int i=0;i<n;i++)
sum+=arr[i];
return sum;
}
template
T addarray(T* arr[],int n)
{
cout << “template B\n”;
T sum=0;
for(int i=0;i<n;i++)
sum+=*arr[i];
return sum;
}
int main()
{
int thing[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];
for(int i=0;i<3;i++)
pd[i]=&mr_E[i].amount;
int thing_sum=addarray(thing,6);
cout << "thing_sum is " << thing_sum <<endl;
double amount_sum=addarray(pd,2);
cout << "amount_sum is " << amount_sum << endl;
}
本文深入解析了C++ Primer Plus第六版第八章的课后习题,涵盖了从基本的字符串操作到复杂的数据结构应用,通过具体代码示例展示了C++语言的高级特性,包括内联函数、结构体、模板函数等,适合C++初学者及进阶者参考学习。
第八章课后习题&spm=1001.2101.3001.5002&articleId=103885458&d=1&t=3&u=5f704d4524264faabf86703e15c20451)
2071

被折叠的 条评论
为什么被折叠?



