《C++ primer plus》第5章练习题

 1.输入两个整数,输出两个整数之间所有整数的和,包括两个整数。

#include<iostream>
using namespace std;

int main()
{
    int num1, num2,num_left,num_right,sum = 0;

    cout << "Input two integers:" << endl;

    cin >> num1 >> num2;

    //比较大小,从小的开始累加
    num_left = num1 < num2 ? num1 : num2;    
    num_right = num1 > num2 ? num1 : num2;    

    for (int i = 0; (num_left+i) <= num_right; i++)
    {
        sum += num_left + i;    
    }

    cout << "Sum of all integers between the two numbers:" << sum << endl;

    system("pause");

}

 

2.输入一个整数,计算它的阶乘,要能够计算100的阶乘(使用long double)。

#include<iostream>
using namespace std;

int main()
{
    double num;
    long double res;

    cin >> num;

    res = num;

    for (int i = 1; num - i >0; i++)
    {
        res *= (num - i);
    }

    cout << res << endl;

    system("pause");

}

 

3.每次输入一个数,输出:到目前为止,前面输入的所有数的和。输入0结束。

#include<iostream>
using namespace std;

int main()
{
    double input_number, sum = 0;

    cout << "Input a number to add(input 0 to quit):" << endl;

    cin >> input_number;

    while (input_number)
    {
        sum += input_number;

        cout << "Until now,the sum of all numbers before: " << sum << endl;
        cout << "Input next number to add(input 0 to quit):" << endl;

        cin >> input_number;
    }

    cout << "Final result:" << sum << endl;
    cout << "done." << endl;

    system("pause");

}

 

4.Daphne进行单利投资,Cleo进行复利投资,两人都投资100美元。Daphne每年的利息(收益)是:原始存款×0.10,Cleo每年的利息(收益)是:当前存款×0.05。也就是说,Daphne每年固定盈利100×0.10=10美元;Cleo今年投资100美元,按5%盈利,下一年的盈利就是5美元,下下年的盈利就是105×5%=5.25美元。计算多少年后,Cleo的投资价值才能超过Daphne,并显示此时两人的投资价值。

#include<iostream>
using namespace std;

const double ratio_D = 0.1, ratio_C = 0.05;

int main()
{
    double mon_D = 100, mon_C = 100;
    double intst_D = mon_D * ratio_D;

    int y = 0;
    do
    {
        ++y;
        mon_D = mon_D + intst_D;
        mon_C = (1+ratio_C)*mon_C;
    } while (mon_C < mon_D);

    cout << "After " << y << " years." << endl;
    cout << "Daphne:$" << mon_D << "\tCleo:$" << mon_C << endl;
    
    system("pause");

}

 

5.假设销售一本书,用char数组(或string对象数组)提示用户输入一年中所有月份的销售量,将输入的销售量存储在一个int数组中,然后程序计算数组中元素的总和,报告一年的销售情况。

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

int main()
{
    string prmt[] =
    { "January","February","March","April",
        "May","June","July","August",
        "September","October","November","December"
    };

    int booksales[12],sales_sum = 0;

    for (int i = 0; i < 12; i++)
    {
        cout << "Input books sales in " << prmt[i] << ":\n";
        cin >> booksales[i];
        sales_sum += booksales[i];
    }

    cout << "The whole sales in this year is:" << sales_sum << endl;

    system("pause");

}

 

6.在第5题的基础上修改程序,使用二维数组来存储输入——3年中每个月的销售量,程序将报告每年的销售量和三年的总销售量。

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

const int years = 3;

int main()
{
    string prmt[] =
    { "January","February","March","April",
        "May","June","July","August",
        "September","October","November","December"
    };

    int booksales[years][12], sum_py[years] = {},sum_ay = 0;

    for ( int y = 0; y < years; y++)
    {
        cout << "\t|| Books sales for " << "YEAR " << y + 1 << " ||\n\n";
        for (int m = 0; m < 12; m++)
        {
            cout << "Input books sales in " << prmt[m] << ":\n";
            cin >> booksales[y][m];
            sum_py[y] += booksales[y][m];
        }
        cout << "\nBooks sales in " << "YEAR " << y + 1 << " is:" << sum_py[y] << "\n\n";
    }

    for (int i = 0; i < years; i++)
        sum_ay += sum_py[i];

    cout << "Books sales of " << years << " years:" << sum_ay << endl;

    system("pause");

}

 

7.设计一个car结构,存储汽车的生产商(字符数组或string对象)和生产年份(整数)。编写程序,向用户询问有多少辆车。随后,程序使用new创建一个由相应数量的car结构组成的动态数组。然后,程序依次提示用户输入生产商和年份。最后,输出所有存储的信息。

#include<iostream>
using namespace std;

struct car_product
{
    char producer[20];
    int year;
};

int main()
{
    int counts;

    cout << "How many cars do your wish catalog? ";
    cin >> counts;
    cin.get();        //清空缓冲区的换行符,防止后面cin.get()停止

    car_product *ptr = new car_product[counts];

    for (int i = 0; i < counts; i++)
    {
        cout << "Car #" << i+1 << ":\n";
        cout << "Please enter the make: ";
        cin.get(ptr[i].producer,20);        //cin.get()会读取整行字符,包括空格,遇到换行符停止
        cout << "Please enter the year made: ";
        cin >> ptr[i].year;
        cin.get();        //同上,清空缓冲区的换行符
    }

    cout << "Here is your collection:" << endl;
    for (int i = 0; i < counts; i++)
    {
        cout << ptr[i].year << " " << ptr[i].producer << endl;
    }

    delete[]ptr;

    system("pause");

}

*要特别注意其中cin和cin.get()的用法。这里学习了输入流的概念,用户的输入都会预先存到缓冲区内,cin有关的输入会先从缓冲区内取数据。cin取数据时,遇到空格或换行符都会停止,取完数据后,会留下换行符。而cin.get()会把空格也读入,遇到换行符停止,同样也会把换行符留在缓冲区内。

*cin.get()不指定读取到哪个对象和长度,会默认读走一个字符,所以也可以起到清空缓冲区的作用。

 

8.用户输入一系列单词,中间用空格隔开。程序使用char数组存储所有单词,然后统计单词“done”之前有多少个单词。

#include<iostream>
using namespace std;

const int MAXSIZE = 100;

int main()
{
    char store[MAXSIZE];
    int counts = 0;

    cin.get(store,MAXSIZE);

    for (int i = 0; i<MAXSIZE ;i++)
    {
        if (store[i] == ' ')
            counts++;
        else if ((store[i] == 'd') && (store[i + 1] == 'o') && (store[i + 2] == 'n') && (store[i + 3] == 'e'))
            break;
        else {};
    }

    cout << "You entered a total of "<<counts<<" words.\n";

    system("pause");

}

 

9.编写循环嵌套程序,要求用户输入一个值,指出要显示多少行,然后,程序按下面的规律显示,假如输入的是5:

....*
...**
..***
.****
*****

第一行显示一个星号,其余用点补充,下面以此类推,直到第五行显示出五个星号。

#include<iostream>
using namespace std;

int main()
{
    int num;

    cout << "Enter number of rows: ";
    cin >> num;

    for (int i = 0; i < num; i++)
    {
        for (int j = num-1; j >i; j--)
        {
            cout << ".";
        }
        
        for (int k = 0; k <= i; k++)
        {
            cout << "*";
        }

        cout << "\n";
    }

    system("pause");

}

 

上一篇:[斯坦福大学2014机器学习教程笔记]第二章-梯度下降线性回归


下一篇:[斯坦福大学2014机器学习教程笔记]第三章-矩阵和向量