C++Primer 第五版 习题答案 第二章 变量和基本类型

2.1

类型 int、long、long long 和 short 的区别是什么?无符号类型和带符号类型的区别是什么?float 和 double的区别是什么?

c++规定int至少和一个short一样大,long至少和一个int一样大,long long至少和一个long一样大
最小尺寸:short 16位 int 16位 long32位 long long 位64

2.2

计算按揭贷款时,对于利率、本金和付款分别应选择何种数据类型?说明你的理由。

double,执行浮点数运算使用double,因为float通常精度不够而且双精度浮点数和单精度单精度浮点数计算代价相差无几。 long double提供的精度一般情况下没有不要且代价高。

2.3

读程序写结果

#include <iostream>
int main()
{
    unsigned u1 = 10, u2 = 42;
    std::cout << u1 - u2 << std::endl;
    std::cout << u2 - u1 << std::endl;
    int i1 = 10, i2 = 42;
    std::cout << i2 - i1 << std::endl;
    std::cout << i1 - i2 << std::endl;
    std::cout << i1 - u1 << std::endl;
    std::cout << u1 - i1 << std::endl;

    return 0;

}

2.4

同上

2.5

a)字符字面值,宽字符字面值,字符串字面值,宽字符串字面值;
b)整形字面值,无符号整形字面值,长整形字面值,无符号长整形字面值,八进制整形字面值,十六进制整形字面值;
c): 浮点型字面值,单精度浮点型字面值,扩展精度浮点型字面值;
d)整形字面值,无符号整形字面值,浮点型字面值,浮点型字面值。

2.6

a)十进制整形
b)八进制整形 八进制整形没有09

2.7

string
long double
非法,整形后不可加f
long double

2.8

#include <iostream>
int main()
{
    std::cout << 2<<"\115\012" << std::endl;
    std::cout << 2<<"\011\115\012" << std::endl;
    return 0;
}

2.9

非法,>>后不能定义
非法,列表初始化存在丢失数据风险,不能执行强制转换
非法,初始化应该分别进行
合法,可进行强制转化

2.10

global_str,local_str为空字符串;
global_int为0;
local_int未初始化,最好初始化(函数体内置类型变量不会被初始化)

2.11

定义
定义
声明

2.12

非法 关键字
合法
非法 关键字
非法 标识符数字开头
合法

2.13

100

2.14

100 45

2.15

合法
不合法,引用类型必须是一个对象
合法
不合法,引用必须初始化

2.16

合法
合法
合法
合法

2.17

#include <iostream>
int main()
{
	int i = 0, & r1 = i;
	i = 5;
	r1 = 10;
	std::cout << i << " " << r1 << std::endl;
}

10 10

2.18

#include <iostream>
int main()
{
	int a = 0, b = 1;
	int* p1 = &a, * p2 = &b;
	p1 = p2;
	std::cout << *p1<<std::endl;
	*p1 = 100;
	std::cout << *p1<<std::endl;
}

2.19

1.指针本身就是一个对象,允许指针的拷贝和赋值,指针的生命周期可以先后指向不同对象
2.指针无须在定义时赋初值

2.20

i赋初值42
p1指向i
改变p1指向的对象i的值 (42*42)

2.21

非法 double* int*
非法 int* int
合法

2.22

指针不是空指针
指针指向的对象不为0

2.23

不能,不能判断指针是否有效

2.24

void* 可以指向任何类型对象
long*不能指向int类型对象

2.25

a)int类型指针 int类型 int类型引用
b)int类型 空指针
c)int类型指针 int类型

2.26

不合法 const int 必须初始化
合法
合法
合法 不合法constint不能改变

2.27

a)不合法 0 是常量,&r不是对常量的引用
b)不合法 若果i2是const int,不对
c)合法
d)合法
e)合法
f)不合法 引用需要初始化
g)合法

2.28

a)不合法。const指针必须初始化
b)不合法。const指针必须初始化
c)不合法。常量ic没有初始化
d)不合法。const指针必须初始化
e)合法,指向常量的指针可以不初始化

2.29

a)合法
b)不合法 p3常量指针
c)不合法。ic是const int
d)不合法。p3常量指针,不能再次赋值
e)非法。p2是常量指针,不能再赋值
f) 非法,ic是const int

2.30

