C++ Primer Plus(第六版)第5章 编程练习答案详解

1. 编写一个要求用户输入两个整数的程序。该程序将计算并输出这两个整数之间(包括这两个整数)所有整数的和。这里假设先输入较小的整数。例如,如果用户输入的是29,则程序将指出2~9之间所有整数的和为44

#include<iostream>

using namespace std;



int main()

{

    int min, max, sum = 0;

    cout << "Enter the min interger:";

    cin >> min;

    cout << "Enter the max interger:";

    cin >> max;

    for (int i = min; i <= max; i++)//for(int i =min;i < max + 1;i++)

         sum += i;

    cout << "Sum = " << sum << endl;

    system("pause");

    return 0;

}

//Enter the min interger : 2

//Enter the max interger : 9

//Sum = 44

//请按任意键继续. . .

 2. 使用array对象(而不是数组)和long double(而不是long long)重新编写程序清单5.4,并计算100!的值。

#include<iostream>

#include<array>

const int ArSize = 101;

using namespace std;



int main()

{

    array<long double, ArSize> factorials;

    factorials[1] = factorials[0] = 1;

    for (int i = 2; i < ArSize; i++)

         factorials[i] = i * factorials[i - 1];

    for (int i = 0; i < ArSize; i++)

         cout << i << " != " << factorials[i] << endl;

    system("pause");

    return 0;

}



//0 != 1

//1 != 1

//2 != 2

//3 != 6

//4 != 24

//5 != 120

//6 != 720

//7 != 5040

//8 != 40320

//9 != 362880

//10 != 3.6288e+06

//11 != 3.99168e+07

//12 != 4.79002e+08

//13 != 6.22702e+09

//14 != 8.71783e+10

//15 != 1.30767e+12

//16 != 2.09228e+13

//17 != 3.55687e+14

//18 != 6.40237e+15

//19 != 1.21645e+17

//20 != 2.4329e+18

//21 != 5.10909e+19

//22 != 1.124e+21

//23 != 2.5852e+22

//24 != 6.20448e+23

//25 != 1.55112e+25

//26 != 4.03291e+26

//27 != 1.08889e+28

//28 != 3.04888e+29

//29 != 8.84176e+30

//30 != 2.65253e+32

//31 != 8.22284e+33

//32 != 2.63131e+35

//33 != 8.68332e+36

//34 != 2.95233e+38

//35 != 1.03331e+40

//36 != 3.71993e+41

//37 != 1.37638e+43

//38 != 5.23023e+44

//39 != 2.03979e+46

//40 != 8.15915e+47

//41 != 3.34525e+49

//42 != 1.40501e+51

//43 != 6.04153e+52

//44 != 2.65827e+54

//45 != 1.19622e+56

//46 != 5.50262e+57

//47 != 2.58623e+59

//48 != 1.24139e+61

//49 != 6.08282e+62

//50 != 3.04141e+64

//51 != 1.55112e+66

//52 != 8.06582e+67

//53 != 4.27488e+69

//54 != 2.30844e+71

//55 != 1.26964e+73

//56 != 7.10999e+74

//57 != 4.05269e+76

//58 != 2.35056e+78

//59 != 1.38683e+80

//60 != 8.32099e+81

//61 != 5.0758e+83

//62 != 3.147e+85

//63 != 1.98261e+87

//64 != 1.26887e+89

//65 != 8.24765e+90

//66 != 5.44345e+92

//67 != 3.64711e+94

//68 != 2.48004e+96

//69 != 1.71122e+98

//70 != 1.19786e+100

//71 != 8.50479e+101

//72 != 6.12345e+103

//73 != 4.47012e+105

//74 != 3.30789e+107

//75 != 2.48091e+109

//76 != 1.88549e+111

//77 != 1.45183e+113

//78 != 1.13243e+115

//79 != 8.94618e+116

//80 != 7.15695e+118

//81 != 5.79713e+120

//82 != 4.75364e+122

//83 != 3.94552e+124

//84 != 3.31424e+126

//85 != 2.8171e+128

//86 != 2.42271e+130

//87 != 2.10776e+132

//88 != 1.85483e+134

//89 != 1.6508e+136

//90 != 1.48572e+138

