C++primer[習題][第二章][31-42]

  • 2.31

r1 = v2; //合法。

p1 = p2; //不合法,p2爲底層const,需要p1也爲底層const。

p2 = p1; //合法,int指針可以轉化爲const int指針。

p1 = p3; //不合法,p3爲底層const,需要p1也爲底層const。

p2 = p3; //合法。

  • 2.32

int null = 0, *p = null;
不合法,int類型的值無法初始化int型指針。
int null = 0, *p = nullptr;

  • 2.33

a = 42; //a賦值42

b = 42; //b賦值42

c = 42; //c賦值42

d = 42; //錯誤,d爲int型指針。

e = 42; //錯誤,e爲const int型指針。

g = 42; //錯誤,g引用的類型爲const int。不能改變值。

  • 2.34
#include <iostream>

using std::cin;
using std::cout;
using std::endl;

int main()
{
    int i = 0, &r = i;
    auto a = r;
    const int ci = i, &cr = ci;
    auto b = ci;
    auto c = cr;
    auto d = &i;
    auto e = &ci;
    auto &g = ci;
    cout << a << endl;
    cout << b << endl;
    cout << c << endl;
    cout << d << endl;
    cout << e << endl;
    cout << g << endl;
    a = 42;
    b = 42;
    c = 42;
    d = 42; //報錯
    e = 42; //報錯
    g = 42; //報錯
    cout << a << endl;
    cout << b << endl;
    cout << c << endl;
    cout << d << endl;
    cout << e << endl;
    cout << g << endl;
    system("pause");
    return 0;
}
  • 2.35

const int i = 42; //i爲int型常量。

auto j = i; const auto &k = i; auto *p = &i; //j爲int型變量,k爲const int型引用,p爲const int型指針。

const auto j2 = i, &k2 = i; //j2爲int型常量,k2爲const int型引用。

  • 2.36
int a = 3, b = 4;       //a,b爲int型變量。
decltype(a) c = a;      //c爲int型變量,值爲a。
decltype((b))d = a;     //d爲int型引用,引用a。
++c;
++d;

程序結束後。
a=4,b=4,c=4,d=4;

  • 2.37
int a = 3, b = 4;       //a,b爲int型變量
decltype(a) c = a;      //a爲int型變量,所以c爲int型變量。
decltype(a = b) d = a;  //d爲int引用,引用a。
  • 2.38

declype與auto處理頂層const和引用不同,decltype會保留頂層const和引用。

const int x = 1;        //x     const int
const int &p = x;       //p     const int &
decltype(x) zz = x;     //zz    const int 
auto yy = x;            //yy    int

decltype(p) zzz = x;    //zzz   const int & 
auto yyy = x;           //yyy   int
  • 2.39

會報錯,提示缺少分號。

  • 2.40
struct Sales_data
{
    std::string bookNo;
    std::string bookName;
    unsigned units_sold = 0;
    double revenue = 0.0;
    /*...*/
};
  • 2.41

1.5.1

#include <iostream>
#include <string>

using std::cin;
using std::cout;
using std::endl;

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

int main()
{
    Sales_data book;
    double num;
    cin >> book.bookNo >> book.units_sold >> num;
    book.revenue = book.units_sold*num;
    cout << book.bookNo <<" "<< book.revenue;
    return 0;
}

1.5.2

#include <iostream>
#include <string>

using std::cin;
using std::cout;
using std::endl;

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

int main()
{
    Sales_data book1, book2;
    double num1, num2;
    cin >> book1.bookNo >> book1.units_sold >> num1;
    cin >> book2.bookNo >> book2.units_sold >> num2;
    book1.revenue = book1.units_sold*num1;
    book2.revenue = book2.units_sold*num2;
    if (book1.bookNo == book2.bookNo)
    {
        unsigned tottalCnt = book1.units_sold + book2.units_sold;
        double totalRevenue = book1.revenue + book2.revenue;
        cout << book1.bookNo << " " << tottalCnt << " " << totalRevenue << endl;
        if (tottalCnt != 0)
            cout << totalRevenue / tottalCnt << endl;
        else
            cout << "no sales" << endl;
        return 0;
    }
    else
    {
        cout << "Data must refer to the same ISBN" << endl;
        return -1;
    }
}

1.6

#include <iostream>
#include <string>

using std::cin;
using std::cout;
using std::endl;

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

int main()
{
    Sales_data total;
    int totalnum;
    if (cin >> total.bookNo >> total.units_sold >> totalnum)
    {
        total.revenue = total.units_sold*totalnum;
        Sales_data trans;
        int transnum;
        if (cin >> trans.bookNo >> trans.units_sold >> transnum)
        {
            trans.revenue = trans.units_sold*transnum;
            if (total.bookNo == trans.bookNo)
            {
                total.units_sold += trans.units_sold;
                total.revenue += trans.revenue;
            }
            else
            {
                cout << total.bookNo << " " << total.units_sold << " " << total.revenue << endl;
                total.bookNo = trans.bookNo;
                total.revenue = trans.revenue;
                total.units_sold = trans.units_sold;
            }
        }
        cout << total.bookNo << " " << total.units_sold << " " << total.revenue << endl;
    }
    else
    {
        cout << "No data?" << endl;
        return -1;
    }
    return 0;
}
  • 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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章