C++ Primer Plus(第6版)编程练习-第五章

2020-06-21  本文已影响0人  薛定鳄的简书猫

内容仅供参考,如有错误,欢迎指正 !

1.编写一个要求用户输入两个整数的程序。该程序将计算并输出这两个整数之间(包括这两个整数)所有整数的和。这里假设先输入较小的整数。例如,如果用户输入的是2和9,则程序将指出2~9之间所有整数的和为44。
#include<iostream>
using namespace std;
int main()
{
    int a, b,sum = 0;
    cout << "Please enter two integer(smaller first)" << endl;
    cout << "Please enter integer1: " ;
    cin >> a;
    cout << "Please enter integer2: " ;
    cin >> b;
    for (int i = a; i <= b; i++)
        sum = i + sum;
    cout << "The sum between integer1 and binteger2 is: " << sum << endl;
    return 0;

}
2.使用array对象(而不是数组)和long double(而不是long long)重新编写程序清单5.4,并计算100!的值。
#include<iostream>
#include<array>
using namespace std;
const int a = 101;
int main()
{
    
    array<long double, a>arr;
    arr[0] = arr[1] = 1.0;
    for (int i = 2; i <a; i++)
        arr[i] = i * arr[i - 1];
    for (int i = 0; i <a; i++)
        cout << i<<" != " << arr[i] << endl;
    return 0;

}
3.编写一个要求用户输入数字的程序。每次输入后,程序都将报告到目前为止,所有输入的累计和,当用户输入0时,程序结束。
#include<iostream>
using namespace std;
int main()
{
    int num, sum=0;
    for (int i=1; i!= 0;i=num)
    {
        cout << "Please enter a num(if num=0,the progream will end):_\b";
        cin >> num;
        sum += num;
        cout << "The adds of your num is: " << sum << endl;
    }
    cout << "Bye~" << endl;
        return 0;


}
4.Daphne以10%的单利投资了100美元。也就是说,每一年的利润都是投资额的10%,即每年10美元:利息=0.10*原始存款。而Cleo在第一年投资100美元的盈利是5%--一共得到105美元。下一年的盈利是105美元的5%--即5.25美元,以此类推。请编写一个程序,计算多少年后,Cleo的投资价值超过Daphne的投资价值。并显示此时两个人的投资价值。
#include<iostream>
int main()
{
    using namespace std;
    double daphne = 100.0;
    double cleo = 100.0;
    int year;
    for (year = 1; cleo <= daphne; year++)
    {
        daphne += 10.0;
        cleo *= 1.05;
    }
    cout << year - 1 << " years later,Cleo's investment is worth more than Daphne's." << endl;
    cout << "Daphne has $" << daphne << endl;
    cout << "Cleo has $" << cleo << endl;
    return 0;
}
5.假设要销售《C++ For Fools》一书。请编写一个程序,输入全年中每个月的销售量(图书数量,而不是销售额)。程序通过循环,使用初始化为月份字符串的char*数组(或string对象数组)逐月进行提示,并将输入数据存储的int数组中。然后,程序计算数组中各元素的总数,并报告这一年的销售情况。
#include<iostream>
#include<string>
using namespace std;
int main()
{
    
    string months[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 for " << months[i] << " : _\b";
        cin >> sales[i];
    }

    for (int i = 0; i < 12; i++)
    {
        cout << months[i] << " sales : " << sales[i] << endl;
        sum += sales[i];
    }

    cout << "Annual sales :" << sum << endl;
    return 0;
}
6.完成编程练习5,但这一次使用大的一个二位数组来存储输入---3年中每个月的销售量。程序将报告每年销量以及三年的总销量。
#include<iostream>
#include<string>
using namespace std;
int main()
{

    string months[12] = {
        "January", "February", "March", "April", "May", "June",
        "July", "August", "September", "October", "November",
        "December"
    };
    int i, j, k, f,sum=0, sale[3][12];
    for (i = 0; i < 3; i++)
    {
        for(j=0;j<11;j++)
        {
            cout << "Please enter the sale nunmber of the " << i + 1 << " year " << months[j];
            cin >> sale[3][12];
        }
    }
    for (k = 0; k < 3; k++)
    {
        int annul[3] = {};
        for (f = 0; f < 11; f++)
        {
            
            annul[k] += sale[k][f];
            
        }
        sum += annul[k];
        cout << "The sales of " << k + 1 << " year is " << annul[k]<<endl;
       
    }
    cout << "The sum of 3 years is " << sum << endl;
    return 0;

}
7.设计一个名为car的结构,用它存储下述有关汽车的信息:生产商(存在字符数组或string对象中的字符串)、生产年份(整数)。编写一个程序向用户询问有多少辆汽车。随后,程序使用new来创建一个有相应数量的car结构组成的动态数组。接下来,程序提示用户输入每辆车的生产商(可能有多个单词组成)和年份信息。请注意,这需要特别小心,因为它将交替读取数值和字符串。最后,程序将显示每个结构的内容。该程序的运行情况如下:

