C++ Primer Plus第五章编程练习答案

这篇具有很好参考价值的文章主要介绍了C++ Primer Plus第五章编程练习答案。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

答案仅供参考,实际运行效果取决于运行平台和运行软件

1.编写一个要求用户输入两个整数的程序。该程序将计算并输出这两个整数之间包括这两个整数)所有整数的和。这里假设先输入较小的整数。例如,如果用户输入的是2和则程序将出29之间所有整数的和为44

#include <iostream>
using namespace std;

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

    cout << "Please enter the first integer: ";
    cin >> num1;
    cout << "Please enter the second integer: ";
    cin >> num2;

    for (int i = num1; i <= num2; i++)
    {
        sum += i;
    }
    cout << "Sum of " << num1 << " to " << num2 << " are " << sum << endl;

    return 0;
}

2.使用array对象(而不是数组)和ong double(而不是lng long)重新编写序清单5.4,并计算100!的值

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

int main()
{
    const int ArSize = 101;
    array<long double, ArSize> factorials;

    factorials[0] = factorials[1] = 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;
}

3.编写一个要求用户输入数字的程序。每次输入后,程序都将报告到目前为止,所有输入的累计和。当用户输入0时,程序结束。

#include <iostream>
using namespace std;

int main()
{
    long long num;
    long long sum = 0LL;

    while (cout << "Please enter an integer(0 to quit): ", cin >> num && num != 0)
    {
        //↑逗号运算符只取最后的结果作为判断条件;
        sum += num;
        cout << "Sum of all integers are " << sum << endl;
    }

    return 0;
}

4.Daphn以10%的单利投资了100美元。也就是说每年的利润都是投资额的10%即每年10美元:

利息 = 0.10X 原始存款
而 CIeo 以 5%的复利投资了 100 美元。也就是说,利息是当前存款《包括获得的利息)的 5%,
利息= 0.05X当前存款Cleo 在第一年投资 100 美元的盈利是5%一一得到了 105 美元下一年的盈利是 105 美元的 5%一一即5.25 美元,依此类推。请编写一个程序,计算多少年后,CIco 的投资价值才能超过 Daphne 的投资价值并显示此时两个人的投资价值。

#include <iostream>
using namespace std;

int main()
{
    int n = 0;
    double daphne_money = 100;
    double cleo_money = 100;

    while (cleo_money <= daphne_money)
    {
        cout << "Year " << ++n << ':' << endl;
        daphne_money += 10;
        cleo_money += cleo_money * 0.05;
        cout << "Cleo's money = " << cleo_money;
        cout << ", Daphne's money = " << daphne_money << endl;
    }
    cout << "After " << n << " years, ";
    cout << "Cleo's money";
    cout << " > Daphne's money." << endl;

    return 0;
}

5.假设要销售《C++ For Fools》 一书。请编写一个程序,输入全年中每个月的销售量(图书数量,而不是销售额)程序通过循环,使用初始化为月份字符的 har *数组(或 string 对象数组)逐月进行提示并将输入的数据储存在一个 iit 数组中。然后,程序计算数组中各元素的总数,并报告这一年的销售情况

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

int main()
{
    const int ArSize = 12;
    const string months[ArSize] = 
    {
        "January", "February","March", 
        "April", "May", "June", "July",
        "August","September", "October",
        "November", "December"
    };
    int sum = 0, sales_volume[ArSize];

    for (int i = 0; i < ArSize; i++)
    {
        cout << "Please enter number of books sold (";
        cout << months[i] << "): ";
        cin >> sales_volume[i];
    }
    for (int i = 0; i < ArSize; i++)
    {
        sum += sales_volume[i];
    }
    cout << "A total of " << sum << " <<C++ For Fools>> books were sold in a year." << endl;

    return 0;
}

.6.完成编程练习 5,但这一次使用一个二维数组来存钻输入一一3 年中每个月的销售量。程序将报告每年销售量以及三年的总销售量。

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

const int NUM = 3;
const int ArSize = 12;

int show_result(int (*x)[ArSize], int n);