//91 != 1.352e+140

//92 != 1.24384e+142

//93 != 1.15677e+144

//94 != 1.08737e+146

//95 != 1.033e+148

//96 != 9.91678e+149

//97 != 9.61928e+151

//98 != 9.42689e+153

//99 != 9.33262e+155

//100 != 9.33262e+157

//请按任意键继续. . .

3. 编写一个要求用户输入数字的程序。每次输入后,程序都将报告到目前为止,所有输入的累计和。当用户输入0时,程序结束。

#include<iostream>

using namespace std;



int main()

{

    double n,sum = 0;

    do

    {

         cout << "Please enter the digital:";

         cin >> n;

         sum += n;

    } while (n != 0);

    cout << "Sum = " << sum << endl;

    system("pause");

    return 0;

}

//Please enter the digital : 10

//Please enter the digital : 20

//Please enter the digital : 3.0

//Please enter the digital : 0

//Sum = 33

//请按任意键继续. . .

4. Daphne以10%的单利投资了100美元。也就是说,每一年的利润都是投资额的10%,即每年10美元:

利息 = 0.10 X 原始存款

而CLeo以5%的复利投资了100美元。也就是说,利息是当前存款(包括获得的利息)的5%:

利息 = 0.05 X 当前存款

Cleo在第一年投资100美元的盈利是5%——得到了105美元。下一年的盈利是105美元的5%——即5.25美元,依此类推。请编写一个程序,计算多少年后,Cleo的投资价值才能超过Daphne的投资价值,并显示此时两个人的投资价值。

#include<iostream>

using namespace std;

const int DEPOSIT_BASE = 100;



int main()

{

    float daphne_deposit = DEPOSIT_BASE;

    float cleo_deposit = DEPOSIT_BASE;

    int year = 0;

    while (daphne_deposit >= cleo_deposit)

    {

         cout << "In " << year++ << " Year: Daphbe = " << daphne_deposit << endl;

         cout << "\tCleo = " << cleo_deposit << endl;

         daphne_deposit += 0.1 * DEPOSIT_BASE;//Daphne的投资每一年都是10美元的利润

         cleo_deposit += 0.05 * cleo_deposit;//Cleo的投资是每一年在前一年的基础上加上0.05*当前存款

    }

    cout << "In " << year << " Year: Daphne = " << daphne_deposit << endl;

    cout << "\tCleo = " << cleo_deposit << endl;

    system("pause");

    return 0;



}

//In 0 Year: Daphbe = 100

//Cleo = 100

//In 1 Year : Daphbe = 110

//Cleo = 105

//In 2 Year : Daphbe = 120

//Cleo = 110.25

//In 3 Year : Daphbe = 130

//Cleo = 115.762

//In 4 Year : Daphbe = 140

//Cleo = 121.551

//In 5 Year : Daphbe = 150

//Cleo = 127.628

//In 6 Year : Daphbe = 160

//Cleo = 134.01

//In 7 Year : Daphbe = 170

//Cleo = 140.71

//In 8 Year : Daphbe = 180

//Cleo = 147.746

//In 9 Year : Daphbe = 190

//Cleo = 155.133

//In 10 Year : Daphbe = 200

//Cleo = 162.889

//In 11 Year : Daphbe = 210

//Cleo = 171.034

//In 12 Year : Daphbe = 220

//Cleo = 179.586

//In 13 Year : Daphbe = 230

//Cleo = 188.565

//In 14 Year : Daphbe = 240

//Cleo = 197.993

//In 15 Year : Daphbe = 250

//Cleo = 207.893

//In 16 Year : Daphbe = 260

//Cleo = 218.287

//In 17 Year : Daphbe = 270

//Cleo = 229.202

//In 18 Year : Daphbe = 280

//Cleo = 240.662

//In 19 Year : Daphbe = 290

//Cleo = 252.695

//In 20 Year : Daphbe = 300

//Cleo = 265.33

//In 21 Year : Daphbe = 310

//Cleo = 278.596

//In 22 Year : Daphbe = 320

//Cleo = 292.526

//In 23 Year : Daphbe = 330

//Cleo = 307.152

//In 24 Year : Daphbe = 340

//Cleo = 322.51