How many cars do you wish to catalog?2
Car #1:
Please enter the make:Hudson Hornet
Please enter the year made:1952
Car #2:
Please enter the make:Kaiser
Please enter the year made:1951
Here is your collection:
1952 HUdson Hornet
1951 Kaiser

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

struct car
{
    string manufacturer;
    int year;
};

int main()
{
    int number;
    cout << "How many do you wish to catalog? ";
    (cin >> number).get();
    car* p = new car[number];
    for (int i = 0; i < number; i++)
    {
        cout << "Car #" << i+1 << ":" << endl;
        cout << "Please enter the make: ";
        getline(cin, p[i].manufacturer);
        cout << "Please enter the year made: ";
       ( cin >> p[i].year).get();
    }
    cout << "Here is your collection:" << endl;
    for (int i = 0; i < number; i++)
        cout << p[i].year << " " << p[i].manufacturer << endl;
    delete[]p;
    return 0;
}
8.编写一个程序,它使用一个char数组和循环来每次读取一个单词,直到用户输入done为止。随后,该程序指出用户输入多少个单词(不包含done)。下面是程序的运行情况:

Enter words(to stop,type the word done):
anteater birthday category dumpster
envy finagle geometry done for sure
You entered a total of 7 words.

您应在程序中包含头文件cstring,并使用strcmp()来进行比较测试。
#include<iostream>
#include<cstring>
using namespace std;

int main()
{
    char word[20];
    cout << "Enter words(to stop,type the word done):" << endl;
    cin >> word;
    int num = 0;
    while (strcmp("done", word))
    {
        num++;
        cin >> word;
    }
    cout << "You entered a total of " << num << " words." << endl;
    return 0;
}
9.编写一个满足前一个练习的中描述的程序,但使用string对象而不是字符数组。请在程序中包含头文件string,并使用关系运算符来进行比较测试。
#include<iostream>
#include<string>
using namespace std;

int main()
{
    string word;
    cout << "Enter words(to stop,type the word done):" << endl;
    cin >> word;
    int num = 0;
    while (word!="done")
    {
        num++;
        cin >> word;
    }
    cout << "You entered a total of " << num << " words." << endl;
    return 0;
}
10.编写一个使用嵌套循环的程序,要求用户输入一个值,指出要显示多少行。然后,程序将显示相应行数的型号,其中第一行包括一个星号,第二行包括两个星号,以此类推。每一行包含的字符数等于用户指定的行数,在星号不够的情况下,在星号前面加句点。程序的运行情况如下:

Enter number of row :5
....*
...**
..***
.****
*****

#include<iostream>
using namespace std;

int main()
{
    int rows;
    cout << "Enter number of rows: ";
    cin >> rows;
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < rows; j++)
        {
            if ((i + j) < rows - 1)
                cout << ".";
            else
                cout << "*";
        }
        cout << endl;
    }
    return 0;
}
上一篇下一篇

猜你喜欢

热点阅读