c++primer第五章编程练习答案

题一

#include <iostream>
using namespace std;
int main()
{
    // test_1
    int a, b, sum = 0;
    cout << "Enter two numbers: " << endl;
    cout << "Enter the smaller number:\n";
    cin >> a;
    cout << "Enter the bigger number: \n";
    cin >> b;
    for (int i = a; i <= b; i++)
        sum = sum + i;
    cout << "Sum: " << sum << endl;
    return 0;
}

题二

#include <iostream>
using namespace std;
int main()
{
    //     // test2
    int n, sum;
    do
    {
        cout << "Enter the number: ";
        cin >> n;
        sum = sum + n;
        cout << "Sum of input numbers: " << sum << endl;
    } while (n);
    return 0;
}

题三

#include <iostream>
using namespace std;
int main()
{
    //     // 5.3
    int a, b;
    a = b = 100;
    int i = 1;
    for (; b <= a; i++)
    {
        a += 10;
        b *= (1 + 0.05);
    }

    cout << "After " << i << "years, \n";
    cout << "Daphne owns " << a << ", Cleo owns " << b << endl;
    return 0;
}

题四

#include <iostream>
using namespace std;
int main()
{
    // 5.4

    const int Month = 12;
    int sales, sum = 0;
    const char *months[Month] =
        {
            "January",
            "February",
            "March",
            "April",
            "May",
            "June",
            "July",
            "August",
            "September",
            "October",
            "November",
            "December"};
    int month_sales[Month];
    for (int i = 0; i < Month; i++)
    {
        cout << "The sale of " << months[i] << " is ";
        cin >> sales;
        month_sales[i] = sales;
        // cout << i << endl;
    }
    for (int j = 0; j < Month; ++j)
        sum += month_sales[j];
    cout << "Sum of all sales is " << sum << endl;
    return 0;
}

题五


#include <iostream>
using namespace std;
int main()
{
    const int Month = 12;
    const char *months[Month] =
        {
            "January",
            "February",
            "March",
            "April",
            "May",
            "June",
            "July",
            "August",
            "September",
            "October",
            "November",
            "December"};
    const char Years[3][25] =
        {
            "First year",
            "Second year",
            "Third year"};
    int sum = 0;
    int sale[3][Month];
    for (int i = 0; i < 3; i++)
    {
        int temp = 0;
        for (int j = 0; j < Month; j++)
        {
            cout << " For the " << Years[i] << ", the sale of ";
            cout << months[j] << " is ";
            cin >> sale[i][j];
            sum += sale[i][j];
            temp += sale[i][j];
        }
        cout << "The " << i + 1 << " year" << temp << endl;
    }
    cout << "Sum of three years is " << sum << endl;
    return 0;
}

题六

#include <iostream>
#include <string>
using namespace std;
struct car
{
    string manufacturer;
    int year;
};
int main()
{
    cout << "How many cars do you wish to catalog? ";
    int car_number;
    cin >> car_number;
    car *car_array = new car[car_number];
    for (int i = 0; i < car_number; i++)
    {
        cout << "Car #" << i + 1 << " : \n";
        cout << "Please enter the make: ";
        cin.get();                               // 需要忽略前面的换行符导致的影响;
        getline(cin, car_array[i].manufacturer); // cin 输入多个单词会出现问题
        cout << "Please enter the year made:";
        cin >> car_array[i].year;
    }
    cout << "Here is your collection:\n";
    for (int i = 0; i < car_number; i++)
    {
        cout << car_array[i].year << " " << car_array[i].manufacturer << endl;
    }
    return 0;
}

题七

#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main()
{
    // 5.7 用char 数组作为比较表达式
    char words[20];
    // string words;
    int i = 0;
    cout << "Enter words (to stop, type the word done): \n";
    do
    {
        cin >> words;
        i++;
    } while (strcmp(words, "done"));
    cout << "You entered a total of " << i - 1 << " words.";
    return 0;
}

 

题八

#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main()
{
    // 5.7 用char 数组作为比较表达式
    // char words[20];
    string words;
    int i = 0;
    cout << "Enter words (to stop, type the word done): \n";
    do
    {
        cin >> words;
        i++;
    } while (words != "done");
    cout << "You entered a total of " << i - 1 << " words.";
    return 0;
}

 

 题九

 

include <iostream>
using namespace std;
int main()
{
    // 5.9
    cout << "Enter number of rows: ";
    int num_rows;
    cin >> num_rows;
    for (int i = 1; i <= num_rows; i++)
    {
        for (int j = 1; j <= num_rows - i; j++)
            cout << ".";
        for (int j = 5; j > num_rows - i; j--)
            cout << "*";
        cout << endl;
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值