C++ Primer (第五版)第二章习题部分答案

文章介绍了作者自学C++过程中遇到的部分习题,包括基本算术运算、转义序列、指针操作、类型推断、类定义及实例等,旨在帮助读者理解和实践C++语言的基础知识。
摘要由CSDN通过智能技术生成

    在我自学C++过程中,我选择了C++Primer这本书,并对部分代码习题进行了求解以及运行结果。接下来几个月我将为大家定时按章节更新习题答案与运行结果,运行环境(Visual Studio Code,windows 11):

C++ Primer (第五版)习题答案总目录:

https://blog.csdn.net/weixin_48467622/article/details/136418131?spm=1001.2014.3001.5501

目录

2.3.读程序写结果

2.8.请利用转义序列编写一段程序,要求先输出2M,然后转到新一行。修改程序使其先输出2,然后输出制表符,再输出M,最后转到新一行。

2.14.下面的程序合法么?如果合法,它将输出什么?

2.17.执行下面这段代码输出什么结果?

2.18.编写代码分别更改指针的值以及指针所指对象的值.

2.20.请叙述下面这段代码的作用.

2.23.给定指针p,你能知道它是否指向了一个合法的对象吗?如果能,叙述判断的思路;如果不能,也请说明原因.

2.34.基于上一个练习中的变量和语句编写一段程序,输出赋值前后变量的内容,你刚才的推断正确吗?如果不对,请反复研读本节的示例直到你明白错在何处为止。

2.35.判断下列定义推断出的类型是什么,然后编写程序进行验证。

2.36.关于下面的代码,请指出每一个变量的类型以及程序结束时它们各自的值。

2.39.编译下面的程序观察其运行结果,注意,如果忘记写类定义体后面的分号会发生什么情况?记录下相关的信息,以后可能会有用。

2.40.根据自己的理解写出 Sales_data 类,最好与书中的例子有所区别。

2.41.使用你自己的Sale_data类重写1.5.1节(第20页)、1.5.2节(第21页)和1.6节(第22页)的练习。眼下先把Sales_data类的定义和main函数放在一个文件里。

1.5.1.仅实现输入、相同类型相加、输出

1.5.2.读取多条销售记录,并统计每个ISBN有几条销售记录,每个ISBN的记录聚在一起。

2.6.3.练习2.42:根据你自己的理解重写一个Sale_data.h头文件,并以此为基础重做2.6.2节(第67页)的练习。

1.5.1.仅实现输入、相同类型相加、输出.

1.5.2.读取多条销售记录,并统计每个ISBN有几条销售记录,每个ISBN的记录聚在一起。


2.3.读程序写结果

#include <iostream>
using namespace std;
int main()
{
   unsigned u = 10,u2 = 42;
   cout << u2 - u <<endl;
   cout << u - u2 <<endl;
   int i = 10, i2 = 42;
   cout << i2 - i << endl;
   cout << i - i2 << endl;
   cout << i - u << endl;
   cout << u - i << endl;
   return 0;
}

2.8.请利用转义序列编写一段程序,要求先输出2M,然后转到新一行。修改程序使其先输出2,然后输出制表符,再输出M,最后转到新一行。

#include <iostream>
using namespace std;
int main()
{
  cout << 2 << "\115\012";
  cout << 2 << "\t\115\012";
  return 0;
}

2.14.下面的程序合法么?如果合法,它将输出什么?

#include <iostream>
using namespace std;
int main()
{
  int i = 100,sum = 0;
  for(int i = 0;i != 10 ; i++)
                sum += i;
  cout<<i<<" "<<sum<<endl;
  return 0;
}

2.17.执行下面这段代码输出什么结果?

#include <iostream>
using namespace std;
int main()
{
  int i,&ri = i;
  i = 5;
  ri = 10;
  cout<< i << " " << ri;
  return 0;
}

2.18.编写代码分别更改指针的值以及指针所指对象的值.

#include <iostream>
using namespace std;
int main()
{
  int a = 6 , b = 66;
  int *p = &a;
  //更改指针的值。
  cout<< p << " " << *p << endl;
  p = &b;
  cout<< p << " " << *p << " " << a <<endl;
  //更改指针所指对象的值。
  *p = 88;
  cout<< p << " " << *p << " " << b <<endl;
  return 0;
}

2.20.请叙述下面这段代码的作用.

#include <iostream>
using namespace std;
int main()
{
  int i =42;
  int *p1 = &i;
  *p1 = *p1 * *p1;//相乘
  cout<< *p1 <<endl;
  return 0;
}

2.23.给定指针p,你能知道它是否指向了一个合法的对象吗?如果能,叙述判断的思路;如果不能,也请说明原因.

