C++ Primer Plus(第6版)Chapter 5 编程题答案

C++ Primer Plus(第6版)Chapter 5 编程题答案

第1题:

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

// task 1
int main()
{
    int i, j;
    int sum = 0;
    cout << "Enter two int: ";
    cin >> i;
    cin >> j;
    for (int x = i; x <= j; x++)
    {
        sum += x;
    }
    cout << "The sum of range(" << i << ", " << j << ") is " << sum << endl;
    cin.get();
    cin.get();
    return 0;
}

第2题:

// task 2
int main()
{
    const int size = 101;
    array<long double, size> lda = {0};
    lda[1] = lda[0] = 1;
    for (int i = 2; i < size; i++)
    {
        lda[i] = i*lda[i - 1];
    }
    cout << "100! = " << lda[100] << endl;
    cin.get();
    cin.get();
    return 0;
}

第3题:

// task 3
int main()
{
    int num;
    int sum = 0;
    cout << "Enter the numbers: ";
    cin >> num;
    while (num)
    {
        sum += num;
        cout << "Now sum is " << sum << " continue: ";  //每次输入都显示目前为止所有输入的累积和
        cin >> num;
    }
    cin.get();
    cin.get();
    return 0;
}

第4题:

// task 4
int main()
{
    double d, c;
    d = c = 100;
    int years = 0;
    while (c <= d)
    {
        d += 10;
        c = c + c*0.05;
        years++;
    }
    cout << years << " years later, Now d is " << d << " and c is " << c << endl;
    cin.get();
    cin.get();
    return 0;
}

第5题:

// task 5
int main()
{
    int sales[12];
    int sum = 0;
    string months[12] = {
        "Jan",
        "Feb",
        "Mar",
        "Apr",
        "May",
        "Jun",
        "Jul",
        "Aug",
        "Sep",
        "Oct",
        "Nov",
        "Dec"
    };
    cout << "Enter the sale of 12 months: ";
    for (int i = 0; i < 12; i++)
    {
        cin >> sales[i];
        cout << "The sale of month " << months[i] << " is " << sales[i] << endl;
        sum += sales[i];
    }
    cout << "The total sales of the year is " << sum << endl;
    cin.get();
    cin.get();
    return 0;
}

第6题:

// task 6
int main()
{
    int sales[3][12];
    int i, j, sum1, sum2;
    sum1 = sum2 = 0;
    cout << "Enter the sale of months of 3 years: ";
    for (i = 0; i < 3; i++)
    {
        for (j = 0; j < 12; j++)
        {
            cin >> sales[i][j];
            sum2 += sales[i][j];
        }
        sum1 += sum2;
        cout << "The " << i + 1 << "st year sales is " << sum2 << endl;
        sum2 = 0;
    }
    cout << "The sale of 3 years is " << sum1 << endl;

    cin.get();
    cin.get();
    return 0;
}

第7题:

// task 7
struct car
{
    char maker[20];
    int year;
};
int main()
{
    int num;
    cout << "How many cars do you wish to catalog? ";
    cin >> num;
    car* pt = new car[num];
    for (int i = 1; i <= num; i++)
    {

        cin.get();  //吃掉之前那个换行
        cout << "Car #" << i << endl;
        cout << "Please enter the make: ";
        cin.getline((pt + (i - 1))->maker, 20);
        cout << "Please enter the year made: ";
        cin >> (pt + (i - 1))->year;

    }
    cout << "Here is your collection:\n";
    for (int i = 0; i < num; i++)
    {
        cout << (pt + i)->year << "\t" << (pt + i)->maker << endl;
    }
    delete[] pt;  //别忘了释放分配的堆内存
    cin.get();
    cin.get();
    return 0;
}

第8题:

// task 8
int main()
{
    const int size = 10;
    char word[size];
    int count = 0;
    cout << "Enter words (to stop, type the word done):\n";
    cin >> word;
    while (strcmp(word, "done"))
    {
        count++;

        cin >> word;
    }
    cout << "You entered a total of " << count << " words" << endl;

    cin.get();
    cin.get();
    return 0;
}

第9题:

// task 9
int main()
{
    string word;
    int count = 0;
    cout << "Enter words (to stop, type the word done):\n";
    cin >> word;
    while (word != "done")
    {
        count++;

        cin >> word;
    }
    cout << "You entered a total of " << count << " words" << endl;

    cin.get();
    cin.get();
    return 0;
}

第10题:

// task 10
int main()
{
    int lines;
    cout << "Enter the num of the lines: ";
    cin >> lines;
    for (int i = 1; i <= lines; i++)
    {
        for (int j = 1; j <= lines - i; j++)
        {
            cout << ".";
        }
        for (int m = 1; m <= i; m++)
        {
            cout << "*";
        }
        cout << endl;
    }

    cin.get();
    cin.get();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值