C++ Primer Plus 第五章编程题

第一题

#include <iostream>

int main(void)
{
        using namespace std;
        int a = 0,b = 0;
        cout << "Please input two integers: " << endl;
        cin >> a;
        cin >> b;
        cout << (a + b)*(b - a + 1)/2 << endl;
        return 0;
}


第二题

#include <iostream>
#include <array>
const int ArSize = 101;

int main(void)
{
        using namespace std;
        array<long double, ArSize> factorials;
        factorials[1] = factorials[0] = 1L;
        for (int i = 2; i < ArSize; i++)
                factorials[i] = i * factorials[i - 1];
        for (int i = 0; i < ArSize; i++)
        cout << i << "! = " << factorials[i] << endl;
        return 0;
}

第三题

#include <iostream>

int main(void)
{
        using namespace std;
        float sum = 0,num;
        while(num != 0)
        {
                cout << "Please enter a number: " << endl;
                cin >> num;
                sum += num;
                cout << "The cumulative sum of all inputs is: " << sum << endl; 
        }
        return 0;
}


​

第四题

#include <iostream>
const int Daphne_Base = 100,Cleo_Base = 100;

int main(void)
{
        using namespace std;
        double Daphne = Daphne_Base, Cleo = Cleo_Base;
        int year = 0;
        while(Cleo <= Daphne)
        {
                Daphne += Daphne_Base * 0.1;
                Cleo *= 1.05;
                year++;
        }
        cout << "Year: " << year << endl;
        cout << "Daphne: " << Daphne << endl;
        cout << "Cleo: " << Cleo << endl;
        return 0;
}

 第五题

#include <iostream>
#include <string>

int main()
{
        using namespace std;
        int num[12];
        int sum = 0;
        const string Month[] = {"Jan", "Feb", "Mar", "Apr", "Mar", "Jun", "Jul","Aug", "Sep", "Oct", "Nov", "Dec"};
        for(int i = 0; i < 12; i++)
        {
                cout << "Please enter " << Month[i] << "'s sales volume: ";
                cin >> num[i];
                sum += num[i];
        }

        cout << "The sales volume this year is " << sum << endl;
        return 0;
}

第六题

#include <iostream>
#include <string>

int main()
{
        using namespace std;
        int sum[4] = {0};
        const string Month[] = {"Jan", "Feb", "Mar", "Apr", "Mar", "Jun", "Jul","Aug", "Sep", "Oct", "Nov", "Dec"};
        const string Year[] = {"The first year", "The second year", "The third year" };
        int volume[3][12];
       for(int year = 0; year < 3; year++)
        {
                cout << Year[year] << ": " << endl;
                for(int month = 0; month < 12; month++)
                {
                        cout << "Please enter " << Month[month] << "'s sales volume: ";
                        cin >> volume[year][month];
                        sum[year] += volume[year][month];
                }
                cout << "The sales volume this year is " << sum[year] << endl;
        }
        for(int year = 0; year < 3; year++)
        {
                sum[3] += sum[year];
        }
        cout << "The total sales volume of the three years is: " << sum[3] << endl;
        return 0;
}

第七题

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

struct Car
{
        string make;
        int year;
};

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

第八题

#include <iostream>
#include <cstring>

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

第九题

#include <iostream>
#include <string>

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

第十题

#include <iostream>

int main(void)
{
        using namespace std;
        int row = 0;
        cout <<  "Enter number of rows: ";
        cin >> row;
        for (int i = 0; i < row; i++)
        {
                for (int j = 0; j < row - i - 1; j++)
                {
                        cout << ".";
                }
                for (int j = 0; j <= i; j++)
                {
                        cout << "*";
                }
                cout << endl;
        }
        return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值