C++ Primer(第五版) 第二章练习答案
目录
-
- C++ Primer(第五版) 第二章练习答案
- 2.1
- 2.2
- 2.3or2.4
- 2.5
- 2.6
- 2.7
- 2.8
- 2.9
- 2.10
- 2.11
- 2.12
- 2.13
- 2.14
- 2.15
- 2.16
- 2.17
- 2.18
- 2.19
- 2.20
- 2.21
- 2.22
- 2.23
- 2.24
- 2.25
- 2.26
- 2.27
- 2.28
- 2.29
- 2.30
- 2.31
- 2.32
- 2.33or34
- 2.35
- 2.36
- 2.37
- 2.38
- 2.39
- 2.40
- 2.41or42
2.1
2.2
2.3or2.4
#include
#include
int main()
{
unsigned u = 10, u2 = 42;
std::cout << u2 - u << std::endl;
std::cout << u - u2 << std::endl;
std::cout << UINT_MAX - 32 + 1 << 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
#include
int main()
{
char a = 'a';
wchar_t b = L'a';
const char str[] = "a";
const wchar_t str2[] = L"a";
int i1 = 10;
unsigned i2 = 10u;
long i3 = 10L;
unsigned long i3 = 10UL;
int i4 = 012;
int i5 = 0xC;
double d1 = 3.14;
float d2 = 3.14F;
long double d3 = 3.14L;
int f1 = 10;
unsigned int f2 = 10u;
double f3 = 10.;
double f4 = 10e-2;
return 0;
}
2.6
#include
int main()
{
int month = 9, day = 7;
int day = 07;
return 0;
}
2.7
#include
int main()
{
std::cout << "Who goes with F\145rgus?\012";
auto i1 = 3.14e1L;
auto i3 = 3.14L;
return 0;
}
2.8
#include
int main()
{
std::cout << "2\tM\n";
return 0;
}
2.9
#include
int main()
{
int input_value = 0;
std::cin >> input_value;
double i = {
3.14};
double salary = 9999.99, wage = salary;
double i = 3.14;
return 0;
}
2.10
#include
#include
std::string global_str;
int global_int;
int main()
{
int local_int;
std::string local_str;
std::cout << global_str << std::endl;
std::cout << global_int << std::endl;
std::cout << local_int << std::endl;
std::cout << local_str << std::endl;
return 0;
}
2.11
#include
extern int ix = 1024;
int iy;
extern int iz;
int main()
{
return 0;
}
2.12
2.13
#include
int i = 42;
int main()
{
int i = 100;
int j = i;
printf("%d\n", j);
return 0;
}
2.14
#include
int main()
{
int i = 100, sum = 0;
for (int i = 0; i != 10; ++i)
sum += i;
std::cout << i << " " << sum << std::endl;
return 0;
}
2.15
2.16
#include
int main()
{
int i = 0, &r1 = i;
double d = 0, &r2 = d;
r2 = 3.14159;
r2 = r1;
i = r2;
r1 = d;
return 0;
}
2.17
#include
int main()
{
int i, &ri = i;
i = 5;
ri = 10;
std::cout << i << " " << ri << std::endl;
return 0;
}
2.18
#include
int main()
{
int *pi = nullptr, a = 10;
pi = &a;
printf("%d\n", *pi);
printf("%d\n", a);
*pi = 20;
printf("%d\n", *pi);
printf("%d\n", a);
return 0;
}
2.19
2.20
#include
int main()
{
int i = 42;
int *p1 = &i;
*p1 = *p1 * *p1;
std::cout << *p1 << std::endl;
return 0;
}
2.21
int main()
{
int i = 0;
double *dp = &i;
int *ip = i;
int *p = &i;
return 0;
}
2.22
int main()
{
int *p = nullptr;
if (p) {
}
if (*p) {
}
return 0;
}
2.23
2.24
int main()
{
int i = 42;
void *p = &i;
long *lp = &i;
return 0;
}
2.25
#include
int main()
{
#if 0
int i = 42;
int *p;
int *&r = p;
r = &i;
printf("%d\n", *r);
printf("%d\n", *p);
printf("%p\n", r);
printf("%p\n", p);
printf("%p\n", &i);
#endif
int *ip, i, &r = i;
int i, *ip = 0;
int *ip, ip2;
return 0;
}
2.26
#include
int main()
{
const int buf;
int cnt = 0;
const int sz = cnt;
++cnt; ++sz;
return 0;
}
2.27
#include
int main()
{
return 0;
}
2.28
#include
int main()
{
int i, *const cp;
int *p1, *const p2;
const int ic, &r = ic;
const int *const p3;
const int *p;
return 0;
}
2.29
#include
int main()
{
int i, *const cp;
int *p1, *const p2;
const int ic, &r = ic;
const int *const p3;
const int *p;
i = ic;
p1 = p3;
p1 = ⁣
p3 = ⁣
p2 = p1;
ic = *p3;
return 0;
}
2.30
#include
int main()
{
int i = 0;
const int v2 = 0;
int v1 = v2;
int *p1 = &v1, &r1 = v1;
const int *p2 = &v2, *const p3 = &i, &r2 = v2;
return 0;
}
2.31
#include
int main()
{
int i = 0;
const int v2 = 0;
int v1 = v2;
int *p1 = &v1, &r1 = v1;
const int *p2 = &v2, *const p3 = &i, &r2 = v2;
r1 = v2;
p1 = p2; p2 = p1;
p1 = p3; p2 = p3;
return 0;
}
2.32
#include
int main()
{
int null = 0, *p = null;
int null = 0, *p = nullptr;
return 0;
}
2.33or34
#include
int main()
{
int i = 10;
const int ci = i, &cr = ci;
auto a = i;
auto b = ci;
auto c = cr;
auto d = &i;
auto e = &ci;
const auto f = ci;
auto &g = ci;
const auto &j = 42;
auto k = ci, &l = i;
auto &m = ci, *p = &ci;
std::cout << a << std::endl;
std::cout << b << std::endl;
std::cout << c << std::endl;
a = 42;
b = 42;
c = 42;
std::cout << a << std::endl;
std::cout << b << std::endl;
std::cout << c << std::endl;
return 0;
}
2.35
#include
int main()
{
const int i = 42;
auto j = i;
const auto &k = i;
auto *p = &i;
const auto j2 = i, &k2 = i;
return 0;
}
2.36
#include
int main()
{
int a = 3, b = 4;
decltype(a) c = a;
decltype((b)) d = a;
++c;
++d;
std::cout << &a << std::endl;
std::cout << &b << std::endl;
std::cout << &c << std::endl;
std::cout << &d << std::endl;
printf("%p\n", &a);
printf("%p\n", &b);
printf("%p\n", &c);
printf("%p\n", &d);
return 0;
}
2.37
#include
int main()
{
int a = 3, b = 4;
decltype(a) c = a;
decltype(a = b) d = a;
printf("%d %d %d %d\n", a, b, c, d);
return 0;
}
2.38
#include
int main()
{
const int a = 10, &b = a;
auto c1 = a;
decltype(a) c2 = a;
auto d1 = b;
decltype(b) d2 = b;
return 0;
}
2.39
#include
struct Foo {
}
int main()
{
return 0;
}
2.40
#ifndef SALES_DATA
#define SALES_DATA
#include
#include
struct Sales_data {
std::string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
};
#endif
2.41or42
#include "2_40.h"
#include
#include
#include
#include
#if 0
int main()
{
std::vector<Sales_data> data;
Sales_data temp;
while (std::cin >> temp.bookNo)
{
std::cin >> temp.units_sold;
std::cin >> temp.revenue;
data.push_back(temp);
}
for (auto &i : data)
{
std::cout << i.bookNo << ", "
<< i.units_sold << ", "
<< i.revenue << std::endl;
}
return 0;
#elif 0
int main()
{
Sales_data data1, data2;
while (true)
{
std::cin >> data1.bookNo >> data1.units_sold >> data1.revenue;
std::cin >> data2.bookNo >> data2.units_sold >> data2.revenue;
if (data1.bookNo == data2.bookNo)
{
std::cout << data1.bookNo << ", "
<< data1.units_sold + data2.units_sold << ", "
<< data1.revenue + data2.revenue << std::endl;
break;
}
else
{
std::cerr << "Re enter" << std::endl;
}
}
return 0;
}
#elif 0
int main()
{
std::map<std::string, Sales_data> datas;
Sales_data temp;
while (std::cin >> temp.bookNo >> temp.units_sold >> temp.revenue)
{
if (datas.find(temp.bookNo) == datas.end())
{
datas.insert(std::pair<std::string, Sales_data>(temp.bookNo, temp));
}
else
{
datas[temp.bookNo].units_sold += temp.units_sold;
datas[temp.bookNo].revenue += temp.revenue;
}
}
for (const auto &i : datas)
std::cout << i.first << ": "
<< i.second.units_sold << " "
<< i.second.revenue << std::endl;
return 0;
}
#elif 0
int main()
{
std::map<std::string, size_t> datas;
Sales_data temp;
while (std::cin >> temp.bookNo >> temp.units_sold >> temp.revenue)
++datas[temp.bookNo];
for (const auto &i : datas)
std::cout << i.first << " : "
<< i.second << std::endl;
return 0;
}
#elif 0
int main()
{
std::map<std::string, std::vector<Sales_data>> datas;
Sales_data temp;
while (std::cin >> temp.bookNo >> temp.units_sold >> temp.revenue)
{
if (datas.find(temp.bookNo) == datas.end())
{
datas.insert(std::pair<std::string, std::vector<Sales_data>>(temp.bookNo, std::vector<Sales_data>()));
datas[temp.bookNo].push_back(temp);
}
else
{
datas[temp.bookNo].push_back(temp);
}
}
for (auto &i : datas)
{
std::cout << i.first << " -> "
<< i.second.size() << std::endl;
for (auto &j : i.second)
std::cout << j.bookNo << " "
<< j.units_sold << " "
<< j.revenue << std::endl;
}
return 0;
}
#elif 1
int main()
{
Sales_data total;
if (std::cin >> total.bookNo >> total.units_sold >> total.revenue)
{
Sales_data trans;
while (std::cin >> trans.bookNo >> trans.units_sold >> trans.revenue)
{
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 << 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 << std::endl;
}
else
{
std::cerr << "No data?!" << std::endl;
return -1;
}
return 0;
}
#endif