《C++ Primer Plus》第五章习题与参考答案

1,内容选自《C++ Primer Plus》(第6版)中文版,2017年1月河北第21次印刷版本
2,文章系笔者学习笔记,若有错误,欢迎指正
3,如有雷同,纯属巧合

5.8 复习题

1.入口条件循环和出口条件循环之间的区别是什么?各种c++循环分别属于其中的哪一种?

入口条件循环在进入入口循环体之前将评估测试表达式。如果条件最初为false,则循环不会执行其循环体。出口条件循环在处理循环体之后评估测试表达式。因此,即使测试表达式最初为false,循环也将执行一次,for和wihle循环都是入口条件循环,而do while是出口条件循环。

2.如果下面的代码片段是有效程序的组成部分,它将打印什么内容?

int i;
for (i = 0; i < 5; i++)
    cout << i;
cout << endl;

它将打印下面的内容:
01234

3.如果下面的代码片段是有效程序的组成部分,它将打印什么内容?

int j;
    for (j = 0; j < 11; j += 3)
        cout << j;
cout << endl << j << endl;

它将打印下面的内容:
0369
12

4.如果下面代码是有效程序的组成部分,它将打印什么内容?

int j = 5;
    while (++j < 9)
        cout << j++ << endl;

它将打印下面的内容:
6
8

5.如果下面代码是有效程序的组成部分,它将打印什么内容?

int k = 8;
    do
        cout << "k=" << k << endl;
    while (k++ < 5);   

它将打印下面的内容:
k=8

6.编写一个打印1、2、4、8、16、32、64的for循环,每轮循环都将计数变量的值乘以2。

for (int i = 1; i <= 64; i *= 2)
{
    cout << i << endl;
}

7.如何在循环体中包括多条语句?

将语句放在一对大括号中将形成一个复合语句或代码块。

8.下面的语句是否有效?如果无效,原因是什么?如果有效,它将完成什么工作?

int x={1,024};

下面的语句又如何呢?

int y;
y = 1.024;

当然,第一条语句是有效的,表达式1,024由两个表达式组成——1和024,用逗号运算符链接。
值为右侧表达式的值。这是024,八进制为20,因此该表达式将20赋值给x。
第二条语句,运算符优先级将导致它被判定成这样:
(y=1),024;
也就是说,左侧表达式将y设置成1,整个表达式的值(没有使用)为024或20(八进制)。

9.在查看输入方面,cin>>ch同cin.get(ch)和ch=cin.get()有什么不同?

cin>>ch将跳过空格、换行符和制表符,其他两种格式将读取这些字符。

5.9 编程练习

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

#include <iostream>
using namespace std;

int main()
{
    int number1, number2;
    int sum = 0;
    cout << "please input two integers,the first is a smaller integer:";
    cin >> number1 >> number2;
    for (int i = number1; i <= number2; i++)
    {
        sum += i;
    }
    cout << "sum:" << sum << endl;
    return 0;
}

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

#include <iostream>
#include <array>
using namespace std;
const int ArSize = 101;

int main()
{
    array<long double, ArSize> ald;
    ald[1] = ald[0] = 1;
    for (int i = 2; i < ArSize; i++)
    {
        ald[i] = i * ald[i - 1];
    }
    for(int i=0;i<ArSize;i++){
        cout << i << "!=" << ald[i] << endl;
    }
    return 0;
}

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

#include <iostream>
using namespace std;

int main()
{
    cout << "please input numbers,until 0:" << endl;
    int number, sum=0;
    do
    {
        cin >> number;
        sum += number;
        cout << "sum of all inputs:" << sum << endl;
    } while (number != 0);
    return 0;
}

4.Daphne以10%的单利投资了100美元。也就是说,每一年的利润都是投资额的10%,即每年10美元:利息=0.10*原始存款。而Cleo在第一年投资100美元的盈利是5%–一共得到105美元。下一年的盈利是105美元的5%–即5.25美元,以此类推。请编写一个程序,计算多少年后,Cleo的投资价值超过Daphne的投资价值。并显示此时两个人的投资价值。