//In 25 Year : Daphbe = 350

//Cleo = 338.635

//In 26 Year : Daphbe = 360

//Cleo = 355.567

//In 27 Year : Daphne = 370

//Cleo = 373.346

//请按任意键继续. . .

5. 假设要销售《C++ For Fools》一书。请编写一个程序,输入全年中每个月的销售量(图书数量,而不是销售额)。程序通过循环,使用初始化为月份字符串的char*数组(或string对象数组)逐月进行提示,并将输入的数据储存在一个int数组中。然后,程序计算数组中各元素的总数,并报告这一年的销售情况。

#include<iostream>

#include<string>

using namespace std;



int main()

{

    const string Month[] = { "JAN","FEB","MAR", "APR", "MAY", "JUN", "JULY", "AUG", "SEP", "OCT", "NOV", "DEC" };

    int sale_amount[12] = { };

    unsigned int sum = 0;

    for (int i = 0; i < 12; i++)

    {

         cout << "Please enter the sale amount of " << Month[i] << endl;

         cin >> sale_amount[i];

    }

    cout << "Input done!" << endl;

    for (int i = 0; i < 12; i++)

    {

         cout << Month[i] << " Sale: " << sale_amount[i] << endl;

         sum += sale_amount[i];

         cout << "Sale amount = " << sum << endl;

    }

    system("pause");

    return 0;



}



//Please enter the sale amount of JAN

//100

//Please enter the sale amount of FEB

//200

//Please enter the sale amount of MAR

//100

//Please enter the sale amount of APR

//200

//Please enter the sale amount of MAY

//100

//Please enter the sale amount of JUN

//200

//Please enter the sale amount of JULY

//100

//Please enter the sale amount of AUG

//200

//Please enter the sale amount of SEP

//100

//Please enter the sale amount of OCT

//200

//Please enter the sale amount of NOV

//100

//Please enter the sale amount of DEC

//200

//Input done!

//JAN Sale : 100

//Sale amount = 100

//FEB Sale : 200

//Sale amount = 300

//MAR Sale : 100

//Sale amount = 400

//APR Sale : 200

//Sale amount = 600

//MAY Sale : 100

//Sale amount = 700

//JUN Sale : 200

//Sale amount = 900

//JULY Sale : 100

//Sale amount = 1000

//AUG Sale : 200

//Sale amount = 1200

//SEP Sale : 100

//Sale amount = 1300

//OCT Sale : 200

//Sale amount = 1500

//NOV Sale : 100

//Sale amount = 1600

//DEC Sale : 200

//Sale amount = 1800

//请按任意键继续. . .

6. 完成编程练习5,但这一次使用一个二维数组来存储输入——3年中每个月的销售量。程序将报告每年销售量以及三年的总销售量。

#include<iostream>

 

#include<string>

using namespace std;

const int months = 12;

const int years = 3;



int main()

{

    const string Month[months] = { "JAN","FEB","MAR", "APR", "MAY", "JUN", "JULY", "AUG", "SEP", "OCT", "NOV", "DEC" };

    int sale_amount[years][months] = { };

    unsigned int sum = 0;

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

    {

         cout << "In " << i+1 << " year`s sale amount "<<endl;

         for (int j = 0; j < 12; j++)

         {

             cout << "Enter the sale amount of " << Month[j] << endl;

             cin >> sale_amount[i][j];

         }

         cout << "End of " << i + 1 << " year`s data." << endl;

    }

    cout << "Input done!" << endl;

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

    {

         for (int j = 0; j < 12; j++)

         {

             cout << Month[j] << " Sale: " << sale_amount[i][j] << endl;

             sum += sale_amount[i][j];

             cout << "Sale amount = " << sum << endl;

         }

    }

    system("pause");

    return 0;



}



//In 1 year`s sale amount

//Enter the sale amount of JAN

//100

//Enter the sale amount of FEB

//200

//Enter the sale amount of MAR

//100

//Enter the sale amount of APR

//200

//Enter the sale amount of MAY

//100

//Enter the sale amount of JUN

//200

//Enter the sale amount of JULY

//100

//Enter the sale amount of AUG

//200

//Enter the sale amount of SEP

//100

//Enter the sale amount of OCT

//200

