C++PRIMER第五版练习题答案第一章

应该有很多小伙伴和我一样,闲来无事买了本C++的书自己啃,课后的练习题做的很揪心,这里我分享下我写的答案,希望能帮助到你,提供源码,就不跑了哈,毕竟现在是第一章,很很很基础,当看到后面,分享到后面的时候,注释会写详细点方便大家一起讨论思考~~

1.1

  1. int main()
  2. {
  3. return 0;
  4. }

1.2

  1. int main()
  2. {
  3. return -1;
  4. }

1.3

  1. #include <iostream>
  2. int main()
  3. {
  4. std::cout<<"Hello,World"<<std::endl;
  5. return 0;
  6. }

1.4

  1. #include <iostream>
  2. int main()
  3. {
  4. int i=0,j=0;
  5. std::cin>>i>>j;
  6. std::cout<<"The ji of "<<i<<" * "<<j<<" is "<<i*j<<std::endl;
  7. return 0;
  8. }

1.5

  1. #include <iostream>
  2. int main()
  3. {
  4. int i=0,j=0;
  5. std::cin>>i>>j;
  6. std::cout<<"The ji of ";
  7. std::cout<<i;
  8. std::cout<<" * ";
  9. std::cout<<j;
  10. std::cout<<" is ";
  11. std::cout<<i*j<<std::endl;
  12. return 0;
  13. }

1.6

  1. /*
  2. 不合法,找不到cout输出,<<符号左侧应都加上std::cout
  3. */

1.7

  1. /*
  2. *下列行为不允许发生,注释界定符不可嵌套
  3. * /* */
  4. *
  5. * /
  6. int main()
  7. {
  8. return 0;
  9. }

1.8

  1. #include <iostream>
  2. int main()
  3. {
  4. std::cout<<"/*"; //可
  5. std::cout<<"*/"; //可
  6. std::cout<</*"*/"*/; //不可
  7. std::cout<</*"*/"/*"/*"*/; //可
  8. }

1.9

  1. #include <iostream>
  2. int main()
  3. {
  4. int sum=0,val=50;
  5. while (val<=100)
  6. {
  7. sum+=val;
  8. ++val;
  9. }
  10. std::cout<<"The sum of 50 to 100 is "<<sum<<std::endl;
  11. return 0;
  12. }

1.10

  1. #include <iostream>
  2. int main()
  3. {
  4. int val=10;
  5. while (val>=0)
  6. {
  7. std::cout<<"value is "<<val<<"\n"<<std::endl;
  8. --val;
  9. }
  10. return 0;
  11. }

1.11++

  1. #include <iostream>
  2. int main()
  3. {
  4. int i=0,j=0;
  5. std::cout<<"please input two numbers and i will give you the num in thems"<<std::endl;
  6. std::cin>>i>>j;
  7. if (i>=j)
  8. {
  9. while (i>=j)
  10. {
  11. std::cout<<"value is "<<i<<"\n"<<std::endl;
  12. --i;
  13. }
  14. }
  15. else
  16. {
  17. while (i<=j)
  18. {
  19. std::cout<<"value is "<<j<<"\n"<<std::endl;
  20. --j;
  21. }
  22. }
  23. return 0;
  24. }

1.12

  1. #include <iostream>
  2. int main()
  3. {
  4. int sum=0;
  5. for(int i=-100;i<=100;++i)
  6. {
  7. sum+=i;
  8. }
  9. std::cout<<"The sum is "<<sum<<std::endl;
  10. }

1.13

  1. #include <iostream>
  2. int main()
  3. {
  4. int sum=0,val=50;
  5. for(;val<=100;++val)
  6. sum+=val;
  7. std::cout<<"The sum of 50 to 100 is "<<sum<<std::endl;
  8. int valu=10;
  9. for (;valu>=0;--valu)
  10. std::cout<<"value is "<<valu<<"\n"<<std::endl;
  11. int i=0,j=0;
  12. std::cout<<"please input two numbers and i will give you the num in thems"<<std::endl;
  13. std::cin>>i>>j;
  14. if (i>=j)
  15. {
  16. for (;i>=j;--i)
  17. std::cout<<"value is "<<i<<"\n"<<std::endl;
  18. }
  19. else
  20. {
  21. for (;i<=j;--j)
  22. std::cout<<"value is "<<j<<"\n"<<std::endl;
  23. }
  24. return 0;
  25. }

