【C++】学习笔记二十——第5章编程练习

C++ primer plus 第六版第5章编程练习

1.

#include<iostream>
int main()
{
    using namespace std;
    cout<<"Please enter 2 numbers:\n";
    cout<<"Enter the smaller number:";
    int min;
    cin>>min;
    cout<<"Enter the larger number:";
    int max;
    cin>>max;
    int sum = 0;
    for(int i=min;i<=max;i++)
    {
        sum = sum+i;
    }
    cout<<"The sum of numbers between "<<min<<" and "<<max<<" is "<<sum<<endl;
    return 0;
}

2.

#include<iostream>
#include<array>
using namespace std;
const int ArSize = 16;
int main()
{
    array<long double, ArSize> factorials;
    factorials[1] = factorials[0] = 1.0L;
    for (int i = 2; i < 101; i++)
        factorials[i] = i*factorials[i - 1];
    for (int i = 0; i < ArSize; i++)
        cout << i << "! = "<< factorials[i] << endl;
    cout<<"100! = "<<factorials[100]<<endl;
    return 0;
}

3.

#include<iostream>
#include<vector>

int main()
{
        using namespace std;
        vector<int> nums(100);
        int i = 0;
        cin >> nums[0];
        int sum = nums[0];
        for (i = 1; nums[i-1]!=0; i++)
        {
            cout << "sum = " << sum << endl;
            cin >> nums[i];
            sum = sum + nums[i];
        }
    return 0;
}

4.

#include<iostream>
int main()
{
    using namespace std;
    double Daphne[100];
    double Cleo[100];
    Daphne[0] = 100;
    Cleo[0] = 100;
    int i = 0;
    while (Daphne[i] >= Cleo[i])
    {
        Daphne[i+1] = 100 + 10 * (i+1);
        Cleo[i+1] = Cleo[i] * 1.05;
        i++;
    }
    cout << i << "年后,Cleo的投资价值将超过Daphne。" << endl;
    cout << "此时,Cleo的投资价值为" << Cleo[i] << "美元;" << "Daphne的投资价值为" << Daphne[i] << "美元。"<<endl;
    system("pause");
    return 0;
}

5.

#include<iostream>
#include<string>
int main()
{
    using namespace std;
    string month[12] = {
        "January",
        "February",
        "March",
        "April",
        "May",
        "June",
        "July",
        "August",
        "September",
        "October",
        "November",
        "December",
    };
    int sales[12];
    int sum = 0;
    for (int i = 0; i < 12; i++)
    {
        cout << "Please enter the sales volume of " << month[i] << ":";
        cin >> sales[i];
        sum = sum + sales[i];
    }
    cout << "The sales volume of the whole year is " << sum << endl;
    system("pause");
    return 0;
}

6.

#include<iostream>
#include<string>
int main()
{
    using namespace std;
    string month[12] = {
        "January",
        "February",
        "March",
        "April",
        "May",
        "June",
        "July",
        "August",
        "September",
        "October",
        "November",
        "December",
    };
    int sales[3][12];
    int sum[3];
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 12; j++)
        {
            cout << "Please enter the sales volume of " << month[j] << " of year"<<i<<":";
            cin >> sales[i][j];
            sum[i] = sum[i] + sales[i][j];
        }
    }
    int sum_all = sum[0] + sum[1] + sum[2];
    cout << "month";
    for (int i = 0; i < 12; i++)
        cout << "\t" << month[i];
    for (int i = 0; i < 3; i++)
    {
        cout << endl << "year"<<i<<"\t";
        for (int j = 0; j < 12; j++)
            cout << sales[i][j] << "\t";
        cout << endl;
    }
    system("pause");
    return 0;
}

7.

#include<iostream>
#include<string>
int main()
{
    using namespace std;
    struct car
    {
        string make;
        int year;
    };
    cout << "How many cars do you wish to catalog? ";
    int num;
    cin >> num;
    cin.get();     //如果没有这一行,cin读取数字后,将回车键生成的换行符留在了输入队列中,
                      //后面的getline看到换行符后,将认为是一个空行 ,并将一个空字符串赋给string对象  
    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;
    system("pause");
    return 0;
}

8.

#include<iostream>
#include<cstring>
int main()
{
    using namespace std;
    char word[20];
    cout << "Enter words (to stop, type the word done): " << endl;
    cin >> word;
    int count = 0;
    while (strcmp(word, "done"))
    {
        ++count;
        cin >> word;
    }
    cout << "You entered a total of " << count << " words.\n";
    system("pause");
    return 0;
}

9.

#include<iostream>
#include<string>
int main()
{
    using namespace std;
    string word;
    cout << "Enter words (to stop, type the word done): " << endl;
    cin >> word;
    int count = 0;
    while (word!="done")
    {
        ++count;
        cin >> word;
    }
    cout << "You entered a total of " << count << " words.\n";
    system("pause");
    return 0;
}

10.

#include<iostream>
int main()
{
    using namespace std;
    cout << "Enter number of rows: ";
    int number;
    cin >> number;
    char * pstar = new char[number];
    for (int i = number-1; i >=0; i--)
    {
        for (int j = 0; j < i; j++)
        {
            pstar[j] = '.'; 
        }
        pstar[i] = '*';
        cout << pstar << "\n";
    }
    delete[] pstar;
    system("pause");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值