//Enter the sale amount of NOV

//100

//Enter the sale amount of DEC

//

//100

//End of 1 year`s data.

//In 2 year`s sale amount

//Enter the sale amount of JAN

//123

//Enter the sale amount of FEB

//250

//Enter the sale amount of MAR

//100

//Enter the sale amount of APR

//200

//Enter the sale amount of MAY

//300

//Enter the sale amount of JUN

//100

//Enter the sale amount of JULY

//300

//Enter the sale amount of AUG

//500

//Enter the sale amount of SEP

//400

//Enter the sale amount of OCT

//100

//Enter the sale amount of NOV

//300

//Enter the sale amount of DEC

//200

//End of 2 year`s data.

//In 3 year`s sale amount

//Enter the sale amount of JAN

//100

//Enter the sale amount of FEB

//200

//Enter the sale amount of MAR

//50

//Enter the sale amount of APR

//300

//Enter the sale amount of MAY

//400

//Enter the sale amount of JUN

//600

//Enter the sale amount of JULY

//123

//Enter the sale amount of AUG

//120

//Enter the sale amount of SEP

//130

//Enter the sale amount of OCT

//150

//Enter the sale amount of NOV

//300

//Enter the sale amount of DEC

//198

//End of 3 year`s data.

//Input done!

//JAN Sale : 100

//Sale amount = 100

//FEB Sale : 200

//Sale amount = 300

//MAR Sale : 100

//Sale amount = 400

//APR Sale : 200

//Sale amount = 600

//MAY Sale : 100

//Sale amount = 700

//JUN Sale : 200

//Sale amount = 900

//JULY Sale : 100

//Sale amount = 1000

//AUG Sale : 200

//Sale amount = 1200

//SEP Sale : 100

//Sale amount = 1300

//OCT Sale : 200

//Sale amount = 1500

//NOV Sale : 100

//Sale amount = 1600

//DEC Sale : 100

//Sale amount = 1700

//JAN Sale : 123

//Sale amount = 1823

//FEB Sale : 250

//Sale amount = 2073

//MAR Sale : 100

//Sale amount = 2173

//APR Sale : 200

//Sale amount = 2373

//MAY Sale : 300

//Sale amount = 2673

//JUN Sale : 100

//Sale amount = 2773

//JULY Sale : 300

//Sale amount = 3073

//AUG Sale : 500

//Sale amount = 3573

//SEP Sale : 400

//Sale amount = 3973

//OCT Sale : 100

//Sale amount = 4073

//NOV Sale : 300

//Sale amount = 4373

//DEC Sale : 200

//Sale amount = 4573

//JAN Sale : 100

//Sale amount = 4673

//FEB Sale : 200

//Sale amount = 4873

//MAR Sale : 50

//Sale amount = 4923

//APR Sale : 300

//Sale amount = 5223

//MAY Sale : 400

//Sale amount = 5623

//JUN Sale : 600

//Sale amount = 6223

//JULY Sale : 123

//Sale amount = 6346

//AUG Sale : 120

//Sale amount = 6466

//SEP Sale : 130

//Sale amount = 6596

//OCT Sale : 150

//Sale amount = 6746

//NOV Sale : 300

//Sale amount = 7046

//DEC Sale : 198

//Sale amount = 7244

//请按任意键继续. . .

7. 设计一个名为car的结构,用它存储下述有关汽车的信息:生产商(存储在字符数组或string对象中的字符串)、生产年份(整数)。编写一个程序,向用户询问有多少辆汽车。随后,程序使用new来创建一个由相应数量的car结构组成的动态数组。接下来,程序提示用户输入每辆车的生产商(可能是由多个单词组成)和年份信息。请注意,这需要特别小心,因为它将交替读取数值和字符串(参见第4章)。最后,程序将显示每个结构的内容。该程序的运行情况如下:

How many cars do you wish to catalog? 2

car #1:

Please enter the make: Hudson Hornet

Please enter the year made: 1952

Car #2:

Please enter the make: Kaiser

Please enter the year made: 1951

Here is your collection:

1952 Hudson Hornet

1951 Kaiser

 

#include<iostream>

#include<string>

using namespace std;



struct car_information

{

    string manufacturer;