int main()
{
    const string months[ArSize] = 
    {
        "January", "February","March", 
        "April", "May", "June", "July",
        "August","September", "October",
        "November", "December"
    };
    int sum, total, sales_volume[NUM][ArSize];

    for (int i = 0; i < NUM; i++)
    {
        cout << "Year " << i + 1 << ": " << endl;
        for (int j = 0; j < ArSize; j++)
        {
            cout << "Please enter number of books sold (";
            cout << months[j] << "): ";
            cin >> sales_volume[i][j];
        }
        cout << endl;
    }

    sum = total = show_result(sales_volume, 0);
    cout << "A total of " << sum << " <<C++ For Fools>> books were sold in the first year." << endl;
    total += sum = show_result(sales_volume, 1);
    cout << "A total of " << sum << " <<C++ For Fools>> books were sold in the second year." << endl;
    total += sum = show_result(sales_volume, 2);
    cout << "A total of " << sum << " <<C++ For Fools>> books were sold in the third year." << endl;
    cout << "A total of " << total << " <<C++ For Fools>> books were sold in three years." << endl;

    return 0;
}

int show_result(int (*x)[ArSize], int n)
{
    int sum = 0;

    for (int i = 0; i < ArSize; i++)
    {
        sum += x[n][i];
    }
    return sum;
}

7.设计一个名为 car 的结构,用它存储下述有关汽车的信息:生产商《存储在字特数组成 sting 对象中的字符串)、生产年份(整数)。编写一个程序,向用户询问有多少辆汽车。随后,程序使用 new 来创建一个由相应数量的 C 结构组成的动态数组。接下来,程序提示用户输入车的生产商(可能由多个单词组成)和年份信息。请注意,这需要特别小心,因为它将交替读取数值和字符串《参见第 4 章)。最后程序将显示每个结构的内容。该程序的运行情况如下:
How many cars do you wish to catalog? 2
Car i1:
Please enter the make: ludgom formet
Pleage enter the year made: 1952
Car 月2:
Pleame ontes the make: Kaiger
Please enter the year made: 1951
Here is your collection;
1952 Fudgon Hornet
195] Kaiser

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

struct car
{
    string producer;
    int year_of_introducion;
};

int main()
{
    int num;

    cout << "How many cars do you wish to catalog? ";
    (cin >> num).get();
    car *many_cars = new car[num];

    for (int i = 0; i < num; i++)
    {
        cout << "Car #" << i + 1 << ':' << endl;
        cout << "Please enter the make: ";
        getline(cin, many_cars[i].producer);
        cout << "Please enter the year made: ";
        (cin >> many_cars[i].year_of_introducion).get();
    }

    cout << "Here is your collection:" << endl;
    for (int i = 0; i < num; i++)
    {
        cout << many_cars[i].year_of_introducion;
        cout << ' ' << many_cars[i].producer << endl;
    }
    delete[] many_cars;

    return 0;
}