1.14

  1. // for循环和while循环的优缺点如下:
  2. // 1、在for循环中,循环控制变量的初始化和修改都放在语句头部分,形式较简洁,且特别适用于循环次数已知的情况。
  3. // 2、在while循环中,循环控制变量的初始化一般放在while语句之前,循环控制变量的修改一般放在循环体中,形式上不如for语句简洁,但它比较适用于循环次数不易预知的情况(用某一条件控制循环)。
  4. // 3、两种形式各有优点,但它们在功能上是等价的,可以相互转换。

1.15

  1. // 编译器各类错误
  2. // 1.语法错误
  3. // 2.类型错误
  4. // 3.声明错误

1.16

  1. #include <iostream>
  2. int main()
  3. {
  4. int sum=0,value = 0;
  5. std::cout<<"I will print your sum and you can input not integer num to end\n";
  6. while (std::cin>>value)
  7. sum+=value;
  8. std::cout<<"The sum is "<<sum<<std::endl;
  9. return 0;
  10. }

1.17&1.18

  1. #include <iostream>
  2. int main()
  3. {
  4. int currVal = 0, val = 0;
  5. if (std::cin >> currVal)
  6. {
  7. int cnt = 1;
  8. while (std::cin >> val)
  9. {
  10. if (val == currVal)
  11. {
  12. ++cnt;
  13. }
  14. else
  15. {
  16. std::cout << currVal << " occurs " << cnt << " times " << std::endl;
  17. currVal = val;
  18. cnt = 1;
  19. }
  20. }
  21. std::cout << currVal << " occurs " << cnt << " times " << std::endl;
  22. }
  23. return 0;
  24. }

1.19

  1. #include <iostream>
  2. int main()
  3. {
  4. int i=0,j=0;
  5. std::cout<<"please input two numbers and i will give you the num in thems"<<std::endl;
  6. std::cin>>i>>j;
  7. if (i>=j)
  8. {
  9. while (i>=j)
  10. {
  11. std::cout<<"value is "<<i<<"\n"<<std::endl;
  12. --i;
  13. }
  14. }
  15. else
  16. {
  17. while (i<=j)
  18. {
  19. std::cout<<"value is "<<j<<"\n"<<std::endl;
  20. --j;
  21. }
  22. }
  23. return 0;
  24. }

1.20

  1. #include <iostream>
  2. #include "Sales_item.h"
  3. int main()
  4. {
  5. Sales_item book;
  6. std::cout <<"input Ctrl+Z exit "<<std::endl;
  7. while (std::cin >> book)
  8. {
  9. std::cout << book <<std::endl;
  10. }
  11. return 0;
  12. }

1.21

  1. #include <iostream>
  2. #include "Sales_item.h"
  3. int main()
  4. {
  5. Sales_item book1,book2;
  6. std::cin >> book1 >> book2;
  7. if (book1.isbn() == book2.isbn())
  8. {
  9. std::cout << book1 + book2 << std::endl;
  10. return 0;
  11. }
  12. else
  13. {
  14. std::cout << "Please input have same isbn data" << std::endl;
  15. return -1;
  16. }
  17. }

1.22

  1. #include <iostream>
  2. #include "Sales_item.h"
  3. int main()
  4. {
  5. Sales_item total;
  6. std::cout <<"Please input same data and then input not same data output and then input Ctrl+Z exit "<<std::endl;
  7. if (std::cin >> total)
  8. {
  9. Sales_item trans;
  10. while (std::cin >> trans)
  11. {
  12. if (total.isbn() == trans.isbn())
  13. {
  14. total += trans;
  15. }
  16. else
  17. {
  18. std::cout << total <<std::endl;
  19. }
  20. }
  21. }
  22. return 0;
  23. }

