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

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

1.1

int main()
{
return 0;
}

1.2

int main()
{
return -1;
}

1.3

#include <iostream>
int main()
{
std::cout<<"Hello,World"<<std::endl;
return 0;
}

1.4

#include <iostream>
int main()
{
int i=0,j=0;
std::cin>>i>>j;
std::cout<<"The ji of "<<i<<" * "<<j<<" is "<<i*j<<std::endl;
return 0;
}

1.5

#include <iostream>
int main()
{
int i=0,j=0;
std::cin>>i>>j;
std::cout<<"The ji of ";
std::cout<<i;
std::cout<<" * ";
std::cout<<j;
std::cout<<" is ";
std::cout<<i*j<<std::endl;
return 0;
}

1.6

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

1.7

/*
*下列行为不允许发生,注释界定符不可嵌套
* /* */
*
* / int main()
{
return 0;
}

1.8

#include <iostream>
int main()
{
std::cout<<"/*"; //可
std::cout<<"*/"; //可
std::cout<</*"*/"*/; //不可
std::cout<</*"*/"/*"/*"*/; //可
}

1.9

#include <iostream>
int main()
{
int sum=0,val=50;
while (val<=100)
{
sum+=val;
++val;
}
std::cout<<"The sum of 50 to 100 is "<<sum<<std::endl;
return 0; }

1.10

#include <iostream>
int main()
{
int val=10;
while (val>=0)
{
std::cout<<"value is "<<val<<"\n"<<std::endl;
--val;
}
return 0; }

1.11++

#include <iostream>
int main()
{
int i=0,j=0;
std::cout<<"please input two numbers and i will give you the num in thems"<<std::endl;
std::cin>>i>>j;
if (i>=j)
{
while (i>=j)
{
std::cout<<"value is "<<i<<"\n"<<std::endl;
--i;
}
}
else
{
while (i<=j)
{
std::cout<<"value is "<<j<<"\n"<<std::endl;
--j;
}
}
return 0; }

1.12

#include <iostream>
int main()
{
int sum=0;
for(int i=-100;i<=100;++i)
{
sum+=i;
}
std::cout<<"The sum is "<<sum<<std::endl;
}

1.13

#include <iostream>
int main()
{
int sum=0,val=50;
for(;val<=100;++val)
sum+=val;
std::cout<<"The sum of 50 to 100 is "<<sum<<std::endl; int valu=10;
for (;valu>=0;--valu)
std::cout<<"value is "<<valu<<"\n"<<std::endl;
int i=0,j=0;
std::cout<<"please input two numbers and i will give you the num in thems"<<std::endl;
std::cin>>i>>j;
if (i>=j)
{
for (;i>=j;--i)
std::cout<<"value is "<<i<<"\n"<<std::endl;
}
else
{
for (;i<=j;--j)
std::cout<<"value is "<<j<<"\n"<<std::endl;
}
return 0;
}

1.14

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

1.15

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

1.16

#include <iostream>
int main()
{
int sum=0,value = 0;
std::cout<<"I will print your sum and you can input not integer num to end\n";
while (std::cin>>value)
sum+=value;
std::cout<<"The sum is "<<sum<<std::endl;
return 0;
}

1.17&1.18

#include <iostream>
int main()
{
int currVal = 0, val = 0;
if (std::cin >> currVal)
{
int cnt = 1;
while (std::cin >> val)
{
if (val == currVal)
{
++cnt;
}
else
{
std::cout << currVal << " occurs " << cnt << " times " << std::endl;
currVal = val;
cnt = 1;
}
}
std::cout << currVal << " occurs " << cnt << " times " << std::endl;
}
return 0;
}

1.19

#include <iostream>
int main()
{
int i=0,j=0;
std::cout<<"please input two numbers and i will give you the num in thems"<<std::endl;
std::cin>>i>>j;
if (i>=j)
{
while (i>=j)
{
std::cout<<"value is "<<i<<"\n"<<std::endl;
--i;
}
}
else
{
while (i<=j)
{
std::cout<<"value is "<<j<<"\n"<<std::endl;
--j;
}
}
return 0; }

1.20

#include <iostream>
#include "Sales_item.h"
int main()
{
Sales_item book;
std::cout <<"input Ctrl+Z exit "<<std::endl;
while (std::cin >> book)
{
std::cout << book <<std::endl;
}
return 0;
}

1.21

#include <iostream>
#include "Sales_item.h"
int main()
{
Sales_item book1,book2;
std::cin >> book1 >> book2;
if (book1.isbn() == book2.isbn())
{
std::cout << book1 + book2 << std::endl;
return 0;
}
else
{
std::cout << "Please input have same isbn data" << std::endl;
return -1;
}
}

1.22

