├── .gitignore
├── Cpp_Primer_Answer.vcxproj
├── Cpp_Primer_Answer.vcxproj.filters
├── Cpp_Primer_Answer.vcxproj.user
├── README.md
├── ch01
├── README.md
├── exercise1_1.cpp
├── exercise1_10.cpp
├── exercise1_11.cpp
├── exercise1_16.cpp
├── exercise1_18.cpp
├── exercise1_20.cpp
├── exercise1_21.cpp
├── exercise1_22.cpp
├── exercise1_23.cpp
└── exercise1_9.cpp
├── ch02
├── README.md
├── exercise2_34.cpp
├── exercise2_4.cpp
├── exercise2_42_1.cpp
├── exercise2_42_2.cpp
└── exercise2_42_3.cpp
├── ch03
├── README.md
├── exercise3_10.cpp
├── exercise3_14.cpp
├── exercise3_15.cpp
├── exercise3_16.cpp
├── exercise3_17.cpp
├── exercise3_20.cpp
├── exercise3_21.cpp
├── exercise3_22.cpp
├── exercise3_23.cpp
├── exercise3_24.cpp
├── exercise3_25.cpp
├── exercise3_2_a.cpp
├── exercise3_2_b.cpp
├── exercise3_31.cpp
├── exercise3_32.cpp
├── exercise3_35.cpp
├── exercise3_36.cpp
├── exercise3_39.cpp
├── exercise3_40.cpp
├── exercise3_41.cpp
├── exercise3_42.cpp
├── exercise3_43.cpp
├── exercise3_44.cpp
├── exercise3_45.cpp
├── exercise3_4_a.cpp
├── exercise3_4_b.cpp
├── exercise3_5_a.cpp
├── exercise3_5_b.cpp
├── exercise3_6.cpp
├── exercise3_7.cpp
├── exercise3_8.cpp
├── exercise_3_1_a.cpp
└── exercise_3_1_b.cpp
├── ch04
├── README.md
├── exercise4_21.cpp
├── exercise4_22.cpp
└── exercise4_28.cpp
├── ch05
├── README.md
├── exercise5_10.cpp
├── exercise5_11.cpp
├── exercise5_12.cpp
├── exercise5_14.cpp
├── exercise5_17.cpp
├── exercise5_19.cpp
├── exercise5_20.cpp
├── exercise5_21.cpp
├── exercise5_23.cpp
├── exercise5_24.cpp
├── exercise5_25.cpp
├── exercise5_5.cpp
├── exercise5_6.cpp
└── exercise5_9.cpp
├── ch06
├── Chapter6.h
├── README.md
├── exercise6_10.cpp
├── exercise6_11.cpp
├── exercise6_12.cpp
├── exercise6_17.cpp
├── exercise6_21.cpp
├── exercise6_22.cpp
├── exercise6_23.cpp
├── exercise6_25.cpp
├── exercise6_27.cpp
├── exercise6_3.cpp
├── exercise6_30.cpp
├── exercise6_33.cpp
├── exercise6_4.cpp
├── exercise6_42.cpp
├── exercise6_47.cpp
├── exercise6_51.cpp
├── exercise6_55.cpp
├── fact.cpp
└── factMain.cpp
├── ch07
├── README.md
├── exercise7_1.cpp
├── exercise7_11.cpp
├── exercise7_11.h
├── exercise7_12.h
├── exercise7_13.cpp
├── exercise7_15.h
├── exercise7_2.h
├── exercise7_21.h
├── exercise7_22.h
├── exercise7_23.h
├── exercise7_24.h
├── exercise7_26.h
├── exercise7_27.cpp
├── exercise7_27.h
├── exercise7_3.cpp
├── exercise7_32.h
├── exercise7_4.h
├── exercise7_41.cpp
├── exercise7_41.h
├── exercise7_41_main.cpp
├── exercise7_5.h
├── exercise7_6.h
├── exercise7_7.cpp
└── exercise7_9.h
├── ch08
├── README.md
├── exercise8_10.cpp
├── exercise8_11.cpp
├── exercise8_13.cpp
├── exercise8_2.cpp
├── exercise8_6.cpp
├── exercise8_7.cpp
├── exercise8_8.cpp
└── exercise8_9.cpp
├── ch09
├── README.md
├── exercise9_13.cpp
├── exercise9_18.cpp
├── exercise9_20.cpp
├── exercise9_24.cpp
├── exercise9_27.cpp
├── exercise9_33.cpp
├── exercise9_34.cpp
├── exercise9_38.cpp
├── exercise9_43.cpp
├── exercise9_44.cpp
├── exercise9_45.cpp
├── exercise9_46.cpp
├── exercise9_47.cpp
├── exercise9_49.cpp
├── exercise9_50.cpp
├── exercise9_51.cpp
└── exercise9_52.cpp
├── ch10
├── README.md
├── exercise10_1.cpp
├── exercise10_11.cpp
├── exercise10_12.cpp
├── exercise10_13.cpp
├── exercise10_16.cpp
├── exercise10_17.cpp
├── exercise10_18.cpp
├── exercise10_19.cpp
├── exercise10_2.cpp
├── exercise10_20.cpp
├── exercise10_22.cpp
├── exercise10_24.cpp
├── exercise10_25.cpp
├── exercise10_27.cpp
├── exercise10_28.cpp
├── exercise10_29.cpp
├── exercise10_3.cpp
├── exercise10_30.cpp
├── exercise10_31.cpp
├── exercise10_32.cpp
├── exercise10_33.cpp
├── exercise10_34.cpp
├── exercise10_35.cpp
├── exercise10_36.cpp
├── exercise10_37.cpp
├── exercise10_4.cpp
├── exercise10_42.cpp
├── exercise10_6.cpp
├── exercise10_9.cpp
└── exercise7_26.h
├── ch11
├── README.md
├── exercise11_12.cpp
├── exercise11_14.cpp
├── exercise11_20.cpp
├── exercise11_23.cpp
├── exercise11_3.cpp
├── exercise11_31.cpp
├── exercise11_32.cpp
├── exercise11_33.cpp
├── exercise11_38.cpp
├── exercise11_4.cpp
└── exercise11_8.cpp
├── ch12
├── README.md
├── exercise12_14.cpp
├── exercise12_15.cpp
├── exercise12_16.cpp
├── exercise12_19.h
├── exercise12_2.cpp
├── exercise12_2.h
├── exercise12_20.cpp
├── exercise12_23.cpp
├── exercise12_24.cpp
├── exercise12_26.cpp
├── exercise12_27.cpp
├── exercise12_27.h
├── exercise12_27_main.cpp
├── exercise12_28.cpp
├── exercise12_6.cpp
└── exercise12_7.cpp
├── ch13
├── README.md
├── exercise13_11.h
├── exercise13_13.cpp
├── exercise13_17_1.cpp
├── exercise13_17_2.cpp
├── exercise13_17_3.cpp
├── exercise13_18.h
├── exercise13_19.h
├── exercise13_22.h
├── exercise13_26.h
├── exercise13_27.h
├── exercise13_28.h
├── exercise13_30.h
├── exercise13_31.h
├── exercise13_34.cpp
├── exercise13_34.h
├── exercise13_39.cpp
├── exercise13_39.h
├── exercise13_44.cpp
├── exercise13_44.h
├── exercise13_44_main.cpp
├── exercise13_5.h
├── exercise13_53.cpp
├── exercise13_53.h
├── exercise13_58.cpp
└── exercise13_8.h
├── ch14
├── README.md
├── exercise14_15.cpp
├── exercise14_15.h
├── exercise14_15_main.cpp
├── exercise14_2.cpp
├── exercise14_2.h
├── exercise14_22.cpp
├── exercise14_22.h
├── exercise14_22_main.cpp
├── exercise14_23.cpp
├── exercise14_23.h
├── exercise14_23_main.cpp
├── exercise14_24.cpp
├── exercise14_24.h
├── exercise14_24_main.cpp
├── exercise14_35.cpp
├── exercise14_36.cpp
├── exercise14_37.cpp
├── exercise14_38.cpp
├── exercise14_40.cpp
├── exercise14_43.cpp
├── exercise14_44.cpp
├── exercise14_45.cpp
├── exercise14_45.h
├── exercise14_45_main.cpp
├── exercise14_5.cpp
├── exercise14_5.h
├── exercise14_5_main.cpp
├── exercise14_7.cpp
├── exercise14_7.h
└── exercise14_7_main.cpp
├── ch15
├── Bulk_quote.h
├── Disc_quote.h
├── Limit_quote.h
├── README.md
├── basket.cpp
├── basket.h
├── exercise15_20.cpp
├── exercise15_26.h
├── exercise15_26_2.h
├── exercise15_27.h
├── exercise15_28.cpp
├── exercise15_3.cpp
├── exercise15_3.h
├── exercise15_5.h
├── exercise15_6.cpp
├── exercise15_7.h
├── main.cpp
├── query.h
└── query_base.h
├── ch16
├── README.md
├── blob.h
├── blobptr.h
├── debugDelete.h
├── delete.h
├── screen.h
├── shared_ptr.h
├── unique_ptr.h
└── vec.h
├── ch17
└── README.md
├── ch18
└── README.md
├── ch19
└── README.md
└── data
├── Sales_item.h
├── Version_test.h
├── books.txt
├── exercise1_17.png
├── exercise1_2.png
├── exercise1_24.png
├── exercise1_7.png
├── exercise2_42.h
├── for_transform.txt
├── letter.txt
├── phonenumbers.txt
├── storyDataFile.txt
└── transform_rules.txt
/.gitignore:
--------------------------------------------------------------------------------
1 | # Compiled Object files
2 | *.slo
3 | *.lo
4 | *.o
5 | *.obj
6 |
7 | # Precompiled Headers
8 | *.gch
9 | *.pch
10 |
11 | # Compiled Dynamic libraries
12 | *.so
13 | *.dylib
14 | *.dll
15 |
16 | # Fortran module files
17 | *.mod
18 |
19 | # Compiled Static libraries
20 | *.lai
21 | *.la
22 | *.a
23 | *.lib
24 |
25 | # Executables
26 | *.exe
27 | *.out
28 | *.app
29 |
30 | Debug
31 | .gitattributes
32 | Cpp_Primer_Answer.sdf
33 | Cpp_Primer_Answer.opensdf
34 | Cpp_Primer_Answer.v12.suo
--------------------------------------------------------------------------------
/Cpp_Primer_Answer.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;hm;inl;inc;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | 头文件
20 |
21 |
22 |
23 |
24 | 源文件
25 |
26 |
27 | 源文件
28 |
29 |
30 |
--------------------------------------------------------------------------------
/Cpp_Primer_Answer.vcxproj.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | false
5 |
6 |
7 | hello world
8 | WindowsLocalDebugger
9 |
10 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # 《C++ Primer》第五版中文版习题答案
2 | ### 说明:
3 | * 我的这个项目大量参考了[Cpp-Primer](https://github.com/Mooophy/Cpp-Primer),我也有过和原作者的讨论并提交过 pull request。所以不能说是我抄的,而且读书人的事,怎么能算抄呢。。。
4 | * 发现了书中有两处错误的地方,其中一处英文原版书中也有错误。一处在[第9章](https://www.zhihu.com/question/39255704),一处在[第14章](https://www.zhihu.com/question/42262890)。
5 | * 编译器是 Visual Studio 2013
6 | * 建议大家把书换成英文版的,这个项目有很多我当时留下来的错误,可能会误导初学者。所以这也证明了学习一定要使用第一手资料,中文资料的作者并不想制造错误但总是会有疏忽的(2019年5月)。
7 |
8 | #### 下面是目录:
9 | - [第1章 : 开始](ch01/README.md)
10 | - 第 I 部分 : C++基础
11 | - [第2章 : 变量和基本类型](ch02/README.md)
12 | - [第3章 : 字符串、向量和数组](ch03/README.md)
13 | - [第4章 : 表达式](ch04/README.md)
14 | - [第5章 : 语句](ch05/README.md)
15 | - [第6章 : 函数](ch06/README.md)
16 | - [第7章 : 类](ch07/README.md)
17 | - 第 II 部分 : C++标准库
18 | - [第8章 : IO库](ch08/README.md)
19 | - [第9章 : 顺序容器](ch09/README.md)
20 | - [第10章 : 泛型算法](ch10/README.md)
21 | - [第11章 : 关联容器](ch11/README.md)
22 | - [第12章 : 动态内存](ch12/README.md)
23 | - 第 III 部分 : 类设计者的工具
24 | - [第13章 : 拷贝控制](ch13/README.md)
25 | - [第14章 : 重载与类型转换](ch14/README.md)
26 | - [第15章 : 面向对象程序设计](ch15/README.md)
27 | - [第16章 : 模版与泛型编程](ch16/README.md)
28 | - 第 IV 部分 : 高级主题
29 | - [第17章 : 标准库与特殊设施](ch17/README.md)
30 | - [第18章 : 用于大型程序的工具](ch18/README.md)
31 | - [第19章 : 特殊工具与技术](ch19/README.md)
32 |
33 |
34 |
35 |
36 |
37 |
--------------------------------------------------------------------------------
/ch01/exercise1_1.cpp:
--------------------------------------------------------------------------------
1 | int main()
2 | {
3 | return -1;
4 | }
--------------------------------------------------------------------------------
/ch01/exercise1_10.cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | int main()
4 | {
5 | int i = 10;
6 |
7 | while (i >= 0)
8 | {
9 | std::cout << i << std::endl;
10 | --i;
11 | }
12 |
13 | return 0;
14 | }
--------------------------------------------------------------------------------
/ch01/exercise1_11.cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | void print_range(int lo, int hi)
4 | {
5 | if (lo > hi)
6 | {
7 | print_range(hi, lo);
8 | return;
9 | }
10 | while (lo <= hi)
11 | {
12 | std::cout << lo << std::endl;
13 | ++lo;
14 | }
15 | }
16 |
17 | int main()
18 | {
19 | int low, high;
20 | std::cout << "please input two numbers : " << std::endl;
21 | std::cin >> low >> high;
22 |
23 | print_range(low, high);
24 | return 0;
25 | }
--------------------------------------------------------------------------------
/ch01/exercise1_16.cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | int main()
4 | {
5 | int sum = 0, value = 0;
6 |
7 | while (std::cin >> value)
8 | {
9 | sum += value;
10 | }
11 |
12 | std::cout << sum << std::endl;
13 |
14 | return 0;
15 | }
--------------------------------------------------------------------------------
/ch01/exercise1_18.cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | int main()
4 | {
5 | int currVal = 0, val = 0;
6 |
7 | if (std::cin >> currVal)
8 | {
9 | int cnt = 1;
10 | while (std::cin >> val)
11 | {
12 | if (val == currVal)
13 | {
14 | ++cnt;
15 | }
16 | else
17 | {
18 | std::cout << currVal << " occurs " << cnt << " times" << std::endl;
19 | currVal = val;
20 | cnt = 1;
21 | }
22 | }
23 | std::cout << currVal << " occurs " << cnt << " times" << std::endl;
24 | }
25 | return 0;
26 | }
--------------------------------------------------------------------------------
/ch01/exercise1_20.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include "Sales_item.h"
3 |
4 | using std::cin;
5 | using std::cout;
6 | using std::endl;
7 |
8 | int main()
9 | {
10 | for (Sales_item item; cin >> item; cout << item << endl);
11 | return 0;
12 | }
--------------------------------------------------------------------------------
/ch01/exercise1_21.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include "Sales_item.h"
3 |
4 | using namespace std;
5 |
6 | int main()
7 | {
8 | Sales_item item1, item2;
9 | cin >> item1 >> item2;
10 |
11 | if (item1.isbn() == item2.isbn())
12 | {
13 | cout << item1 + item2 << endl;
14 | }
15 | else
16 | {
17 | cerr << "Different ISBN." << endl;
18 | }
19 |
20 | return 0;
21 | }
--------------------------------------------------------------------------------
/ch01/exercise1_22.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include "Sales_item.h"
3 |
4 | int main()
5 | {
6 | Sales_item total;
7 | if (std::cin >> total)
8 | {
9 | Sales_item trans;
10 | while (std::cin >> trans)
11 | {
12 | if (total.isbn() == trans.isbn())
13 | total += trans;
14 | else
15 | {
16 | std::cout << total << std::endl;
17 | total = trans;
18 | }
19 | }
20 | std::cout << total << std::endl;
21 | }
22 | else
23 | {
24 | std::cerr << "No data?!" << std::endl;
25 | return -1;
26 | }
27 |
28 | return 0;
29 | }
--------------------------------------------------------------------------------
/ch01/exercise1_23.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include "Sales_item.h"
3 |
4 | int main()
5 | {
6 | Sales_item currItem, valItem;
7 | if (std::cin >> currItem)
8 | {
9 | int cnt = 1;
10 | while (std::cin >> valItem)
11 | {
12 | if (valItem.isbn() == currItem.isbn())
13 | {
14 | ++cnt;
15 | }
16 | else
17 | {
18 | std::cout << currItem << " occurs " << cnt << " times " << std::endl;
19 | currItem = valItem;
20 | cnt = 1;
21 | }
22 | }
23 | std::cout << currItem << " occurs " << cnt << " times " << std::endl;
24 | }
25 | return 0;
26 | }
--------------------------------------------------------------------------------
/ch01/exercise1_9.cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | int main()
4 | {
5 | int sum = 0, i = 50;
6 |
7 | while (i <= 100)
8 | {
9 | sum += i;
10 | ++i;
11 | }
12 |
13 | std::cout << sum << std::endl;
14 |
15 | return 0;
16 | }
--------------------------------------------------------------------------------
/ch02/exercise2_34.cpp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huangmingchuan/Cpp_Primer_Answers/925506ca53f7b2e8501bc8eaaee3dba8c03d9ec2/ch02/exercise2_34.cpp
--------------------------------------------------------------------------------
/ch02/exercise2_4.cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | int main()
4 | {
5 | unsigned u = 10, u2 = 42;
6 | std::cout << u2 - u << std::endl; // 32
7 | std::cout << u - u2 << std::endl; // 4294967264
8 | int i = 10, i2 = 42;
9 | std::cout << i2 - i << std::endl; // 32
10 | std::cout << i - i2 << std::endl; // -32
11 | std::cout << i - u << std::endl; // 0
12 | std::cout << u - i << std::endl; // 0
13 |
14 | return 0;
15 | }
--------------------------------------------------------------------------------
/ch02/exercise2_42_1.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include "exercise2_42.h"
3 |
4 | int main()
5 | {
6 | Sales_data book;
7 | double price;
8 | std::cin >> book.bookNo >> book.units_sold >> price;
9 | book.CalcRevenue(price);
10 | book.Print();
11 |
12 | return 0;
13 | }
--------------------------------------------------------------------------------
/ch02/exercise2_42_2.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include "exercise2_42.h"
3 |
4 | int main()
5 | {
6 | Sales_data book1, book2;
7 | double price1, price2;
8 | std::cin >> book1.bookNo >> book1.units_sold >> price1;
9 | std::cin >> book2.bookNo >> book2.units_sold >> price2;
10 | book1.CalcRevenue(price1);
11 | book2.CalcRevenue(price2);
12 |
13 | if (book1.bookNo == book2.bookNo)
14 | {
15 | book1.AddData(book2);
16 | book1.Print();
17 | return 0;
18 | }
19 | else
20 | {
21 | std::cerr << "Data must refer to same ISBN" << std::endl;
22 | return -1;
23 | }
24 | }
--------------------------------------------------------------------------------
/ch02/exercise2_42_3.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include "exercise2_42.h"
3 |
4 | int main()
5 | {
6 | Sales_data total;
7 | double totalPrice;
8 | if (std::cin >> total.bookNo >> total.units_sold >> totalPrice)
9 | {
10 | total.CalcRevenue(totalPrice);
11 | Sales_data trans;
12 | double transPrice;
13 | while (std::cin >> trans.bookNo >> trans.units_sold >> transPrice)
14 | {
15 | trans.CalcRevenue(transPrice);
16 | if (total.bookNo == trans.bookNo)
17 | {
18 | total.AddData(trans);
19 | }
20 | else
21 | {
22 | total.Print();
23 | total.SetData(trans);
24 | }
25 | }
26 | total.Print();
27 | return 0;
28 | }
29 | else
30 | {
31 | std::cerr << "No data?!" << std::endl;
32 | return -1;
33 | }
34 | }
--------------------------------------------------------------------------------
/ch03/exercise3_10.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 |
5 | using std::string;
6 | using std::cin;
7 | using std::cout;
8 | using std::endl;
9 |
10 | int main()
11 | {
12 | string s = "this, is. a :string!";
13 | string result;
14 |
15 | for (auto x : s)
16 | {
17 | if (!ispunct(x))
18 | {
19 | result += x;
20 | }
21 | }
22 |
23 | cout << result << endl;
24 | return 0;
25 | }
--------------------------------------------------------------------------------
/ch03/exercise3_14.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include
5 |
6 | using std::cin;
7 | using std::cout;
8 | using std::endl;
9 | using std::vector;
10 |
11 | int main()
12 | {
13 | vector v;
14 | int i;
15 | while (cin >> i)
16 | {
17 | v.push_back(i);
18 | }
19 | return 0;
20 | }
--------------------------------------------------------------------------------
/ch03/exercise3_15.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include
5 |
6 | using std::cin;
7 | using std::cout;
8 | using std::endl;
9 | using std::vector;
10 | using std::string;
11 |
12 | int main()
13 | {
14 | vector v;
15 | string i;
16 | while (cin >> i)
17 | {
18 | v.push_back(i);
19 | }
20 | return 0;
21 | }
--------------------------------------------------------------------------------
/ch03/exercise3_16.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include
5 |
6 | using std::cin;
7 | using std::cout;
8 | using std::endl;
9 | using std::vector;
10 | using std::string;
11 |
12 | int main()
13 | {
14 | vector v1; // size:0, no values.
15 | vector v2(10); // size:10, value:0
16 | vector v3(10, 42); // size:10, value:42
17 | vector v4{ 10 }; // size:1, value:10
18 | vector v5{ 10, 42 }; // size:2, value:10, 42
19 | vector v6{ 10 }; // size:10, value:""
20 | vector v7{ 10, "hi" }; // size:10, value:"hi"
21 |
22 | cout << "v1 size :" << v1.size() << endl;
23 | cout << "v2 size :" << v2.size() << endl;
24 | cout << "v3 size :" << v3.size() << endl;
25 | cout << "v4 size :" << v4.size() << endl;
26 | cout << "v5 size :" << v5.size() << endl;
27 | cout << "v6 size :" << v6.size() << endl;
28 | cout << "v7 size :" << v7.size() << endl;
29 |
30 | cout << "v1 content: ";
31 | for (auto i : v1)
32 | {
33 | cout << i << " , ";
34 | }
35 | cout << endl;
36 |
37 | cout << "v2 content: ";
38 | for (auto i : v2)
39 | {
40 | cout << i << " , ";
41 | }
42 | cout << endl;
43 |
44 | cout << "v3 content: ";
45 | for (auto i : v3)
46 | {
47 | cout << i << " , ";
48 | }
49 | cout << endl;
50 |
51 | cout << "v4 content: ";
52 | for (auto i : v4)
53 | {
54 | cout << i << " , ";
55 | }
56 | cout << endl;
57 |
58 | cout << "v5 content: ";
59 | for (auto i : v5)
60 | {
61 | cout << i << " , ";
62 | }
63 | cout << endl;
64 |
65 | cout << "v6 content: ";
66 | for (auto i : v6)
67 | {
68 | cout << i << " , ";
69 | }
70 | cout << endl;
71 |
72 | cout << "v7 content: ";
73 | for (auto i : v7)
74 | {
75 | cout << i << " , ";
76 | }
77 | cout << endl;
78 | return 0;
79 | }
--------------------------------------------------------------------------------
/ch03/exercise3_17.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include
5 |
6 | using std::cin;
7 | using std::cout;
8 | using std::endl;
9 | using std::vector;
10 | using std::string;
11 |
12 | int main()
13 | {
14 | vector v;
15 | string s;
16 |
17 | while (cin >> s)
18 | {
19 | v.push_back(s);
20 | }
21 |
22 | for (auto &str : v)
23 | {
24 | for (auto &c : str)
25 | {
26 | c = toupper(c);
27 | }
28 | }
29 |
30 | for (auto i : v)
31 | {
32 | cout << i << endl;
33 | }
34 | return 0;
35 | }
--------------------------------------------------------------------------------
/ch03/exercise3_20.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include
5 |
6 | using std::cin;
7 | using std::cout;
8 | using std::endl;
9 | using std::vector;
10 | using std::string;
11 |
12 | int main()
13 | {
14 | vector ivec;
15 | int i;
16 | while (cin >> i)
17 | {
18 | ivec.push_back(i);
19 | }
20 |
21 | for (int i = 0; i < ivec.size() - 1; ++i)
22 | {
23 | cout << ivec[i] + ivec[i + 1] << endl;
24 | }
25 |
26 | //---------------------------------
27 | cout << "---------------------------------" << endl;
28 |
29 | int m = 0;
30 | int n = ivec.size() - 1;
31 | while (m < n)
32 | {
33 | cout << ivec[m] + ivec[n] << endl;
34 | ++m;
35 | --n;
36 | }
37 | return 0;
38 | }
--------------------------------------------------------------------------------
/ch03/exercise3_21.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include
5 |
6 | using std::vector;
7 | using std::string;
8 | using std::cout;
9 | using std::endl;
10 |
11 | void check_and_print(const vector& vec)
12 | {
13 | cout << "size: " << vec.size() << " content: [";
14 | for (auto it = vec.begin(); it != vec.end(); ++it)
15 | cout << *it << (it != vec.end() - 1 ? "," : "");
16 | cout << "]\n" << endl;
17 | }
18 |
19 | void check_and_print(const vector& vec)
20 | {
21 |
22 | cout << "size: " << vec.size() << " content: [";
23 | for (auto it = vec.begin(); it != vec.end(); ++it)
24 | cout << *it << (it != vec.end() - 1 ? "," : "");
25 | cout << "]\n" << endl;
26 | }
27 |
28 | int main()
29 | {
30 | vector v1;
31 | vector v2(10);
32 | vector v3(10, 42);
33 | vector v4{ 10 };
34 | vector v5{ 10, 42 };
35 | vector v6{ 10 };
36 | vector v7{ 10, "hi" };
37 |
38 | check_and_print(v1);
39 | check_and_print(v2);
40 | check_and_print(v3);
41 | check_and_print(v4);
42 | check_and_print(v5);
43 | check_and_print(v6);
44 | check_and_print(v7);
45 |
46 | return 0;
47 | }
--------------------------------------------------------------------------------
/ch03/exercise3_22.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include
5 |
6 | using namespace std;
7 |
8 | int main()
9 | {
10 | vector text;
11 | text.push_back("aaaaaaaaaa aaaaaaaaa aaaaaa");
12 | text.push_back("");
13 | text.push_back("bbbbbbbbbbbbbb bbbbbbbbbbb bbbbbbbbbbbbb");
14 |
15 | for (auto it = text.begin(); it != text.end() && !it->empty(); ++it)
16 | {
17 | for (auto &c : *it)
18 | {
19 | if (isalpha(c)) c = toupper(c);
20 | }
21 | }
22 |
23 | for (auto it : text)
24 | {
25 | cout << it << endl;
26 | }
27 | return 0;
28 | }
--------------------------------------------------------------------------------
/ch03/exercise3_23.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | using namespace std;
5 |
6 | int main()
7 | {
8 | vector ivec(10, 42);
9 |
10 | for (auto &it : ivec)
11 | {
12 | it = it * 2;
13 | cout << it << endl;
14 | }
15 |
16 | return 0;
17 | }
--------------------------------------------------------------------------------
/ch03/exercise3_24.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include
5 |
6 | using std::cin;
7 | using std::cout;
8 | using std::endl;
9 | using std::vector;
10 | using std::string;
11 |
12 | int main()
13 | {
14 | vector ivec;
15 | int i;
16 | while (cin >> i)
17 | {
18 | ivec.push_back(i);
19 | }
20 |
21 | for (auto it = ivec.begin(); it != ivec.end() - 1; ++it)
22 | {
23 | cout << *it + *(it + 1) << endl;
24 | }
25 |
26 | //---------------------------------
27 | cout << "---------------------------------" << endl;
28 |
29 | auto it1 = ivec.begin();
30 | auto it2 = ivec.end() - 1;
31 | while (it1 < it2)
32 | {
33 | cout << *it1 + *it2 << endl;
34 | ++it1;
35 | --it2;
36 | }
37 | return 0;
38 | }
--------------------------------------------------------------------------------
/ch03/exercise3_25.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | using std::vector; using std::cout; using std::cin; using std::endl;
5 |
6 | int main()
7 | {
8 | vector scores(11, 0);
9 | unsigned grade;
10 | while (cin >> grade)
11 | {
12 | if (grade <= 100)
13 | ++*(scores.begin() + grade / 10);
14 | }
15 |
16 | for (auto s : scores)
17 | cout << s << " ";
18 | cout << endl;
19 |
20 | return 0;
21 | }
--------------------------------------------------------------------------------
/ch03/exercise3_2_a.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | using std::string;
5 | using std::cin;
6 | using std::cout;
7 | using std::endl;
8 | using std::getline;
9 |
10 | int main()
11 | {
12 | string s;
13 | while (getline(cin,s))
14 | {
15 | cout << s << endl;
16 | }
17 | return 0;
18 | }
--------------------------------------------------------------------------------
/ch03/exercise3_2_b.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | using std::string;
5 | using std::cin;
6 | using std::cout;
7 | using std::endl;
8 |
9 | int main()
10 | {
11 | string s;
12 | while (cin >> s)
13 | {
14 | cout << s << endl;
15 | }
16 | return 0;
17 | }
--------------------------------------------------------------------------------
/ch03/exercise3_31.cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | int main()
4 | {
5 | int arr[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
6 |
7 | return 0;
8 | }
--------------------------------------------------------------------------------
/ch03/exercise3_32.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | using std::cout; using std::endl; using std::vector;
4 |
5 | int main()
6 | {
7 | // array
8 | int arr[10];
9 | for (int i = 0; i < 10; ++i) arr[i] = i;
10 | int arr2[10];
11 | for (int i = 0; i < 10; ++i) arr2[i] = arr[i];
12 |
13 | // vector
14 | vector v(10);
15 | for (int i = 0; i != 10; ++i) v[i] = arr[i];
16 | vector v2(v);
17 | for (auto i : v2) cout << i << " ";
18 | cout << endl;
19 |
20 | return 0;
21 | }
--------------------------------------------------------------------------------
/ch03/exercise3_35.cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | using std::cout;
4 | using std::endl;
5 |
6 | int main()
7 | {
8 | const int size = 10;
9 | int arr[size];
10 | for (auto ptr = arr; ptr != arr + size; ++ptr) *ptr = 0;
11 |
12 | for (auto i : arr) cout << i << " ";
13 | cout << endl;
14 |
15 | return 0;
16 | }
--------------------------------------------------------------------------------
/ch03/exercise3_36.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 |
5 | using namespace std;
6 |
7 | bool compare(int* const pb1, int* const pe1, int* const pb2, int* const pe2)
8 | {
9 | if ((pe1 - pb1) != (pe2 - pb2))
10 | return false;
11 | else
12 | {
13 | for (int* i = pb1, *j = pb2; (i != pe1) && (j != pe2); ++i, ++j)
14 | if (*i != *j) return false;
15 | }
16 |
17 | return true;
18 | }
19 |
20 | int main()
21 | {
22 | int arr1[3] = { 0, 1, 2 };
23 | int arr2[3] = { 0, 2, 4 };
24 |
25 | if (compare(begin(arr1), end(arr1), begin(arr2), end(arr2)))
26 | cout << "The two arrays are equal." << endl;
27 | else
28 | cout << "The two arrays are not equal." << endl;
29 |
30 | cout << "==========" << endl;
31 |
32 | vector vec1 = { 0, 1, 2 };
33 | vector vec2 = { 0, 1, 2 };
34 |
35 | if (vec1 == vec2)
36 | cout << "The two vectors are equal." << endl;
37 | else
38 | cout << "The two vectors are not equal." << endl;
39 |
40 | return 0;
41 | }
--------------------------------------------------------------------------------
/ch03/exercise3_39.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | using std::cout; using std::endl; using std::string;
5 |
6 | int main()
7 | {
8 | string s1("aaaaaaaaaa"), s2("bbbbbbbbbb");
9 | if (s1 == s2)
10 | cout << "same string." << endl;
11 | else if (s1 > s2)
12 | cout << "aaaaaaaaaa > bbbbbbbbbb" << endl;
13 | else
14 | cout << "aaaaaaaaaa < bbbbbbbbbb" << endl;
15 |
16 | cout << "=========" << endl;
17 |
18 | const char* cs1 = "aaaaaaaaaa";
19 | const char* cs2 = "bbbbbbbbbb";
20 | auto result = strcmp(cs1, cs2);
21 | if (result == 0)
22 | cout << "same string." << endl;
23 | else if (result < 0)
24 | cout << "aaaaaaaaaa < bbbbbbbbbb" << endl;
25 | else
26 | cout << "aaaaaaaaaa > bbbbbbbbbb" << endl;
27 |
28 | return 0;
29 | }
--------------------------------------------------------------------------------
/ch03/exercise3_40.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | const char cstr1[] = "Hello";
5 | const char cstr2[] = "world!";
6 |
7 | int main()
8 | {
9 | char cstr3[100];
10 |
11 | strcpy_s(cstr3, cstr1);
12 | strcat_s(cstr3, " ");
13 | strcat_s(cstr3, cstr2);
14 |
15 | std::cout << cstr3 << std::endl;
16 | }
--------------------------------------------------------------------------------
/ch03/exercise3_41.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | using namespace std;
5 |
6 | int main()
7 | {
8 | int arr[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
9 | vector v(begin(arr), end(arr));
10 |
11 | for (auto i : v) cout << i << " ";
12 | cout << endl;
13 |
14 | return 0;
15 | }
--------------------------------------------------------------------------------
/ch03/exercise3_42.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | using namespace std;
5 |
6 | int main()
7 | {
8 | vector v{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
9 | int arr[10];
10 | for (int i = 0; i != v.size(); ++i)
11 | arr[i] = v[i];
12 |
13 | for (auto i : arr) cout << i << " ";
14 | cout << endl;
15 |
16 | return 0;
17 | }
--------------------------------------------------------------------------------
/ch03/exercise3_43.cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | using std::cout;
4 | using std::endl;
5 |
6 | int main()
7 | {
8 | int arr[3][4] =
9 | {
10 | { 0, 1, 2, 3 },
11 | { 4, 5, 6, 7 },
12 | { 8, 9, 10, 11 }
13 | };
14 |
15 | // range for
16 | for (const int(&row)[4] : arr)
17 | for (int col : row) cout << col << " ";
18 |
19 | cout << endl;
20 |
21 | // for loop
22 | for (size_t i = 0; i != 3; ++i)
23 | for (size_t j = 0; j != 4; ++j) cout << arr[i][j] << " ";
24 |
25 | cout << endl;
26 |
27 | // using pointers.
28 | for (int(*row)[4] = arr; row != arr + 3; ++row)
29 | for (int *col = *row; col != *row + 4; ++col) cout << *col << " ";
30 |
31 | cout << endl;
32 |
33 | return 0;
34 | }
--------------------------------------------------------------------------------
/ch03/exercise3_44.cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | using std::cout;
4 | using std::endl;
5 |
6 | int main()
7 | {
8 | int ia[3][4] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
9 |
10 | using int_array = int[4];
11 |
12 | for (int_array& p : ia)
13 | for (int q : p)
14 | cout << q << " ";
15 | cout << endl;
16 |
17 | for (size_t i = 0; i != 3; ++i)
18 | for (size_t j = 0; j != 4; ++j)
19 | cout << ia[i][j] << " ";
20 | cout << endl;
21 |
22 | for (int_array* p = ia; p != ia + 3; ++p)
23 | for (int *q = *p; q != *p + 4; ++q)
24 | cout << *q << " ";
25 | cout << endl;
26 |
27 | return 0;
28 | }
--------------------------------------------------------------------------------
/ch03/exercise3_45.cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | using std::cout;
4 | using std::endl;
5 |
6 | int main()
7 | {
8 | int ia[3][4] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
9 |
10 | for (auto& p : ia)
11 | for (auto q : p)
12 | cout << q << " ";
13 | cout << endl;
14 |
15 | for (auto i = 0; i != 3; ++i)
16 | for (auto j = 0; j != 4; ++j)
17 | cout << ia[i][j] << " ";
18 | cout << endl;
19 |
20 | for (auto p = ia; p != ia + 3; ++p)
21 | for (auto q = *p; q != *p + 4; ++q)
22 | cout << *q << " ";
23 | cout << endl;
24 |
25 | return 0;
26 | }
--------------------------------------------------------------------------------
/ch03/exercise3_4_a.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | using std::string;
4 | using std::cin;
5 | using std::cout;
6 | using std::endl;
7 |
8 | int main()
9 | {
10 | string str1, str2;
11 | while (cin >> str1 >> str2)
12 | {
13 | if (str1 == str2)
14 | cout << "The two strings are equal." << endl;
15 | else
16 | cout << "The larger string is " << ((str1 > str2) ? str1 : str2);
17 | }
18 |
19 | return 0;
20 | }
--------------------------------------------------------------------------------
/ch03/exercise3_4_b.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | using std::string;
4 | using std::cin;
5 | using std::cout;
6 | using std::endl;
7 |
8 | int main()
9 | {
10 | string str1, str2;
11 | while (cin >> str1 >> str2)
12 | {
13 | if (str1.size() == str2.size())
14 | cout << "The two strings have the same length." << endl;
15 | else
16 | cout << "The longer string is " << ((str1.size() > str2.size()) ? str1 : str2) << endl;
17 | }
18 |
19 | return 0;
20 | }
--------------------------------------------------------------------------------
/ch03/exercise3_5_a.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | using std::string;
5 | using std::cin;
6 | using std::cout;
7 | using std::endl;
8 |
9 | int main()
10 | {
11 | string result, s;
12 | while (cin >> s)
13 | {
14 | result += s;
15 | }
16 | cout << result << endl;
17 |
18 | return 0;
19 | }
--------------------------------------------------------------------------------
/ch03/exercise3_5_b.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | using std::string;
5 | using std::cin;
6 | using std::cout;
7 | using std::endl;
8 |
9 | int main()
10 | {
11 | string result, s;
12 | while (cin >> s)
13 | {
14 | result += s + " ";
15 | }
16 | cout << result << endl;
17 |
18 | return 0;
19 | }
--------------------------------------------------------------------------------
/ch03/exercise3_6.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 |
5 | using std::string;
6 | using std::cin;
7 | using std::cout;
8 | using std::endl;
9 |
10 | int main()
11 | {
12 | string s = "this is a string";
13 |
14 | for (auto &x : s)
15 | {
16 | x = 'X';
17 | }
18 |
19 | cout << s << endl;
20 | return 0;
21 | }
--------------------------------------------------------------------------------
/ch03/exercise3_7.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 |
5 | using std::string;
6 | using std::cin;
7 | using std::cout;
8 | using std::endl;
9 |
10 | int main()
11 | {
12 | string s = "this is a string";
13 |
14 | for (char x : s)
15 | {
16 | x = 'X';
17 | }
18 |
19 | cout << s << endl;
20 | return 0;
21 | }
--------------------------------------------------------------------------------
/ch03/exercise3_8.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 |
5 | using std::string;
6 | using std::cin;
7 | using std::cout;
8 | using std::endl;
9 |
10 | int main()
11 | {
12 | string s = "this is a string";
13 |
14 | decltype(s.size()) i = 0;
15 | while (i != s.size())
16 | {
17 | s[i] = 'X';
18 | ++i;
19 | }
20 | cout << s << endl;
21 | for (i = 0; i != s.size(); ++i)
22 | {
23 | s[i] = 'Y';
24 | }
25 | cout << s << endl;
26 | return 0;
27 | }
--------------------------------------------------------------------------------
/ch03/exercise_3_1_a.cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | using std::cin;
4 | using std::cout;
5 | using std::endl;
6 |
7 | int main()
8 | {
9 | int sum = 0;
10 | for (int val = 1; val <= 10; ++val) sum += val;
11 |
12 | cout << "Sum of 1 to 10 inclusive is " << sum << endl;
13 |
14 | return 0;
15 | }
--------------------------------------------------------------------------------
/ch03/exercise_3_1_b.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "exercise2_42.h"
4 |
5 | using std::cin;
6 | using std::cout;
7 | using std::endl;
8 | using std::cerr;
9 |
10 | int main()
11 | {
12 | Sales_data data1, data2;
13 |
14 | double price = 0;
15 |
16 | cin >> data1.bookNo >> data1.units_sold >> price;
17 |
18 | data1.revenue = data1.units_sold * price;
19 |
20 | cin >> data2.bookNo >> data2.units_sold >> price;
21 | data2.revenue = data2.units_sold * price;
22 |
23 | if (data1.bookNo == data2.bookNo)
24 | {
25 | unsigned totalCnt = data1.units_sold + data2.units_sold;
26 | double totalRevenue = data1.revenue + data2.revenue;
27 |
28 | cout << data1.bookNo << " " << totalCnt
29 | << " " << totalRevenue << " ";
30 | if (totalCnt != 0)
31 | cout << totalRevenue / totalCnt << endl;
32 | else
33 | cout << "(no sales)" << endl;
34 |
35 | return 0;
36 | }
37 | else
38 | {
39 | cerr << "Data must refer to the same ISBN" << endl;
40 | return -1;
41 | }
42 | }
--------------------------------------------------------------------------------
/ch04/exercise4_21.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | using std::cout;
5 | using std::endl;
6 | using std::vector;
7 |
8 | int main()
9 | {
10 | vector ivec{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };
11 |
12 | for (auto i : ivec)
13 | {
14 | cout << ((i & 0x1) ? i * 2 : i) << " ";
15 | }
16 | cout << endl;
17 |
18 | return 0;
19 | }
--------------------------------------------------------------------------------
/ch04/exercise4_22.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using std::cout; using std::cin; using std::endl;
3 |
4 | int main()
5 | {
6 | for (unsigned g; cin >> g;)
7 | {
8 | auto result = g > 90 ? "high pass" : g < 60 ? "fail" : g < 75 ? "low pass" : "pass";
9 | cout << result << endl;
10 |
11 | // -------------------------
12 | if (g > 90) cout << "high pass";
13 | else if (g < 60) cout << "fail";
14 | else if (g < 75) cout << "low pass";
15 | else cout << "pass";
16 | cout << endl;
17 | }
18 |
19 | return 0;
20 | }
--------------------------------------------------------------------------------
/ch04/exercise4_28.cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | using namespace std;
4 |
5 | int main()
6 | {
7 | cout << "bool:\t\t" << sizeof(bool) << " bytes" << endl << endl;
8 |
9 | cout << "char:\t\t" << sizeof(char) << " bytes" << endl;
10 | cout << "wchar_t:\t" << sizeof(wchar_t) << " bytes" << endl;
11 | cout << "char16_t:\t" << sizeof(char16_t) << " bytes" << endl;
12 | cout << "char32_t:\t" << sizeof(char32_t) << " bytes" << endl << endl;
13 |
14 | cout << "short:\t\t" << sizeof(short) << " bytes" << endl;
15 | cout << "int:\t\t" << sizeof(int) << " bytes" << endl;
16 | cout << "long:\t\t" << sizeof(long) << " bytes" << endl;
17 | cout << "long long:\t" << sizeof(long long) << " bytes" << endl << endl;
18 |
19 | cout << "float:\t\t" << sizeof(float) << " bytes" << endl;
20 | cout << "double:\t\t" << sizeof(double) << " bytes" << endl;
21 | cout << "long double:\t" << sizeof(long double) << " bytes" << endl << endl;
22 |
23 | return 0;
24 | }
--------------------------------------------------------------------------------
/ch05/exercise5_10.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using std::cin; using std::cout; using std::endl;
3 |
4 | int main()
5 | {
6 | unsigned aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0;
7 | char ch;
8 | while (cin >> ch)
9 | switch (ch)
10 | {
11 | case 'a':
12 | case 'A':
13 | ++aCnt;
14 | break;
15 | case 'e':
16 | case 'E':
17 | ++eCnt;
18 | break;
19 | case 'i':
20 | case 'I':
21 | ++iCnt;
22 | break;
23 | case 'o':
24 | case 'O':
25 | ++oCnt;
26 | break;
27 | case 'u':
28 | case 'U':
29 | ++uCnt;
30 | break;
31 | }
32 |
33 | cout << "Number of vowel a(A): \t" << aCnt << '\n'
34 | << "Number of vowel e(E): \t" << eCnt << '\n'
35 | << "Number of vowel i(I): \t" << iCnt << '\n'
36 | << "Number of vowel o(O): \t" << oCnt << '\n'
37 | << "Number of vowel u(U): \t" << uCnt << endl;
38 |
39 | return 0;
40 | }
--------------------------------------------------------------------------------
/ch05/exercise5_11.cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | using std::cin; using std::cout; using std::endl;
4 |
5 | int main()
6 | {
7 | unsigned aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0, spaceCnt = 0, tabCnt = 0, newLineCnt = 0;
8 | char ch;
9 | while (cin >> std::noskipws >> ch)
10 | switch (ch)
11 | {
12 | case 'a':
13 | case 'A':
14 | ++aCnt;
15 | break;
16 | case 'e':
17 | case 'E':
18 | ++eCnt;
19 | break;
20 | case 'i':
21 | case 'I':
22 | ++iCnt;
23 | break;
24 | case 'o':
25 | case 'O':
26 | ++oCnt;
27 | break;
28 | case 'u':
29 | case 'U':
30 | ++uCnt;
31 | break;
32 | case ' ':
33 | ++spaceCnt;
34 | break;
35 | case '\t':
36 | ++tabCnt;
37 | break;
38 | case '\n':
39 | ++newLineCnt;
40 | break;
41 | }
42 |
43 | cout << "Number of vowel a(A): \t" << aCnt << '\n'
44 | << "Number of vowel e(E): \t" << eCnt << '\n'
45 | << "Number of vowel i(I): \t" << iCnt << '\n'
46 | << "Number of vowel o(O): \t" << oCnt << '\n'
47 | << "Number of vowel u(U): \t" << uCnt << '\n'
48 | << "Number of space: \t" << spaceCnt << '\n'
49 | << "Number of tab char: \t" << tabCnt << '\n'
50 | << "Number of new line: \t" << newLineCnt << endl;
51 |
52 | return 0;
53 | }
--------------------------------------------------------------------------------
/ch05/exercise5_12.cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | using std::cin; using std::cout; using std::endl;
4 |
5 | int main()
6 | {
7 | unsigned aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0, spaceCnt = 0, tabCnt = 0, newLineCnt = 0, ffCnt = 0, flCnt = 0, fiCnt = 0;
8 | char ch, prech = '\0';
9 | while (cin >> std::noskipws >> ch)
10 | {
11 | switch (ch)
12 | {
13 | case 'a':
14 | case 'A':
15 | ++aCnt;
16 | break;
17 | case 'e':
18 | case 'E':
19 | ++eCnt;
20 | break;
21 | case 'i':
22 | if (prech == 'f') ++fiCnt;
23 | case 'I':
24 | ++iCnt;
25 | break;
26 | case 'o':
27 | case 'O':
28 | ++oCnt;
29 | break;
30 | case 'u':
31 | case 'U':
32 | ++uCnt;
33 | break;
34 | case ' ':
35 | ++spaceCnt;
36 | break;
37 | case '\t':
38 | ++tabCnt;
39 | break;
40 | case '\n':
41 | ++newLineCnt;
42 | break;
43 | case 'f':
44 | if (prech == 'f') ++ffCnt;
45 | break;
46 | case 'l':
47 | if (prech == 'f') ++flCnt;
48 | break;
49 | }
50 | prech = ch;
51 | }
52 |
53 | cout << "Number of vowel a(A): \t" << aCnt << '\n'
54 | << "Number of vowel e(E): \t" << eCnt << '\n'
55 | << "Number of vowel i(I): \t" << iCnt << '\n'
56 | << "Number of vowel o(O): \t" << oCnt << '\n'
57 | << "Number of vowel u(U): \t" << uCnt << '\n'
58 | << "Number of space: \t" << spaceCnt << '\n'
59 | << "Number of tab char: \t" << tabCnt << '\n'
60 | << "Number of new line: \t" << newLineCnt << '\n'
61 | << "Number of ff: \t" << ffCnt << '\n'
62 | << "Number of fl: \t" << flCnt << '\n'
63 | << "Number of fi: \t" << fiCnt << endl;
64 |
65 | return 0;
66 | }
--------------------------------------------------------------------------------
/ch05/exercise5_14.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | using std::cout; using std::cin; using std::endl; using std::string; using std::pair;
5 |
6 | int main()
7 | {
8 | pair max_duplicated;
9 | int count = 0;
10 | for (string str, prestr; cin >> str; prestr = str)
11 | {
12 | if (str == prestr) ++count;
13 | else count = 0;
14 | if (count > max_duplicated.second) max_duplicated = { prestr, count };
15 | }
16 |
17 | if (max_duplicated.first.empty()) cout << "There's no duplicated string." << endl;
18 | else cout << "the word " << max_duplicated.first << " occurred " << max_duplicated.second + 1 << " times. " << endl;
19 |
20 | return 0;
21 | }
--------------------------------------------------------------------------------
/ch05/exercise5_17.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | using std::cout; using std::vector;
5 |
6 | bool is_prefix(const vector& lhs, const vector& rhs)
7 | {
8 | if (lhs.size() > rhs.size())
9 | return is_prefix(rhs, lhs);
10 | for (unsigned i = 0; i != lhs.size(); ++i)
11 | if (lhs[i] != rhs[i])
12 | return false;
13 | return true;
14 | }
15 |
16 | int main()
17 | {
18 | vector l{ 0, 1, 1, 2 };
19 | vector r{ 0, 1, 1, 2, 3, 5, 8 };
20 | cout << (is_prefix(r, l) ? "yes\n" : "no\n");
21 |
22 | return 0;
23 | }
--------------------------------------------------------------------------------
/ch05/exercise5_19.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | using std::cout; using std::cin; using std::endl; using std::string;
5 |
6 | int main()
7 | {
8 | string rsp;
9 | do
10 | {
11 | cout << "Input two strings: ";
12 | string str1, str2;
13 | cin >> str1 >> str2;
14 | cout << (str1 <= str2 ? str1 : str2)
15 | << " is less than the other. " << "\n\n"
16 | << "More? Enter yes or no: ";
17 | cin >> rsp;
18 | } while (tolower(rsp[0]) == 'y');
19 | return 0;
20 | }
--------------------------------------------------------------------------------
/ch05/exercise5_20.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | using std::cout; using std::cin; using std::endl; using std::string;
4 |
5 | int main()
6 | {
7 | string read, tmp;
8 | while (cin >> read)
9 | if (read == tmp) break; else tmp = read;
10 |
11 | if (cin.eof()) cout << "no word was repeated." << endl;
12 | else cout << read << " occurs twice in succession." << endl;
13 |
14 | return 0;
15 | }
--------------------------------------------------------------------------------
/ch05/exercise5_21.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | using namespace std;
5 |
6 | int main()
7 | {
8 | string curr, prev;
9 | bool no_twice = true;
10 | while (cin >> curr)
11 | {
12 | if (isupper(curr[0]) && prev == curr)
13 | {
14 | cout << curr << ": occurs twice in succession." << endl;
15 | no_twice = false;
16 | break;
17 | }
18 | prev = curr;
19 | }
20 |
21 | if (no_twice)
22 | cout << "no word was repeated." << endl;
23 |
24 | return 0;
25 | }
--------------------------------------------------------------------------------
/ch05/exercise5_23.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using std::cin;
3 | using std::cout;
4 | using std::endl;
5 |
6 | int main()
7 | {
8 | int i, j;
9 | cin >> i >> j;
10 | cout << i / j << endl;
11 |
12 | return 0;
13 | }
--------------------------------------------------------------------------------
/ch05/exercise5_24.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | int main(void)
5 | {
6 | int i, j;
7 | std::cin >> i >> j;
8 | if (j == 0)
9 | throw std::runtime_error("divisor is 0");
10 | std::cout << i / j << std::endl;
11 |
12 | return 0;
13 | }
--------------------------------------------------------------------------------
/ch05/exercise5_25.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | using std::cin; using std::cout; using std::endl; using std::runtime_error;
4 |
5 | int main(void)
6 | {
7 | int i, j;
8 | cout << "please input tow numbers: " << endl;
9 | while (cin >> i >> j)
10 | {
11 | try
12 | {
13 | if (j == 0)
14 | throw runtime_error("divisor is 0");
15 | cout << i / j << endl;
16 | }
17 | catch (runtime_error err)
18 | {
19 | cout << err.what() << "\nTry Again? Enter y or n" << endl;
20 | char c;
21 | cin >> c;
22 | if (c != 'y')
23 | break;
24 | }
25 | cout << "please input tow numbers: " << endl;
26 | }
27 |
28 | return 0;
29 | }
--------------------------------------------------------------------------------
/ch05/exercise5_5.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | using std::vector; using std::string; using std::cout; using std::endl; using std::cin;
5 |
6 | int main()
7 | {
8 | vector scores = { "F", "D", "C", "B", "A", "A++" };
9 | for (int g; cin >> g;)
10 | {
11 | string letter;
12 | if (g < 60)
13 | {
14 | letter = scores[0];
15 | }
16 | else
17 | {
18 | letter = scores[(g - 50) / 10];
19 | if (g != 100)
20 | letter += g % 10 > 7 ? "+" : g % 10 < 3 ? "-" : "";
21 | cout << letter << endl;
22 | }
23 | }
24 |
25 | return 0;
26 | }
--------------------------------------------------------------------------------
/ch05/exercise5_6.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | using std::vector; using std::string; using std::cout; using std::endl; using std::cin;
5 |
6 | int main()
7 | {
8 | vector scores = { "F", "D", "C", "B", "A", "A++" };
9 |
10 | int grade = 0;
11 | while (cin >> grade)
12 | {
13 | string lettergrade = grade < 60 ? scores[0] : scores[(grade - 50) / 10];
14 | lettergrade += (grade == 100 || grade < 60) ? "" : (grade % 10 > 7) ? "+" : (grade % 10 < 3) ? "-" : "";
15 | cout << lettergrade << endl;
16 | }
17 |
18 |
19 | return 0;
20 | }
--------------------------------------------------------------------------------
/ch05/exercise5_9.cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | using std::cout; using std::endl; using std::cin;
4 |
5 | int main()
6 | {
7 | unsigned aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0;
8 | char ch;
9 | while (cin >> ch)
10 | {
11 | if (ch == 'a') ++aCnt;
12 | else if (ch == 'e') ++eCnt;
13 | else if (ch == 'i') ++iCnt;
14 | else if (ch == 'o') ++oCnt;
15 | else if (ch == 'u') ++uCnt;
16 | }
17 | cout << "Number of vowel a: \t" << aCnt << '\n'
18 | << "Number of vowel e: \t" << eCnt << '\n'
19 | << "Number of vowel i: \t" << iCnt << '\n'
20 | << "Number of vowel o: \t" << oCnt << '\n'
21 | << "Number of vowel u: \t" << uCnt << endl;
22 |
23 | return 0;
24 | }
--------------------------------------------------------------------------------
/ch06/Chapter6.h:
--------------------------------------------------------------------------------
1 | int fact(int val);
2 | int func();
3 |
4 | template
5 | T abs(T i)
6 | {
7 | return i >= 0 ? i : -i;
8 | }
--------------------------------------------------------------------------------
/ch06/exercise6_10.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | void swap(int* lhs, int* rhs)
5 | {
6 | int tmp;
7 | tmp = *lhs;
8 | *lhs = *rhs;
9 | *rhs = tmp;
10 | }
11 |
12 | int main()
13 | {
14 | for (int lft, rht; std::cout << "Please Enter:\n", std::cin >> lft >> rht;)
15 | {
16 | swap(&lft, &rht);
17 | std::cout << lft << " " << rht << std::endl;
18 | }
19 |
20 | return 0;
21 | }
--------------------------------------------------------------------------------
/ch06/exercise6_11.cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | void reset(int &i)
4 | {
5 | i = 0;
6 | }
7 |
8 | int main()
9 | {
10 | int i = 42;
11 | reset(i);
12 | std::cout << i << std::endl;
13 | return 0;
14 | }
--------------------------------------------------------------------------------
/ch06/exercise6_12.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 |
5 | void swap(int& lhs, int& rhs)
6 | {
7 | int temp = lhs;
8 | lhs = rhs;
9 | rhs = temp;
10 | }
11 |
12 | int main()
13 | {
14 | for (int left, right; std::cout << "Please Enter:\n", std::cin >> left >> right;)
15 | {
16 | swap(left, right);
17 | std::cout << left << " " << right << std::endl;
18 | }
19 |
20 | return 0;
21 | }
--------------------------------------------------------------------------------
/ch06/exercise6_17.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 |
5 | using std::cout; using std::endl; using std::string;
6 |
7 | bool any_capital(string const& str)
8 | {
9 | for (auto ch : str)
10 | if (isupper(ch)) return true;
11 | return false;
12 | }
13 |
14 | void to_lowercase(string& str)
15 | {
16 | for (auto& ch : str) ch = tolower(ch);
17 | }
18 |
19 | int main()
20 | {
21 | string hello("Hello World!");
22 | cout << any_capital(hello) << endl;
23 |
24 | to_lowercase(hello);
25 | cout << hello << endl;
26 |
27 | return 0;
28 | }
--------------------------------------------------------------------------------
/ch06/exercise6_21.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using std::cout;
3 |
4 | int larger_one(int i, const int *p)
5 | {
6 | return (i > *p) ? i : *p;
7 | }
8 |
9 | int main()
10 | {
11 | int i = 6;
12 | cout << larger_one(7, &i);
13 |
14 | return 0;
15 | }
--------------------------------------------------------------------------------
/ch06/exercise6_22.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | void swap(int*& lft, int*& rht)
5 | {
6 | auto tmp = lft;
7 | lft = rht;
8 | rht = tmp;
9 | }
10 |
11 | int main()
12 | {
13 | int i = 42, j = 99;
14 | auto lft = &i;
15 | auto rht = &j;
16 | swap(lft, rht);
17 | std::cout << *lft << " " << *rht << std::endl;
18 |
19 | return 0;
20 | }
--------------------------------------------------------------------------------
/ch06/exercise6_23.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using std::cout; using std::endl; using std::begin; using std::end;
3 |
4 | void print(int i)
5 | {
6 | cout << i << endl;
7 | }
8 |
9 | void print(const int *beg, const int *end)
10 | {
11 | while (beg != end)
12 | cout << *beg++ << endl;
13 | }
14 |
15 | void print(const int ia[], size_t size)
16 | {
17 | for (size_t i = 0; i != size; ++i)
18 | {
19 | cout << ia[i] << endl;
20 | }
21 | }
22 |
23 | void print(int (&arr)[2])
24 | {
25 | for (auto i : arr)
26 | cout << i << endl;
27 | }
28 |
29 | int main()
30 | {
31 | int i = 0, j[2] = { 0, 1 };
32 |
33 | print(i);
34 | print(begin(j), end(j));
35 | print(j, end(j) - begin(j));
36 | print(j);
37 |
38 | return 0;
39 | }
--------------------------------------------------------------------------------
/ch06/exercise6_25.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | int main(int argc, char **argv)
5 | {
6 | std::string str;
7 | for (int i = 1; i != argc; ++i)
8 | str += std::string(argv[i]) + " ";
9 |
10 | std::cout << str << std::endl;
11 | return 0;
12 | }
--------------------------------------------------------------------------------
/ch06/exercise6_27.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | int sum(std::initializer_list const& il)
5 | {
6 | int sum = 0;
7 | for (auto i : il)
8 | sum += i;
9 | return sum;
10 | }
11 |
12 | int main(void)
13 | {
14 | auto il = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
15 | std::cout << sum(il) << std::endl;
16 |
17 | return 0;
18 | }
--------------------------------------------------------------------------------
/ch06/exercise6_3.cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | int fact(int i)
4 | {
5 | return i > 1 ? i * fact(i - 1) : 1;
6 | }
7 |
8 | int main()
9 | {
10 | std::cout << fact(5) << std::endl;
11 | return 0;
12 | }
--------------------------------------------------------------------------------
/ch06/exercise6_30.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | using namespace std;
5 |
6 | bool str_subrange(const string& str1, const string& str2)
7 | {
8 | if (str1.size() == str2.size())
9 | return str1 == str2;
10 | auto size = (str1.size() < str2.size()) ? str1.size() : str2.size();
11 |
12 | for (decltype(size) i = 0; i != size; ++i)
13 | {
14 | if (str1[i] != str2[i])
15 | return;
16 | }
17 | }
18 |
19 | int main(void)
20 | {
21 |
22 | return 0;
23 | }
--------------------------------------------------------------------------------
/ch06/exercise6_33.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | using namespace std;
5 |
6 | using Iter = vector::const_iterator;
7 |
8 | void print(Iter first, Iter last)
9 | {
10 | if (first == last)
11 | {
12 | cout << "over!" << endl;
13 | return;
14 | }
15 | cout << *first << " ";
16 | print(++first, last);
17 |
18 | }
19 |
20 | int main()
21 | {
22 | vector vec{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };
23 | print(vec.cbegin(), vec.cend());
24 |
25 | return 0;
26 | }
--------------------------------------------------------------------------------
/ch06/exercise6_4.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | int fact(int i)
5 | {
6 | return i > 1 ? i * fact(i - 1) : 1;
7 | }
8 |
9 | void interactive_fact()
10 | {
11 | std::string const prompt = "Enter a number within [1, 13) :\n";
12 | std::string const out_of_range = "Out of range, please try again.\n";
13 | for (int i; std::cout << prompt, std::cin >> i;)
14 | {
15 | if (i < 1 || i > 12)
16 | {
17 | std::cout << out_of_range;
18 | continue;
19 | }
20 | std::cout << fact(i) << std::endl;
21 | }
22 | }
23 |
24 | int main()
25 | {
26 | interactive_fact();
27 | return 0;
28 | }
--------------------------------------------------------------------------------
/ch06/exercise6_42.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | using std::string;
5 | using std::cout;
6 | using std::endl;
7 |
8 | string make_plural(size_t ctr, const string& word, const string& ending = "s")
9 | {
10 | return (ctr > 1) ? word + ending : word;
11 | }
12 |
13 | int main()
14 | {
15 | cout << "singual: " << make_plural(1, "success", "es") << " "
16 | << make_plural(1, "failure") << endl;
17 | cout << "plural : " << make_plural(2, "success", "es") << " "
18 | << make_plural(2, "failure") << endl;
19 |
20 | return 0;
21 | }
--------------------------------------------------------------------------------
/ch06/exercise6_47.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | using namespace std;
5 |
6 | using Iter = vector::const_iterator;
7 |
8 | #define NDEBUG
9 |
10 | void print(Iter first, Iter last)
11 | {
12 | #ifndef NDEBUG
13 | cout << "vector size: " << last - first << endl;
14 | #endif
15 | if (first == last)
16 | {
17 | cout << "over!" << endl;
18 | return;
19 | }
20 | cout << *first << " ";
21 | print(++first, last);
22 |
23 | }
24 |
25 | int main()
26 | {
27 | vector vec{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };
28 | print(vec.cbegin(), vec.cend());
29 |
30 | return 0;
31 | }
--------------------------------------------------------------------------------
/ch06/exercise6_51.cpp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/huangmingchuan/Cpp_Primer_Answers/925506ca53f7b2e8501bc8eaaee3dba8c03d9ec2/ch06/exercise6_51.cpp
--------------------------------------------------------------------------------
/ch06/exercise6_55.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | using namespace std;
5 |
6 | int add(int a, int b) { return a + b; }
7 | int subtract(int a, int b) { return a - b; }
8 | int multiply(int a, int b) { return a * b; }
9 | int divide(int a, int b) { return b != 0 ? a / b : 0; }
10 |
11 | int main()
12 | {
13 | int func(int, int);
14 | vector v;
15 | v.push_back(add);
16 | v.push_back(subtract);
17 | v.push_back(multiply);
18 | v.push_back(divide);
19 |
20 | for (auto i : v)
21 | {
22 | cout << i(6, 2) << ", "; // 8, 4, 12, 3
23 | }
24 | cout << endl;
25 | return 0;
26 | }
--------------------------------------------------------------------------------
/ch06/fact.cpp:
--------------------------------------------------------------------------------
1 | #include "Chapter6.h"
2 | #include
3 |
4 | int fact(int val)
5 | {
6 | if (val == 0 || val == 1) return 1;
7 | else return val * fact(val - 1);
8 | }
9 |
10 | int func()
11 | {
12 | int n, ret = 1;
13 | std::cout << "input a number: ";
14 | std::cin >> n;
15 | while (n > 1) ret *= n--;
16 | return ret;
17 | }
--------------------------------------------------------------------------------
/ch06/factMain.cpp:
--------------------------------------------------------------------------------
1 | #include "Chapter6.h"
2 | #include
3 |
4 | int main()
5 | {
6 | std::cout << "5! is " << fact(5) << std::endl;
7 | std::cout << func() << std::endl;
8 | std::cout << abs(-9.78) << std::endl;
9 | }
--------------------------------------------------------------------------------
/ch07/exercise7_1.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | using std::cin; using std::cout; using std::endl; using std::string;
4 |
5 | struct Sales_data
6 | {
7 | string bookNo;
8 | unsigned units_sold = 0;
9 | double revenue = 0.0;
10 | };
11 |
12 | int main()
13 | {
14 | Sales_data total;
15 | if (cin >> total.bookNo >> total.units_sold >> total.revenue)
16 | {
17 | Sales_data trans;
18 | while (cin >> trans.bookNo >> trans.units_sold >> trans.revenue)
19 | {
20 | if (total.bookNo == trans.bookNo)
21 | {
22 | total.units_sold += trans.units_sold;
23 | total.revenue += trans.revenue;
24 | }
25 | else
26 | {
27 | cout << total.bookNo << " " << total.units_sold << " " << total.revenue << endl;
28 | total = trans;
29 | }
30 | }
31 | cout << total.bookNo << " " << total.units_sold << " " << total.revenue << endl;
32 | }
33 | else
34 | {
35 | std::cerr << "No data?!" << std::endl;
36 | return -1;
37 | }
38 | return 0;
39 | }
--------------------------------------------------------------------------------
/ch07/exercise7_11.cpp:
--------------------------------------------------------------------------------
1 | #include "exercise7_11.h"
2 |
3 | int main()
4 | {
5 | Sales_data item1;
6 | print(std::cout, item1) << std::endl;
7 |
8 | Sales_data item2("0-201-78345-X");
9 | print(std::cout, item2) << std::endl;
10 |
11 | Sales_data item3("0-201-78345-X", 3, 20.00);
12 | print(std::cout, item3) << std::endl;
13 |
14 | Sales_data item4(std::cin);
15 | print(std::cout, item4) << std::endl;
16 |
17 | return 0;
18 | }
--------------------------------------------------------------------------------
/ch07/exercise7_11.h:
--------------------------------------------------------------------------------
1 | #ifndef CP5_ex7_11_h
2 | #define CP5_ex7_11_h
3 |
4 | #include
5 | #include
6 |
7 | struct Sales_data
8 | {
9 | Sales_data() = default;
10 | Sales_data(const std::string &s) :bookNo(s) {}
11 | Sales_data(const std::string &s, unsigned n, double p) :bookNo(s), units_sold(n), revenue(n*p) {}
12 | Sales_data(std::istream &is);
13 |
14 | std::string isbn() const { return bookNo; };
15 | Sales_data& combine(const Sales_data&);
16 |
17 | std::string bookNo;
18 | unsigned units_sold = 0;
19 | double revenue = 0.0;
20 | };
21 |
22 | std::istream &read(std::istream &is, Sales_data &item)
23 | {
24 | double price = 0;
25 | is >> item.bookNo >> item.units_sold >> price;
26 | item.revenue = price * item.units_sold;
27 | return is;
28 | }
29 |
30 | std::ostream &print(std::ostream &os, const Sales_data &item)
31 | {
32 | os << item.isbn() << " " << item.units_sold << " " << item.revenue;
33 | return os;
34 | }
35 |
36 | Sales_data add(const Sales_data &lhs, const Sales_data &rhs)
37 | {
38 | Sales_data sum = lhs;
39 | sum.combine(rhs);
40 | return sum;
41 | }
42 |
43 | Sales_data::Sales_data(std::istream &is)
44 | {
45 | read(is, *this);
46 | }
47 |
48 | Sales_data& Sales_data::combine(const Sales_data& rhs)
49 | {
50 | units_sold += rhs.units_sold;
51 | revenue += rhs.revenue;
52 | return *this;
53 | }
54 |
55 | #endif
--------------------------------------------------------------------------------
/ch07/exercise7_12.h:
--------------------------------------------------------------------------------
1 | #ifndef CP5_ex7_12_h
2 | #define CP5_ex7_12_h
3 |
4 | #include
5 | #include
6 |
7 | struct Sales_data;
8 | std::istream &read(std::istream&, Sales_data&);
9 |
10 | struct Sales_data
11 | {
12 | Sales_data() = default;
13 | Sales_data(const std::string &s) :bookNo(s) {}
14 | Sales_data(const std::string &s, unsigned n, double p) :bookNo(s), units_sold(n), revenue(n*p) {}
15 | Sales_data(std::istream &is) { read(is, *this); }
16 |
17 | std::string isbn() const { return bookNo; };
18 | Sales_data& combine(const Sales_data&);
19 |
20 | std::string bookNo;
21 | unsigned units_sold = 0;
22 | double revenue = 0.0;
23 | };
24 |
25 | Sales_data& Sales_data::combine(const Sales_data& rhs)
26 | {
27 | units_sold += rhs.units_sold;
28 | revenue += rhs.revenue;
29 | return *this;
30 | }
31 |
32 | std::istream &read(std::istream &is, Sales_data &item)
33 | {
34 | double price = 0;
35 | is >> item.bookNo >> item.units_sold >> price;
36 | item.revenue = price * item.units_sold;
37 | return is;
38 | }
39 |
40 | std::ostream &print(std::ostream &os, const Sales_data &item)
41 | {
42 | os << item.isbn() << " " << item.units_sold << " " << item.revenue;
43 | return os;
44 | }
45 |
46 | Sales_data add(const Sales_data &lhs, const Sales_data &rhs)
47 | {
48 | Sales_data sum = lhs;
49 | sum.combine(rhs);
50 | return sum;
51 | }
52 |
53 | #endif
--------------------------------------------------------------------------------
/ch07/exercise7_13.cpp:
--------------------------------------------------------------------------------
1 | #include "exercise7_12.h"
2 |
3 | int main()
4 | {
5 | Sales_data total(std::cin);
6 | if (!total.isbn().empty())
7 | {
8 | std::istream &is = std::cin;
9 | while (is)
10 | {
11 | Sales_data trans(is);
12 | if (total.isbn() == trans.isbn())
13 | total.combine(trans);
14 | else
15 | {
16 | print(std::cout, total) << std::endl;
17 | total = trans;
18 | }
19 | }
20 | print(std::cout, total) << std::endl;
21 | }
22 | else
23 | {
24 | std::cerr << "No data?!" << std::endl;
25 | return -1;
26 | }
27 |
28 | return 0;
29 | }
--------------------------------------------------------------------------------
/ch07/exercise7_15.h:
--------------------------------------------------------------------------------
1 | #ifndef CP5_ex7_15_h
2 | #define CP5_ex7_15_h
3 |
4 | #include
5 | #include
6 |
7 | struct Person;
8 | std::istream &read(std::istream&, Person&);
9 |
10 | struct Person
11 | {
12 | Person() = default;
13 | Person(const std::string& sname, const std::string& saddr) :name(sname), address(saddr) {}
14 | Person(std::istream &is) { read(is, *this); }
15 |
16 | std::string getName() const { return name; }
17 | std::string getAddress() const { return address; }
18 |
19 | std::string name;
20 | std::string address;
21 | };
22 |
23 | std::istream &read(std::istream &is, Person &person)
24 | {
25 | is >> person.name >> person.address;
26 | return is;
27 | }
28 |
29 | std::ostream &print(std::ostream &os, const Person &person)
30 | {
31 | os << person.name << " " << person.address;
32 | return os;
33 | }
34 |
35 | #endif
--------------------------------------------------------------------------------
/ch07/exercise7_2.h:
--------------------------------------------------------------------------------
1 | #ifndef CP5_ex7_02_h
2 | #define CP5_ex7_02_h
3 |
4 | #include
5 |
6 | struct Sales_data
7 | {
8 | std::string isbn() const { return bookNo; };
9 | Sales_data& combine(const Sales_data&);
10 |
11 | std::string bookNo;
12 | unsigned units_sold = 0;
13 | double revenue = 0.0;
14 | };
15 |
16 | Sales_data& Sales_data::combine(const Sales_data& rhs)
17 | {
18 | units_sold += rhs.units_sold;
19 | revenue += rhs.revenue;
20 | return *this;
21 | }
22 |
23 | #endif
--------------------------------------------------------------------------------
/ch07/exercise7_21.h:
--------------------------------------------------------------------------------
1 | #ifndef CP5_ex7_21_h
2 | #define CP5_ex7_21_h
3 |
4 | #include
5 | #include
6 |
7 | class Sales_data
8 | {
9 | friend std::istream &read(std::istream &is, Sales_data &item);
10 | friend std::ostream &print(std::ostream &os, const Sales_data &item);
11 | friend Sales_data add(const Sales_data &lhs, const Sales_data &rhs);
12 |
13 | public:
14 | Sales_data() = default;
15 | Sales_data(const std::string &s) :bookNo(s) {}
16 | Sales_data(const std::string &s, unsigned n, double p) :bookNo(s), units_sold(n), revenue(n*p) {}
17 | Sales_data(std::istream &is) { read(is, *this); }
18 |
19 | std::string isbn() const { return bookNo; };
20 | Sales_data& combine(const Sales_data&);
21 |
22 | private:
23 | std::string bookNo;
24 | unsigned units_sold = 0;
25 | double revenue = 0.0;
26 | };
27 |
28 | Sales_data& Sales_data::combine(const Sales_data& rhs)
29 | {
30 | units_sold += rhs.units_sold;
31 | revenue += rhs.revenue;
32 | return *this;
33 | }
34 |
35 | std::istream &read(std::istream &is, Sales_data &item)
36 | {
37 | double price = 0;
38 | is >> item.bookNo >> item.units_sold >> price;
39 | item.revenue = price * item.units_sold;
40 | return is;
41 | }
42 |
43 | std::ostream &print(std::ostream &os, const Sales_data &item)
44 | {
45 | os << item.isbn() << " " << item.units_sold << " " << item.revenue;
46 | return os;
47 | }
48 |
49 | Sales_data add(const Sales_data &lhs, const Sales_data &rhs)
50 | {
51 | Sales_data sum = lhs;
52 | sum.combine(rhs);
53 | return sum;
54 | }
55 |
56 | #endif
--------------------------------------------------------------------------------
/ch07/exercise7_22.h:
--------------------------------------------------------------------------------
1 | #ifndef CP5_ex7_22_h
2 | #define CP5_ex7_22_h
3 |
4 | #include
5 | #include
6 |
7 | struct Person
8 | {
9 | friend std::istream &read(std::istream &is, Person &person);
10 | friend std::ostream &print(std::ostream &os, const Person &person);
11 |
12 | public:
13 | Person() = default;
14 | Person(const std::string sname, const std::string saddr) :name(sname), address(saddr) {}
15 | Person(std::istream &is) { read(is, *this); }
16 |
17 | std::string getName() const { return name; }
18 | std::string getAddress() const { return address; }
19 | private:
20 | std::string name;
21 | std::string address;
22 | };
23 |
24 | std::istream &read(std::istream &is, Person &person)
25 | {
26 | is >> person.name >> person.address;
27 | return is;
28 | }
29 |
30 | std::ostream &print(std::ostream &os, const Person &person)
31 | {
32 | os << person.name << " " << person.address;
33 | return os;
34 | }
35 |
36 | #endif
--------------------------------------------------------------------------------
/ch07/exercise7_23.h:
--------------------------------------------------------------------------------
1 | #ifndef CP5_ex7_23_h
2 | #define CP5_ex7_23_h
3 | #include
4 |
5 | class Screen
6 | {
7 | public:
8 | using pos = std::string::size_type;
9 |
10 | Screen() = default;
11 | Screen(pos ht, pos wd, char c) :height(ht), width(wd), contents(ht*wd, c)
12 | {}
13 |
14 | char get() const { return contents[cursor]; }
15 | char get(pos r, pos c) const { return contents[r*width + c]; }
16 |
17 | private:
18 | pos cursor = 0;
19 | pos height = 0;
20 | pos width = 0;
21 | std::string contents;
22 | };
23 |
24 | #endif
--------------------------------------------------------------------------------
/ch07/exercise7_24.h:
--------------------------------------------------------------------------------
1 | #ifndef CP5_ex7_24_h
2 | #define CP5_ex7_24_h
3 | #include
4 |
5 | class Screen
6 | {
7 | public:
8 | using pos = std::string::size_type;
9 |
10 | Screen() = default;
11 | Screen(pos ht, pos wd) :height(ht), width(wd), contents(ht*wd, ' ') {}
12 | Screen(pos ht, pos wd, char c) :height(ht), width(wd), contents(ht*wd, c) {}
13 |
14 | char get() const { return contents[cursor]; }
15 | char get(pos r, pos c) const { return contents[r*width + c]; }
16 |
17 | private:
18 | pos cursor = 0;
19 | pos height = 0;
20 | pos width = 0;
21 | std::string contents;
22 | };
23 |
24 | #endif
--------------------------------------------------------------------------------
/ch07/exercise7_26.h:
--------------------------------------------------------------------------------
1 | #ifndef CP5_ex7_26_h
2 | #define CP5_ex7_26_h
3 |
4 | #include
5 | #include
6 |
7 | class Sales_data
8 | {
9 | friend std::istream &read(std::istream &is, Sales_data &item);
10 | friend std::ostream &print(std::ostream &os, const Sales_data &item);
11 | friend Sales_data add(const Sales_data &lhs, const Sales_data &rhs);
12 |
13 | public:
14 | Sales_data() = default;
15 | Sales_data(const std::string &s) :bookNo(s) {}
16 | Sales_data(const std::string &s, unsigned n, double p) :bookNo(s), units_sold(n), revenue(n*p) {}
17 | Sales_data(std::istream &is) { read(is, *this); }
18 |
19 | std::string isbn() const { return bookNo; };
20 | Sales_data& combine(const Sales_data&);
21 |
22 | private:
23 | inline double avg_price() const;
24 |
25 | private:
26 | std::string bookNo;
27 | unsigned units_sold = 0;
28 | double revenue = 0.0;
29 | };
30 |
31 | inline
32 | double Sales_data::avg_price() const
33 | {
34 | return units_sold ? revenue / units_sold : 0;
35 | }
36 |
37 | std::istream &read(std::istream &is, Sales_data &item);
38 | std::ostream &print(std::ostream &os, const Sales_data &item);
39 | Sales_data add(const Sales_data &lhs, const Sales_data &rhs);
40 |
41 | #endif
--------------------------------------------------------------------------------
/ch07/exercise7_27.cpp:
--------------------------------------------------------------------------------
1 | #include "exercise7_27.h"
2 |
3 | int main()
4 | {
5 | Screen myScreen(5, 5, 'X');
6 | myScreen.move(4, 0).set('#').display(std::cout);
7 | std::cout << "\n";
8 | myScreen.display(std::cout);
9 | std::cout << "\n";
10 |
11 | return 0;
12 | }
--------------------------------------------------------------------------------
/ch07/exercise7_27.h:
--------------------------------------------------------------------------------
1 | #ifndef CP5_ex7_27_h
2 | #define CP5_ex7_27_h
3 |
4 | #include
5 | #include
6 |
7 | class Screen
8 | {
9 | public:
10 | using pos = std::string::size_type;
11 |
12 | Screen() = default; // 1
13 | Screen(pos ht, pos wd) :height(ht), width(wd), contents(ht*wd, ' ') {} // 2
14 | Screen(pos ht, pos wd, char c) :height(ht), width(wd), contents(ht*wd, c) {} // 3
15 |
16 | char get() const { return contents[cursor]; }
17 | char get(pos r, pos c) const { return contents[r*width + c]; }
18 | inline Screen& move(pos r, pos c);
19 | inline Screen& set(char c);
20 | inline Screen& set(pos r, pos c, char ch);
21 |
22 | const Screen& display(std::ostream &os) const { do_display(os); return *this; }
23 | Screen& display(std::ostream &os) { do_display(os); return *this; }
24 |
25 | private:
26 | void do_display(std::ostream &os) const { os << contents; }
27 |
28 | private:
29 | pos cursor = 0;
30 | pos height = 0, width = 0;
31 | std::string contents;
32 | };
33 |
34 | inline Screen& Screen::move(pos r, pos c)
35 | {
36 | cursor = r*width + c;
37 | return *this;
38 | }
39 |
40 | inline Screen& Screen::set(char c)
41 | {
42 | contents[cursor] = c;
43 | return *this;
44 | }
45 |
46 | inline Screen& Screen::set(pos r, pos c, char ch)
47 | {
48 | contents[r*width + c] = ch;
49 | return *this;
50 | }
51 |
52 | #endif
--------------------------------------------------------------------------------
/ch07/exercise7_3.cpp:
--------------------------------------------------------------------------------
1 | #include "exercise7_2.h"
2 | #include
3 | using std::cin; using std::cout; using std::endl;
4 |
5 | int main()
6 | {
7 | Sales_data total;
8 | if (cin >> total.bookNo >> total.units_sold >> total.revenue)
9 | {
10 | Sales_data trans;
11 | while (cin >> trans.bookNo >> trans.units_sold >> trans.revenue)
12 | {
13 | if (total.isbn() == trans.isbn())
14 | total.combine(trans);
15 | else
16 | {
17 | cout << total.bookNo << " " << total.units_sold << " " << total.revenue << endl;
18 | total = trans;
19 | }
20 | }
21 | cout << total.bookNo << " " << total.units_sold << " " << total.revenue << endl;
22 | }
23 | else
24 | {
25 | std::cerr << "No data?!" << std::endl;
26 | return -1;
27 | }
28 |
29 | return 0;
30 | }
--------------------------------------------------------------------------------
/ch07/exercise7_32.h:
--------------------------------------------------------------------------------
1 | #ifndef CP5_ex7_32_h
2 | #define CP5_ex7_32_h
3 |
4 | #include
5 | #include
6 | #include
7 |
8 | class Screen;
9 |
10 | class Window_mgr
11 | {
12 | public:
13 | using ScreenIndex = std::vector::size_type;
14 | inline void clear(ScreenIndex);
15 |
16 | private:
17 | std::vector screens;
18 | };
19 |
20 | class Screen
21 | {
22 | friend void Window_mgr::clear(ScreenIndex);
23 |
24 | public:
25 | using pos = std::string::size_type;
26 |
27 | Screen() = default;
28 | Screen(pos ht, pos wd) :height(ht), width(wd), contents(ht*wd,' ') {}
29 | Screen(pos ht, pos wd, char c) :height(ht), width(wd), contents(ht*wd, c) {}
30 |
31 | char get() const { return contents[cursor]; }
32 | char get(pos r, pos c) const { return contents[r*width + c]; }
33 | inline Screen& move(pos r, pos c);
34 | inline Screen& set(char c);
35 | inline Screen& set(pos r, pos c, char ch);
36 |
37 | const Screen& display(std::ostream& os) const { do_display(os); return *this; }
38 | Screen& display(std::ostream& os) { do_display(os); return *this; }
39 |
40 | private:
41 | void do_display(std::ostream &os) const { os << contents; }
42 |
43 | private:
44 | pos cursor = 0;
45 | pos width = 0, height = 0;
46 | std::string contents;
47 | };
48 |
49 |
50 | inline void Window_mgr::clear(ScreenIndex i)
51 | {
52 | Screen& s = screens[i];
53 | s.contents = std::string(s.height*s.width,' ');
54 | }
55 |
56 | inline Screen& Screen::move(pos r, pos c)
57 | {
58 | cursor = r*width + c;
59 | return *this;
60 | }
61 |
62 | inline Screen& Screen::set(char c)
63 | {
64 | contents[cursor] = c;
65 | return *this;
66 | }
67 |
68 | inline Screen& Screen::set(pos r, pos c, char ch)
69 | {
70 | contents[r*width + c] = ch;
71 | return *this;
72 | }
73 |
74 |
75 | #endif
--------------------------------------------------------------------------------
/ch07/exercise7_4.h:
--------------------------------------------------------------------------------
1 | #ifndef CP5_ex7_04_h
2 | #define CP5_ex7_04_h
3 |
4 | #include
5 |
6 | class Person
7 | {
8 | std::string name;
9 | std::string address;
10 | };
11 |
12 | #endif
--------------------------------------------------------------------------------
/ch07/exercise7_41.cpp:
--------------------------------------------------------------------------------
1 | #include "exercise7_41.h"
2 |
3 | Sales_data::Sales_data(std::istream &is) : Sales_data()
4 | {
5 | std::cout << "Sales_data(istream &is)" << std::endl;
6 | read(is, *this);
7 | }
8 |
9 | Sales_data& Sales_data::combine(const Sales_data& rhs)
10 | {
11 | units_sold += rhs.units_sold;
12 | revenue += rhs.revenue;
13 | return *this;
14 | }
15 |
16 | std::istream &read(std::istream &is, Sales_data &item)
17 | {
18 | double price = 0;
19 | is >> item.bookNo >> item.units_sold >> price;
20 | item.revenue = price * item.units_sold;
21 | return is;
22 | }
23 |
24 | std::ostream &print(std::ostream &os, const Sales_data &item)
25 | {
26 | os << item.isbn() << " " << item.units_sold << " " << item.revenue;
27 | return os;
28 | }
29 |
30 | Sales_data add(const Sales_data &lhs, const Sales_data &rhs)
31 | {
32 | Sales_data sum = lhs;
33 | sum.combine(rhs);
34 | return sum;
35 | }
--------------------------------------------------------------------------------
/ch07/exercise7_41.h:
--------------------------------------------------------------------------------
1 | #ifndef CP5_ex7_41_h
2 | #define CP5_ex7_41_h
3 |
4 | #include
5 | #include
6 |
7 | class Sales_data
8 | {
9 | friend std::istream &read(std::istream &is, Sales_data &item);
10 | friend std::ostream &print(std::ostream &os, const Sales_data &item);
11 | friend Sales_data add(const Sales_data &lhs, const Sales_data &rhs);
12 |
13 | public:
14 | Sales_data(const std::string &s, unsigned n, double p) :bookNo(s), units_sold(n), revenue(n*p)
15 | {
16 | std::cout << "Sales_data(const std::string&, unsigned, double)" << std::endl;
17 | }
18 |
19 | Sales_data() : Sales_data("", 0, 0.0f)
20 | {
21 | std::cout << "Sales_data()" << std::endl;
22 | }
23 |
24 | Sales_data(const std::string &s) : Sales_data(s, 0, 0.0f)
25 | {
26 | std::cout << "Sales_data(const std::string&)" << std::endl;
27 | }
28 |
29 | Sales_data(std::istream &is);
30 |
31 | std::string isbn() const { return bookNo; }
32 | Sales_data& combine(const Sales_data&);
33 |
34 | private:
35 | inline double avg_price() const;
36 |
37 | private:
38 | std::string bookNo;
39 | unsigned units_sold = 0;
40 | double revenue = 0.0;
41 | };
42 |
43 | inline
44 | double Sales_data::avg_price() const
45 | {
46 | return units_sold ? revenue / units_sold : 0;
47 | }
48 |
49 | std::istream &read(std::istream &is, Sales_data &item);
50 | std::ostream &print(std::ostream &os, const Sales_data &item);
51 | Sales_data add(const Sales_data &lhs, const Sales_data &rhs);
52 |
53 | #endif
--------------------------------------------------------------------------------
/ch07/exercise7_41_main.cpp:
--------------------------------------------------------------------------------
1 | #include "exercise7_41.h"
2 |
3 | using std::cout; using std::endl;
4 |
5 | int main()
6 | {
7 | cout << "1. default way: " << endl;
8 | cout << "----------------" << endl;
9 | Sales_data s1;
10 |
11 | cout << "\n2. use std::string as parameter: " << endl;
12 | cout << "----------------" << endl;
13 | Sales_data s2("CPP-Primer-5th");
14 |
15 | cout << "\n3. complete parameters: " << endl;
16 | cout << "----------------" << endl;
17 | Sales_data s3("CPP-Primer-5th", 3, 25.8);
18 |
19 | cout << "\n4. use istream as parameter: " << endl;
20 | cout << "----------------" << endl;
21 | Sales_data s4(std::cin);
22 |
23 | return 0;
24 | }
--------------------------------------------------------------------------------
/ch07/exercise7_5.h:
--------------------------------------------------------------------------------
1 | #ifndef CP5_ex7_05_h
2 | #define CP5_ex7_05_h
3 |
4 | #include
5 |
6 | class Person
7 | {
8 | std::string name;
9 | std::string address;
10 | public:
11 | auto get_name() const -> std::string const& { return name; }
12 | auto get_addr() const -> std::string const& { return address; }
13 | };
14 |
15 | #endif
--------------------------------------------------------------------------------
/ch07/exercise7_6.h:
--------------------------------------------------------------------------------
1 | #ifndef CP5_ex7_06_h
2 | #define CP5_ex7_06_h
3 |
4 | #include
5 | #include
6 |
7 | struct Sales_data
8 | {
9 | std::string const& isbn() const { return bookNo; };
10 | Sales_data& combine(const Sales_data&);
11 |
12 | std::string bookNo;
13 | unsigned units_sold = 0;
14 | double revenue = 0.0;
15 | };
16 |
17 | Sales_data& Sales_data::combine(const Sales_data& rhs)
18 | {
19 | units_sold += rhs.units_sold;
20 | revenue += rhs.revenue;
21 | return *this;
22 | }
23 |
24 | std::istream &read(std::istream &is, Sales_data &item)
25 | {
26 | double price = 0;
27 | is >> item.bookNo >> item.units_sold >> price;
28 | item.revenue = price * item.units_sold;
29 | return is;
30 | }
31 |
32 | std::ostream &print(std::ostream &os, const Sales_data &item)
33 | {
34 | os << item.isbn() << " " << item.units_sold << " " << item.revenue;
35 | return os;
36 | }
37 |
38 | Sales_data add(const Sales_data &lhs, const Sales_data &rhs)
39 | {
40 | Sales_data sum = lhs;
41 | sum.combine(rhs);
42 | return sum;
43 | }
44 |
45 | #endif
--------------------------------------------------------------------------------
/ch07/exercise7_7.cpp:
--------------------------------------------------------------------------------
1 | #include "exercise7_6.h"
2 |
3 | int main()
4 | {
5 | Sales_data total;
6 | if (read(std::cin, total))
7 | {
8 | Sales_data trans;
9 | while (read(std::cin, trans))
10 | {
11 | if (total.isbn() == trans.isbn())
12 | total.combine(trans);
13 | else
14 | {
15 | print(std::cout, total) << std::endl;
16 | total = trans;
17 | }
18 | }
19 | print(std::cout, total) << std::endl;
20 | }
21 | else
22 | {
23 | std::cerr << "No data?!" << std::endl;
24 | return -1;
25 | }
26 |
27 | return 0;
28 | }
--------------------------------------------------------------------------------
/ch07/exercise7_9.h:
--------------------------------------------------------------------------------
1 | #ifndef CP5_ex7_09_h
2 | #define CP5_ex7_09_h
3 |
4 | #include
5 | #include
6 |
7 | struct Person
8 | {
9 | std::string const& getName() const { return name; }
10 | std::string const& getAddress() const { return address; }
11 |
12 | std::string name;
13 | std::string address;
14 | };
15 |
16 | std::istream &read(std::istream &is, Person &person)
17 | {
18 | return is >> person.name >> person.address;
19 | }
20 |
21 | std::ostream &print(std::ostream &os, const Person &person)
22 | {
23 | return os << person.name << " " << person.address;
24 | }
25 |
26 | #endif
--------------------------------------------------------------------------------
/ch08/README.md:
--------------------------------------------------------------------------------
1 | ## 练习8.1
2 |
3 | > 编写函数,接受一个istream&参数,返回值类型也是istream&。此函数须从给定流中读取数据,直至遇到文件结束标识时停止。它将读取的数据打印在标准输出上。完成这些操作后,在返回流之前,对流进行复位,使其处于有效状态。
4 |
5 | ```cpp
6 | std::istream& func(std::istream &is)
7 | {
8 | std::string buf;
9 | while (is >> buf)
10 | std::cout << buf << std::endl;
11 | is.clear();
12 | return is;
13 | }
14 | ```
15 |
16 | ## [练习8.2](exercise8_2.cpp)
17 |
18 | > 测试函数,调用参数为cin。
19 |
20 | ## 练习8.3
21 |
22 | > 什么情况下,下面的while循环会终止?
23 | ```cpp
24 | while (cin >> i) /* ... */
25 | ```
26 |
27 | 如果 `badbit`、`failbit`、`eofbit` 的任一个被置位,那么检测流状态的条件会失败。
28 |
29 | ## 练习8.4
30 |
31 | > 编写函数,以读模式打开一个文件,将其内容读入到一个string的vector中,将每一行作为一个独立的元素存于vector中。
32 |
33 | ```cpp
34 | void ReadFileToVec(const string& fileName, vector& vec)
35 | {
36 | ifstream ifs(fileName);
37 | if (ifs)
38 | {
39 | string buf;
40 | while (getline(ifs, buf))
41 | vec.push_back(buf);
42 | }
43 | }
44 | ```
45 |
46 | ## 练习8.5
47 |
48 | > 重写上面的程序,将每个单词作为一个独立的元素进行存储。
49 |
50 | ```cpp
51 | void ReadFileToVec(const string& fileName, vector& vec)
52 | {
53 | ifstream ifs(fileName);
54 | if (ifs)
55 | {
56 | string buf;
57 | while (ifs >> buf)
58 | vec.push_back(buf);
59 | }
60 | }
61 | ```
62 |
63 | ## [练习8.6](exercise8_6.cpp)
64 |
65 | > 重写7.1.1节的书店程序,从一个文件中读取交易记录。将文件名作为一个参数传递给main。
66 |
67 | ## [练习8.7](exercise8_7.cpp)
68 |
69 | > 修改上一节的书店程序,将结果保存到一个文件中。将输出文件名作为第二个参数传递给main函数。
70 |
71 | ## [练习8.8](exercise8_8.cpp)
72 |
73 | > 修改上一题的程序,将结果追加到给定的文件末尾。对同一个输出文件,运行程序至少两次,检验数据是否得以保留。
74 |
75 | ## [练习8.9](exercise8_9.cpp)
76 |
77 | > 使用你为8.1.2节第一个练习所编写的函数打印一个istringstream对象的内容。
78 |
79 | ## [练习8.10](exercise8_10.cpp)
80 |
81 | > 编写程序,将来自一个文件中的行保存在一个vector中。然后使用一个istringstream从vector读取数据元素,每次读取一个单词。
82 |
83 | ## [练习8.11](exercise8_11.cpp)
84 |
85 | > 本节的程序在外层while循环中定义了istringstream 对象。如果record 对象定义在循环之外,你需要对程序进行怎样的修改?重写程序,将record的定义移到while 循环之外,验证你设想的修改方法是否正确。
86 |
87 | ## 练习8.12
88 |
89 | > 我们为什么没有在PersonInfo中使用类内初始化?
90 |
91 | 因为这里只需要聚合类就够了,所以没有必要在 PersionInfo 中使用类内初始化。
92 |
93 | ## [练习8.13](exercise8_13.cpp)
94 |
95 | > 重写本节的电话号码程序,从一个命名文件而非cin读取数据。
96 |
97 | ## 练习8.14
98 |
99 | > 我们为什么将entry和nums定义为 const auto& ?
100 |
101 | * 它们都是类类型,因此使用引用避免拷贝。
102 | * 在循环当中不会改变它们的值,因此用 const。
--------------------------------------------------------------------------------
/ch08/exercise8_10.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include
5 | #include
6 |
7 | using std::vector; using std::string; using std::ifstream; using std::istringstream; using std::cout; using std::endl; using std::cerr;
8 |
9 | int main()
10 | {
11 | ifstream ifs("E:/code/cpp_primer_answer/Cpp_Primer_Answers/data/books.txt");
12 | if (!ifs)
13 | {
14 | cerr << "No data?" << endl;
15 | return -1;
16 | }
17 |
18 | vector vecLine;
19 | string line;
20 | while (getline(ifs, line))
21 | vecLine.push_back(line);
22 |
23 | for (auto &s : vecLine)
24 | {
25 | istringstream iss(s);
26 | string word;
27 | while (iss >> word)
28 | cout << word << ", ";
29 | cout << endl;
30 | }
31 |
32 | return 0;
33 | }
--------------------------------------------------------------------------------
/ch08/exercise8_11.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include
5 | using std::vector; using std::string; using std::cin; using std::istringstream;
6 |
7 | struct PersonInfo
8 | {
9 | string name;
10 | vector phones;
11 | };
12 |
13 | int main()
14 | {
15 | string line, word;
16 | vector people;
17 | istringstream record;
18 | while (getline(cin, line))
19 | {
20 | PersonInfo info;
21 | record.clear();
22 | record.str(line);
23 | record >> info.name;
24 | while (record >> word)
25 | info.phones.push_back(word);
26 | people.push_back(info);
27 | }
28 |
29 | for (auto &p : people)
30 | {
31 | std::cout << p.name << " ";
32 | for (auto &s : p.phones)
33 | std::cout << s << " ";
34 | std::cout << std::endl;
35 | }
36 |
37 | return 0;
38 | }
--------------------------------------------------------------------------------
/ch08/exercise8_13.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include
5 | #include
6 | #include
7 |
8 | using std::vector; using std::string; using std::cin; using std::istringstream;
9 | using std::ostringstream; using std::ifstream; using std::cerr; using std::cout; using std::endl;
10 |
11 | struct PersonInfo
12 | {
13 | string name;
14 | vector