C++ Primer Plus第六版 第八章 编程练习答案

时间:2021-10-16 14:39:16

23333 时隔一年我来填坑了 CPP继续往后读然后把题目写了_(:з」∠)_ 写到哪更到哪 不一次性更完了


//第一题
#include <iostream>

void PrintStr(char *str, int flag = 0);

int main()
{
char str[] = "fuck fuck fuck";
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);
return 0;
}

void PrintStr(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;
}




//第二题
#include <iostream>
#include <cstring>

struct CandyBar{
char name[30];
double weight;
int heat;
};

void setCandyBar(CandyBar &c, const char *s = "Millennium Munch", const double w = 2.85, const int h = 350);
void disp(const CandyBar &c);

int main()
{
CandyBar c;
setCandyBar(c);
disp(c);
char name[30];
double weight;
int heat;
std::cin.getline(name, 30);
std::cin >> weight >> heat;
setCandyBar(c, name, weight, heat);
disp(c);
return 0;
}

void setCandyBar(CandyBar &c, const char *s, const double w, const int h)
{
strcpy(c.name, s);
c.weight = w;
c.heat = h;
}

void disp(const CandyBar &c)
{
std::cout << c.name << std::endl << c.weight << std::endl << c.heat << std::endl;
}


//第三题
#include <iostream>
#include <string>

void changeUpper(std::string &s);

int main()
{
std::string s;
while(getline(std::cin, s) && s[0] != 'q')
{
changeUpper(s);
std::cout << s << std::endl;
}
return 0;
}

void changeUpper(std::string &s)
{
for(int i = 0; i < s.size(); ++i)
s[i] = toupper(s[i]);
}


//第四题
#include <iostream>
#include <cstring>

struct stringy{
char *str;
int ct;
};

void set(stringy &s, const char *str);
void show(const char *str, const int count = 1);
void show(const stringy &s, const int count = 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 &s, const char *str)
{
int len = strlen(str);
s.ct = len;
s.str = new char(len + 1); //要留一个字符存放'\0' 所以分配len + 1空间
strcpy(s.str, str);
}

void show(const char *str, const int count)
{
for(int i = 0 ; i < count ; ++i)
std::cout << str << std::endl;
}

void show(const stringy &s, const int count)
{
for(int i = 0 ;i < count ; ++i)
std::cout << s.str << std::endl;
}




//第五题
#include <iostream>

template <typename T>
T max5(T *a);

int main()
{
int a[5] = {2, 3, 1, 5, 4};
double b[5] = {1.1, 3.3, 5.5, 4.4, 2.2};
std::cout << max5(a) << std::endl;
std::cout << max5(b) << std::endl;
return 0;
}

template <typename T>
T max5(T *a)
{
T max = -1;
for(int i = 0 ;i < 5 ; ++i)
max = max < a[i] ? a[i] : max;
return max;
}


//第六题
#include <iostream>
#include <cstring>

template <typename T>
T maxn(T *a, int len);

template <> char *maxn(char *a[], int len);

int main()
{
int a[6] = {1, 2, 3, 4, 5, 6};
double b[4] = {1.1, 2.2, 3.3, 4.4};
char *c[5] = {"fuck", "fuckfuck", "fuckfuckfuck", "fuckfuckfuckfuck", "fuckfuckfuckfuckfuck"};
std::cout << maxn(a, 6) << std::endl;
std::cout << maxn(b, 4) << std::endl;
std::cout << maxn(c, 5) << std::endl;
return 0;
}

template <typename T>
T maxn(T *a, int len)
{
T max = -1;
for(int i = 0 ; i < len ; ++i)
max = max < a[i] ? a[i] : max;
return max;
}

template <> char *maxn(char *a[], int len)
{
int max = -1;
char *p = NULL;
for(int i = 0 ; i < len ; ++i)
{
if((int)strlen(a[i])> max)
{
max = strlen(a[i]);
p = a[i];
}
}
return p;
}



//第七题
#include <iostream>

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

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

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

int main()
{
int things[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;
std::cout << SumArray(things, 6) << std::endl;
std::cout << SumArray(pd, 3) << std::endl;
return 0;
}

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

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