原创 C++ Primer Plus 第八章 课后编程练习题1-7

//1题
#include
int number = 1;
void show(char *, int n = number);
using namespace std;
int main()
{
char a[] = “wo jiao wxp!”;
show(a);
show(a);
show(a);

return 0;

}

void show(char * a, int n)
{
for (int i = 0; i < n; i++)
cout <<“第” << number<<"调用函数: " << a << endl;
number++;
}

//2题
#include
using namespace std;
struct CandBar {
char cb_name[40];
float cb_weight;
int cb_heat;
};
void storage(CandBar &, char *, double, int);
void show(const CandBar &);

int main()
{
char name[] = “Millennium Munch”;
CandBar mill;
storage(mill, name, 2.85, 350);
show(mill);

return 0;

}

void storage(CandBar &a, char * p, double b, int n)
{
int i = 0;
while(*p)
{
a.cb_name[i] = *p;
i++;
p++;
}
a.cb_weight = b;
a.cb_heat = n;
}

void show(const CandBar &a)
{
cout << a.cb_name << endl;
cout << a.cb_weight << endl;
cout << a.cb_heat << endl;
}
//3题
#include
#include
#include
using namespace std;
void storage(string &);
int main()
{
string sentence;
cout <<"Enter a string (q to quit): ";
while (getline(cin, sentence) && sentence != “q”)
{
storage(sentence);
cout << sentence << endl;
cout <<"Next string (q to quit): ";
}
return 0;
}
void storage(string &a)
{
int i = 0;
while(a[i])
{
a[i] = toupper(a[i]);
i++;
}
}

//4题
#include
#include
using namespace std;
struct stringy {
char * str;
int ct;
};
void set(stringy &, const char a[]);
void show(stringy a, int n = 1); //使用函数模版

void show(char a[], int n = 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!”);
//主程序没有,自己添加的。
delete [] beany.str;

return 0;

}
void set(stringy &b, const char add[])
{

b.str = new char[strlen(add)+1];
strcpy(b.str, add);
b.ct = strlen(add)+1;

}

void show(stringy a,int n)
{
for (int i = 0; i < n; i++)
std::cout << a.str <<’ ’ << a.ct <<std::endl ;
}

void show(char a[], int n)
{
for(int i = 0; i < n; i++)
std::cout << a << std::endl;
}
//5题
#include
template
T max5(T a[5]);
int main()
{
int num[5] = {1,2,3,5,4};
double d[5] = {1.1, 2.1, 5.1, 3.1, 4.1};
int n;
double b;
n = max5(num);
b = max5(d);
std::cout<<"num max = "<< n << std::endl;
std::cout<<"d max = "<< b << std::endl;

return 0;

}
template
T max5(T a[5])
{
T max = 0;
for(int i = 0; i < 5; i++)
(max < a[i]) ? max = a[i] : max = max;
return max;
}
//6题
#include
#include
template
T maxn(T a[], int n);
template<>char * maxn<char *>(char *a[], int n);
int main()
{
using namespace std;
int number[6] = {11,22,3,5,67,2};
double b[4] = {1.1, 4.4,3.2,2.2};
char * p[5] = {
“aaaaaaa”,
“bbbbbbbbb”,
“cccccccccccc”,
“dddddddddddd”,
“eeeeee”
};

int maxni;
maxni = maxn(number, 6);
double maxnd;
maxnd = maxn(b, 4);
char *maxnp;
maxnp = maxn<>(p,5);
cout <<"maxni = " << maxni << endl;
cout <<"maxnd = " << maxnd << endl;
cout <<"maxnp = " << maxnp << endl;

return 0;

}
template
T maxn(T a[], int n)
{
T max = 0;
for (int i = 0; i < n; i++)
(max > a[i]) ? max = max : max = a[i];

return max;

}
template<>char * maxn(char *a[], int n)
{
char *p;
p = a[0];
for (int i = 1; i < n; i++)
{
strlen§ < strlen(a[i]) ? p = a[i] : p = p;
}
return p;
}
//7题
#include
template
T ShowArray(T arr[], int n);

template
T ShowArray(T * arr[], int n);
struct debts {
char name[50];
double amount;
};

int main()
{
using namespace std;
int things[6] = {13, 31, 103, 301, 310, 130};
int thing;
struct debts mr_E[3] =
{
{“Ima Wolfe”, 2400.0},
{“Ura Foxe”, 1300.0},
{“Iby Stout”, 1800.0}
};
double * pd[3];
double debt;
for (int i = 0; i < 3; i++)
pd[i] = &mr_E[i].amount;

thing = ShowArray(things, 6);
debt = ShowArray(pd, 3);
cout <<"The sum of things is " << thing <<'.' << endl;
cout <<"The sum of the debt is " << debt <<'.' << endl;

return 0;

}
template
T ShowArray(T arr[], int n)
{
using namespace std;
T total = 0;
for (int i = 0; i < n; i++)
total += arr[i];
return total;
}

template
T ShowArray(T * arr[], int n)
{
using namespace std;
T total = 0;
for (int i = 0; i < n; i++)
total += *arr[i];
return total;
}

发布了85 篇原创文章 · 获赞 1 · 访问量 1889

猜你喜欢

转载自blog.csdn.net/Tekkenwxp/article/details/104033319
今日推荐