#include <iostream>
#include "Sales_item.h"
int main()
{
Sales_item total;
std::cout <<"Please input same data and then input not same data output and then input Ctrl+Z exit "<<std::endl;
if (std::cin >> total)
{
Sales_item trans;
while (std::cin >> trans)
{
if (total.isbn() == trans.isbn())
{
total += trans;
}
else
{
std::cout << total <<std::endl;
}
}
}
return 0;
}

1.23

#include <iostream>
#include "Sales_item.h"
int main()
{
Sales_item total;
std::cout <<"Please input same data and then input not same data output and then input Ctrl+Z exit "<<std::endl;
if (std::cin >> total)
{
Sales_item trans;
int cnt = 1;
while (std::cin >> trans)
{
if (total.isbn() == trans.isbn())
{
//total += trans;
++cnt;
}
else
{
std::cout << total.isbn() << " occours " << cnt << " times " <<std::endl;
total = trans;
cnt = 1;
}
}
std::cout << total.isbn() << " occours " << cnt << " times " <<std::endl;
}
return 0;
}

1.24

#include <iostream>
#include "Sales_item.h"
/**
* @brief 文件重定向: $ addItems <infile>outfile
* 此处我们控制台查看一下测试的文件内容,然后再重定向,再看一下重定向里的内容
* 1. type book_sales.txt
* 2. 1.24.exe <book_sales.txt>1.24.txt
* 3. type 1.24.txt
*
*
*/ int main()
{
Sales_item total;
std::cout <<"Please input same data and then input not same data output and then input Ctrl+Z exit "<<std::endl;
if (std::cin >> total)
{
Sales_item trans;
int cnt = 1;
while (std::cin >> trans)
{
if (total.isbn() == trans.isbn())
{
//total += trans;
++cnt;
}
else
{
std::cout << total.isbn() << " occours " << cnt << " times " <<std::endl;
total = trans;
cnt = 1;
}
}
std::cout << total.isbn() << " occours " << cnt << " times " <<std::endl;
}
return 0;
}

1.25

#include <iostream>
#include "Sales_item.h"
int main()
{
Sales_item total;
if (std::cin >> total)
{
Sales_item trans;
while (std::cin >> trans)
{
if (total.isbn() == trans.isbn())
{
total += trans;
}
else
{
std::cout << total << std::endl;
total = trans;
}
}
std::cout << total << std::endl;
}
else
{
std::cerr << "No data?!" << std::endl;
return -1;
}
return 0;
}

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

Sales_item.h