1.23

  1. #include <iostream>
  2. #include "Sales_item.h"
  3. int main()
  4. {
  5. Sales_item total;
  6. std::cout <<"Please input same data and then input not same data output and then input Ctrl+Z exit "<<std::endl;
  7. if (std::cin >> total)
  8. {
  9. Sales_item trans;
  10. int cnt = 1;
  11. while (std::cin >> trans)
  12. {
  13. if (total.isbn() == trans.isbn())
  14. {
  15. //total += trans;
  16. ++cnt;
  17. }
  18. else
  19. {
  20. std::cout << total.isbn() << " occours " << cnt << " times " <<std::endl;
  21. total = trans;
  22. cnt = 1;
  23. }
  24. }
  25. std::cout << total.isbn() << " occours " << cnt << " times " <<std::endl;
  26. }
  27. return 0;
  28. }

1.24

  1. #include <iostream>
  2. #include "Sales_item.h"
  3. /**
  4. * @brief 文件重定向: $ addItems <infile>outfile
  5. * 此处我们控制台查看一下测试的文件内容,然后再重定向,再看一下重定向里的内容
  6. * 1. type book_sales.txt
  7. * 2. 1.24.exe <book_sales.txt>1.24.txt
  8. * 3. type 1.24.txt
  9. *
  10. *
  11. */
  12. int main()
  13. {
  14. Sales_item total;
  15. std::cout <<"Please input same data and then input not same data output and then input Ctrl+Z exit "<<std::endl;
  16. if (std::cin >> total)
  17. {
  18. Sales_item trans;
  19. int cnt = 1;
  20. while (std::cin >> trans)
  21. {
  22. if (total.isbn() == trans.isbn())
  23. {
  24. //total += trans;
  25. ++cnt;
  26. }
  27. else
  28. {
  29. std::cout << total.isbn() << " occours " << cnt << " times " <<std::endl;
  30. total = trans;
  31. cnt = 1;
  32. }
  33. }
  34. std::cout << total.isbn() << " occours " << cnt << " times " <<std::endl;
  35. }
  36. return 0;
  37. }

1.25

  1. #include <iostream>
  2. #include "Sales_item.h"
  3. int main()
  4. {
  5. Sales_item total;
  6. if (std::cin >> total)
  7. {
  8. Sales_item trans;
  9. while (std::cin >> trans)
  10. {
  11. if (total.isbn() == trans.isbn())
  12. {
  13. total += trans;
  14. }
  15. else
  16. {
  17. std::cout << total << std::endl;
  18. total = trans;
  19. }
  20. }
  21. std::cout << total << std::endl;
  22. }
  23. else
  24. {
  25. std::cerr << "No data?!" << std::endl;
  26. return -1;
  27. }
  28. return 0;
  29. }

最后的这个头文件也就是我们要导入的,买书的应该都知道怎么下,这里贴一下源码~~

