C++ Primer Plus 第六版_编程练习(4)(Chapter_five 1-5)

编程工具用得好,搞起研究事半功倍。导师的意见是,学好C++,其他编程都不怕。暂时没有迫切的实战需要,于是决定从最基础的学起,挑了《C++ Primer Plus (第六版)》这本书,开始啃吧。编程练习还是一定要做的,每天一点,记在这里。

5_1

#include <iostream>
using namespace std;

int main()
{
    int lower_bound_;
    int upper_bound_;

    cout << "Enter the lower bound: ";
    cin >> lower_bound_;
    cout << "Enter the upper bound: ";
    cin >> upper_bound_;

    int i;
    int sum = 0;

    for (i = lower_bound_; i <= upper_bound_; i++)
    {
        sum = sum + i;
    }
    cout << "Summer of the number between them is " << sum << endl;

    return 0;
}

5_2

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

const int Asize = 101;

int main()
{
    array<long double, Asize> factorials;
    factorials[0] = factorials[1] = 1;
    for (int i = 2; i < Asize; i++)
    {
        factorials[i] = factorials[i - 1] * i;
    }
    for (int j = 0; j < Asize; j++)
    {
        cout << j << "! = " << factorials[j] << endl;
    }

    return 0;
}

5_3

#include <iostream>
using namespace std;

int main()
{
    double i;
    double sum = 0;

    cout << "Enter a number(0 to qiut): ";
    cin >> i;

    while (i != 0)
    {

        sum = sum + i;
        cout << "Summer of the number that you have entered is " << sum << endl;
        cout << "Enter a number(0 to qiut): ";
        cin >> i;
    }

    cout << "You have entered 0." << endl;
    return 0;
}

5_4

#include <iostream>
using namespace std;

const double Value = 100;
const double Pre_1 = 0.10;
const double Pre_2 = 0.05;

int main()
{
    double value_Daphne = Value;
    double value_Cleo = Value;
    int count_ = 1;

    while (true)
    {
        value_Daphne += Value * Pre_1;
        value_Cleo += value_Cleo * Pre_2;

        if (value_Cleo > value_Daphne)
        {
            cout << "After " << count_ << " years, Cleo will get much more value than Daphne." << endl;
            cout << "Cleo's value: " << value_Cleo << endl;
            cout << "Daphne's value: " << value_Daphne << endl;
            break;
        }
        count_++;
    }

    return 0;
}

5_5

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

int main()
{
    string month[12] = { "January", "February", "March", "April", "May", "June", "July",
        "August", "September", "October", "November", "December" };
    int num[12] = {};
    int sum = 0;
    cout << "Enter the sales of each month: " << endl;

    for (int i = 0; i < 12; i++)
    {
        cout << month[i] << " : ";
        cin >> num[i];
        sum += num[i];
    }

    cout << "《C++ For Fools》 has been sold " << sum << " this year." << endl;

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值