v2不能改变,是顶层const
v1都不是
p1都不是
r1都不是
p2指向的对象不能改变,p2可改变 是底层const
p3既是顶层有是底层
r2是底层

2.31

合法 v2是顶层const
不合法 p2是底层const
合法
不合法
合法

2.32

非法

#include <iostream>
int main()
{
	int null = 0,*p = &null;
}
	

2.33

合法 合法 合法 非法 非法 非法

2.34

#include <iostream>

int main()
{
    int i = 0, & r = i;
    auto a = r;   // a is an int (r is an alias for i, which has type int)

    const int ci = i, & cr = ci;
    auto b = ci; // b is an int (top-level const in ci is dropped)
    auto c = cr; // c is an int (cr is an alias for ci whose const is top-level)
    auto d = &i; // d is an int* (& ofan int objectis int*)
    auto e = &ci; // e is const int*(& of a const object is low-level const)

    const auto f = ci; // deduced type of ci is int; f has type const int
    auto& g = ci; // g is a const int& that is bound to ci

    a = 42; b = 42; c = 42; *d = 42; e = &c;

    return 0;
}

2.35

j int
k 常量int的引用
p 常量int的指针
j2 常量int
k2 常量int的引用

2.36

a int
b int
c int
d int&
4 4 4 4

2.37

a int 3
b int 4
c int 3
d in& 3

2.38

如果使用引用类型,auto会识别为其引用对象的类型;
decltype会识别为引用的类型;
decltype(())的差别;
顶层const差异。

2.39

类后没有;

2.40

struct Sale_data
{
    std::string bookNo;
    std::string bookName;
    unsigned units_sold = 0;
    double revenue = 0.0;
    double price = 0.0;
};

2.41

#include <iostream>
#include <string>

struct Sales_data
{
    std::string bookNo;
    unsigned units_sold = 0;
    double revenue = 0.0;
};

int main()
{
    Sales_data total;
    double totalPrice;
    if (std::cin >> total.bookNo >> total.units_sold >> totalPrice)
    {
        total.revenue = total.units_sold * totalPrice;

        Sales_data trans;
        double transPrice;
        while (std::cin >> trans.bookNo >> trans.units_sold >> transPrice)
        {
            trans.revenue = trans.units_sold * transPrice;

            if (total.bookNo == trans.bookNo)
            {
                total.units_sold += trans.units_sold;
                total.revenue += trans.revenue;
            }
            else
            {
                std::cout << total.bookNo << " " << total.units_sold << " " << total.revenue << " ";
                if (total.units_sold != 0)
                    std::cout << total.revenue / total.units_sold << std::endl;
                else
                    std::cout << "(no sales)" << std::endl;

                total.bookNo = trans.bookNo;
                total.units_sold = trans.units_sold;
                total.revenue = trans.revenue;
            }
        }

        std::cout << total.bookNo << " " << total.units_sold << " " << total.revenue << " ";
        if (total.units_sold != 0)
            std::cout << total.revenue / total.units_sold << std::endl;
        else
            std::cout << "(no sales)" << std::endl;

        return 0;
    }
    else
    {
        std::cerr << "No data?!" << std::endl;
        return -1;  // indicate failure
    }
}

2.42
头文件

#ifndef SALES_DATA_H_
#define SALES_DATA_H_

#include <string>

struct Sales_data
{
    std::string bookNo;
    unsigned units_sold = 0;
    double revenue = 0.0;
};

#endif

#include <string>
#include <iostream>
#include "Sales_data.h"

int main()
{
	Sales_data data1, data2;
	double price = 0;

	std::cin >> data1.bookNo >> data1.units_sold >> price;
	data1.revenue = data1.units_sold * price;
	std::cin >> data2.bookNo >> data2.units_sold >> price;
	data2.revenue = data2.units_sold * price;

	if(data1.bookNo == data2.bookNo)
	{
		unsigned totalCnt = data1.units_sold + data2.units_sold;
		double totalRevenue = data1.revenue + data2.revenue;
		std::cout << data1.bookNo << " " << totalCnt << " " << totalRevenue << " ";
		if(totalCnt != 0)
			std::cout << totalRevenue/totalCnt << std::endl;
		else
			std::cout << "(no sales)" << std::endl;
		return 0;
	}else
	{
		std::cerr << "Data must refer to the same ISBN" << std::endl;
		return -1;
	}

	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值