Sales_item.h

  1. /*
  2. * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
  3. * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
  4. * copyright and warranty notices given in that book:
  5. *
  6. * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
  7. *
  8. *
  9. * "The authors and publisher have taken care in the preparation of this book,
  10. * but make no expressed or implied warranty of any kind and assume no
  11. * responsibility for errors or omissions. No liability is assumed for
  12. * incidental or consequential damages in connection with or arising out of the
  13. * use of the information or programs contained herein."
  14. *
  15. * Permission is granted for this code to be used for educational purposes in
  16. * association with the book, given proper citation if and when posted or
  17. * reproduced.Any commercial use of this code requires the explicit written
  18. * permission of the publisher, Addison-Wesley Professional, a division of
  19. * Pearson Education, Inc. Send your request for permission, stating clearly
  20. * what code you would like to use, and in what specific way, to the following
  21. * address:
  22. *
  23. * Pearson Education, Inc.
  24. * Rights and Permissions Department
  25. * One Lake Street
  26. * Upper Saddle River, NJ 07458
  27. * Fax: (201) 236-3290
  28. */
  29. /* This file defines the Sales_item class used in chapter 1.
  30. * The code used in this file will be explained in
  31. * Chapter 7 (Classes) and Chapter 14 (Overloaded Operators)
  32. * Readers shouldn't try to understand the code in this file
  33. * until they have read those chapters.
  34. */
  35. #ifndef SALESITEM_H
  36. // we're here only if SALESITEM_H has not yet been defined
  37. #define SALESITEM_H
  38. // Definition of Sales_item class and related functions goes here
  39. #include <iostream>
  40. #include <string>
  41. class Sales_item {
  42. // these declarations are explained section 7.2.1, p. 270
  43. // and in chapter 14, pages 557, 558, 561
  44. friend std::istream& operator>>(std::istream&, Sales_item&);
  45. friend std::ostream& operator<<(std::ostream&, const Sales_item&);
  46. friend bool operator<(const Sales_item&, const Sales_item&);
  47. friend bool
  48. operator==(const Sales_item&, const Sales_item&);
  49. public:
  50. // constructors are explained in section 7.1.4, pages 262 - 265
  51. // default constructor needed to initialize members of built-in type
  52. Sales_item() = default;
  53. Sales_item(const std::string &book): bookNo(book) { }
  54. Sales_item(std::istream &is) { is >> *this; }
  55. public:
  56. // operations on Sales_item objects
  57. // member binary operator: left-hand operand bound to implicit this pointer
  58. Sales_item& operator+=(const Sales_item&);
  59. // operations on Sales_item objects
  60. std::string isbn() const { return bookNo; }
  61. double avg_price() const;
  62. // private members as before
  63. private:
  64. std::string bookNo; // implicitly initialized to the empty string
  65. unsigned units_sold = 0; // explicitly initialized
  66. double revenue = 0.0;
  67. };
  68. // used in chapter 10
  69. inline
  70. bool compareIsbn(const Sales_item &lhs, const Sales_item &rhs)
  71. { return lhs.isbn() == rhs.isbn(); }
  72. // nonmember binary operator: must declare a parameter for each operand
  73. Sales_item operator+(const Sales_item&, const Sales_item&);
  74. inline bool
  75. operator==(const Sales_item &lhs, const Sales_item &rhs)
  76. {
  77. // must be made a friend of Sales_item
  78. return lhs.units_sold == rhs.units_sold &&
  79. lhs.revenue == rhs.revenue &&
  80. lhs.isbn() == rhs.isbn();
  81. }
  82. inline bool
  83. operator!=(const Sales_item &lhs, const Sales_item &rhs)
  84. {
  85. return !(lhs == rhs); // != defined in terms of operator==
  86. }
  87. // assumes that both objects refer to the same ISBN
  88. Sales_item& Sales_item::operator+=(const Sales_item& rhs)
  89. {
  90. units_sold += rhs.units_sold;
  91. revenue += rhs.revenue;
  92. return *this;
  93. }
  94. // assumes that both objects refer to the same ISBN
  95. Sales_item
  96. operator+(const Sales_item& lhs, const Sales_item& rhs)
  97. {
  98. Sales_item ret(lhs); // copy (|lhs|) into a local object that we'll return
  99. ret += rhs; // add in the contents of (|rhs|)
  100. return ret; // return (|ret|) by value
  101. }
  102. std::istream&
  103. operator>>(std::istream& in, Sales_item& s)
  104. {
  105. double price;
  106. in >> s.bookNo >> s.units_sold >> price;
  107. // check that the inputs succeeded
  108. if (in)
  109. s.revenue = s.units_sold * price;
  110. else
  111. s = Sales_item(); // input failed: reset object to default state
  112. return in;
  113. }
  114. std::ostream&
  115. operator<<(std::ostream& out, const Sales_item& s)
  116. {
  117. out << s.isbn() << " " << s.units_sold << " "
  118. << s.revenue << " " << s.avg_price();
  119. return out;
  120. }
  121. double Sales_item::avg_price() const
  122. {
  123. if (units_sold)
  124. return revenue/units_sold;
  125. else
  126. return 0;
  127. }
  128. #endif

