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

来源:互联网 发布:winxp升级win7软件 编辑:程序博客网 时间:2024/06/06 19:40

编程工具用得好,搞起研究事半功倍。导师的意见是,学好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
原创粉丝点击