C++ - Primer - Plus(第五版)习题解答(3)

2019-03-22 17:31

Solutions for Programming Exercises in C++ Primer Plus, 5th Edition

void showbox(box b); void setbox(box * pb);

int main(void) {

box carton = {\ setbox(&carton); showbox(carton); return 0; }

void showbox(box b) {

using namespace std;

cout << \ << \ << \ << \

<< \}

void setbox(box * pb) {

pb->volume = pb->height * pb->width * pb->length; }

// pe7-4.cpp -- probability of winning #include

long double probability(unsigned numbers, unsigned picks);

int main() {

using namespace std; double total, choices; double mtotal;

double probability1, probability2;

cout << \ \ while ((cin >> total >> choices) && choices <= total) {

cout << \ \ if (!(cin >> mtotal)) break;

cout << \\

<< (probability1 = probability(total, choices) ) << \ cout << \ << (probability2 = probability(mtotal, 1) ) << \ cout << \

cout << probability1 * probability2; // compute the probability cout << \

cout << \ }

cout << \ return 0; }

// the following function calculates the probability of picking picks // numbers correctly from numbers choices

long double probability(unsigned numbers, unsigned picks) {

long double result = 1.0; // here come some local variables

SP 11 of 65 September 2, 2004

Solutions for Programming Exercises in C++ Primer Plus, 5th Edition

long double n; unsigned p;

for (n = numbers, p = picks; p > 0; n--, p--) result = result * n / p ; return result; }

// pe7-6.cpp

#include

int Fill_array(double ar[], int size);

void Show_array(const double ar[], int size); void Reverse_array(double ar[], int size); const int LIMIT = 10;

int main( ) {

using namespace std; double values[LIMIT];

int entries = Fill_array(values, LIMIT); cout << \ Show_array(values, entries); cout << \ Reverse_array(values, entries); Show_array(values, entries);

cout << \ Reverse_array(values + 1, entries - 2); Show_array(values, entries);

return 0; }

int Fill_array(double ar[], int size) {

using namespace std; int n;

cout << \ for (n = 0; n < size; n++) {

cin >> ar[n]; if (!cin) break; }

return n; }

void Show_array(const double ar[], int size) {

using namespace std; int n;

for (n = 0; n < size; n++) {

cout << ar[n]; if (n % 8 == 7) cout << endl; else

cout << ' '; }

if (n % 8 != 0) cout << endl; }

void Reverse_array(double ar[], int size) {

int i, j;

SP 12 of 65 September 2, 2004

Solutions for Programming Exercises in C++ Primer Plus, 5th Edition

double temp;

for (i = 0, j = size - 1; i < j; i++, j--) {

temp = ar[i]; ar[i] = ar[j]; ar[j] = temp; } }

//pe7-9.cpp

#include

double calculate(double x, double y, double (*pf)(double, double)); double add(double x, double y); double sub(double x, double y); double mean(double x, double y);

int main(void) {

using namespace std;

double (*pf[3])(double,double) = {add, sub, mean}; char * op[3] = {\ double a, b;

cout << \ int i;

while (cin >> a >> b) {

// using function names

cout << calculate(a, b, add) << \ cout << calculate(a, b, mean) << \ // using pointers

for (i = 0; i < 3; i++)

cout << calculate(a, b, pf[i]) << \ << op[i] << \ }

cout << \ return 0; }

double calculate(double x, double y, double (*pf)(double, double)) {

return (*pf)(x, y); }

double add(double x, double y) {

return x + y; }

double sub(double x, double y) {

return x - y; }

double mean(double x, double y) {

return (x + y) / 2.0; }

SP 13 of 65 September 2, 2004

Solutions for Programming Exercises in C++ Primer Plus, 5th Edition

Chapter 8

// pe8-1.cpp

#include

void silly(const char * s, int n = 0); int main(void) {

using namespace std;

char * p1 = \

silly(p1);

for (int i = 0; i < 3; i++) {

cout << i << \ silly(p1, i); }

cout << \ return 0; }

void silly(const char * s, int n) {

using namespace std; static int uses = 0;

int lim = ++uses; if (n == 0) lim = 1;

for (int i = 0; i < lim; i++) cout << s; }

// pe8-4.cpp

#include

#include // for strlen(), strcpy() using namespace std;

struct stringy {

char * str; // points to a string

int ct; // length of string (not counting '\\0') };

void show(const char *str, int cnt = 1); void show(const stringy & bny, int cnt = 1); void set(stringy & bny, const char * str);

int main(void) {

stringy beany;

char testing[] = \

set(beany, testing); // first argument is a reference, // allocates space to hold copy of testing, // sets str member of beany to point to the // new block, copies testing to new block, // and sets ct member of beany

show(beany); // prints member string once show(beany, 2); // prints member string twice testing[0] = 'D'; testing[1] = 'u';

show(testing); // prints testing string once

SP 14 of 65 September 2, 2004

Solutions for Programming Exercises in C++ Primer Plus, 5th Edition

show(testing, 3); // prints testing string thrice show(\ return 0; }

void show(const char *str, int cnt) {

while(cnt-- > 0) {

cout << str << endl; } }

void show(const stringy & bny, int cnt) {

while(cnt-- > 0) {

cout << bny.str << endl; } }

void set(stringy & bny, const char * str) {

bny.ct = strlen(str);

bny.str = new char[bny.ct+1]; strcpy(bny.str, str); }

// pe8-5.cpp

#include

template T max5(T ar[]) {

int n;

T max = ar[0];

for (n = 1; n < 5; n++) if (ar[n] > max) max = ar[n]; return max; }

const int LIMIT = 5; int main( ) {

using namespace std;

double ard[LIMIT] = { -3.4, 8.1, -76.4, 34.4, 2.4}; int ari[LIMIT] = {2, 3, 8, 1, 9}; double md; int mi;

md = max5(ard); mi = max5(ari);

cout << \ cout << \

return 0; }

SP 15 of 65 September 2, 2004


C++ - Primer - Plus(第五版)习题解答(3).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:2019-2024年中国洗衣机行业前景预测及投资咨询报告(目录) - 图

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: