5.9 编程练习

在这里插入图片描述
在这里插入图片描述

#include <iostream>

int main()
{
    using namespace std;

    int a, b;

    cout << "Please enter the first integer:";
    cin >> a;
    cout << "Please enter the second integer:";
    cin >> b;

    int s = 0;
    while (a <= b)
    {
        s += a;
        ++a;
    }

    cout << "The sum of them is " << s << endl;

    return 0;
}

在这里插入图片描述

#include <iostream>

int main()
{
    using namespace std;

    float a;

    cout << "Please enter the first number:";
    cin >> a;

    float s = 0;
    while (a != 0)
    {
        s += a;
        cout << "The sum of numbers until now: " << s << endl;
        cout << "Please enter the next number:";
        cin >> a;
    }

    cout << "The sum of numbers until now: " << s << endl;

    return 0;
}

在这里插入图片描述

#include <iostream>

int main()
{
    using namespace std;

    const float a_D = 0.1;
    const float a_C = 0.05;

    float d = 100;
    float c = 100;

    int year = 0;

    while (c <= d)
    {
        c = (1+a_C) * c;
        d += (1+a_D) * 100;
        ++year;
    }

    cout << "The years are : " << year << endl;

    return 0;
}

在这里插入图片描述

#include <iostream>
#include <string>

int main()
{
    using namespace std;

    const int Month = 12;

    const string month[Month] =
            {
            "January",
            "February",
            "March",
            "April",
            "May",
            "June",
            "July",
            "August",
            "September",
            "October",
            "November",
            "December"
            };

    int sale[Month] = {0};
    int s = 0;

    cout << "Please enter the sales of 12 months.\n";
    for (int i=0; i < Month; ++i)
    {
        cout << month[i] << ":";
        cin >> sale[i];
        s += sale[i];
    }

    cout << "The sum of sales are : " << s << endl;

    return 0;
}

在这里插入图片描述

#include <iostream>
#include <string>

int main()
{
    using namespace std;

    const int Month = 12;

    const string month[Month] =
            {
            "January",
            "February",
            "March",
            "April",
            "May",
            "June",
            "July",
            "August",
            "September",
            "October",
            "November",
            "December"
            };

    int sale[3][Month] = {0};
    int sum = 0;


    for (int j = 0; j < 3; ++j) {
        int s = 0;
        cout << j << " Please enter the sales of 12 months.\n";
        for (int i = 0; i < Month; ++i) {
            cout << month[i] << ":";
            cin >> sale[j][i];
            s += sale[j][i];
        }
        cout << j << " The sum of sales are : " << s << endl;
        sum += s;
    }
    cout << "The sum of sales of the 3 years are : " << sum << endl;

    return 0;
}

在这里插入图片描述

  1. [1]
#include <iostream>
#include <string>

int main()
{
    using namespace std;

    struct Car
    {
        string brand;
        int year;
    };

    int n = 0;
    cout << "How many cars do you wish to catalog?";
    cin >> n;

    Car * ps = new Car[n];

    int i = 0;
    while (i < n)
    {
        cout << "Car #" << i+1 << ":\n";
        cout << "Please enter the make:";
        cin.get();
        getline(cin, ps[i].brand);      // [1]

        cout << "Please enter the year made:";
        cin >> ps[i].year;

        ++i;
    }

    cout << "Here is your collection:\n";
    cout << ps[0].year << " " << ps[0].brand << endl;
    cout << ps[1].year << " " << ps[1].brand << endl;

    return 0;
}

在这里插入图片描述

7.

#include <iostream>
#include <cstring>

int main()
{
    using namespace std;

    const int ArSize = 100;

    char str[ArSize];

    cout << "Enter words (to stop, type the word done):\n";
    cin >> str;

    int i = 0;
    while (strcmp(str, "done"))
    {
        cin >> str;
        ++i;
    }

    cout << "You entered a total of " << i << " words.\n";

    return 0;
}

在这里插入图片描述

8.

#include <iostream>
#include <string>


int main()
{
    using namespace std;

    string str;

    cout << "Enter words (to stop, type the word done):\n";
    cin >> str;

    int i = 0;
    while (str != "done")
    {
        cin >> str;
        ++i;
    }

    cout << "You entered a total of " << i << " words.\n";

    return 0;
}

在这里插入图片描述

#include <iostream>

int main()
{
    using namespace std;

    int num;

    cout << "Enter number of rows:";
    cin >> num;

    int i = 0;
    for (int i = 0; i < num; ++i)
    {
        for (int j = 0; j < num; ++j)
        {
            if ((num-j-1) > i)
                cout << ".";
            else
                cout << "*";
        }
        cout << endl;
    }

    return 0;
}

在这里插入图片描述

参考资料:
[1] 4.3.3 string类I/O

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

淘淘图兔兔呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值