C++primer[习题][第二章][31-42]

来源:互联网 发布:韩国中国聊天软件 编辑:程序博客网 时间:2024/04/29 06:40
  • 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 intconst int &p = x;       //p     const int &decltype(x) zz = x;     //zz    const int auto yy = x;            //yy    intdecltype(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
原创粉丝点击