    int date;

};

int main()

{

    int car_number;

    car_information *pcar;

    cout << "How many cars do you wish to catlog?";

    cin >> car_number;

    cin.get();

    pcar = new car_information[car_number];

    for (int i = 0; i < car_number; i++)

    {

         cout << "Car # " << i + 1 << ":" << endl;

         cout << "Please enter the make:";

         getline(cin, pcar[i].manufacturer);

         cout << "Please enter the year made:";

         cin >> pcar[i].date;

         cin.get();

    }

    cout << "Here is your collection:" << endl;

    for (int i = 0; i < car_number; i++)

    {

         cout << pcar[i].date << " " << pcar[i].manufacturer << endl;

    }

    system("pause");

    return 0;

}



//How many cars do you wish to catlog ? 2

//Car # 1:

//Please enter the make : Hodson Hornet

//Please enter the year made : 1952

//Car # 2:

//Please enter the make : Kaiser

//Please enter the year made : 1951

//Here is your collection :

//1952 Hodson Hornet

//1951 Kaiser

//请按任意键继续. . .

8. 编写一个程序,它使用一个char数组和循环来每次读取一个单词,直到用户输入done为止。随后,该程序指出用户输入了多少个单词(不包括done在内)。下面是该程序的运行情况:

Enter words (to tstop, type the word done):

anteater birthday category dumpster

envy finagle geometry done for sure

You entered a total of 7 words.

您应在程序中包含头文件cstring,并使用函数strcmp()来进行比较测试。

 

#include<iostream>

#include<cstring>

using namespace std;



const int SIZE = 20;

const char FINISHEED[] = "done";



int main()

{

    int counter = 0;

    char words[SIZE];

    cout << "Enter words(to stop,type the word done):" << endl;

    while (strcmp(FINISHEED, words) != 0)

    {

         counter++;

         cin >> words;

         cin.get();

    }

    cout << "You entered a total of " << counter - 1 << " words." << endl;

    system("pause");

    return 0;

}



//Enter words(to stop,type the word done):

//anteater birthday category dumpster envy finagle geometry done for sure

//You entered a total of 7 words.

//请按任意键继续. . .

9. 编写一个满足前一个练习中描述的程序,但使用string对象而不是字符数组。请在程序中包含头文件string,并使用关系运算符来进行比较测试。

 

#include<iostream>

#include<string>

using namespace std;



const char FINISHEED[] = "done";



int main()

{

    int counter = 0;

    string words;

    cout << "Enter words(to stop,type the word done):" << endl;

    while (words != FINISHEED)

    {

         counter++;

         cin >> words;

         cin.get();

    }

    cout << "You entered a total of " << counter - 1 << " words." << endl;

    system("pause");

    return 0;

}



//Enter words(to stop,type the word done):

//anteater birthday category dumpster envy finagle geometry done for sure

//You entered a total of 7 words.

//请按任意键继续. . .

10. 编写一个使用嵌套循环的程序,要求用户输入一个值,指出要显示多少行。然后,程序将显示相应行数的星号,其中第一行包括一个星号,第二行包括两个星号,依此类推。每一行包含的字符数等于用户指定的行数,在星号不够的情况下,在星号前面加上句点。该程序的运行情况如下:

Enter number of rows: 5

....*

...**

..***

.****

**** 

 

#include<iostream>

using namespace std;



int main()

{

    int line;

    cout << "Enter number of words:";

    cin >> line;

    for (int i = 0; i < line; i++)

    {

         for (int j = 0; j < line - i - 1; j++)

         {

             cout << " .";

         }

         for (int j = 0; j < i; j++)

         {

             cout << " *";

         }

         cout << endl;

    }

    system("pause");

    return 0;

}

//Enter number of words : 5

//. . . .

//. . . *

//. . * *

//. * * *

//* * * *

//请按任意键继续. . .



//Enter number of words : 10

//. . . . . . . . .

//. . . . . . . . *

//. . . . . . . * *

//. . . . . . * * *

//. . . . . * * * *

//. . . . * * * * *

//. . . * * * * * *

//. . * * * * * * *

//. * * * * * * * *

//* * * * * * * * *

//请按任意键继续. . .

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

长沙有肥鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值