1. 程式人生 > >C++Primer第五版 習題答案 第二章 變數和基本型別(variables and basic types)

C++Primer第五版 習題答案 第二章 變數和基本型別(variables and basic types)

2.1

C++語言規定一個int至少和一個short一樣大,一個long至少和一個int一樣大,一個long long至少和一個long一樣大。每個的最小尺寸分別為:short,16位;int,16位;long,32位;long long,64位。
除去布林型和擴充套件的字元型外,其他整形可以劃分為帶符號的和無符號的兩種。帶符號型別可以表示正數、負數或0,無符號型別則僅能表示大於等於0的值。
float最小尺寸為6位有效值,double最小尺寸為10位有效值。

2.2

都選用double,利率4.5%=0.045,本金和付款金額都是以元為單位,錢的最小金額是分,需要精確到小數點後兩位,所以都選用double。

2.3

4294967264
32
32
-32
0
0

2.4

#include <iostream>

int main()
{
    unsigned int u = 10, u2 = 42;
    std::cout << u - u2 << std::endl;
    std::cout << u2 - u << std::endl;

    int i = 10, i2 = 42;
    std::cout << i2 - i << std::endl;
    std::cout
<< i - i2 << std::endl; std::cout << i - u << std::endl; std::cout << u - i << std::endl; return 0; }

2.5

(a)字元字面值,寬字元字面值,字串字面值,寬字串字面值;
(b)整形字面值,無符號整形字面值,長整形字面值,無符號長整形字面值,八進位制整形字面值,十六進位制整形字面值;
(c)浮點型字面值,單精度浮點型字面值,擴充套件精度浮點型字面值;
(d)整形字面值,無符號整形字面值,浮點型字面值,浮點型字面值。

2.6

(a)十進位制整形;
(b)八進位制整形,八進位制總沒有09。

2.7

(a)string
(b)long double
(c)非法,整形字面值不可加字尾f
(d)long double

2.8

#include <iostream>

int main()
{
    std::cout << 2 << "\115\012";
    std::cout << 2 << "\011\115\012";

    return 0;
}

2.9

(編譯時記得使用C++11標準編譯”-std=c++11”)
(a)非法,>>運算子後不能定義;
(b)非法,不能執行強制轉換;
(c)非法,同一語句的初始化應該分別進行;
(d)合法,已強制轉換。

2.10

global_str,local_str為空字串
global_int為0
local_int未初始化,沒有初始值(測試下來也是0,最好都初始化掉)

2.11

(a)定義;
(b)定義;
(c)宣告。

2.12

(a)非法,關鍵詞;
(b)合法;
(c)非法;
(d)非法,字母、下劃線開頭;
(e)合法。

2.13

100

2.14

100 45

2.15

(a)合法;(b)不合法,引用型別的初始值必須是一個物件;
(c)合法;(d)不合法,引用型別必須初始化。

2.16

(a)合法
(b)合法
(c)合法
(d)合法

2.17

10 10

2.18

int a = 0, b = 1;
int *p1 = &a, *p2 = p1;

// change the value of a pointer.
p1 = &b;
// change the value to which the pointer points
*p2 = b;

2.19

1.指標本身就是一個物件,允許對指標賦值和拷貝,而且在指標的生命週期內它可以先後指向幾個不同的物件;2.指標無須在定義時賦初值。

2.20

p指向i,i最後的值為1746(42*42)。

2.21

(a)非法,一個是double*,一個是int*;
(b)非法,一個是int*,一個是int;
(c)合法。

2.22

指標是不是空指標
指標所指的物件是不是0

2.23

不能,不能判斷指標是否有效

2.24

型別不一樣,void*可以表示任何型別的物件

2.25

(a)指向int的指標,int型別,int的引用;
(b)int型別,int型別的指標;
(c)int型別指標,int型別。

2.26

(a)不合法,const int必須初始化;
(b)合法;
(c)合法;
(d)++cnt,合法;++sz,不合法,const int不能改變。

2.27

(a)0是常量,&r不是對常量的引用,所以可以改變,這顯然是不對的;
(b)如果i2是const int,這是不對的;
(c)合法;
(d)合法;
(e)合法;
(f)不合法,沒有初始化;
(g)合法。

2.28

(a)不合法,常量指標未初始化;
(b)不合法,常量指標未初始化;
(c)不合法,常量ic未初始化;
(d)不合法,常量指標未初始化;
(e)合法,指向常量的指標可以不初始化。

2.29

(a)合法;
(b)非法,p3是指向const int的指標;
(c)非法,ic是const int;
(d)非法,p3是常量指標,不能再次賦值;
(e)非法,p2是常量指標,不能再次賦值;
(f)非法,ic是const int。

2.30

v2不能改變,是頂層const;v1都不是;p1都不是,r1都不是;p2所指的物件不能改變,底層const;p3既是頂層,又是底層;r2底層。

2.31

r1 = v2; // 合法,v2為頂層const
p1 = p2; // 非法,p2為底層const
p2 = p1; // 合法
p1 = p3; // 非法
p2 = p3; // 合法

2.32

非法,int null = 0, *p = &null;

2.33

a=42; // 合法
b=42; // 合法
c=42; // 合法
d=42; // 非法
e=42; // 非法
g=42; // 非法

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的引用;常量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 int & 3

2.38

如果使用引用型別,auto會識別為其引用物件的型別,
decltype會識別為引用的型別。
decltype(())的差別。
頂層const差異

2.39

struct Foo { /* empty  */ } // Note: no semicolon
int main()
{
    return 0;
}

Error message: [Error] expected ‘;’ after struct definition

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

1.5.1(這裡有3題,我這裡只簡單實現該型別的輸入、相加和輸出)

#include <string>
#include <iostream>

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

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;
}

1.5.2和1.6(這裡也有3題,我這裡只實現讀取多條銷售記錄,並統計每個ISBN有幾條銷售記錄,每個ISBN的記錄聚在一起)

#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

Sales_data.h

#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

1.5.1(這裡有3題,我這裡只簡單實現該型別的輸入、相加和輸出)

#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;
}

1.5.2和1.6(這裡也有3題,我這裡只實現讀取多條銷售記錄,並統計每個ISBN有幾條銷售記錄,每個ISBN的記錄聚在一起)

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

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
    }
}