IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> C++Primer第五版 ——— (ch1)课后习题参考答案 -> 正文阅读

[C++知识库]C++Primer第五版 ——— (ch1)课后习题参考答案

练习 1.3

编写程序,在标准输出上打印Hello, World。

#include <iostream>
using namespace std;
int main()
{
	char arr[10] = { 0 };
	char arr0[10] = { 0 };
	char arr1[] = "hello, world";
	cin >> arr;
	cin >> arr0;
	cout << arr << " " << arr0 << endl;
	cout << arr1 << endl;

	return 0;
}

在这里插入图片描述

练习 1.4

我们的程序使用加法运算符+来将两个数相加。编写程序使用惩罚运算符*,来打印两个数的积。

#include <iostream>
using namespace std;
int main()
{
	int a = 0, b = 0;
	cout << "please enter two number:";
	cin >> a >> b;
	cout << "the sum of " << a << " and " << b << " is " << a + b << endl;
	cout << "the multiply of " << a << " and " << b << " is " << a * b << endl;
	
	return 0;
}

在这里插入图片描述

练习1.5

我们将所有输出放在一条很长的语句中。重写程序,将每个运算对象的打印操作放在一条独立的语句中。

#include <iostream>
using namespace std;
int main()
{
	int a = 0;
	int b = 0;

	cout << "please enter two number:";
	cin >> a;
	cin >> b;
	cout << "the sum of ";
	cout << a;
	cout << " and ";
	cout << b;
	cout << " is ";
	cout << a + b;
	cout << endl;
	cout << "the multiply of ";
	cout << a;
	cout << " and ";
	cout << b;
	cout << " is ";
	cout << a * b;
	cout << endl;
	
	return 0;
}

在这里插入图片描述

练习1.6

解释下面的程序片段是否合法。

std::cout << "The sum of " << v1;
	  << "and " << v2;
	  << " is " << v1+v2 << std::endl;

不合法。前两行的末尾有分号,代表语句结束,第2、3两行为两条新的语句。而这两条语句在“<<”之前缺少了输出流,应在“<<”之前加上"cout"。
在这里插入图片描述
改为:
在这里插入图片描述

练习1.7

编译一个包含不正确的嵌套注释的程序,观察编译器返回的错误信息。

#include <iostream>
 
/*
* 注释对/* */不能嵌套
* “不能嵌套”几个字会被认为是源码,像剩余程序一样处理
*/
 
int main (void)
{
	return 0;
}

在这里插入图片描述

练习1.8

指出下列哪些输出语句是合法的(如果有的话)。

std::cout << "/*";
std::cout << "*/";
std::cout << /* "*/" */;
std::cout << /* "*/" /* "/*" */;

预测编译这些语句会产生什么样的结果,实际编译这些语句来验证你的答案(编写一个小程序,每次将上述一条语句作为其主体),改正每个编译错误。

第一条和第二条语句是合法的,第三条语句的第一个双引号和第四个双引号左引号被注释掉了,第二个双引号和第三个双引号之间的“*/”被认为是字符串的文字内容。
在这里插入图片描述

练习1.9

编写程序,使用while循环将50到100的整数相加。

#include <iostream>
using namespace std;
int main()
{
	int a = 50;
	int cut = 0;

	while (a <= 100)
	{
		cut += a;
		++a;
	}
	cout << "sum is " << cut << endl;
	
	return 0;
}

在这里插入图片描述

练习1.10

除了++运算符将运算对象的值增加1以外,还有一个递减运算符(–)实现将值减少1。编写程序,使用递减运算符在循环中按递减序打印出10到0之间的整数。

#include <iostream>
using namespace std;
void main()
{
	int i=10;
	while (i >= 0)
	{
		cout << i << endl;	
		--i;		
	}	
}

在这里插入图片描述

练习1.11

编写程序,提示用户输入两个整数,打印出这两个整数所指定的范围内的所有整数。

#include <iostream>
using namespace std;
int main()
{
	int a = 0;
	int b = 0;
	cout << "输入两个整数:";
	cin >> a >> b;
	if (a > b)
	{
		int t = 0;
		t = a;
		a = b;
		b = t;
	}
	while (a <= b)
	{
		cout << a << " ";
		++a;
	}
	return 0;
}

在这里插入图片描述

练习1.12

下面的for循环完成了什么功能?sum的终值是多少?

int sum = 0;
for (int i = -100; i <= 100; ++i)
	sum += i;

-100到100之间(包含-100和100)的整数相加,sum的终值为0。

在这里插入图片描述

练习1.14

对比for循环和while循环,两种形式的优缺点各是什么?

循环次数已知的情况下,for循环更为简洁;无法预知循环次数是,while循环更合适。

练习1.16

编写程序,从cin读取一组数,输出其和。

在这里插入图片描述
在这里插入图片描述

练习1.18

编译并运行本节的程序,给它输入全都相等的值。再次运行程序,输入没有重复的值。

#include <iostream>
using namespace std;
int main()
{
	int a = 0;
	int b = 0;
	if (cin >> a)
	{
		int cut = 1;
		while (cin >> b)
		{
			if (a == b)
			{
				++cut;
			}
			else
			{
				cout << a << "连续重复个数为:" << cut << "个" << endl;
				cut = 0;
				a = b;
			}
		}
		cout << a << "连续重复个数为:" << cut << "个" << endl;
	}
	return 0;
}

在这里插入图片描述
在这里插入图片描述

练习1.20

在网站http://www.informit.com/title/0321714113上,第1章的代码目录汇总包含了头文件Sales_item.h。将它拷贝到你自己的工作目录中。用它编写一个程序,读取一组书籍销售记录,将每条记录打印到标准输出上。

Sales_item.h

#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

#include <iostream>
#include "Sales_item.h"
using namespace std;
 
int main (void)
{
	Sales_item book;
	cout << "请输入销售记录:" << endl;
	while (cin >> book)
		cout << "ISBN、销售额和平均售价为 " << book << endl;
	return 0;
}

在这里插入图片描述

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2022-05-01 15:31:12  更:2022-05-01 15:32:17 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/21 2:46:11-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码