C++ Primer Plus第六版 编程练习第五章

1.

#include <iostream>

int main()
{
    using namespace std;
    cout << "输入两个整数: ";
    int a,b;
    cin >> a;
    cin >> b;
    long sum = 0;
    for (int i = a; i <= b; i++)
        sum += i;
    cout << "两数之间的和为: " << sum << endl;
    return 0;
}

2.

#include <iostream>
#include <array>

const int ArSize = 101;

int main()
{
    using namespace std;

    array<long double, ArSize> factorials;
    factorials[0] = factorials[1] = 1.0;

    for (int i = 2; i < ArSize; i++)
        factorials[i] = (long double)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;
    cout << "输入一个数字(0结束): ";
    int n, sum = 0;
    cin >> n;
    while (n != 0)
    {
        sum += n;
        cout << "Sum: " << sum << endl;
        cout << "输入一个数字(0结束): ";
        cin >> n;
    }
    cout << "Bye";
    return 0;
}

4.

#include <iostream>

int main()
{
    using namespace std;
    double daphne = 100.0;
    double cleo = 100.0;
    int year;
    for (year = 1; cleo <= daphne; year++)
    {
        daphne += 10.0;
        cleo *= 1.05;
    }
    cout << year - 1 << " 年之后超过, " << endl;
    cout << "Daphne 有 $" << daphne << endl;
    cout << "Cleo 有 $" << cleo << endl;
    return 0;
}

5.

#include <iostream>
#include <string>

int main()
{
    using namespace std;
    string months[12] = {
        "一月", "二月", "三月", "四月", "五月", "六月",
        "七月", "八月", "九月", "十月", "十一月","十二月"
    };
    int sales[12];
    for (int i = 0; i < 12; ++i)
    {
        cout << "输入" << months[i] << "销售量: ";
        cin >> sales[i];
    }
    int sum = 0;
    for (int i = 0; i < 12; ++i)
    {
        cout << months[i] << ": " << sales[i] << endl;
        sum += sales[i];
    }
    cout << "今年销售量为: " << sum << endl;
    return 0;
}

6.

int main()
{
    using namespace std;
    string months[12] = {
        "一月", "二月", "三月", "四月", "五月", "六月",
        "七月", "八月", "九月", "十月", "十一月","十二月"
    };
    int sales[3][12];
    for (int y = 0; y < 3; ++y)
    {
        cout  << endl;
        for (int m = 0; m < 12; ++m)
        {
            cout << "输入第" << y+1 << "年" << months[m] << "销售量: ";
            cin >> sales[y][m];
        }
    }
    int sum = 0;
    for (int y = 0; y < 3; ++y)
    {
        cout << "第 " << y << "年: " << endl;
        for (int m = 0; m < 12; ++m)
        {
            cout << months[m] << ": " << sales[y][m] << endl;
            sum += sales[y][m];
        }
    }
    cout << "今年销售量为:" << sum << endl;
    return 0;
}

7.

#include <iostream>
#include <string>

struct Car {
    std::string producer;
    int year;
};

int main()
{
    using namespace std;

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

8.

#include <iostream>
#include <cstring>

const int StrLen = 20;

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

9.

#include <iostream>

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

10.

#include <iostream>

int main()
{
    using namespace std;
    cout << "Enter number of rows: ";
    int nrow;
    cin >> nrow;
    for (int i = 0; i < nrow; ++i)
    {
        for (int j = 0; j < nrow; ++j)
        {
            if (nrow - j > i + 1)
                cout << ".";
            else
                cout << "*";
        }
        cout << endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值