在C++程序中,应该尽量初始化所有指针,并且尽可能等定义了对象之后再定义指向它的指针.如果实在不清楚应该指向何处,就把它初始化为nullptr或者0,这样程序就能检测并知道它有没有指向一个具体的对象了.其中nullptr是C++11新标准刚刚引入的一个特殊字面值,它可以转换成任意其他的指针类型.
如果不注意初始化所有指针而贸然判断指针的值,则有可能引发不可预知的结果.一种处理的办法是把if置于try结构中,当程序块顺利执行时,表示p指向了合法的对象;当程序块出错跳转到catch语句时,表示p没有指向合法的对象.

2.34.基于上一个练习中的变量和语句编写一段程序,输出赋值前后变量的内容,你刚才的推断正确吗?如果不对,请反复研读本节的示例直到你明白错在何处为止。

#include <iostream>
using namespace std;
int main()
{
  int i = 0, &r = i;
  auto a = r;
  const int ci = i, &cr = ci;
  auto b = ci; // b是一个整数(ci的顶层const特性被忽略掉了)
	auto c = cr; // c是一个整数(cr是ci的别名,ci本身是一个顶层const)
	auto d = &i; // d是一个整型指针(整数的地址就是指向整数的指针)
	auto e = &ci; // e是一个指向整数常量的指针(对常量对象去地址是一种底层const)
	const auto f = ci; // ci的推演类型是int,f是const int
	auto &g = ci;
  cout << a << " "
       << b << " "
       << c << " "
       << *d << " "
       << *e << " "
       << f << " "
       << g <<endl;
  a = 100;
  b = 100;
  c = 100;
  *d = 100;
  cout << a << " "
       << b << " "
       << c << " "
       << *d << " "
       << *e << " "
       << f << " "
       << g <<endl;
  return 0;
}

2.35.判断下列定义推断出的类型是什么,然后编写程序进行验证。

#include <iostream>
#include <typeinfo>
using namespace std;
int main()
{
  const int i = 42;
  auto j = i; 
  const auto &k = i;
  auto *p = &i; 
  const auto j2 = i, &k2 = i;
  cout<< typeid(j).name() <<endl;
  cout<< typeid(k).name() <<endl;
  cout<< typeid(p).name() <<endl;
  cout<< typeid(j2).name() <<endl;
  cout<< typeid(k2).name() <<endl;
  return 0;
}

2.36.关于下面的代码,请指出每一个变量的类型以及程序结束时它们各自的值。

#include <iostream>
#include <typeinfo>
using namespace std;
int main()
{
  int a = 3, b = 4;
  decltype(a) c = a;
  decltype((b)) d = a;
  ++c;
  ++d;
  cout<< typeid(a).name() << ":" << a <<endl;
  cout<< typeid(b).name() << ":" << b <<endl;
  cout<< typeid(c).name() << ":" << c <<endl;
  cout<< typeid(d).name() << ":" << d <<endl;
  return 0;
}

2.39.编译下面的程序观察其运行结果,注意,如果忘记写类定义体后面的分号会发生什么情况?记录下相关的信息,以后可能会有用。

#include <iostream>
#include <typeinfo>
using namespace std;
struct Foo { /* 此处为空  */ } // 注意:没有分号
int main()
{
    return 0;
}

2.40.根据自己的理解写出 Sales_data 类,最好与书中的例子有所区别。

include <iostream>
#include <string>
using namespace std;
struct Sale_data
{
  string bookNo;
  string bookName;
  unsigned units_sold = 0;
  double revenue = 0.0;
  double price = 0.0;
};

2.41.使用你自己的Sale_data类重写1.5.1节(第20页)、1.5.2节(第21页)和1.6节(第22页)的练习。眼下先把Sales_data类的定义和main函数放在一个文件里。

1.5.1.仅实现输入、相同类型相加、输出

#include <iostream>
#include <string>
using namespace std;
struct Sale_data
{
  string bookNo;
  unsigned units_sold = 0;
  double revenue = 0.0;
};
int main()
{
    Sale_data data1 , data2;
    double price = 0;
    cin>> data1.bookNo >> data1.units_sold >> price;
    data1.revenue = price*data1.units_sold;
    cin>> data2.bookNo >> data2.units_sold >> price;
    data2.revenue = price*data2.units_sold;
    if(data1.bookNo == data2.bookNo)
    {
      unsigned totalcnt = data1.units_sold + data2.units_sold;
      double totalrevenue = data1.revenue + data1.revenue;
      cout<< data1.bookNo << ":" << totalcnt << " "<< totalrevenue << " ";
      if(totalcnt != 0)
        cout<< totalrevenue/totalcnt <<endl;
      else 
        cout<< "no sales!" << endl;
    }
    else
       cout<< "Different!" <<endl;
    return 0;
}