/*
* This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
* Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
* copyright and warranty notices given in that book:
*
* "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
*
*
* "The authors and publisher have taken care in the preparation of this book,
* but make no expressed or implied warranty of any kind and assume no
* responsibility for errors or omissions. No liability is assumed for
* incidental or consequential damages in connection with or arising out of the
* use of the information or programs contained herein."
*
* Permission is granted for this code to be used for educational purposes in
* association with the book, given proper citation if and when posted or
* reproduced.Any commercial use of this code requires the explicit written
* permission of the publisher, Addison-Wesley Professional, a division of
* Pearson Education, Inc. Send your request for permission, stating clearly
* what code you would like to use, and in what specific way, to the following
* address:
*
* Pearson Education, Inc.
* Rights and Permissions Department
* One Lake Street
* Upper Saddle River, NJ 07458
* Fax: (201) 236-3290
*/ /* This file defines the Sales_item class used in chapter 1.
* The code used in this file will be explained in
* Chapter 7 (Classes) and Chapter 14 (Overloaded Operators)
* Readers shouldn't try to understand the code in this file
* until they have read those chapters.
*/ #ifndef SALESITEM_H
// we're here only if SALESITEM_H has not yet been defined
#define SALESITEM_H // Definition of Sales_item class and related functions goes here
#include <iostream>
#include <string> class Sales_item {
// these declarations are explained section 7.2.1, p. 270
// and in chapter 14, pages 557, 558, 561
friend std::istream& operator>>(std::istream&, Sales_item&);
friend std::ostream& operator<<(std::ostream&, const Sales_item&);
friend bool operator<(const Sales_item&, const Sales_item&);
friend bool
operator==(const Sales_item&, const Sales_item&);
public:
// constructors are explained in section 7.1.4, pages 262 - 265
// default constructor needed to initialize members of built-in type
Sales_item() = default;
Sales_item(const std::string &book): bookNo(book) { }
Sales_item(std::istream &is) { is >> *this; }
public:
// operations on Sales_item objects
// member binary operator: left-hand operand bound to implicit this pointer
Sales_item& operator+=(const Sales_item&); // operations on Sales_item objects
std::string isbn() const { return bookNo; }
double avg_price() const;
// private members as before
private:
std::string bookNo; // implicitly initialized to the empty string
unsigned units_sold = 0; // explicitly initialized
double revenue = 0.0;
}; // used in chapter 10
inline
bool compareIsbn(const Sales_item &lhs, const Sales_item &rhs)
{ return lhs.isbn() == rhs.isbn(); } // nonmember binary operator: must declare a parameter for each operand
Sales_item operator+(const Sales_item&, const Sales_item&); inline bool
operator==(const Sales_item &lhs, const Sales_item &rhs)
{
// must be made a friend of Sales_item
return lhs.units_sold == rhs.units_sold &&
lhs.revenue == rhs.revenue &&
lhs.isbn() == rhs.isbn();
} inline bool
operator!=(const Sales_item &lhs, const Sales_item &rhs)
{
return !(lhs == rhs); // != defined in terms of operator==
} // assumes that both objects refer to the same ISBN
Sales_item& Sales_item::operator+=(const Sales_item& rhs)
{
units_sold += rhs.units_sold;
revenue += rhs.revenue;
return *this;
} // assumes that both objects refer to the same ISBN
Sales_item
operator+(const Sales_item& lhs, const Sales_item& rhs)
{
Sales_item ret(lhs); // copy (|lhs|) into a local object that we'll return
ret += rhs; // add in the contents of (|rhs|)
return ret; // return (|ret|) by value
} std::istream&
operator>>(std::istream& in, Sales_item& s)
{
double price;
in >> s.bookNo >> s.units_sold >> price;
// check that the inputs succeeded
if (in)
s.revenue = s.units_sold * price;
else
s = Sales_item(); // input failed: reset object to default state
return in;
} std::ostream&
operator<<(std::ostream& out, const Sales_item& s)
{
out << s.isbn() << " " << s.units_sold << " "
<< s.revenue << " " << s.avg_price();
return out;
} double Sales_item::avg_price() const
{
if (units_sold)
return revenue/units_sold;
else
return 0;
}
#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. 【MCU】国民N32固件库移植

    目录 前言 移植N32Gxxx系列要点 前言 链接: 李柱明博客 移植AT32库&FreeRTOS教程 由于大部分国产MCU移植固件库.RTOS源码都是差不多的,所以本文不讲细节,如想熟悉移植 ...

  2. java例题_16 九九乘法表

    1 /*题目:输出 9*9 口诀. 2 程序分析:分行与列考虑,共 9 行 9 列,i 控制行,j 控制列. 3 */ 4 5 /*分析 6 * 用两侧for循环,外层循环还要控制换行 7 * 换行时 ...

  3. Android studio 简易登录界面

    •参考资料 [1]:视频资源 [2]:Android TextView设置图标,调整图标大小 •效果展示图 •前置知识 TextView EditText Button 以及按压效果,点击事件 •出现 ...

  4. 冒泡算法(BubbleSort)

    /*冒泡排序原理 比较相邻的元素.如果前一个元素比后一个元素大,就交换这两个元素的位置. 对每一对相邻元素做同样的工作,从开始第一对元素到结尾的最后一对元素.最终最后位置的元素就是最大值.实现步骤 1 ...

  5. [状压DP]炮兵阵地

    炮 兵 阵 地 炮兵阵地 炮兵阵地 题目描述 司令部的将军们打算在 N ∗ M N*M N∗M的网格地图上部署他们的炮兵部队.一个 N ∗ M N*M N∗M的地图由 N N N行 M M M列组成, ...

  6. 将Java编译为本地代码

    将Java编译为本地代码 通常Java程序的执行流程为:将Java代码编译为Byte Code(字节码),然后JVM执行引擎执行编译好的Byte Code.这是一种中间语言的特性,它的好处就是可以做到 ...

  7. [Fundamental of Power Electronics]-PART II-8. 变换器传递函数-8.1 Bode图回顾

    8.0 序 工程设计过程主要包括以下几个过程: 1.定义规格与其他设计目标 2.提出一个电路.这是一个创造性的过程,需要利用工程师的实际见识和经验. 3.对电路进行建模.变换器的功率级建模方法已经在第 ...

  8. lms微服务框架主机介绍

    目录 概念 .net的通用主机 .net的web主机 lms的业务主机类型 用于托管业务应用的普通主机 1. 创建一个应用台程序 2. 安装Silky.Lms.NormHost包 3. 注册LMS服务 ...

  9. 通过Dapr实现一个简单的基于.net的微服务电商系统(二)——通讯框架讲解

    首先感谢张队@geffzhang公众号转发了上一篇文章,希望广大.neter多多推广dapr,让云原生更快更好的在.net这片土地上落地生根. 目录:一.通过Dapr实现一个简单的基于.net的微服务 ...

  10. 20 行简单实现一个 unstated-next 🎅

    前言 unstated-next 基于 React 心智模型(hook+context)而设计的状态管理. 在 react hook 出现之前,有基于单一数据源,使用纯函数修改状态的 redux &a ...