1. 程式人生 > >《C++ primer plus 英文版 第六版》Chapter 5

《C++ primer plus 英文版 第六版》Chapter 5

ota ostream 英文 do while numbers primer eval esp ram

Chapter Review

1
An entry-condition loop evaluates a test expression before entering the body of the loop. If the condition is initially false, the loop never executes its body. An exit-condition loop evaluates a test expression after processing the body of the loop. Thus, the loop body is executed once, even if the test expression is initially false

. The for and while loops are entry-condition loops, and the do while loop is an exit-condition loop.
2
It would print the following:

01234
    

Not that cout << endl; is not part of the loop body (because there are no braces).

3
It would print the following:

0369
    
12
    

4
It would print the following:

6
    
8
    

5
It would print the following:

k = 8;

6
It‘s simplest to use the *= operator:

for (int num = 1; num <= 64; num *= 2)
    cout << num << " ";

7
You enclose the statements within paired braces to form a single compound statement, or block.
8
Yes, the first statement is valid. The expression 1,024 consists of two expressions — 1 and 024 — joined by a comma operator. The value of the right-hand expression. This is 024, which is octal for 20, so the declaration assigns the value 20

to x. The second statement is also valid. However, operator precedence causes it to be evaluated as follows:
(y = 1), 024;
That is, the left expression sets y to 1, and the value of the entire expression, which isn‘t used, is 024, or 20.
9
The cin >> ch form skips over spaces, newlines, and tabs when it encounters them. The other two forms read those characters.

Programming Exercises

1

#include <iostream>

int main()
{
    using namespace std;
    
    int n1, n2;
    int sum = 0;
    
    cout << "Input two integer numbers (example: 2 9): ";
    cin >> n1;
    cin >> n2;
    
    for (int i = n1; i <= n2; ++i)
    {
        sum += i;
    }
    cout << "sum = " << sum << endl;
    
    return 0;
}

2

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

int main()
{
    using namespace std;
    
    array<long double, ArSize> factorials;
    factorials[1] = factorials[0] = 1.0L;
    
    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;
    }
    
    return 0;
}

3

#include <iostream>

int main()
{
    using namespace std;
    
    double x;
    double sum = 0.0;
    
    cin >> x;
    while (x != 0.0)
    {
        sum += x;
        cin >> x;
    }
    cout << "sum = " << sum << endl;
    
    return 0;
}

4

#include <iostream>

int main()
{
    using namespace std;
    
    double d, c;
    d = c = 100.0;
   
    int i;
    for (i = 0; d >= c; ++i)
    {
        d += 0.1 * 100.0;
        c *= 1.05;
    }
    
    cout << i << " year(s)\n";
    cout << "Daphne: " << d << endl;
    cout << "Cleo: " << c << endl;
    
    return 0;
}

5

#include <iostream>

const char * const Months[12] =
{
    "January",
    "February",
    "March",
    "April",
    "May",
    "June",
    "July",
    "August",
    "September",
    "October",
    "November",
    "December"
};

int main()
{
    using namespace std;
    
    int volumes[12];
    int sum = 0;
    
    for (int i = 0; i < 12; ++i)
    {
        cout << "Enter the sales volume of " << Months[i] << ": ";
        cin >> volumes[i];
    }
    
    for (int i = 0; i < 12; ++i)
        sum += volumes[i];
    
    cout << "Sum = " << sum << endl;
    
    return 0;
}

6

#include <iostream>

const char * const Months[12] =
{
    "January",
    "February",
    "March",
    "April",
    "May",
    "June",
    "July",
    "August",
    "September",
    "October",
    "November",
    "December"
};

int main()
{
    using namespace std;
    
    int volumes[3][12];
    int sum = 0;
    int total = 0;
    
    for (int i = 0; i < 3; ++i)
    {
        cout << "Enter the sales volumes of year: " << i + 1 << endl << endl;
        for (int j = 0; j < 12; ++j)
        {
            cout << "Enter the sales volumes of " << Months[j] << ": ";
            cin >> volumes[i][j];
        }
        
    }
    
    for (int i = 0; i < 3; ++i)
    {
        for (int j = 0; j < 12; ++j)
        {
            sum += volumes[i][j];
        }
        cout << "Sales volume of year " << i + 1 << " is " << sum << endl;
        total += sum;
        sum = 0;
    }
    cout << "Sales volumes of 3 years are: " << total << endl;
    
    return 0;
}

7

#include <iostream>

struct car
{
    char make[40];
    int year;
};

int main()
{
    using namespace std;
    
    int num;
    car * cars;
    
    cout << "How many cars do you wish to catalog: ";
    cin >> num;
    cars = new car[num];
    
    for (int i = 0; i < num; i++)
    {
        cout << "Car #" << i + 1 << ":\n";
        cout << "Please enter the make: ";
        //cin >> cars[i].make;
        cin.getline(cars[i].make, 40);
        cin.get();
        cout << "Please enter the year made: ";
        cin >> cars[i].year;
    }
    
    cout << "Here is your collection:\n";
    for (int i = 0; i < num; ++i)
        cout << cars[i].year << " " << cars[i].make << endl;
    
    delete [] cars;
    
    return 0;
}

8

#include <iostream>
#include <cstring>

int main()
{
    using namespace std;
    
    char word[20];
    int count = 0;
    
    cout << "Enter word (to stop, type the word done):\n";
    cin >> word;
    while (strcmp(word, "done"))
    {
        ++count;
        cin >> word;
    }
    cout << "You entered a total of " << count << " words.\n";
    
    return 0;
}

9

#include <iostream>
#include <string>

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

10

#include <iostream>

int main()
{
    using namespace std;
    
    cout << "Enter number of rows: ";
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) // row
    {
        for (int j = 0; j < n; ++j) // column
            if (j < (n - (i + 1)))
                cout << ".";
            else
                cout << "*";
        
        cout << endl;
    }
    
    return 0;
}

《C++ primer plus 英文版 第六版》Chapter 5