1.5.2.读取多条销售记录,并统计每个ISBN有几条销售记录,每个ISBN的记录聚在一起。

#include <iostream>
#include <string>
using namespace std;
struct Sale_data
{
  string bookNo;
  unsigned units_sold = 0;
  double revenue = 0.0;
};
int main()
{
    Sale_data total;
    double totalprice;
    if(cin >> total.bookNo >> total.units_sold >> totalprice)
    {
      total.revenue = total.units_sold*totalprice;
      Sale_data nextdata;
      double nextprice;
      while(cin >> nextdata.bookNo >> nextdata.units_sold >> nextprice)
      {
        nextdata.revenue = nextprice*nextdata.units_sold;
        if(total.bookNo == nextdata.bookNo)
        {
          total.units_sold += nextdata.units_sold;
          total.revenue += nextdata.revenue;
        }
        else
        {
          cout << total.bookNo << ":" << total.units_sold << " " << total.revenue << " ";
          if(total.units_sold != 0)  
             cout << total.revenue/total.units_sold << endl;
          else  
             cout << "No Sales!" << endl;
          total.bookNo = nextdata.bookNo;
          total.units_sold =nextdata.units_sold;
          total.revenue = nextdata.revenue;
        }
      }
       cout << total.bookNo << ":" << total.units_sold << " " << total.revenue << " ";
          if(total.units_sold != 0)  
             cout << total.revenue/total.units_sold << endl;
          else  
             cout << "No Sales!" << endl;
    }
    else cout << "No data!" << endl;
    return 0;
}

2.6.3.练习2.42:根据你自己的理解重写一个Sale_data.h头文件,并以此为基础重做2.6.2节(第67页)的练习。

1.5.1.仅实现输入、相同类型相加、输出.

Sale_data.h

#ifndef SALE_DATA_H
#define SALE_DATA_H
#include <iostream>
#include <string>
using namespace std;
struct Sale_data
{
  string bookNo;
  unsigned units_sold = 0;
  double revenue = 0.0;
};
#endif 

cpp 

#include "Sale_data.h"
int main()
{
     Sale_data data1 , data2;
    double price = 0;
    cin>> data1.bookNo >> data1.units_sold >> price;
    data1.revenue = price*data1.units_sold;
    cin>> data2.bookNo >> data2.units_sold >> price;
    data2.revenue = price*data2.units_sold;
    if(data1.bookNo == data2.bookNo)
    {
      unsigned totalcnt = data1.units_sold + data2.units_sold;
      double totalrevenue = data1.revenue + data1.revenue;
      cout<< data1.bookNo << ":" << totalcnt << " "<< totalrevenue << " ";
      if(totalcnt != 0)
        cout<< totalrevenue/totalcnt <<endl;
      else 
        cout<< "no sales!" << endl;
    }
    else
       cout<< "Different!" <<endl;
    return 0;
}

1.5.2.读取多条销售记录,并统计每个ISBN有几条销售记录,每个ISBN的记录聚在一起。

Sale_data.h

#ifndef SALE_DATA_H
#define SALE_DATA_H
#include <iostream>
#include <string>
using namespace std;
struct Sale_data
{
  string bookNo;
  unsigned units_sold = 0;
  double revenue = 0.0;
};
#endif 

cpp

#include "Sale_data.h"
int main()
{
    Sale_data total;
    double totalprice;
    if(cin >> total.bookNo >> total.units_sold >> totalprice)
    {
      total.revenue = total.units_sold*totalprice;
      Sale_data nextdata;
      double nextprice;
      while(cin >> nextdata.bookNo >> nextdata.units_sold >> nextprice)
      {
        nextdata.revenue = nextprice*nextdata.units_sold;
        if(total.bookNo == nextdata.bookNo)
        {
          total.units_sold += nextdata.units_sold;
          total.revenue += nextdata.revenue;
        }
        else
        {
          cout << total.bookNo << ":" << total.units_sold << " " << total.revenue << " ";
          if(total.units_sold != 0)  
             cout << total.revenue/total.units_sold << endl;
          else  
             cout << "No Sales!" << endl;
          total.bookNo = nextdata.bookNo;
          total.units_sold =nextdata.units_sold;
          total.revenue = nextdata.revenue;
        }
      }
       cout << total.bookNo << ":" << total.units_sold << " " << total.revenue << " ";
          if(total.units_sold != 0)  
             cout << total.revenue/total.units_sold << endl;
          else  
             cout << "No Sales!" << endl;
    }
    else cout << "No data!" << endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Joey.Chao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值