8.编写一个程序,它使用一个 char 数组和环来每次读取一个单词,直用户输入 done 为止。随后该程序指出用户输入了多少个单词(不包括 done 在内)。下面是该程序的运行情况:
Enter words (to stop, type the word donel :
anteater birthday category dumpater
envy finagle geometry dome for sure
You entered a total of 7 words
您应在程序中包含头文件 cstring,并使用雨数 stremp()来进行比较测试

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

int main()
{
    const int ArSize = 256;
    char str[ArSize];
    unsigned int count = 0;

    cout << "Enter words (to stop, type the word done):" << endl;
    while (cin >> str, strcmp("done", str))
    {
        ++count;
    }
    cout << "You entered a total of " << count << " words." << endl;

    return 0;
}

9.编写一个满足前一个练习中描述的程序,但使用 sting 对象而不是字符数组。请在程序中包含文件 string,并使用关系运算符来进行比较测试。

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

int main()
{
    string str;
    unsigned int count = 0;

    cout << "Enter words (to stop, type the word done):" << endl;
    while (cin >> str, str != "done")
    {
        ++count;
    }
    cout << "You entered a total of " << count << " words." << endl;

    return 0;
}

10.编写一个使用嵌套循环的程序,要求用户输入一个值,指出要显示多少行。然后,程序将显示相应行数的星号,其中第一行包括一个星号,第二行包括两个星号,依此类推。每一行包含的字符数等于用户指定的行数,在星号不够的情况下,在星号前面加上句点。该程序的运行情况如下:
Enter qumber of rOwa: 5
....*

...**

..***

.****

*****文章来源地址https://www.toymoban.com/news/detail-698978.html

#include <iostream>
using namespace std;

int main()
{
    int row;

    cout << "Enter number of rows: ";
    cin >> row;
    for (int i = 1; i <= row; i++)
    {
        for (int j = i; j <= row - 1; j++)
        {
            cout << ".";
        }
        for (int j = 1; j <= i; j++)
        {
            cout << "*";
        }
        cout << endl;
    }

    return 0;
}

到了这里,关于C++ Primer Plus第五章编程练习答案的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • C Primer Plus第六章编程练习答案

    学完C语言之后,我就去阅读《C Primer Plus》这本经典的C语言书籍,对每一章的编程练习题都做了相关的解答,仅仅代表着我个人的解答思路,如有错误,请各位大佬帮忙点出! 1.编写一个程序,创建一个包含26个元素的数组,并在其中储存26个小 写字母。然后打印数组的所有

    2024年02月06日
    浏览(34)
  • C Primer Plus第十四章编程练习答案

    学完C语言之后,我就去阅读《C Primer Plus》这本经典的C语言书籍,对每一章的编程练习题都做了相关的解答,仅仅代表着我个人的解答思路,如有错误,请各位大佬帮忙点出! 由于使用的是命令行参数常用于linux系统或者vscode,但此代码是运行于vs2022的,测试截图就不弄了。

    2024年02月07日
    浏览(47)
  • C Primer Plus第十六章编程练习答案

    学完C语言之后,我就去阅读《C Primer Plus》这本经典的C语言书籍,对每一章的编程练习题都做了相关的解答,仅仅代表着我个人的解答思路,如有错误,请各位大佬帮忙点出! 由于使用的是命令行参数常用于linux系统或者vscode,但此代码是运行于vs2022的,测试截图就不弄了。

    2024年02月07日
    浏览(32)
  • C Primer Plus (中文版)第10章编程练习 参考答案(仅供参考~)

    🌴 C Primer Plus第10章编程练习~ 加油加油!🍭 ☘️欢迎大家讨论 批评指正~ 🍎1.修改程序清单10.7的rain.c程序,用指针进行计算(仍然要声明并初始化数组)。计算每年的总降水量、年平均降水量和5年中每月的平均降水量 🍐编写一个程序,初始化一个double类型的数组,然后把

    2024年02月04日
    浏览(40)
  • C++ Primer第五版_第九章习题答案(51~52)

    练习9.51 设计一个类,它有三个unsigned成员,分别表示年、月和日。为其编写构造函数,接受一个表示日期的string参数。你的构造函数应该能处理不同的数据格式,如January 1,1900、1/1/1990、Jan 1 1900 等。

    2023年04月11日
    浏览(34)
  • C++ Primer第五版_第十八章习题答案(11~20)

    练习18.11 为什么 what 函数不应该抛出异常? what中如果抛出异常,需要try catch捕获,再调用what,一直循环,直达内存耗尽。 练习18.12 将你为之前各章练习编写的程序放置在各自的命名空间中。也就是说,命名空间chapter15包含Query程序的代码,命名空间chapter10包含TextQuery的代码

    2024年02月06日
    浏览(33)
  • C++ Primer第五版_第十四章习题答案(41~50)

    练习14.41 你认为 C++ 11 标准为什么要增加 lambda?对于你自己来说,什么情况下会使用 lambda,什么情况下会使用类? 使用 lambda 是非常方便的,当需要使用一个函数,而这

    2024年02月01日
    浏览(27)
  • C++ Primer第五版_第十六章习题答案(41~50)

    练习16.41 编写一个新的 sum 版本,它返回类型保证足够大,足以容纳加法结果。 练习16.42 对下面每个调用,确定 T 和 val

    2024年02月04日
    浏览(30)
  • C++ Primer第五版_第十六章习题答案(51~60)

    练习16.51 调用本节中的每个 foo,确定 sizeof…(Args) 和 sizeof…(rest)分别返回什么。 练习16.52 编写一个程序验证上一题的答案。 练习16.53 编写你自己版本的 print 函数,并打印一个、两个及五个实参来测试它,要打印的每个实参都应有不同的

    2024年02月05日
    浏览(26)
  • C Primer Plus 第6版 编程练习 chapter 16

    开发一个包含你需要的预处理定义的头文件。 test.c diceroll.h 两个数的调和平均数这样计算:先得到两数的倒数,然后计算两个倒数的平均值,最后取计算结果的倒数。使用#define指令定义一个宏函数,,执行该运算。编写一个简单的程序测试该宏。 极坐标向量的模(即向量的

    2024年01月19日
    浏览(32)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包