《C++ Primer Plus(第6版)》编程练习代码 Chapter 5

Chapter 5

5.1

#include<iostream>

int main()
{
    using namespace std;
    int min, max, sum = 0;
    cout << "Enter min number: ";
    cin >> min;
    cout << "Enter max number: ";
    cin >> max;
    for (int i = min; i <= max; i++)
    {
        sum += i;
    }
    cout << "Result: " << sum;
    cin.get();
    cin.get();
}

5.2

#include<array>
#include<iostream>

int main()
{
    using namespace std;
    const int N = 101;
    array<long double, N> factorials;
    factorials[0] = factorials[1] = 1LL;
    for (int i = 2; i < N; i++)
        factorials[i] = i * factorials[i - 1];
    for (int i = 0; i < N; i++)
        cout << i << "! = " << factorials[i] << endl;
    cin.get();
    cin.get();
}

5.3

#include<iostream>

int main()
{
    using namespace std;
    long sum = 0, x;
    for (cin >> x; x != 0; cin >> x)
    {
        sum += x;
        cout << "Sum result: " << sum << endl;
    }
    cout << "Program ends.";
    cin.get();
    cin.get();
}

5.4

#include<iostream>

int main()
{
    using namespace std;
    double Daphne, Cleo, SD, SC;
    int i = 1;
    Daphne = Cleo = 100;
    SD = Daphne * 0.1;
    SC = Cleo * 0.05;
    while (SD > SC)
    {
        SD += Daphne * 0.1;
        SC += (SC + Cleo) * 0.05;
        ++i;
    }
    cout << "After " << i << " years, Cleo > Daphne.\n";
    cout << "Daphne's value: " << SD << endl;
    cout << "Cleo;s value: " << SC << endl;
    cin.get();
    cin.get();
}

5.5

#include<iostream>
#include<string>

int main()
{
    using namespace std;
    string month[12] =
    {
        "Jan",
        "Feb",
        "Mar",
        "Apr",
        "May",
        "Jun",
        "Jul",
        "Aug",
        "Sep",
        "Oct",
        "Nov",
        "Dec"
    };
    int sale[12], sum = 0;
    for (int i = 0; i < 12; i++)
    {
        cin >> sale[i];
        sum += sale[i];
    }
    cout << "Sales in this year:\n\n";
    for (int i = 0; i < 12; i++)
    {
        cout << month[i] << ": \t" << sale[i] << endl;
    }
    cout << "Sum of sales: " << sum;
    cin.get();
    cin.get();
}

5.6

#include<iostream>
#include<string>

int main()
{
    using namespace std;
    string month[12] =
    {
        "Jan",
        "Feb",
        "Mar",
        "Apr",
        "May",
        "Jun",
        "Jul",
        "Aug",
        "Sep",
        "Oct",
        "Nov",
        "Dec"
    };
    int sale[12][3], sum = 0;
    for (int i = 0; i < 12; i++)
        for (int j = 0; j < 3; j++)
        {
            cin >> sale[i][j];
            sum += sale[i][j];
        }
    cout << "Sales in three years:\n\n";
    for (int i = 0; i < 12; i++)
    {
        cout << month[i] << ": \t" ;
        for (int j = 0; j < 3; j++)
        {
            cout << sale[i][j] << "\t";
        }
        cout << endl;
    }
    cout << "Sum of sales: " << sum;
    cin.get();
    cin.get();
}

5.7

#include<iostream>
#include<string>

using namespace std;

struct car
{
    string make;
    int year;
};

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

    delete [] carInfo;
    cin.get();
    cin.get();
    return 0;
}

5.8

#include<iostream>
#include<cstring>

using namespace std;

int main()
{
    cout << "Enter words (to stop, type the word done):\n";
    char word[30];
    int n = 0;
    cin >> word;
    while (strcmp(word, "done"))
    {
        cin >> word;
        n++;
    }
    cout << "You entered a total of " << n << " words.";
    cin.get();
    cin.get();
}

5.9

#include<iostream>
#include<cstring>
#include<string>

using namespace std;

int main()
{
    cout << "Enter words (to stop, type the word done):\n";
    string word;
    int n = 0;
    cin >> word;
    while (word != "done")
    {
        cin >> word;
        n++;
    }
    cout << "You entered a total of " << n << " words.";
    cin.get();
    cin.get();
}

5.10

#include<iostream>

using namespace std;

int main()
{
    cout << "Enter numbet of rows: ";
    int row;
    cin >> row;
    for (int i = 1; i <= row; i++)
    {
        for (int j = 0; j < (row - i); j++)
            cout << ".";                    //print"."
        for (int j = 0; j < i; j++)
            cout << "*";                    //print"*"
        cout << endl;
    }
    cin.get();
    cin.get();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值