#include <iostream>
using namespace std;

int main()
{
    int year = 0;
    double Daphne = 100, Cleo = 100;
    while(Daphne >= Cleo)
    {
        Daphne += 10;
        Cleo *= 1.05;
        year++;
    }
    cout << year << " years later," << "Cleo>Daphne" << endl;
    cout << "Dephne:" << Daphne << endl;
    cout << "Cleo:" << Cleo << endl;
    return 0;
}

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

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string month[12] = {"january", "February", "March", "April", "May", "June",
                        "July", "August", "Septemper", "October", "November", "December"};
    int sales[12], sum = 0;
    for (int i = 0; i < 12; i++)
    {
        cout << "please input the sales of " << month[i] << ": ";
        cin >> sales[i];
        cout << endl;
        sum += sales[i];
    }
    cout << "the sales of whole year is: " << sum;
    return 0;
}

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

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string year[3] = {"The First Year", "The Second Year;", "The Third Year;"};
    string month[12] = {"january", "February", "March", "April", "May", "June",
                        "July", "August", "Septemper", "October", "November", "December"};
    int sales[12][3];
    int sums[3] ={};
    for (int i = 0; i < 3; i++)
    {
        cout << "sales of " << year[i] << endl;
        for (int j = 0; j < 12; j++)
        {
            cout << "please input the sales of " << month[j] << ": ";
            cin >> sales[j][i];
            cout << endl;
            sums[i] += sales[j][i];
        }
        cout << "the sales of " << year[i] << " is: " << sums[i] << endl;
    }
    cout << "the sales of three years is: " << sums[0]+sums[1]+sums[2] << endl;
    return 0;
}

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

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
{
    string make;
    int year;
};

int main()
{
    cout << "How many cars do you wish to catalog?";
    int carNum;
    cin >> carNum;
    cin.get();
    car *ps = new car[carNum];
    for (int i = 0; i < carNum; i++)
    {
        cout << "Car #" << i + 1 << ":" << endl;
        cout << "PLease enter the make:";
        getline(cin, ps[i].make);
        cout << "PLease enter the year made:";
        cin >> ps[i].year;
        cin.get();
    }
    cout << "Here is your collection:" << endl;
    for (int i = 0; i < carNum; i++)
    {
        cout << ps[i].year << " " << ps[i].make << endl;
    }
    delete[] ps;
    return 0;
}

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

Enter words(to stop,type the word done):
anteater birthday category dumpster
envy finagle geometry done for sure
You entered a total of 7words.

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

#include <iostream>
#include <cstring>
using namespace std;

int main()
{
    cout << "Enter words(to stop,type the word done):" << endl;
    int num = 0;
    char word[25];
    cin >> word;
    while (strcmp(word, "done") != 0)
    {
        num++;
        cin >> word;
    }
    cout << "You entered a total of " << num << " words." << endl;
    return 0;
}

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

#include <iostream>
#include <string>
using namespace std;

int main()
{
    cout << "Enter words(to stop,type the word done):" << endl;
    int num = 0;
    string word;
    cin >> word;
    while (word != "done")
    {
        num++;
        cin >> word;
    }
    cout << "You entered a total of " << num << " words." << endl;
    return 0;
}

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

Enter number of row : 5
....*
...**
..***
.****
*****
#include <iostream>
using namespace std;

int main()
{
    cout << "Enter number of row:";
    int rows;
    cin >> rows;
    for (int i = 1; i <= rows; i++)
    {
        for (int j = 1; j <= rows - i; j++)
        {
            cout << ".";
        }
        for (int k = 0; k < i; k++)
        {
            cout << "*";
        }
        cout << endl;
    }
    return 0;
}
发布了8 篇原创文章 · 获赞 1 · 访问量 686

猜你喜欢

转载自blog.csdn.net/Heisenberg_Li/article/details/102741933