C++PRIMER第五版练习题答案第一章的更多相关文章

  1. 《C++Primer》第五版习题答案--第一章【学习笔记】

    C++Primer第五版习题解答---第一章 ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考. 作者:cosefy Date: 2022/1/7 第一章:开始 练习1.3 #includ ...

  2. 《C++Primer》第五版习题答案--第二章【学习笔记】

    C++Primer第五版习题解答---第二章 ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考. 作者:cosefy Date: 2020/1/9 第二章:变量和基本类型 练习2.1: 类 ...

  3. C++Primer第五版——习题答案和解析

    感谢原文博主的分享:https://blog.csdn.net/misayaaaaa/article/details/53786215 新手入门必看的书.知识是一个系统化并且相互关联的体系,零散的东西 ...

  4. Windows程序设计(第五版)学习:第一章 起步

    第一章 起步 1,windows主要的三个动态库: kernel32.dll负责操作系统的传统工作,包括内存管理.文件输入以及任务管理等. user32.dll负责用户界面的操作,即所有窗口的管理 g ...

  5. C++Primer第五版——习题答案目录

    目前正在刷<C++Primer>这本书,会在博客上记录课后习题答案,答案仅供参考. 因为水平有限,如有有误之处,希望大家不吝指教,谢谢! 目录地址 使用的系统为:win 10,编译器:VS ...

  6. C++Primer第五版——习题答案详解(一)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第1章 开始&&第2章 变量和基本类型 练习1.3 #include&l ...

  7. C++Primer第五版——习题答案详解(五)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第6章 函数 练习6.4 #include<iostream> using ...

  8. C++Primer第五版——习题答案详解(十)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第11章 关联容器 练习11.3 #include<iostream> #i ...

  9. C++Primer第五版——习题答案详解(二)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第3章 字符串.向量和数组 练习3.2 一次读入一整行 #include<iost ...

随机推荐

  1. C语言之动态内存管理

    C语言之动态内存管理 大纲: 储存器原理 为什么存在动态内存的开辟 malloc() free() calloc() realloc() 常见错误 例题 柔性数组 零(上).存储器原理 之前我们提到了 ...

  2. 如何开发一个APP——转自知乎

    作者:简单点链接:https://www.zhihu.com/question/22999185/answer/155469014来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...

  3. C++并发与多线程学习笔记--互斥量、用法、死锁概念

    互斥量(mutex)的基本概念 互斥量的用法 lock(), unlock() std::lock_guard类模板 死锁 死锁演示 死锁的一般解决方案 std::lock()函数模板 std::lo ...

  4. js 日期加减

    加: console.log(moment().format("YYYY-MM-DD HH:mm:ss")); //当前时间 console.log(moment().add(10 ...

  5. [Fundamental of Power Electronics]-PART I-3.稳态等效电路建模,损耗和效率-3.4 如何获得模型的输入端口

    3.4 如何获得模型的输入端口 Fig 3.16 Buck converter example 让我们尝试使用3.3.3节的步骤来推导图3.16所示的Buck变换器的模型.电感绕组电阻同样由串联电阻\ ...

  6. BUAA_OS lab4 难点梳理

    BUAA_OS lab4 难点梳理 lab4体会到了OS难度的飞升.实验需要掌握的重点有以下: 系统调用流程 进程通信机制 fork 本lab理解难度较高,接下来将以以上三部分分别梳理. 系统调用 概 ...

  7. uni-app&H5&Android混合开发一 || 最全面的uni-app离线打包Android平台教程

    前言: 为什么会写这么一个教程,因为很久之前做过一个对接银行POS我们的系统是使用的H5开发的app应用.但是假如对结果银行相关业务的小伙伴应该都清楚,银行的业务相对于其他的对接方而言安全性比较高,而 ...

  8. jumpserver2

    测试环境 CPU: 64位双核处理器 内存: 4G DDR3 数据库:mysql 版本大于等于 5.6 mariadb 版本大于等于 5.5.6 环境 系统: CentOS 7 IP: 192.168 ...

  9. 洛谷P1422 小玉家的电费

    题目描述 夏天到了,各家各户的用电量都增加了许多,相应的电费也交的更多了.小玉家今天收到了一份电费通知单.小玉看到上面写:据闽价电[2006]27号规定,月用电量在150千瓦时及以下部分按每千瓦时0. ...

  10. PAT 乙级 -- 1007 -- 素数对猜想

    题目简述 让我们定义 dn 为:dn = pn+1 - pn,其中 pi 是第i个素数.显然有 d1=1 且对于n>1有 dn 是偶数."素数对猜想"认为"存在无穷 ...