├── .gitignore ├── Accelerated.sln ├── Chapter0 ├── 0_0.cpp ├── 0_0.h ├── 0_1.cpp ├── 0_1.h ├── 0_10.cpp ├── 0_10.h ├── 0_2.cpp ├── 0_2.h ├── 0_3.cpp ├── 0_3.h ├── 0_4.cpp ├── 0_4.h ├── 0_5.cpp ├── 0_5.h ├── 0_6.cpp ├── 0_6.h ├── 0_7.cpp ├── 0_7.h ├── 0_8.cpp ├── 0_8.h ├── 0_9.cpp ├── 0_9.h ├── Chapter0.cpp ├── Chapter0.vcxproj ├── ReadMe.txt ├── stdafx.cpp ├── stdafx.h └── targetver.h ├── Chapter01 ├── 1_0.cpp ├── 1_0.h ├── 1_1.cpp ├── 1_1.h ├── 1_2.cpp ├── 1_2.h ├── 1_3.cpp ├── 1_3.h ├── 1_4.cpp ├── 1_4.h ├── 1_5.cpp ├── 1_5.h ├── 1_6.cpp ├── 1_6.h ├── Chapter01.cpp ├── Chapter01.vcxproj ├── ReadMe.txt ├── stdafx.cpp ├── stdafx.h └── targetver.h ├── Chapter02 ├── 2_0.cpp ├── 2_0.h ├── 2_1.cpp ├── 2_1.h ├── 2_10.cpp ├── 2_10.h ├── 2_2.cpp ├── 2_2.h ├── 2_3.cpp ├── 2_3.h ├── 2_4.cpp ├── 2_4.h ├── 2_5.cpp ├── 2_5.h ├── 2_6.cpp ├── 2_6.h ├── 2_7.cpp ├── 2_7.h ├── 2_8.cpp ├── 2_8.h ├── 2_9.cpp ├── 2_9.h ├── Chapter02.cpp ├── Chapter02.vcxproj ├── ReadMe.txt ├── stdafx.cpp ├── stdafx.h └── targetver.h ├── Chapter03 ├── 3_0.cpp ├── 3_0.h ├── 3_2.cpp ├── 3_2.h ├── 3_3.cpp ├── 3_3.h ├── 3_4.cpp ├── 3_4.h ├── 3_5.cpp ├── 3_5.h ├── 3_6.cpp ├── 3_6.h ├── Chapter03.cpp ├── Chapter03.vcxproj ├── ReadMe.txt ├── stdafx.cpp ├── stdafx.h └── targetver.h ├── Chapter04 ├── 4_0.cpp ├── 4_0.h ├── 4_1.cpp ├── 4_1.h ├── 4_2.cpp ├── 4_2.h ├── 4_3.cpp ├── 4_3.h ├── 4_4.cpp ├── 4_4.h ├── 4_5.cpp ├── 4_5.h ├── 4_6.cpp ├── 4_6.h ├── 4_7.cpp ├── 4_7.h ├── 4_8.cpp ├── 4_8.h ├── Chapter04.cpp ├── Chapter04.vcxproj ├── ReadMe.txt ├── Student_info.cpp ├── grade.cpp ├── grade.h ├── median.cpp ├── median.h ├── square.cpp ├── square.h ├── stdafx.cpp ├── stdafx.h ├── student_info.h └── targetver.h ├── Chapter05 ├── 5_0.cpp ├── 5_0.h ├── 5_1.cpp ├── 5_1.h ├── 5_10.cpp ├── 5_10.h ├── 5_11.cpp ├── 5_11.h ├── 5_2.cpp ├── 5_2.h ├── 5_3.cpp ├── 5_3.h ├── 5_4.cpp ├── 5_4.h ├── 5_5.cpp ├── 5_5.h ├── 5_6.cpp ├── 5_6.h ├── 5_7.cpp ├── 5_7.h ├── 5_8.cpp ├── 5_8.h ├── 5_9.cpp ├── 5_9.h ├── Chapter05.cpp ├── Chapter05.vcxproj ├── ReadMe.txt ├── Student_info.cpp ├── grade.cpp ├── grade.h ├── median.cpp ├── median.h ├── stdafx.cpp ├── stdafx.h ├── student_info.h ├── students10.txt ├── students100.txt ├── students1000.txt ├── students10000.txt ├── targetver.h ├── words.cpp └── words.h ├── Chapter06 ├── 6_0.cpp ├── 6_0.h ├── 6_1.cpp ├── 6_1.h ├── 6_2.cpp ├── 6_2.h ├── 6_3.cpp ├── 6_3.h ├── 6_4.cpp ├── 6_4.h ├── 6_5.cpp ├── 6_5.h ├── 6_6.cpp ├── 6_6.h ├── 6_7.cpp ├── 6_7.h ├── 6_8.cpp ├── 6_8.h ├── 6_9.cpp ├── 6_9.h ├── Chapter06.cpp ├── Chapter06.vcxproj ├── ReadMe.txt ├── Student_info.cpp ├── grade.cpp ├── grade.h ├── stdafx.cpp ├── stdafx.h ├── student_info.h ├── students10.txt ├── targetver.h ├── vec.cpp ├── vec.h ├── words.cpp └── words.h ├── Chapter07 ├── 7_0.cpp ├── 7_0.h ├── 7_1.cpp ├── 7_1.h ├── 7_2.cpp ├── 7_2.h ├── 7_3.cpp ├── 7_3.h ├── 7_4.cpp ├── 7_4.h ├── 7_5.cpp ├── 7_5.h ├── 7_6.cpp ├── 7_6.h ├── 7_7.cpp ├── 7_7.h ├── 7_8.cpp ├── 7_8.h ├── 7_9.cpp ├── 7_9.h ├── Chapter07.cpp ├── Chapter07.vcxproj ├── ReadMe.txt ├── Student_info.cpp ├── grade.cpp ├── grade.h ├── grammar.txt ├── lines.txt ├── median.cpp ├── median.h ├── numbers.cpp ├── numbers.h ├── stdafx.cpp ├── stdafx.h ├── student_info.h ├── targetver.h ├── urls.txt ├── words.cpp └── words.h ├── Chapter08 ├── 8_0.cpp ├── 8_0.h ├── 8_1.cpp ├── 8_1.h ├── 8_2.cpp ├── 8_2.h ├── 8_3.cpp ├── 8_3.h ├── 8_4.cpp ├── 8_4.h ├── 8_5.cpp ├── 8_5.h ├── 8_6.cpp ├── 8_6.h ├── 8_7.cpp ├── 8_7.h ├── 8_8.cpp ├── 8_8.h ├── Chapter08.cpp ├── Chapter08.vcxproj ├── ReadMe.txt ├── grade.cpp ├── grade.h ├── grammar.txt ├── lines.txt ├── numbers.cpp ├── numbers.h ├── stdafx.cpp ├── stdafx.h ├── student_info.cpp ├── student_info.h ├── students10.txt ├── targetver.h ├── vec.h ├── words.cpp └── words.h ├── Chapter09 ├── 9_0.cpp ├── 9_0.h ├── 9_1.cpp ├── 9_1.h ├── 9_2.cpp ├── 9_2.h ├── 9_3.cpp ├── 9_3.h ├── 9_4.cpp ├── 9_4.h ├── 9_5.cpp ├── 9_5.h ├── 9_6.cpp ├── 9_6.h ├── 9_7.cpp ├── 9_7.h ├── Chapter09.cpp ├── Chapter09.vcxproj ├── ReadMe.txt ├── stdafx.cpp ├── stdafx.h ├── student_info.cpp ├── student_info.h └── targetver.h ├── Chapter10 ├── 10_0.cpp ├── 10_0.h ├── 10_1.cpp ├── 10_1.h ├── 10_2.cpp ├── 10_2.h ├── 10_3.cpp ├── 10_3.h ├── 10_4.cpp ├── 10_4.h ├── 10_5.cpp ├── 10_5.h ├── 10_6.cpp ├── 10_6.h ├── Chapter10.cpp ├── Chapter10.vcxproj ├── ReadMe.txt ├── in.txt ├── out.txt ├── stdafx.cpp ├── stdafx.h ├── student_info.cpp ├── student_info.h └── targetver.h ├── Chapter11 ├── 11_0.cpp ├── 11_0.h ├── 11_5.cpp ├── 11_5.h ├── 11_6.cpp ├── 11_6.h ├── 11_7.cpp ├── 11_7.h ├── 11_8.cpp ├── 11_8.h ├── 11_9.cpp ├── 11_9.h ├── Chapter11.cpp ├── Chapter11.vcxproj ├── ReadMe.txt ├── stdafx.cpp ├── stdafx.h ├── student_info.cpp ├── student_info.h ├── students10.txt ├── students100.txt ├── students1000.txt ├── students10000.txt ├── targetver.h └── vec.h ├── Chapter12 ├── 12_0.cpp ├── 12_0.h ├── 12_1.cpp ├── 12_1.h ├── 12_10.cpp ├── 12_10.h ├── 12_11.cpp ├── 12_11.h ├── 12_12.cpp ├── 12_12.h ├── 12_13.cpp ├── 12_13.h ├── 12_14.cpp ├── 12_14.h ├── 12_2.cpp ├── 12_2.h ├── 12_3.cpp ├── 12_3.h ├── 12_4.cpp ├── 12_4.h ├── 12_5.cpp ├── 12_5.h ├── 12_6.cpp ├── 12_6.h ├── 12_7.cpp ├── 12_7.h ├── 12_8.cpp ├── 12_8.h ├── 12_9.cpp ├── 12_9.h ├── Chapter12.cpp ├── Chapter12.vcxproj ├── ReadMe.txt ├── stdafx.cpp ├── stdafx.h ├── str.cpp ├── str.h ├── str_c.cpp ├── str_c.h ├── targetver.h └── vec.h ├── Chapter13 ├── 13_0.cpp ├── 13_0.h ├── 13_1.cpp ├── 13_1.h ├── 13_2.cpp ├── 13_2.h ├── 13_3.cpp ├── 13_3.h ├── 13_4.cpp ├── 13_4.h ├── 13_5.cpp ├── 13_5.h ├── 13_6.cpp ├── 13_6.h ├── 13_7.cpp ├── 13_7.h ├── 13_8.cpp ├── 13_8.h ├── Chapter13.cpp ├── Chapter13.vcxproj ├── ReadMe.txt ├── stdafx.cpp ├── stdafx.h ├── student_info.cpp ├── student_info.h └── targetver.h ├── Chapter14 ├── 14_0.cpp ├── 14_0.h ├── 14_1.cpp ├── 14_1.h ├── 14_2.cpp ├── 14_2.h ├── 14_3.cpp ├── 14_3.h ├── 14_4.cpp ├── 14_4.h ├── 14_5.cpp ├── 14_5.h ├── 14_6.cpp ├── 14_6.h ├── Chapter14.cpp ├── Chapter14.vcxproj ├── ReadMe.txt ├── handle.h ├── ptr.h ├── ptr_r.h ├── ref_counter.h ├── stdafx.cpp ├── stdafx.h ├── str.cpp ├── str.h ├── student_info.cpp ├── student_info.h ├── student_info_p.cpp ├── student_info_p.h ├── targetver.h └── vec.h ├── Chapter15 ├── 15_0.cpp ├── 15_0.h ├── 15_1.cpp ├── 15_1.h ├── 15_2.cpp ├── 15_2.h ├── 15_3.cpp ├── 15_3.h ├── 15_4.cpp ├── 15_4.h ├── 15_5.cpp ├── 15_5.h ├── 15_6.cpp ├── 15_6.h ├── Chapter15.cpp ├── Chapter15.vcxproj ├── ReadMe.txt ├── pic.cpp ├── pic.h ├── pic_v.cpp ├── pic_v.h ├── ptr.h ├── stdafx.cpp ├── stdafx.h ├── str.cpp ├── str.h ├── targetver.h └── vec.h ├── Chapter16 ├── 16_0.cpp ├── 16_0.h ├── 16_1.cpp ├── 16_1.h ├── Chapter16.cpp ├── Chapter16.vcxproj ├── ReadMe.txt ├── pic.cpp ├── pic.h ├── ptr.h ├── stdafx.cpp ├── stdafx.h ├── student_info.cpp ├── student_info.h └── targetver.h └── README.md /Chapter0/0_0.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 0-0, 0_0.cpp 3 | 4 | Compile and run the Hello, world! program. 5 | */ 6 | 7 | #include "stdafx.h" 8 | #include "0_0.h" 9 | 10 | #include 11 | 12 | void ex0_0() 13 | { 14 | std::cout << "Hello, World!" << std::endl; 15 | } -------------------------------------------------------------------------------- /Chapter0/0_0.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_0_0_H 2 | #define GUARD_0_0_H 3 | 4 | void ex0_0(); 5 | 6 | #endif // !GUARD_0_0_H -------------------------------------------------------------------------------- /Chapter0/0_1.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 0-1, 0_1.cpp 3 | 4 | What does the following statement do? 5 | 6 | 3 + 4; 7 | 8 | */ 9 | 10 | #include "stdafx.h" 11 | #include "0_1.h" 12 | 13 | #include 14 | 15 | void ex0_1() 16 | { 17 | std::cout << 3 + 4 << std::endl; 18 | } -------------------------------------------------------------------------------- /Chapter0/0_1.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_0_1_H 2 | #define GUARD_0_1_H 3 | 4 | void ex0_1(); 5 | 6 | #endif // !GUARD_0_1_H -------------------------------------------------------------------------------- /Chapter0/0_10.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 0-10, 0_10.cpp 3 | 4 | Rewrite the Hello, world! program so that a newline occurs everywhere that 5 | whitespace is allowed in the program. 6 | */ 7 | 8 | #include "stdafx.h" 9 | #include "0_10.h" 10 | 11 | #include 12 | 13 | int 14 | ex0_10 15 | ( 16 | ) 17 | { 18 | std 19 | :: 20 | cout 21 | << 22 | "Hello, world!" 23 | << 24 | std 25 | :: 26 | endl 27 | ; 28 | return 29 | 0 30 | ; 31 | } -------------------------------------------------------------------------------- /Chapter0/0_10.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_0_10_H 2 | #define GUARD_0_10_H 3 | 4 | int ex0_10(); 5 | 6 | #endif // !GUARD_0_10_H -------------------------------------------------------------------------------- /Chapter0/0_2.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 0-2, 0_2.cpp 3 | 4 | Write a program that, when run, writes 5 | 6 | This (") is a quote, and this (\) is a backslash. 7 | */ 8 | 9 | #include "stdafx.h" 10 | #include "0_2.h" 11 | 12 | #include 13 | 14 | void ex0_2() 15 | { 16 | std::cout << "This (\") is a quote, and this (\\) is a backslash." << std::endl; 17 | } -------------------------------------------------------------------------------- /Chapter0/0_2.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_0_2_H 2 | #define GUARD_0_2_H 3 | 4 | void ex0_2(); 5 | 6 | #endif // !GUARD_0_2_H -------------------------------------------------------------------------------- /Chapter0/0_3.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 0-3, 0_3.cpp 3 | 4 | The string literal "\t" represents a tab character; different C++ implementations 5 | display tabs in different ways. Experiment with your implementation to learn how it 6 | treats tabs. 7 | */ 8 | 9 | #include "stdafx.h" 10 | #include "0_3.h" 11 | 12 | #include 13 | 14 | void ex0_3() 15 | { 16 | std::cout << "this is a tab \ttest." << std::endl 17 | << "\ttab" << std::endl 18 | << "\ttab \ttest." << std::endl; 19 | } -------------------------------------------------------------------------------- /Chapter0/0_3.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_0_3_H 2 | #define GUARD_0_3_H 3 | 4 | void ex0_3(); 5 | 6 | #endif // !GUARD_0_3_H -------------------------------------------------------------------------------- /Chapter0/0_4.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 0-4, 0_4.cpp 3 | 4 | Write a program that, when run, writes the Hello, world! program as its 5 | output. 6 | */ 7 | 8 | #include "stdafx.h" 9 | #include "0_4.h" 10 | 11 | #include 12 | 13 | void ex0_4() 14 | { 15 | std::cout << "// A small C++ program" << std::endl 16 | << "#include " << std::endl << std::endl 17 | << "int main()" << std::endl 18 | << "{" << std::endl 19 | << "\tstd::cout << \"Hello, World!\" << std::endl;" << std::endl 20 | << "\treturn 0;" << std::endl 21 | << "}" << std::endl << std::endl; 22 | } -------------------------------------------------------------------------------- /Chapter0/0_4.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_0_4_H 2 | #define GUARD_0_4_H 3 | 4 | void ex0_4(); 5 | 6 | #endif // !GUARD_0_4_H -------------------------------------------------------------------------------- /Chapter0/0_5.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 0-5, 0_5.cpp 3 | 4 | Is this a valid program? Why or why not? 5 | 6 | #include 7 | int main() std::cout << "Hello, world!" << std::endl; 8 | */ 9 | 10 | #include "stdafx.h" 11 | #include "0_5.h" 12 | 13 | #include 14 | 15 | // Missing braces for function body 16 | //void ex0_5() std::cout << "Hello, world!" << std::endl; 17 | void ex0_5() { std::cout << "Hello, world!" << std::endl; } -------------------------------------------------------------------------------- /Chapter0/0_5.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_0_5_H 2 | #define GUARD_0_5_H 3 | 4 | void ex0_5(); 5 | 6 | #endif // !GUARD_0_5_H -------------------------------------------------------------------------------- /Chapter0/0_6.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 0-6, 0_6.cpp 3 | 4 | Is this a valid program? Why or why not? 5 | 6 | #include 7 | int main() {{{{{{ std::cout << "Hello, world!" << std::endl; }}}}}} 8 | */ 9 | 10 | #include "stdafx.h" 11 | #include "0_6.h" 12 | 13 | #include 14 | 15 | void ex0_6() {{{{{{ std::cout << "Hello, world!" << std::endl; }}}}}} -------------------------------------------------------------------------------- /Chapter0/0_6.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_0_6_H 2 | #define GUARD_0_6_H 3 | 4 | void ex0_6(); 5 | 6 | #endif // !GUARD_0_6_H -------------------------------------------------------------------------------- /Chapter0/0_7.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 0-7, 0_7.cpp 3 | 4 | What about this one? 5 | 6 | #include 7 | 8 | int main() 9 | { 10 | /* This is a comment that extends over several lines 11 | because it uses /* and *(/) as its starting and ending delimiters * / 12 | std::cout << "Does this work?" << std::endl; 13 | return 0; 14 | } 15 | */ 16 | 17 | #include "stdafx.h" 18 | #include "0_7.h" 19 | 20 | #include 21 | 22 | // Incorrect use of comment delimiters. 23 | /** 24 | int ex0_7() 25 | { 26 | /* This is a comment that extends over several lines 27 | because it uses /* and */ /**as its starting and ending delimiters * / 28 | std::cout << "Does this work?" << std::endl; 29 | return 0; 30 | } 31 | */ 32 | 33 | int ex0_7() 34 | { 35 | /* This is a comment that extends over several lines 36 | because it uses /* and *(/) as its starting and ending delimiters */ 37 | std::cout << "Does this work?" << std::endl; 38 | return 0; 39 | } -------------------------------------------------------------------------------- /Chapter0/0_7.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_0_7_H 2 | #define GUARD_0_7_H 3 | 4 | int ex0_7(); 5 | 6 | #endif // !GUARD_0_7_H -------------------------------------------------------------------------------- /Chapter0/0_8.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 0-8, 0_8.cpp 3 | 4 | ...and this one? 5 | 6 | #include 7 | int main() 8 | { 9 | // This is a comment that extends over several lines 10 | // by using // at the beginning of each line instead of using /* 11 | // or *(/) to delimit comments. 12 | std::cout << "Does this work?" << std::endl; 13 | return 0; 14 | } 15 | */ 16 | 17 | #include "stdafx.h" 18 | #include "0_8.h" 19 | 20 | #include 21 | 22 | int ex0_8() 23 | { 24 | // This is a comment that extends over several lines 25 | // by using // at the beginning of each line instead of using /* 26 | // or */ to delimit comments. 27 | std::cout << "Does this work?" << std::endl; 28 | return 0; 29 | } -------------------------------------------------------------------------------- /Chapter0/0_8.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_0_8_H 2 | #define GUARD_0_8_H 3 | 4 | int ex0_8(); 5 | 6 | #endif // !GUARD_0_8_H -------------------------------------------------------------------------------- /Chapter0/0_9.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 0-9, 0_9.cpp 3 | 4 | What is the shortest valid program? 5 | */ 6 | 7 | #include "stdafx.h" 8 | #include "0_9.h" 9 | 10 | void ex0_9() {}; -------------------------------------------------------------------------------- /Chapter0/0_9.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_0_9_H 2 | #define GUARD_0_9_H 3 | 4 | void ex0_9(); 5 | 6 | #endif // !GUARD_0_9_H -------------------------------------------------------------------------------- /Chapter0/Chapter0.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Chapter 0, Chapter0.cpp 3 | 4 | Getting started. 5 | */ 6 | 7 | #include "stdafx.h" 8 | 9 | #include "0_0.h" 10 | #include "0_1.h" 11 | #include "0_2.h" 12 | #include "0_3.h" 13 | #include "0_4.h" 14 | #include "0_5.h" 15 | #include "0_6.h" 16 | #include "0_7.h" 17 | #include "0_8.h" 18 | #include "0_9.h" 19 | #include "0_10.h" 20 | 21 | int main() 22 | { 23 | ex0_10(); 24 | } -------------------------------------------------------------------------------- /Chapter0/stdafx.cpp: -------------------------------------------------------------------------------- 1 | // stdafx.cpp : source file that includes just the standard includes 2 | // Chapter0.pch will be the pre-compiled header 3 | // stdafx.obj will contain the pre-compiled type information 4 | 5 | #include "stdafx.h" 6 | 7 | // TODO: reference any additional headers you need in STDAFX.H 8 | // and not in this file 9 | -------------------------------------------------------------------------------- /Chapter0/stdafx.h: -------------------------------------------------------------------------------- 1 | // stdafx.h : include file for standard system include files, 2 | // or project specific include files that are used frequently, but 3 | // are changed infrequently 4 | // 5 | 6 | #pragma once 7 | 8 | #include "targetver.h" 9 | 10 | #include 11 | #include 12 | 13 | 14 | 15 | // TODO: reference additional headers your program requires here 16 | -------------------------------------------------------------------------------- /Chapter0/targetver.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | // Including SDKDDKVer.h defines the highest available Windows platform. 4 | 5 | // If you wish to build your application for a previous Windows platform, include WinSDKVer.h and 6 | // set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h. 7 | 8 | #include 9 | -------------------------------------------------------------------------------- /Chapter01/1_0.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 1-0, 1_0.cpp 3 | 4 | Compile, execute, and test the programs in this chapter. 5 | */ 6 | 7 | #include "stdafx.h" 8 | #include "1_0.h" 9 | 10 | #include 11 | #include 12 | 13 | void AskName(std::ostream& outStream, std::istream& inStream) 14 | { 15 | outStream << "Please enter your first name: "; 16 | std::string name; 17 | inStream >> name; 18 | outStream << "Hello, " + name + "!" << std::endl; 19 | } 20 | 21 | void AskNameFramed(std::ostream& outStream, std::istream& inStream) 22 | { 23 | outStream << "Please enter your first name: "; 24 | std::string name; 25 | inStream >> name; 26 | 27 | const std::string greeting = "Hello, " + name + "!"; 28 | const std::string spaces(greeting.size(), ' '); 29 | const std::string line3 = '*' + greeting + '*'; 30 | const std::string line2 = '*' + spaces + '*'; 31 | const std::string line1(line2.size(), '*'); 32 | 33 | outStream << line1 << std::endl << line2 << std::endl << line3 34 | << std::endl << line2 << std::endl << line1 << std::endl; 35 | } 36 | 37 | int ex1_0() 38 | { 39 | AskName(std::cout, std::cin); 40 | AskNameFramed(std::cout, std::cin); 41 | 42 | return 0; 43 | } -------------------------------------------------------------------------------- /Chapter01/1_0.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_1_0_H 2 | #define GUARD_1_0_H 3 | 4 | int ex1_0(); 5 | 6 | #endif // !GUARD_1_0_H -------------------------------------------------------------------------------- /Chapter01/1_1.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 1-1, 1_1.cpp 3 | 4 | Are the following definitions valid? Why or why not? 5 | 6 | const std::string hello = "Hello"; 7 | const std::string message = hello + ", world" + "!"; 8 | */ 9 | 10 | #include "stdafx.h" 11 | #include "1_1.h" 12 | 13 | #include 14 | 15 | int ex1_1() 16 | { 17 | const std::string hello = "Hello"; 18 | const std::string message = hello + ", world" + "!"; 19 | 20 | return 0; 21 | } -------------------------------------------------------------------------------- /Chapter01/1_1.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_1_1_H 2 | #define GUARD_1_1_H 3 | 4 | int ex1_1(); 5 | 6 | #endif // !GUARD_1_1_H -------------------------------------------------------------------------------- /Chapter01/1_2.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 1-2, 1_2.cpp 3 | 4 | Are the following definitions valid? Why or why not? 5 | 6 | const std::string exclam = "!"; 7 | const std::string message = "Hello" + ", world" + exclam; 8 | */ 9 | 10 | #include "stdafx.h" 11 | #include "1_2.h" 12 | 13 | #include 14 | 15 | int ex1_2() 16 | { 17 | const std::string exclam = "!"; 18 | 19 | // Can't add two string literals, the + operator has left-to-right associativity 20 | // const std::string message = "Hello" + ", world" + exclam; 21 | const std::string message = "Hello" + (", world" + exclam); 22 | 23 | return 0; 24 | } -------------------------------------------------------------------------------- /Chapter01/1_2.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_1_2_H 2 | #define GUARD_1_2_H 3 | 4 | int ex1_2(); 5 | 6 | #endif // !GUARD_1_2_H -------------------------------------------------------------------------------- /Chapter01/1_3.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 1-3, 1_3.cpp 3 | 4 | Is the following program valid? If so, what does it do? If not, why not? 5 | 6 | #include 7 | #include 8 | int main() 9 | { 10 | { const std::string s = "a string"; 11 | std::cout << s << std::endl; } 12 | 13 | { const std::string s = "another string"; 14 | std::cout << s << std::endl; } 15 | return 0; 16 | } 17 | */ 18 | 19 | #include "stdafx.h" 20 | #include "1_3.h" 21 | 22 | #include 23 | #include 24 | 25 | int ex1_3() 26 | { 27 | { 28 | const std::string s = "a string"; 29 | std::cout << s << std::endl; 30 | } 31 | 32 | { 33 | const std::string s = "another string"; 34 | std::cout << s << std::endl; 35 | } 36 | 37 | return 0; 38 | } -------------------------------------------------------------------------------- /Chapter01/1_3.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_1_3_H 2 | #define GUARD_1_3_H 3 | 4 | int ex1_3(); 5 | 6 | #endif // !GUARD_1_3_H -------------------------------------------------------------------------------- /Chapter01/1_4.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 1-4, 1_4.cpp 3 | 4 | What about this one? What if we change }} to };} in the third line from the end? 5 | 6 | #include 7 | #include 8 | 9 | int main() 10 | { 11 | { const std::string s = "a string"; 12 | std::cout << s << std::endl; 13 | { const std::string s = "another string"; 14 | std::cout << s << std::endl; }} 15 | return 0; 16 | } 17 | */ 18 | 19 | #include "stdafx.h" 20 | #include "1_4.h" 21 | 22 | #include 23 | #include 24 | 25 | int ex1_4() 26 | { 27 | { 28 | const std::string s = "a string"; 29 | std::cout << s << std::endl; 30 | { 31 | const std::string s = "another string"; 32 | std::cout << s << std::endl; 33 | };} 34 | return 0; 35 | } -------------------------------------------------------------------------------- /Chapter01/1_4.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_1_4_H 2 | #define GUARD_1_4_H 3 | 4 | int ex1_4(); 5 | 6 | #endif // !GUARD_1_4_H -------------------------------------------------------------------------------- /Chapter01/1_5.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 1-5, 1_5.cpp 3 | 4 | Is this program valid? If so, what does it do? If not, say why not, and rewrite it to 5 | be valid. 6 | 7 | #include 8 | #include 9 | int main() 10 | { 11 | { std::string s = "a string"; 12 | { std::string x = s + ", really"; 13 | std::cout << s << std::endl; } 14 | std::cout << x << std::endl; 15 | } 16 | return 0; 17 | } 18 | */ 19 | 20 | #include "stdafx.h" 21 | #include "1_5.h" 22 | 23 | #include 24 | #include 25 | 26 | int ex1_5() 27 | { 28 | { 29 | std::string s = "a string"; 30 | { 31 | std::string x = s + ", really"; 32 | std::cout << s << std::endl; 33 | std::cout << x << std::endl; 34 | } 35 | // x is undefined in current scope 36 | // std::cout << x << std::endl; 37 | } 38 | return 0; 39 | } -------------------------------------------------------------------------------- /Chapter01/1_5.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_1_5_H 2 | #define GUARD_1_5_H 3 | 4 | int ex1_5(); 5 | 6 | #endif // !GUARD_1_5_H -------------------------------------------------------------------------------- /Chapter01/1_6.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 1-6, 1_6.cpp 3 | 4 | What does the following program do if, when it asks you for input, you type two 5 | names (for example, Samuel Beckett)? Predict the behavior before running the program, 6 | then try it. 7 | 8 | #include 9 | #include 10 | int main() 11 | { 12 | std::cout << "What is your name? "; 13 | std::string name; 14 | std::cin >> name; 15 | std::cout << "Hello, " << name 16 | << std::endl << "And what is yours? "; 17 | std::cin >> name; 18 | std::cout << "Hello, " << name 19 | << "; nice to meet you too!" << std::endl; 20 | return 0; 21 | } 22 | */ 23 | 24 | #include "stdafx.h" 25 | #include "1_6.h" 26 | 27 | #include 28 | #include 29 | 30 | int ex1_6() 31 | { 32 | std::cout << "What is your name? "; 33 | std::string name; 34 | std::cin >> name; 35 | std::cout << "Hello, " << name 36 | << std::endl << "And what is yours? "; 37 | std::cin >> name; 38 | std::cout << "Hello, " << name 39 | << "; nice to meet you too!" << std::endl; 40 | return 0; 41 | } -------------------------------------------------------------------------------- /Chapter01/1_6.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_1_6_H 2 | #define GUARD_1_6_H 3 | 4 | int ex1_6(); 5 | 6 | #endif // !GUARD_1_6_H -------------------------------------------------------------------------------- /Chapter01/Chapter01.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Chapter 01, Chapter01.cpp 3 | 4 | Looping and counting. 5 | */ 6 | 7 | #include "stdafx.h" 8 | 9 | #include "1_0.h" 10 | #include "1_1.h" 11 | #include "1_2.h" 12 | #include "1_3.h" 13 | #include "1_4.h" 14 | #include "1_5.h" 15 | #include "1_6.h" 16 | 17 | int main() 18 | { 19 | return ex1_6(); 20 | } 21 | 22 | -------------------------------------------------------------------------------- /Chapter01/stdafx.cpp: -------------------------------------------------------------------------------- 1 | // stdafx.cpp : source file that includes just the standard includes 2 | // Chapter01.pch will be the pre-compiled header 3 | // stdafx.obj will contain the pre-compiled type information 4 | 5 | #include "stdafx.h" 6 | 7 | // TODO: reference any additional headers you need in STDAFX.H 8 | // and not in this file 9 | -------------------------------------------------------------------------------- /Chapter01/stdafx.h: -------------------------------------------------------------------------------- 1 | // stdafx.h : include file for standard system include files, 2 | // or project specific include files that are used frequently, but 3 | // are changed infrequently 4 | // 5 | 6 | #pragma once 7 | 8 | #include "targetver.h" 9 | 10 | #include 11 | #include 12 | 13 | 14 | 15 | // TODO: reference additional headers your program requires here 16 | -------------------------------------------------------------------------------- /Chapter01/targetver.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | // Including SDKDDKVer.h defines the highest available Windows platform. 4 | 5 | // If you wish to build your application for a previous Windows platform, include WinSDKVer.h and 6 | // set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h. 7 | 8 | #include 9 | -------------------------------------------------------------------------------- /Chapter02/2_0.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 2-0, 2_0.cpp 3 | 4 | Compile and run the program presented in this chapter. 5 | */ 6 | 7 | #include "stdafx.h" 8 | #include "2_0.h" 9 | 10 | #include 11 | using std::cin; 12 | using std::cout; 13 | using std::endl; 14 | 15 | #include 16 | using std::string; 17 | 18 | 19 | int ex2_0() 20 | { 21 | cout << "Please enter your first name: "; 22 | 23 | string name; 24 | cin >> name; 25 | 26 | const string greeting = "Hello, " + name + "!"; 27 | const int pad = 1; 28 | const int rows = pad * 2 + 3; 29 | const string::size_type cols = greeting.size() + pad * 2 + 2; 30 | 31 | cout << endl; 32 | 33 | for (int r = 0; r != rows; ++r) 34 | { 35 | string::size_type c = 0; 36 | 37 | while (c != cols) 38 | { 39 | 40 | if (r == pad + 1 && c == pad + 1) 41 | { 42 | cout << greeting; 43 | c += greeting.size(); 44 | } 45 | else 46 | { 47 | if (r == 0 || r == rows - 1 || 48 | c == 0 || c == cols - 1) 49 | cout << "*"; 50 | else 51 | cout << " "; 52 | ++c; 53 | } 54 | } 55 | 56 | cout << endl; 57 | } 58 | 59 | return 0; 60 | } 61 | -------------------------------------------------------------------------------- /Chapter02/2_0.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_2_0_H 2 | #define GUARD_2_0_H 3 | 4 | int ex2_0(); 5 | 6 | #endif // !GUARD_2_0_H -------------------------------------------------------------------------------- /Chapter02/2_1.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 2-1, 2_1.cpp 3 | 4 | Change the framing program so that it writes its greeting with no separation from 5 | the frame. 6 | */ 7 | 8 | #include "stdafx.h" 9 | #include "2_1.h" 10 | 11 | #include 12 | using std::cin; 13 | using std::cout; 14 | using std::endl; 15 | 16 | #include 17 | using std::string; 18 | 19 | int ex2_1() 20 | { 21 | cout << "Please enter your first name: "; 22 | 23 | string name; 24 | cin >> name; 25 | 26 | const string greeting = "Hello, " + name + "!"; 27 | const int pad = 0; 28 | const int rows = pad * 2 + 3; 29 | const string::size_type cols = greeting.size() + pad * 2 + 2; 30 | 31 | cout << endl; 32 | 33 | for (int r = 0; r != rows; ++r) 34 | { 35 | string::size_type c = 0; 36 | 37 | while (c != cols) 38 | { 39 | 40 | if (r == pad + 1 && c == pad + 1) 41 | { 42 | cout << greeting; 43 | c += greeting.size(); 44 | } 45 | else 46 | { 47 | if (r == 0 || r == rows - 1 || 48 | c == 0 || c == cols - 1) 49 | cout << "*"; 50 | else 51 | cout << " "; 52 | ++c; 53 | } 54 | } 55 | 56 | cout << endl; 57 | } 58 | 59 | return 0; 60 | } -------------------------------------------------------------------------------- /Chapter02/2_1.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_2_1_H 2 | #define GUARD_2_1_H 3 | 4 | int ex2_1(); 5 | 6 | #endif // !GUARD_2_1_H -------------------------------------------------------------------------------- /Chapter02/2_10.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 2-10, 2_10.cpp 3 | 4 | Explain each of the uses of std:: in the following program: 5 | 6 | int main() { 7 | int k = 0; 8 | while (k != n) { // invariant: we have written k asterisks so far 9 | using std::cout; 10 | cout << "*"; 11 | ++k; 12 | } 13 | std::cout << std::endl; // std:: is required here 14 | return 0; 15 | } 16 | 17 | */ 18 | 19 | #include "stdafx.h" 20 | #include "2_9.h" 21 | 22 | #include 23 | using std::cout; 24 | using std::endl; 25 | 26 | const int n = 5; 27 | 28 | int ex2_10() 29 | { 30 | int k = 0; 31 | while (k != n) 32 | { 33 | using std::cout; // the using statement is only relevant to the local while scope 34 | cout << "*"; 35 | ++k; 36 | } 37 | std::cout << std::endl; // std:: is required here 38 | return 0; 39 | } -------------------------------------------------------------------------------- /Chapter02/2_10.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_2_10_H 2 | #define GUARD_2_10_H 3 | 4 | int ex2_10(); 5 | 6 | #endif // !GUARD_2_10_H -------------------------------------------------------------------------------- /Chapter02/2_2.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 2-2, 2_2.cpp 3 | 4 | Change the framing program so that it uses a different amount of space to 5 | separate the sides from the greeting than it uses to separate the top and bottom 6 | borders from the greeting. 7 | */ 8 | 9 | #include "stdafx.h" 10 | #include "2_2.h" 11 | 12 | #include 13 | using std::cin; 14 | using std::cout; 15 | using std::endl; 16 | 17 | #include 18 | using std::string; 19 | 20 | 21 | int ex2_2() 22 | { 23 | cout << "Please enter your first name: "; 24 | 25 | string name; 26 | cin >> name; 27 | 28 | const string greeting = "Hello, " + name + "!"; 29 | 30 | const int vert_pad = 1; 31 | const int horz_pad = 3; 32 | 33 | const int rows = vert_pad * 2 + 3; 34 | const string::size_type cols = greeting.size() + horz_pad * 2 + 2; 35 | 36 | cout << endl; 37 | 38 | for (int r = 0; r != rows; ++r) 39 | { 40 | string::size_type c = 0; 41 | 42 | while (c != cols) 43 | { 44 | 45 | if (r == vert_pad + 1 && c == horz_pad + 1) 46 | { 47 | cout << greeting; 48 | c += greeting.size(); 49 | } 50 | else 51 | { 52 | if (r == 0 || r == rows - 1 || 53 | c == 0 || c == cols - 1) 54 | cout << "*"; 55 | else 56 | cout << " "; 57 | ++c; 58 | } 59 | } 60 | 61 | cout << endl; 62 | } 63 | 64 | return 0; 65 | } -------------------------------------------------------------------------------- /Chapter02/2_2.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_2_2_H 2 | #define GUARD_2_2_H 3 | 4 | int ex2_2(); 5 | 6 | #endif // !GUARD_2_2_H -------------------------------------------------------------------------------- /Chapter02/2_3.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 2-3, 2_3.cpp 3 | 4 | Rewrite the framing program to ask the user to supply the amount of spacing to 5 | leave between the frame and the greeting. 6 | */ 7 | 8 | #include "stdafx.h" 9 | #include "2_3.h" 10 | 11 | #include 12 | using std::cin; 13 | using std::cout; 14 | using std::endl; 15 | using std::istream; 16 | using std::ostream; 17 | 18 | #include 19 | using std::string; 20 | 21 | int FrameDynamic(istream& in, ostream& out, const int pad) 22 | { 23 | cout << "Please enter your first name: "; 24 | 25 | string name; 26 | in >> name; 27 | 28 | const string greeting = "Hello, " + name + "!"; 29 | const int rows = pad * 2 + 3; 30 | const string::size_type cols = greeting.size() + pad * 2 + 2; 31 | 32 | out << endl; 33 | 34 | for (int r = 0; r != rows; ++r) 35 | { 36 | string::size_type c = 0; 37 | 38 | while (c != cols) 39 | { 40 | 41 | if (r == pad + 1 && c == pad + 1) 42 | { 43 | out << greeting; 44 | c += greeting.size(); 45 | } 46 | else 47 | { 48 | if (r == 0 || r == rows - 1 || 49 | c == 0 || c == cols - 1) 50 | out << "*"; 51 | else 52 | out << " "; 53 | ++c; 54 | } 55 | } 56 | 57 | out << endl; 58 | } 59 | 60 | return 0; 61 | } 62 | 63 | int ex2_3() 64 | { 65 | int padding; 66 | cout << "Please enter frame padding: "; 67 | cin >> padding; 68 | cout << endl; 69 | 70 | return FrameDynamic(cin, cout, padding); 71 | } -------------------------------------------------------------------------------- /Chapter02/2_3.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_2_3_H 2 | #define GUARD_2_3_H 3 | 4 | int ex2_3(); 5 | 6 | #endif // !GUARD_2_3_H -------------------------------------------------------------------------------- /Chapter02/2_4.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_2_4_H 2 | #define GUARD_2_4_H 3 | 4 | int ex2_4(); 5 | 6 | #endif // !GUARD_2_4_H -------------------------------------------------------------------------------- /Chapter02/2_5.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 2-5, 2_5.cpp 3 | 4 | Write a set of "*" characters so that they form a square, a rectangle, and a 5 | triangle. 6 | */ 7 | 8 | #include "stdafx.h" 9 | #include "2_5.h" 10 | 11 | #include 12 | using std::cout; 13 | using std::endl; 14 | 15 | int ex2_5() 16 | { 17 | for (int i = 0; i < 7; i++) 18 | { 19 | for (int j = 0; j < 10; j++) 20 | { 21 | if (j == 0 || j == 10 - 1 || i == 0 || i == 7 - 1) 22 | cout << "*"; 23 | else 24 | cout << " "; 25 | } 26 | cout << endl; 27 | } 28 | 29 | cout << endl; 30 | 31 | for (int i = 0; i < 10; i++) 32 | { 33 | for (int j = 0; j < 10; j++) 34 | { 35 | if (j == 0 || j == 10 - 1 || i == 0 || i == 10 - 1) 36 | cout << "*"; 37 | else 38 | cout << " "; 39 | } 40 | cout << endl; 41 | } 42 | 43 | cout << endl; 44 | 45 | const int height = 7; 46 | const int width = height * 2 - 1; 47 | 48 | for (int i = 0; i < height; i++) 49 | { 50 | for (int j = 0; j < width; j++) 51 | { 52 | if ((i == height - 1) || (i == 0 && j == height - 1)) 53 | cout << "*"; 54 | else if (i > 0 && (j == height - 1 + i || j == height - 1 - i)) 55 | cout << "*"; 56 | else 57 | cout << " "; 58 | } 59 | cout << endl; 60 | } 61 | 62 | return 0; 63 | } -------------------------------------------------------------------------------- /Chapter02/2_5.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_2_5_H 2 | #define GUARD_2_5_H 3 | 4 | int ex2_5(); 5 | 6 | #endif // !GUARD_2_5_H -------------------------------------------------------------------------------- /Chapter02/2_6.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 2-6, 2_6.cpp 3 | 4 | What does the following code do? 5 | 6 | int i = 0; 7 | while (i < 10) { 8 | i += 1; 9 | std::cout << i << std::endl; 10 | } 11 | */ 12 | 13 | #include "stdafx.h" 14 | #include "2_6.h" 15 | 16 | #include 17 | 18 | int ex2_6() 19 | { 20 | int i = 0; 21 | 22 | while (i < 10) 23 | { 24 | i += 1; 25 | std::cout << i << std::endl; 26 | } 27 | 28 | return 0; 29 | } -------------------------------------------------------------------------------- /Chapter02/2_6.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_2_6_H 2 | #define GUARD_2_6_H 3 | 4 | int ex2_6(); 5 | 6 | #endif // !GUARD_2_6_H -------------------------------------------------------------------------------- /Chapter02/2_7.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 2-7, 2_7.cpp 3 | 4 | Write a program to count down from 10 to -5 . 5 | */ 6 | 7 | #include "stdafx.h" 8 | #include "2_7.h" 9 | 10 | #include 11 | using std::cout; 12 | using std::endl; 13 | 14 | int ex2_7() 15 | { 16 | int x = 10; 17 | 18 | while (x >= -5) 19 | { 20 | cout << x-- << endl; 21 | } 22 | 23 | return 0; 24 | } -------------------------------------------------------------------------------- /Chapter02/2_7.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_2_7_H 2 | #define GUARD_2_7_H 3 | 4 | int ex2_7(); 5 | 6 | #endif // !GUARD_2_7_H -------------------------------------------------------------------------------- /Chapter02/2_8.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 2-8, 2_8.cpp 3 | 4 | Write a program to generate the product of the numbers in the range [1, 10) . 5 | */ 6 | 7 | #include "stdafx.h" 8 | #include "2_8.h" 9 | 10 | #include 11 | using std::cout; 12 | using std::endl; 13 | 14 | int ex2_8() 15 | { 16 | int product = 1; 17 | 18 | for (int i = product + 1; i < 10; i++) 19 | product *= i; 20 | 21 | cout << product << endl; 22 | 23 | return 0; 24 | } -------------------------------------------------------------------------------- /Chapter02/2_8.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_2_8_H 2 | #define GUARD_2_8_H 3 | 4 | int ex2_8(); 5 | 6 | #endif // !GUARD_2_8_H -------------------------------------------------------------------------------- /Chapter02/2_9.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 2-9, 2_9.cpp 3 | 4 | Write a program that asks the user to enter two numbers and tells the user which 5 | number is larger than the other. 6 | */ 7 | 8 | #include "stdafx.h" 9 | #include "2_9.h" 10 | 11 | #include 12 | using std::cin; 13 | using std::cout; 14 | using std::endl; 15 | 16 | int ex2_9() 17 | { 18 | int num1, num2; 19 | 20 | cout << "Enter the first number: "; 21 | cin >> num1; 22 | cout << "Enter the second number: "; 23 | cin >> num2; 24 | if (num1 == num2) 25 | { 26 | cout << "Both numbers are equal" << endl; 27 | } 28 | else if (num1 > num2) 29 | { 30 | cout << num1 << " is bigger than " << num2 << endl; 31 | } 32 | else 33 | { 34 | cout << num2 << " is bigger than " << num1 << endl; 35 | } 36 | 37 | return 0; 38 | } -------------------------------------------------------------------------------- /Chapter02/2_9.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_2_9_H 2 | #define GUARD_2_9_H 3 | 4 | int ex2_9(); 5 | 6 | #endif // !GUARD_2_9_H -------------------------------------------------------------------------------- /Chapter02/Chapter02.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Chapter 02, Chapter02.cpp 3 | 4 | Looping and counting. 5 | */ 6 | 7 | #include "stdafx.h" 8 | 9 | #include "2_0.h" 10 | #include "2_1.h" 11 | #include "2_2.h" 12 | #include "2_3.h" 13 | #include "2_4.h" 14 | #include "2_5.h" 15 | #include "2_6.h" 16 | #include "2_7.h" 17 | #include "2_8.h" 18 | #include "2_9.h" 19 | #include "2_10.h" 20 | 21 | int main() 22 | { 23 | return ex2_10(); 24 | } -------------------------------------------------------------------------------- /Chapter02/stdafx.cpp: -------------------------------------------------------------------------------- 1 | // stdafx.cpp : source file that includes just the standard includes 2 | // Chapter02.pch will be the pre-compiled header 3 | // stdafx.obj will contain the pre-compiled type information 4 | 5 | #include "stdafx.h" 6 | 7 | // TODO: reference any additional headers you need in STDAFX.H 8 | // and not in this file 9 | -------------------------------------------------------------------------------- /Chapter02/stdafx.h: -------------------------------------------------------------------------------- 1 | // stdafx.h : include file for standard system include files, 2 | // or project specific include files that are used frequently, but 3 | // are changed infrequently 4 | // 5 | 6 | #pragma once 7 | 8 | #include "targetver.h" 9 | 10 | #include 11 | #include 12 | 13 | 14 | 15 | // TODO: reference additional headers your program requires here 16 | -------------------------------------------------------------------------------- /Chapter02/targetver.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | // Including SDKDDKVer.h defines the highest available Windows platform. 4 | 5 | // If you wish to build your application for a previous Windows platform, include WinSDKVer.h and 6 | // set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h. 7 | 8 | #include 9 | -------------------------------------------------------------------------------- /Chapter03/3_0.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_3_0_H 2 | #define GUARD_3_0_H 3 | 4 | int ex3_0(); 5 | 6 | #endif // !GUARD_3_0_H -------------------------------------------------------------------------------- /Chapter03/3_2.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 3-2, 3_2.cpp 3 | 4 | Write a program to compute and print the quartiles (that is, the quarter of the 5 | numbers with the largest values, the next highest quarter, and so on) of a set of 6 | integers. 7 | */ 8 | 9 | #include "stdafx.h" 10 | #include "3_2.h" 11 | 12 | #include 13 | using std::vector; 14 | 15 | #include 16 | using std::sort; 17 | using std::copy; 18 | 19 | #include 20 | using std::ostream; 21 | using std::cout; 22 | using std::endl; 23 | 24 | #include 25 | using std::stringstream; 26 | 27 | #include 28 | 29 | int quartiles(vector& someInts) 30 | { 31 | sort(someInts.begin(), someInts.end()); 32 | 33 | int quarter = someInts.size() / 4; 34 | int remainder = someInts.size() % 4; 35 | 36 | if (quarter == 0) 37 | { 38 | cout << "Vector is smaller than 4 elements." << endl; 39 | return 1; 40 | } 41 | 42 | int start = someInts.size(); 43 | 44 | for (int i = 0; i < 4; i++, remainder--) 45 | { 46 | stringstream result; 47 | int quarterSize = remainder > 0 ? quarter + 1 : quarter; 48 | int stop = start - quarterSize; 49 | vector::const_iterator first = someInts.begin() + stop; 50 | vector::const_iterator last = someInts.begin() + start; 51 | copy(first, last, std::ostream_iterator(result, " ")); 52 | cout << "Quarter " << std::to_string(i + 1) << ": " + result.str() << endl; 53 | start -= quarterSize; 54 | } 55 | 56 | return 0; 57 | } 58 | 59 | int ex3_2() 60 | { 61 | vector someInts = { 10, 12, 5, 8, 13, 25, 1, 121, 85, 99, 120, 180, 75, 205, 16, 22, 99, 16 }; 62 | 63 | return quartiles(someInts); 64 | } -------------------------------------------------------------------------------- /Chapter03/3_2.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_3_2_H 2 | #define GUARD_3_2_H 3 | 4 | int ex3_2(); 5 | 6 | #endif // !GUARD_3_2_H -------------------------------------------------------------------------------- /Chapter03/3_3.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 3-3, 3_3.cpp 3 | 4 | Write a program to count how many times each distinct word appears in its input. 5 | */ 6 | 7 | #include "stdafx.h" 8 | #include "3_3.h" 9 | 10 | #include 11 | using std::vector; 12 | 13 | #include 14 | using std::string; 15 | 16 | #include 17 | using std::cout; 18 | using std::cin; 19 | using std::endl; 20 | 21 | #include 22 | using std::sort; 23 | 24 | vector GetInput() 25 | { 26 | string word; 27 | vector words; 28 | 29 | cout << "Please enter some words:" << endl; 30 | 31 | while (cin >> word) 32 | { 33 | words.push_back(word); 34 | } 35 | 36 | return words; 37 | } 38 | 39 | 40 | int CountDistinctWord(vector words) 41 | { 42 | 43 | if (words.size() < 2) 44 | { 45 | cout << "Please enter atleast 2 words." << endl; 46 | return 1; 47 | } 48 | 49 | sort(words.begin(), words.end()); 50 | 51 | for (vector::size_type i = 0, counter = 0; i < words.size(); i++) 52 | { 53 | counter++; 54 | if (i + 1 == words.size() || words[i] != words[i + 1]) 55 | { 56 | cout << words[i] << ": " << counter << endl; 57 | counter = 0; 58 | } 59 | } 60 | 61 | return 0; 62 | } 63 | 64 | int ex3_3() 65 | { 66 | return CountDistinctWord(GetInput()); 67 | } -------------------------------------------------------------------------------- /Chapter03/3_3.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_3_3_H 2 | #define GUARD_3_3_H 3 | 4 | #include 5 | #include 6 | 7 | int ex3_3(); 8 | std::vector GetInput(); 9 | 10 | #endif // !GUARD_3_3_H -------------------------------------------------------------------------------- /Chapter03/3_4.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 3-4, 3_4.cpp 3 | 4 | Write a program to report the length of the longest and shortest string in its 5 | input. 6 | */ 7 | 8 | #include "stdafx.h" 9 | #include "3_4.h" 10 | #include "3_3.h" 11 | 12 | #include 13 | using std::vector; 14 | 15 | #include 16 | using std::string; 17 | 18 | #include 19 | using std::cout; 20 | using std::endl; 21 | 22 | int LongestShortestString(vector words) 23 | { 24 | 25 | if (words.size() < 1) 26 | { 27 | cout << "Please enter atleast 1 words." << endl; 28 | return 1; 29 | } 30 | 31 | vector::size_type shortest, longest; 32 | shortest = longest = words[0].size(); 33 | 34 | for (vector::size_type i = 1; i < words.size(); i++) 35 | { 36 | if (words[i].size() < shortest) 37 | shortest = words[i].size(); 38 | if (words[i].size() > longest) 39 | longest = words[i].size(); 40 | } 41 | 42 | cout << "The shortest word is " << shortest << " characters long." << endl 43 | << "The longest word is " << longest << " characters long." << endl; 44 | 45 | return 0; 46 | } 47 | 48 | int ex3_4() 49 | { 50 | return LongestShortestString(GetInput()); 51 | } -------------------------------------------------------------------------------- /Chapter03/3_4.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_3_4_H 2 | #define GUARD_3_4_H 3 | 4 | int ex3_4(); 5 | 6 | #endif // !GUARD_3_4_H -------------------------------------------------------------------------------- /Chapter03/3_5.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeertArien/c-accelerated/c0ae9f66b1733de04f3133db2e9d8af6d555fe6e/Chapter03/3_5.cpp -------------------------------------------------------------------------------- /Chapter03/3_5.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_3_5_H 2 | #define GUARD_3_5_H 3 | 4 | int ex3_5(); 5 | 6 | #endif // !GUARD_3_5_H -------------------------------------------------------------------------------- /Chapter03/3_6.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeertArien/c-accelerated/c0ae9f66b1733de04f3133db2e9d8af6d555fe6e/Chapter03/3_6.cpp -------------------------------------------------------------------------------- /Chapter03/3_6.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_3_6_H 2 | #define GUARD_3_6_H 3 | 4 | int ex3_6(); 5 | 6 | #endif // !GUARD_3_6_H -------------------------------------------------------------------------------- /Chapter03/Chapter03.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Chapter 03, Chapter03.cpp 3 | 4 | Working with batches of data. 5 | */ 6 | 7 | #include "stdafx.h" 8 | 9 | #include "3_0.h" 10 | #include "3_2.h" 11 | #include "3_3.h" 12 | #include "3_4.h" 13 | #include "3_5.h" 14 | #include "3_6.h" 15 | 16 | int main() 17 | { 18 | ex3_6(); 19 | } 20 | 21 | -------------------------------------------------------------------------------- /Chapter03/stdafx.cpp: -------------------------------------------------------------------------------- 1 | // stdafx.cpp : source file that includes just the standard includes 2 | // Chapter03.pch will be the pre-compiled header 3 | // stdafx.obj will contain the pre-compiled type information 4 | 5 | #include "stdafx.h" 6 | 7 | // TODO: reference any additional headers you need in STDAFX.H 8 | // and not in this file 9 | -------------------------------------------------------------------------------- /Chapter03/stdafx.h: -------------------------------------------------------------------------------- 1 | // stdafx.h : include file for standard system include files, 2 | // or project specific include files that are used frequently, but 3 | // are changed infrequently 4 | // 5 | 6 | #pragma once 7 | 8 | #include "targetver.h" 9 | 10 | #include 11 | #include 12 | 13 | 14 | 15 | // TODO: reference additional headers your program requires here 16 | -------------------------------------------------------------------------------- /Chapter03/targetver.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | // Including SDKDDKVer.h defines the highest available Windows platform. 4 | 5 | // If you wish to build your application for a previous Windows platform, include WinSDKVer.h and 6 | // set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h. 7 | 8 | #include 9 | -------------------------------------------------------------------------------- /Chapter04/4_0.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 4-0, 4_0.cpp 3 | 4 | Compile, execute, and test the programs in this chapter. 5 | */ 6 | 7 | #include "stdafx.h" 8 | #include "4_0.h" 9 | 10 | #include 11 | using std::vector; 12 | 13 | #include 14 | using std::string; 15 | 16 | #include 17 | using std::cin; 18 | using std::cout; 19 | using std::streamsize; 20 | using std::endl; 21 | 22 | #include 23 | using std::setprecision; 24 | 25 | #include 26 | using std::sort; 27 | using std::max; 28 | 29 | #include 30 | using std::domain_error; 31 | 32 | #include "student_info.h" 33 | #include "grade.h" 34 | 35 | int ex4_0() 36 | { 37 | vector students; 38 | Student_info record; 39 | string::size_type maxlen = 0; 40 | 41 | while (read(cin, record)) 42 | { 43 | maxlen = max(maxlen, record.name.size()); 44 | students.push_back(record); 45 | } 46 | 47 | sort(students.begin(), students.end(), compare); 48 | 49 | for (vector::size_type i = 0; i != students.size(); i++) 50 | { 51 | cout << students[i].name << string(maxlen + 1 - students[i].name.size(), ' '); 52 | 53 | try 54 | { 55 | double final_grade = grade(students[i]); 56 | streamsize prec = cout.precision(); 57 | cout << setprecision(3) << final_grade << setprecision(prec); 58 | } 59 | catch (domain_error e) 60 | { 61 | cout << e.what(); 62 | } 63 | cout << endl; 64 | } 65 | 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /Chapter04/4_0.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_4_0_H 2 | #define GUARD_4_0_H 3 | 4 | int ex4_0(); 5 | 6 | #endif // !GUARD_4_0_H -------------------------------------------------------------------------------- /Chapter04/4_1.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeertArien/c-accelerated/c0ae9f66b1733de04f3133db2e9d8af6d555fe6e/Chapter04/4_1.cpp -------------------------------------------------------------------------------- /Chapter04/4_1.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_4_1_H 2 | #define GUARD_4_1_H 3 | 4 | int ex4_1(); 5 | 6 | #endif // !GUARD_4_1_H -------------------------------------------------------------------------------- /Chapter04/4_2.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 4-2, 4_2.cpp 3 | 4 | Write a program to calculate the squares of int values up to 100. The program 5 | should write two columns: The first lists the value; the second contains the square of 6 | that value. Use setw (described above) to manage the output so that the values line up 7 | in columns. 8 | */ 9 | 10 | #include "stdafx.h" 11 | #include "4_2.h" 12 | 13 | #include 14 | using std::vector; 15 | 16 | #include 17 | using std::domain_error; 18 | 19 | #include 20 | using std::ostream; 21 | using std::streamsize; 22 | using std::cout; 23 | using std::endl; 24 | 25 | #include 26 | using std::setw; 27 | 28 | vector square(const unsigned int range) 29 | { 30 | vector squares; 31 | if (range > squares.max_size()) 32 | throw domain_error("Range argument exceeds vector limit."); 33 | 34 | for (unsigned int i = 0; i < range; i++) 35 | squares.push_back(i * i); 36 | 37 | return squares; 38 | } 39 | 40 | unsigned GetNumberOfDigits(unsigned i) 41 | { 42 | return i > 0 ? (int)log10((double)i) + 1 : 1; 43 | } 44 | 45 | ostream& print_squares(ostream& os, const vector& v) 46 | { 47 | if (os) 48 | { 49 | for (vector::size_type i = 0; i < v.size(); i++) 50 | { 51 | streamsize s_size = os.width(); 52 | os << setw(2) << i << " " << setw(4) << v[i] << endl; 53 | os.width(s_size); 54 | } 55 | } 56 | 57 | return os; 58 | } 59 | 60 | int ex4_2() 61 | { 62 | vector v = square(100); 63 | print_squares(cout, v); 64 | 65 | return 0; 66 | } -------------------------------------------------------------------------------- /Chapter04/4_2.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_4_2_H 2 | #define GUARD_4_2_H 3 | 4 | #include 5 | 6 | int ex4_2(); 7 | std::vector square(const unsigned int); 8 | unsigned GetNumberOfDigits(unsigned); 9 | 10 | #endif // !GUARD_4_2_H -------------------------------------------------------------------------------- /Chapter04/4_3.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 4-3, 4_3.cpp 3 | 4 | What happens if we rewrite the previous program to allow values up to but not 5 | including 1000 but neglect to change the arguments to setw? Rewrite the program to 6 | be more robust in the face of changes that allow i to grow without adjusting the setw 7 | arguments. 8 | */ 9 | 10 | #include "stdafx.h" 11 | #include "4_3.h" 12 | #include "4_2.h" 13 | 14 | #include 15 | using std::vector; 16 | 17 | #include 18 | using std::ostream; 19 | using std::streamsize; 20 | using std::cout; 21 | using std::endl; 22 | 23 | #include 24 | using std::setw; 25 | 26 | ostream& print_squares_robust(ostream& os, const vector& v) 27 | { 28 | if (os) 29 | { 30 | unsigned digits_i = GetNumberOfDigits(v.size()); 31 | unsigned digits_v = GetNumberOfDigits(v.back()); 32 | 33 | for (vector::size_type i = 0; i < v.size(); i++) 34 | { 35 | streamsize s_size = os.width(); 36 | os << setw(digits_i) << i << " " << setw(digits_v) << v[i] << endl; 37 | os.width(s_size); 38 | } 39 | } 40 | 41 | return os; 42 | } 43 | 44 | int ex4_3() 45 | { 46 | vector v = square(1000); 47 | print_squares_robust(cout, v); 48 | 49 | return 0; 50 | } -------------------------------------------------------------------------------- /Chapter04/4_3.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_4_3_H 2 | #define GUARD_4_3_H 3 | 4 | int ex4_3(); 5 | 6 | #endif // !GUARD_4_3_H -------------------------------------------------------------------------------- /Chapter04/4_4.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 4-4, 4_4.cpp 3 | 4 | Now change your squares program to use double values instead of ints. Use 5 | manipulators to manage the output so that the values line up in columns. 6 | */ 7 | 8 | #include "stdafx.h" 9 | #include "4_4.h" 10 | 11 | #include 12 | using std::vector; 13 | 14 | #include 15 | using std::ostream; 16 | using std::streamsize; 17 | using std::cout; 18 | using std::endl; 19 | 20 | #include 21 | using std::domain_error; 22 | 23 | #include 24 | using std::setw; 25 | 26 | vector square_double(const unsigned int range) 27 | { 28 | vector squares; 29 | if (range > squares.max_size()) 30 | throw domain_error("Range argument exceeds vector limit."); 31 | 32 | for (unsigned int i = 0; i < range; i++) 33 | squares.push_back(i * i); 34 | 35 | return squares; 36 | } 37 | 38 | double GetNumberOfDigits(const double i) 39 | { 40 | return i > 0 ? log10(i) + 1 : 1; 41 | } 42 | 43 | ostream& print_squares(ostream& os, const vector& v) 44 | { 45 | if (os) 46 | { 47 | streamsize digits_i = (streamsize) GetNumberOfDigits(v.size()); 48 | streamsize digits_v = (streamsize) GetNumberOfDigits(v.back()); 49 | 50 | for (vector::size_type i = 0; i < v.size(); i++) 51 | { 52 | streamsize s_size = os.width(); 53 | os << setw(digits_i) << i << " " << setw(digits_v) << v[i] << endl; 54 | os.width(s_size); 55 | } 56 | } 57 | 58 | return os; 59 | } 60 | 61 | int ex4_4() 62 | { 63 | vector v = square_double(500); 64 | print_squares(cout, v); 65 | 66 | return 0; 67 | } -------------------------------------------------------------------------------- /Chapter04/4_4.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_4_4_H 2 | #define GUARD_4_4_H 3 | 4 | int ex4_4(); 5 | 6 | #endif // !GUARD_4_4_H -------------------------------------------------------------------------------- /Chapter04/4_5.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_4_5_H 2 | #define GUARD_4_5_H 3 | 4 | int ex4_5(); 5 | 6 | #endif // !GUARD_4_5_H -------------------------------------------------------------------------------- /Chapter04/4_6.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_4_6_H 2 | #define GUARD_4_6_H 3 | 4 | #include 5 | 6 | struct Student_info_extra { 7 | std::string name; 8 | double grade; 9 | }; 10 | 11 | int ex4_6(); 12 | 13 | #endif // !GUARD_4_6_H -------------------------------------------------------------------------------- /Chapter04/4_7.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 4-7, 4_7.cpp 3 | 4 | Write a program to calculate the average of the numbers stored in a 5 | vector. 6 | */ 7 | 8 | #include "stdafx.h" 9 | #include "4_7.h" 10 | 11 | #include 12 | using std::vector; 13 | 14 | #include 15 | using std::cout; 16 | using std::endl; 17 | 18 | double average(const vector& nums) 19 | { 20 | double sum = 0; 21 | 22 | for (vector::size_type i = 0; i < nums.size(); i++) 23 | sum += nums[i]; 24 | 25 | return sum / nums.size(); 26 | } 27 | 28 | int ex4_7() 29 | { 30 | vector numbers = { 10.2, 15.7, 18.5, 9.7, 25.2 }; 31 | cout << average(numbers) << endl; 32 | 33 | return 0; 34 | } -------------------------------------------------------------------------------- /Chapter04/4_7.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_4_7_H 2 | #define GUARD_4_7_H 3 | 4 | int ex4_7(); 5 | 6 | #endif // !GUARD_4_7_H -------------------------------------------------------------------------------- /Chapter04/4_8.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 4-8, 4_8.cpp 3 | 4 | If the following code is legal, what can we infer about the return type of f? 5 | */ 6 | 7 | #include "stdafx.h" 8 | #include "4_8.h" 9 | 10 | #include 11 | using std::vector; 12 | 13 | vector f() 14 | { 15 | return{ 10.2, 17.5 }; 16 | } 17 | 18 | int ex4_8() 19 | { 20 | int n = 1; 21 | double d = f()[n]; // f() => return vector met minstens n + 1 elementen 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Chapter04/4_8.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_4_8_H 2 | #define GUARD_4_8_H 3 | 4 | int ex4_8(); 5 | 6 | #endif // !GUARD_4_8_H -------------------------------------------------------------------------------- /Chapter04/Chapter04.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Chapter 04, Chapter04.cpp 3 | 4 | Organizing programs and data. 5 | */ 6 | 7 | #include "stdafx.h" 8 | 9 | #include "4_0.h" 10 | #include "4_1.h" 11 | #include "4_2.h" 12 | #include "4_3.h" 13 | #include "4_4.h" 14 | #include "4_5.h" 15 | #include "4_6.h" 16 | #include "4_7.h" 17 | #include "4_8.h" 18 | 19 | int main() 20 | { 21 | return ex4_8(); 22 | } 23 | 24 | -------------------------------------------------------------------------------- /Chapter04/Student_info.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "stdafx.h" 3 | #include "student_info.h" 4 | #include "grade.h" 5 | 6 | using std::cout; using std::cin; 7 | using std::endl; 8 | using std::istream; using std::vector; 9 | 10 | bool compare(const Student_info& x, const Student_info& y) { 11 | return x.name < y.name; 12 | } 13 | 14 | istream& read(istream& is, Student_info& s) { 15 | is >> s.name >> s.midterm >> s.final; 16 | read_hw(is, s.homework); 17 | 18 | return is; 19 | } 20 | 21 | istream& read_hw(istream& is, vector& hw) { 22 | 23 | if (is) { 24 | is.clear(); 25 | 26 | double x; 27 | while (is >> x) 28 | hw.push_back(x); 29 | 30 | is.clear(); 31 | } 32 | 33 | return is; 34 | } -------------------------------------------------------------------------------- /Chapter04/grade.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "stdafx.h" 4 | #include "grade.h" 5 | #include "median.h" 6 | #include "student_info.h" 7 | 8 | using std::domain_error; using std::vector; 9 | 10 | double grade(double midterm, double final, double homework) { 11 | return 0.2 *midterm + 0.4 * final + 0.4 * homework; 12 | } 13 | 14 | double grade(double midterm, double final, const std::vector& hw) { 15 | if (hw.size() == 0) 16 | throw domain_error("Student has done no homework"); 17 | return grade(midterm, final, median(hw)); 18 | } 19 | 20 | double grade(const Student_info& s) { 21 | return grade(s.midterm, s.final, s.homework); 22 | } -------------------------------------------------------------------------------- /Chapter04/grade.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_grade_h 2 | #define GUARD_grade_h 3 | 4 | #include 5 | #include "student_info.h" 6 | 7 | double grade(double, double, double); 8 | double grade(double, double, const std::vector&); 9 | double grade(const Student_info&); 10 | 11 | #endif // !GUARD_grade_h 12 | -------------------------------------------------------------------------------- /Chapter04/median.cpp: -------------------------------------------------------------------------------- 1 | #include "stdafx.h" 2 | #include 3 | #include 4 | #include 5 | #include "median.h" 6 | 7 | using std::domain_error; using std::sort; using std::vector; 8 | 9 | double median(vector vec) { 10 | typedef vector::size_type vec_sz; 11 | 12 | vec_sz size = vec.size(); 13 | if (size == 0) 14 | throw domain_error("median of empty vector"); 15 | 16 | sort(vec.begin(), vec.end()); 17 | vec_sz mid = size / 2; 18 | 19 | return size % 2 == 0 ? (vec[mid] + vec[mid - 1]) / 2 : vec[mid]; 20 | } -------------------------------------------------------------------------------- /Chapter04/median.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_median_h 2 | #define GUARD_median_h 3 | 4 | #include 5 | 6 | double median(std::vector vec); 7 | 8 | #endif // !GUARD_median 9 | -------------------------------------------------------------------------------- /Chapter04/square.cpp: -------------------------------------------------------------------------------- 1 | #include "stdafx.h" 2 | #include "square.h" 3 | #include 4 | #include 5 | #include 6 | 7 | using std::vector; using std::domain_error; 8 | using std::ostream; using std::endl; 9 | using std::streamsize; using std::setw; 10 | 11 | 12 | 13 | vector square_double(const unsigned int range) { 14 | vector squares; 15 | if (range > squares.max_size()) 16 | throw domain_error("Range argument exceeds vector limit."); 17 | 18 | for (unsigned int i = 0; i < range; i++) 19 | squares.push_back(i * i); 20 | 21 | return squares; 22 | } 23 | 24 | 25 | 26 | ostream& print_squares(ostream& os, const vector& v) { 27 | if (os) { 28 | unsigned digits_i = GetNumberOfDigitss(v.size()); 29 | unsigned digits_v = GetNumberOfDigitss(v.back()); 30 | 31 | for (vector::size_type i = 0; i < v.size(); i++) { 32 | streamsize s_size = os.width(); 33 | os << setw(digits_i) << i << " " << setw(digits_v) << v[i] << endl; 34 | os.width(s_size); 35 | } 36 | } 37 | 38 | return os; 39 | } 40 | 41 | unsigned GetNumberOfDigitss(unsigned i) 42 | { 43 | return i > 0 ? (int)log10((double)i) + 1 : 1; 44 | } 45 | -------------------------------------------------------------------------------- /Chapter04/square.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_square_h 2 | #define GUARD_square_h 3 | 4 | #include 5 | #include 6 | 7 | std::vector square(const unsigned int range); 8 | std::vector square_double(const unsigned int range); 9 | std::ostream& print_squares(std::ostream&, const std::vector&); 10 | std::ostream& print_squares(std::ostream&, const std::vector&); 11 | unsigned GetNumberOfDigitss(unsigned); 12 | 13 | #endif // !GUARD_square_h 14 | 15 | -------------------------------------------------------------------------------- /Chapter04/stdafx.cpp: -------------------------------------------------------------------------------- 1 | // stdafx.cpp : source file that includes just the standard includes 2 | // Chapter04.pch will be the pre-compiled header 3 | // stdafx.obj will contain the pre-compiled type information 4 | 5 | #include "stdafx.h" 6 | 7 | // TODO: reference any additional headers you need in STDAFX.H 8 | // and not in this file 9 | -------------------------------------------------------------------------------- /Chapter04/stdafx.h: -------------------------------------------------------------------------------- 1 | // stdafx.h : include file for standard system include files, 2 | // or project specific include files that are used frequently, but 3 | // are changed infrequently 4 | // 5 | 6 | #pragma once 7 | 8 | #include "targetver.h" 9 | 10 | #include 11 | #include 12 | 13 | 14 | 15 | // TODO: reference additional headers your program requires here 16 | -------------------------------------------------------------------------------- /Chapter04/student_info.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_Student_info 2 | #define GUARD_Student_info 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | struct Student_info { 9 | std::string name; 10 | double midterm, final; 11 | std::vector homework; 12 | }; 13 | 14 | bool compare(const Student_info&, const Student_info&); 15 | std::istream& read(std::istream&, Student_info&); 16 | std::istream& read_hw(std::istream&, std::vector&); 17 | 18 | #endif // !GUARD_student_info -------------------------------------------------------------------------------- /Chapter04/targetver.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | // Including SDKDDKVer.h defines the highest available Windows platform. 4 | 5 | // If you wish to build your application for a previous Windows platform, include WinSDKVer.h and 6 | // set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h. 7 | 8 | #include 9 | -------------------------------------------------------------------------------- /Chapter05/5_0.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 5-0, 5_0.cpp 3 | 4 | Compile, execute, and test the programs in this chapter. 5 | */ 6 | 7 | #include "stdafx.h" 8 | 9 | #include 10 | using std::string; 11 | 12 | #include 13 | using std::vector; 14 | 15 | #include 16 | using std::cin; 17 | using std::cout; 18 | using std::endl; 19 | 20 | #include "words.h" 21 | 22 | int test_string_split() 23 | { 24 | string s; 25 | 26 | while (getline(cin, s)) 27 | { 28 | vector v = split(s); 29 | 30 | for (vector::size_type i = 0; i != v.size(); ++i) 31 | cout << v[i] << endl; 32 | } 33 | 34 | return 0; 35 | } 36 | 37 | int test_input_split() 38 | { 39 | string s; 40 | 41 | while (cin >> s) 42 | cout << s << endl; 43 | 44 | return 0; 45 | } 46 | 47 | int test_frame() 48 | { 49 | vector v1 = { "one test", "two test", "aye caramba" }; 50 | vector v2 = { "one direction", "mama mia", "mamo testo", "lake district mall" }; 51 | 52 | vector vert = vcat(frame(v1), frame(v2)); 53 | 54 | vector::const_iterator iter = vert.begin(); 55 | 56 | while (iter != vert.end()) 57 | cout << *iter++ << endl; 58 | 59 | vector horz = hcat(frame(v1), frame(v2)); 60 | 61 | iter = horz.begin(); 62 | 63 | while (iter != horz.end()) 64 | cout << *iter++ << endl; 65 | 66 | return 0; 67 | } -------------------------------------------------------------------------------- /Chapter05/5_0.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_5_0_H 2 | #define GUARD_5_0_H 3 | 4 | int test_string_split(); 5 | int test_input_split(); 6 | int test_frame(); 7 | 8 | #endif // !GUARD_5_0_H -------------------------------------------------------------------------------- /Chapter05/5_1.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_5_1_H 2 | #define GUARD_5_1_H 3 | 4 | int ex5_1(); 5 | 6 | #endif // !GUARD_5_1_H -------------------------------------------------------------------------------- /Chapter05/5_10.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 5-10, 5_10.cpp 3 | 4 | Palindromes are words that are spelled the same right to left as left to right. Write 5 | a program to find all the palindromes in a dictionary. Next, find the longest palindrome. 6 | */ 7 | 8 | #include "stdafx.h" 9 | 10 | #include "5_10.h" 11 | 12 | #include 13 | using std::string; 14 | 15 | #include 16 | using std::vector; 17 | 18 | #include 19 | using std::ostream; 20 | using std::cout; 21 | using std::endl; 22 | 23 | bool is_palindrome(const string& s) 24 | { 25 | string::const_iterator iter = s.begin(); 26 | string::const_reverse_iterator r_iter = s.rbegin(); 27 | 28 | while (iter != r_iter.base() && iter + 1 != r_iter.base()) 29 | if (tolower(*iter++) != tolower(*r_iter++)) 30 | return false; 31 | 32 | return true; 33 | } 34 | 35 | vector get_palindromes(const vector& v) 36 | { 37 | vector p; 38 | vector::const_iterator iter = v.begin(); 39 | 40 | while (iter != v.end()) 41 | { 42 | if (is_palindrome(*iter)) 43 | p.push_back(*iter); 44 | ++iter; 45 | } 46 | 47 | return p; 48 | } 49 | 50 | ostream& write_palindromes(ostream& os, const vector& v) 51 | { 52 | vector palindromes = get_palindromes(v); 53 | 54 | for (vector::size_type i = 0; i < palindromes.size(); i++) 55 | os << palindromes[i] << endl; 56 | 57 | return os; 58 | } 59 | 60 | int ex5_10() { 61 | vector words = { "TEST" , "teet" , "racecar", "BIKE", "six", "madam", "EIGHT", "reDder", "ten" }; 62 | write_palindromes(cout, words); 63 | 64 | return 0; 65 | } -------------------------------------------------------------------------------- /Chapter05/5_10.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_5_10_H 2 | #define GUARD_5_10_H 3 | 4 | int ex5_10(); 5 | 6 | #endif // !GUARD_5_10_H -------------------------------------------------------------------------------- /Chapter05/5_11.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_5_11_H 2 | #define GUARD_5_11_H 3 | 4 | int ex5_11(); 5 | 6 | #endif // !GUARD_5_11_H -------------------------------------------------------------------------------- /Chapter05/5_2.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_5_2_H 2 | #define GUARD_5_2_H 3 | 4 | int ex5_2(); 5 | 6 | #endif // !GUARD_5_2_H -------------------------------------------------------------------------------- /Chapter05/5_3.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 5-3, 5_3.cpp 3 | 4 | By using a typedef, we can write one version of the program that implements 5 | either a vector-based solution or a list-based one. Write and test this version of the 6 | program. 7 | */ 8 | 9 | #include "stdafx.h" 10 | 11 | #include "5_3.h" 12 | 13 | #include 14 | using std::string; 15 | 16 | #include 17 | using std::vector; 18 | 19 | #include 20 | using std::list; 21 | 22 | #include 23 | using std::cout; 24 | using std::endl; 25 | 26 | #include 27 | using std::ifstream; 28 | 29 | #include 30 | using std::chrono::steady_clock; 31 | 32 | #include "student_info.h" 33 | 34 | typedef list students_t; 35 | //typedef vector students_t; 36 | 37 | 38 | students_t extract_fails_template(students_t& students) 39 | { 40 | students_t fail; 41 | students_t::iterator iter = students.begin(); 42 | 43 | while (iter != students.end()) 44 | { 45 | if (fgrade(*iter)) 46 | { 47 | fail.push_back(*iter); 48 | iter = students.erase(iter); 49 | } 50 | else 51 | ++iter; 52 | } 53 | return fail; 54 | } 55 | 56 | int ex5_3() 57 | { 58 | string file = "students10.txt"; 59 | ifstream infile; 60 | 61 | students_t students, failed; 62 | 63 | infile.open(file); 64 | read(infile, students); 65 | infile.close(); 66 | infile.clear(); 67 | 68 | steady_clock::time_point begin = std::chrono::steady_clock::now(); 69 | failed = extract_fails_template(students); 70 | steady_clock::time_point end = std::chrono::steady_clock::now(); 71 | cout << "Microseconds to extract failed from " << file << " = " << std::chrono::duration_cast(end - begin).count() << endl; 72 | 73 | return 0; 74 | } -------------------------------------------------------------------------------- /Chapter05/5_3.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_5_3_H 2 | #define GUARD_5_3_H 3 | 4 | int ex5_3(); 5 | 6 | #endif // !GUARD_5_3_H -------------------------------------------------------------------------------- /Chapter05/5_4.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_5_4_H 2 | #define GUARD_5_4_H 3 | 4 | int ex5_4(); 5 | 6 | #endif // !GUARD_5_4_H -------------------------------------------------------------------------------- /Chapter05/5_5.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 5-5, 5_5.cpp 3 | 4 | Write a function named center(const vector&) that returns a 5 | picture in which all the lines of the original picture are padded out to their full width, and 6 | the padding is as evenly divided as possible between the left and right sides of the 7 | picture. What are the properties of pictures for which such a function is useful? How can 8 | you tell whether a given picture has those properties? 9 | */ 10 | 11 | #include "stdafx.h" 12 | 13 | #include "5_5.h" 14 | 15 | #include 16 | using std::vector; 17 | 18 | #include 19 | using std::string; 20 | 21 | #include 22 | using std::cout; 23 | using std::endl; 24 | 25 | #include "words.h" 26 | 27 | vector center(const vector& v) 28 | { 29 | vector v_out; 30 | vector::size_type maxlen = width(v); 31 | vector::const_iterator iter = v.begin(); 32 | 33 | while (iter != v.end()) 34 | { 35 | int spaces_l = (maxlen - iter->size()) / 2; 36 | int spaces_r = maxlen - iter->size() - spaces_l; 37 | v_out.push_back(string(spaces_l, ' ') + *iter++ + string(spaces_r, ' ')); 38 | } 39 | 40 | return v_out; 41 | } 42 | 43 | int ex5_5() 44 | { 45 | vector v = { "This is really great", "really", "super awesome", "awesomesauce" }; 46 | vector centered_v = center(v); 47 | 48 | vector::const_iterator iter = centered_v.begin(); 49 | 50 | while (iter != centered_v.end()) 51 | cout << *iter++ << endl; 52 | 53 | return 0; 54 | } -------------------------------------------------------------------------------- /Chapter05/5_5.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_5_5_H 2 | #define GUARD_5_5_H 3 | 4 | int ex5_5(); 5 | 6 | #endif // !GUARD_5_5_H -------------------------------------------------------------------------------- /Chapter05/5_6.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeertArien/c-accelerated/c0ae9f66b1733de04f3133db2e9d8af6d555fe6e/Chapter05/5_6.cpp -------------------------------------------------------------------------------- /Chapter05/5_6.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_5_6_H 2 | #define GUARD_5_6_H 3 | 4 | int ex5_6(); 5 | 6 | #endif // !GUARD_5_6_H -------------------------------------------------------------------------------- /Chapter05/5_7.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeertArien/c-accelerated/c0ae9f66b1733de04f3133db2e9d8af6d555fe6e/Chapter05/5_7.cpp -------------------------------------------------------------------------------- /Chapter05/5_7.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_5_7_H 2 | #define GUARD_5_7_H 3 | 4 | int ex5_7(); 5 | 6 | #endif // !GUARD_5_7_H -------------------------------------------------------------------------------- /Chapter05/5_8.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeertArien/c-accelerated/c0ae9f66b1733de04f3133db2e9d8af6d555fe6e/Chapter05/5_8.cpp -------------------------------------------------------------------------------- /Chapter05/5_8.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_5_8_H 2 | #define GUARD_5_8_H 3 | 4 | int ex5_8(); 5 | 6 | #endif // !GUARD_5_8_H -------------------------------------------------------------------------------- /Chapter05/5_9.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 5-9, 5_9.cpp 3 | 4 | Write a program to write the lowercase words in the input followed by the 5 | uppercase words. 6 | */ 7 | 8 | #include "stdafx.h" 9 | 10 | #include "5_9.h" 11 | 12 | #include 13 | using std::ostream; 14 | using std::cout; 15 | using std::endl; 16 | 17 | #include 18 | using std::vector; 19 | 20 | #include 21 | using std::string; 22 | 23 | #include 24 | using std::sort; 25 | 26 | #include 27 | using std::greater; 28 | 29 | ostream& write_lower_upper(ostream& os, const vector& v) 30 | { 31 | vector v_copy = v; 32 | sort(v_copy.begin(), v_copy.end(), greater()); 33 | 34 | for (vector::size_type i = 0; i < v_copy.size(); i++) 35 | { 36 | os << v_copy[i] << endl; 37 | } 38 | 39 | return os; 40 | } 41 | 42 | int ex5_9() 43 | { 44 | vector words = { "TEST" , "test" , "mountain", "BIKE", "six", "seven", "EIGHT", "NINE", "ten" }; 45 | write_lower_upper(cout, words); 46 | 47 | return 0; 48 | } -------------------------------------------------------------------------------- /Chapter05/5_9.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_5_9_H 2 | #define GUARD_5_9_H 3 | 4 | int ex5_9(); 5 | 6 | #endif // !GUARD_5_9_H -------------------------------------------------------------------------------- /Chapter05/Chapter05.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Chapter 05, Chapter05.cpp 3 | 4 | Using sequential containers and analyzing strings. 5 | */ 6 | 7 | #include "stdafx.h" 8 | 9 | #include "5_0.h" 10 | #include "5_1.h" 11 | #include "5_2.h" 12 | #include "5_3.h" 13 | #include "5_4.h" 14 | #include "5_5.h" 15 | #include "5_6.h" 16 | #include "5_7.h" 17 | #include "5_8.h" 18 | #include "5_9.h" 19 | #include "5_10.h" 20 | #include "5_11.h" 21 | 22 | int main() 23 | { 24 | return ex5_11(); 25 | } -------------------------------------------------------------------------------- /Chapter05/grade.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "stdafx.h" 4 | #include "grade.h" 5 | #include "median.h" 6 | #include "student_info.h" 7 | 8 | using std::domain_error; using std::vector; 9 | 10 | double grade(double midterm, double final, double homework) { 11 | return 0.2 *midterm + 0.4 * final + 0.4 * homework; 12 | } 13 | 14 | double grade(double midterm, double final, const std::vector& hw) { 15 | if (hw.size() == 0) 16 | throw domain_error("Student has done no homework"); 17 | return grade(midterm, final, median(hw)); 18 | } 19 | 20 | double grade(const Student_info& s) { 21 | return s.grade; 22 | } -------------------------------------------------------------------------------- /Chapter05/grade.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_grade_h 2 | #define GUARD_grade_h 3 | 4 | #include 5 | #include "student_info.h" 6 | 7 | double grade(double, double, double); 8 | double grade(double, double, const std::vector&); 9 | double grade(const Student_info&); 10 | 11 | #endif // !GUARD_grade_h 12 | -------------------------------------------------------------------------------- /Chapter05/median.cpp: -------------------------------------------------------------------------------- 1 | #include "stdafx.h" 2 | #include 3 | #include 4 | #include 5 | #include "median.h" 6 | 7 | using std::domain_error; using std::sort; using std::vector; 8 | 9 | double median(vector vec) { 10 | typedef vector::size_type vec_sz; 11 | 12 | vec_sz size = vec.size(); 13 | if (size == 0) 14 | throw domain_error("median of empty vector"); 15 | 16 | sort(vec.begin(), vec.end()); 17 | vec_sz mid = size / 2; 18 | 19 | return size % 2 == 0 ? (vec[mid] + vec[mid - 1]) / 2 : vec[mid]; 20 | } -------------------------------------------------------------------------------- /Chapter05/median.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_median_h 2 | #define GUARD_median_h 3 | 4 | #include 5 | 6 | double median(std::vector vec); 7 | 8 | #endif // !GUARD_median 9 | -------------------------------------------------------------------------------- /Chapter05/stdafx.cpp: -------------------------------------------------------------------------------- 1 | // stdafx.cpp : source file that includes just the standard includes 2 | // Chapter05.pch will be the pre-compiled header 3 | // stdafx.obj will contain the pre-compiled type information 4 | 5 | #include "stdafx.h" 6 | 7 | // TODO: reference any additional headers you need in STDAFX.H 8 | // and not in this file 9 | -------------------------------------------------------------------------------- /Chapter05/stdafx.h: -------------------------------------------------------------------------------- 1 | // stdafx.h : include file for standard system include files, 2 | // or project specific include files that are used frequently, but 3 | // are changed infrequently 4 | // 5 | 6 | #pragma once 7 | 8 | #include "targetver.h" 9 | 10 | #include 11 | #include 12 | 13 | 14 | 15 | // TODO: reference additional headers your program requires here 16 | -------------------------------------------------------------------------------- /Chapter05/student_info.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_Student_info 2 | #define GUARD_Student_info 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | struct Student_info { 10 | std::string name; 11 | double grade; 12 | }; 13 | 14 | bool compare(const Student_info&, const Student_info&); 15 | std::istream& read(std::istream&, Student_info&); 16 | std::istream& read(std::istream&, std::vector&); 17 | std::istream& read(std::istream&, std::list&); 18 | std::istream& read_hw(std::istream&, std::vector&); 19 | bool fgrade(const Student_info&); 20 | std::vector extract_fails(std::vector&); 21 | std::list extract_fails(std::list&); 22 | 23 | #endif // !GUARD_student_info 24 | 25 | -------------------------------------------------------------------------------- /Chapter05/students10.txt: -------------------------------------------------------------------------------- 1 | Geert 50 80 90 45 30 80 2 | Pieter 40 50 30 40 50 50 40 3 | Lotte 40 80 90 45 60 70 4 | Jan 30 20 80 40 50 30 40 5 | Joris 80 90 60 70 60 70 60 6 | Geert 50 80 90 45 30 80 7 | Pieter 40 50 30 40 50 50 40 8 | Lotte 40 80 90 45 60 70 9 | Jan 30 20 80 40 50 30 40 10 | Joris 80 90 60 70 60 70 60 -------------------------------------------------------------------------------- /Chapter05/targetver.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | // Including SDKDDKVer.h defines the highest available Windows platform. 4 | 5 | // If you wish to build your application for a previous Windows platform, include WinSDKVer.h and 6 | // set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h. 7 | 8 | #include 9 | -------------------------------------------------------------------------------- /Chapter05/words.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_WORDS_H 2 | #define GUARD_WORDS_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | std::vector split(const std::string&); 10 | std::string::size_type width(const std::vector&); 11 | std::vector frame(const std::vector&); 12 | std::vector vcat(const std::vector&, const std::vector&); 13 | std::vector hcat(const std::vector&, const std::vector&); 14 | bool compare_ignore_case(std::string, std::string); 15 | 16 | #endif // !GUARD_WORDS_H 17 | -------------------------------------------------------------------------------- /Chapter06/6_0.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 6-0, 6_0.cpp 3 | 4 | Compile, execute, and test the programs in this chapter. 5 | */ 6 | 7 | #include "stdafx.h" 8 | #include "6_0.h" 9 | 10 | #include 11 | using std::vector; 12 | 13 | #include 14 | using std::cout; 15 | using std::cin; 16 | using std::endl; 17 | 18 | #include "student_info.h" 19 | 20 | int ex6_0() 21 | { 22 | vector did, didnt; 23 | Student_info student; 24 | 25 | while (read(cin, student)) 26 | { 27 | if (did_all_hw(student)) 28 | did.push_back(student); 29 | else 30 | didnt.push_back(student); 31 | } 32 | 33 | if (did.empty()) 34 | { 35 | cout << "No student did all the homework!" << endl; 36 | return 1; 37 | } 38 | 39 | if (didnt.empty()) 40 | { 41 | cout << "Every student did all the homework!" << endl; 42 | return 1; 43 | } 44 | 45 | write_analysis(cout, "median", median_analysis, did, didnt); 46 | write_analysis(cout, "average", average_analysis, did, didnt); 47 | 48 | return 0; 49 | } -------------------------------------------------------------------------------- /Chapter06/6_0.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_6_0_H 2 | #define GUARD_6_0_H 3 | 4 | int ex6_0(); 5 | 6 | #endif // !GUARD_6_0_H -------------------------------------------------------------------------------- /Chapter06/6_1.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeertArien/c-accelerated/c0ae9f66b1733de04f3133db2e9d8af6d555fe6e/Chapter06/6_1.cpp -------------------------------------------------------------------------------- /Chapter06/6_1.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_6_1_H 2 | #define GUARD_6_1_H 3 | 4 | int ex6_1(); 5 | 6 | #endif // !GUARD_6_1_H -------------------------------------------------------------------------------- /Chapter06/6_2.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 6-2, 6_2.cpp 3 | 4 | Write a program to test the find_urls function. 5 | */ 6 | 7 | #include "stdafx.h" 8 | 9 | #include "6_2.h" 10 | 11 | #include 12 | using std::string; 13 | 14 | #include 15 | using std::vector; 16 | 17 | #include 18 | using std::cout; 19 | using std::endl; 20 | 21 | #include "words.h" 22 | 23 | int ex6_2() 24 | { 25 | string s = "http:// http://test.com toast.com ftp://fttpt :// ://google.be f://l"; 26 | vector v = find_urls(s); 27 | vector::iterator iter = v.begin(); 28 | 29 | while (iter != v.end()) 30 | cout << *iter++ << endl; 31 | 32 | return 0; 33 | } -------------------------------------------------------------------------------- /Chapter06/6_2.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_6_2_H 2 | #define GUARD_6_2_H 3 | 4 | int ex6_2(); 5 | 6 | #endif // !GUARD_6_2_H -------------------------------------------------------------------------------- /Chapter06/6_3.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 6-3, 6_3.cpp 3 | 4 | What does this program fragment do? 5 | 6 | vector u(10, 100); 7 | vector v; 8 | copy(u.begin(), u.end(), v.begin()); 9 | 10 | Write a program that contains this fragment, and compile and execute it. 11 | */ 12 | 13 | #include "stdafx.h" 14 | 15 | #include "6_3.h" 16 | 17 | #include 18 | using std::vector; 19 | 20 | using std::copy; 21 | 22 | int ex6_3() 23 | { 24 | vector u(10, 100); 25 | vector v; 26 | copy(u.begin(), u.end(), v.begin()); 27 | 28 | return 0; 29 | } -------------------------------------------------------------------------------- /Chapter06/6_3.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_6_3_H 2 | #define GUARD_6_3_H 3 | 4 | int ex6_3(); 5 | 6 | #endif // !GUARD_6_3_H -------------------------------------------------------------------------------- /Chapter06/6_4.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 6-4, 6_4.cpp 3 | 4 | Correct the program you wrote in the previous exercise to copy from u into v. 5 | There are at least two possible ways to correct the program. Implement both, and 6 | describe the relative advantages and disadvantages of each approach. 7 | */ 8 | 9 | #include "stdafx.h" 10 | 11 | #include "6_4.h" 12 | 13 | #include 14 | using std::vector; 15 | 16 | using std::copy; 17 | 18 | int ex6_4() 19 | { 20 | vector u(10, 100); 21 | 22 | //vector v(u); // Direct initialization as copy of vector u 23 | //vector v = u; // Implicit initialization as copy of vector u 24 | 25 | vector v(u.size()); // first initialized with default value 26 | copy(u.begin(), u.end(), v.begin()); 27 | 28 | return 0; 29 | } -------------------------------------------------------------------------------- /Chapter06/6_4.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_6_4_H 2 | #define GUARD_6_4_H 3 | 4 | int ex6_4(); 5 | 6 | #endif // !GUARD_6_4_H -------------------------------------------------------------------------------- /Chapter06/6_5.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 6-5, 6_5.cpp 3 | 4 | Use a library algorithm to concatenate all the elements of a vector. 5 | */ 6 | 7 | #include "stdafx.h" 8 | 9 | #include "6_5.h" 10 | 11 | #include 12 | using std::vector; 13 | 14 | #include 15 | using std::string; 16 | 17 | #include 18 | using std::transform; 19 | 20 | #include 21 | using std::back_inserter; 22 | 23 | #include 24 | using std::cout; 25 | using std::endl; 26 | 27 | #include 28 | using std::ifstream; 29 | 30 | #include "student_info.h" 31 | #include "vec.h" 32 | 33 | double optimistic_median_analysis(const vector& students) 34 | { 35 | vector grades; 36 | transform(students.begin(), students.end(), back_inserter(grades), optimistic_median); 37 | return median(grades); 38 | } 39 | 40 | int test_optimistic_median(string file) 41 | { 42 | vector students, did, didnt; 43 | Student_info student; 44 | 45 | ifstream infile; 46 | 47 | infile.open(file); 48 | read(infile, students); 49 | 50 | for (vector::const_iterator it = students.begin(); it != students.end(); it++) 51 | { 52 | if (did_all_hw(*it)) 53 | did.push_back(*it); 54 | else 55 | didnt.push_back(*it); 56 | } 57 | 58 | if (did.empty()) 59 | { 60 | cout << "No student did all the homework!" << endl; 61 | return 1; 62 | } 63 | 64 | if (didnt.empty()) 65 | { 66 | cout << "Every student did all the homework!" << endl; 67 | return 1; 68 | } 69 | 70 | write_analysis(cout, "median of homework turned in", optimistic_median_analysis, did, didnt); 71 | 72 | return 0; 73 | } 74 | 75 | int ex6_5() 76 | { 77 | return test_optimistic_median("students10.txt"); 78 | } -------------------------------------------------------------------------------- /Chapter06/6_5.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_6_5_H 2 | #define GUARD_6_5_H 3 | 4 | int ex6_5(); 5 | 6 | #endif // !GUARD_6_5_H -------------------------------------------------------------------------------- /Chapter06/6_6.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeertArien/c-accelerated/c0ae9f66b1733de04f3133db2e9d8af6d555fe6e/Chapter06/6_6.cpp -------------------------------------------------------------------------------- /Chapter06/6_6.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_6_6_H 2 | #define GUARD_6_6_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #include "student_info.h" 9 | 10 | int ex6_6(); 11 | void write_analysis(std::ostream&, const std::string&, double method(const Student_info&), 12 | const std::vector&, const std::vector&); 13 | 14 | #endif // !GUARD_6_6_H -------------------------------------------------------------------------------- /Chapter06/6_7.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeertArien/c-accelerated/c0ae9f66b1733de04f3133db2e9d8af6d555fe6e/Chapter06/6_7.cpp -------------------------------------------------------------------------------- /Chapter06/6_7.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_6_7_H 2 | #define GUARD_6_7_H 3 | 4 | int ex6_7(); 5 | 6 | #endif // !GUARD_6_7_H -------------------------------------------------------------------------------- /Chapter06/6_8.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_6_8_H 2 | #define GUARD_6_8_H 3 | 4 | int ex6_8(); 5 | 6 | #endif // !GUARD_6_8_H -------------------------------------------------------------------------------- /Chapter06/6_9.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 6-9, 6_9.cpp 3 | 4 | Use a library algorithm to concatenate all the elements of a vector. 5 | */ 6 | 7 | #include "stdafx.h" 8 | 9 | #include "6_9.h" 10 | 11 | #include 12 | using std::vector; 13 | 14 | #include 15 | using std::string; 16 | 17 | #include 18 | using std::cout; 19 | using std::endl; 20 | 21 | #include 22 | using std::accumulate; 23 | 24 | string concat(const vector& v) 25 | { 26 | string s; 27 | return accumulate(v.begin(), v.end(), s); 28 | } 29 | 30 | int ex6_9() 31 | { 32 | vector v = { "one direction", "mama mia", "mamo testo", "lake district mall" }; 33 | cout << concat(v) << endl; 34 | 35 | return 0; 36 | } -------------------------------------------------------------------------------- /Chapter06/6_9.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_6_9_H 2 | #define GUARD_6_9_H 3 | 4 | int ex6_9(); 5 | 6 | #endif // !GUARD_6_9_H -------------------------------------------------------------------------------- /Chapter06/Chapter06.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Chapter 06, Chapter06.cpp 3 | 4 | Using library algorithms. 5 | */ 6 | 7 | #include "stdafx.h" 8 | 9 | #include "6_0.h" 10 | #include "6_1.h" 11 | #include "6_2.h" 12 | #include "6_3.h" 13 | #include "6_4.h" 14 | #include "6_5.h" 15 | #include "6_6.h" 16 | #include "6_7.h" 17 | #include "6_8.h" 18 | #include "6_9.h" 19 | 20 | int main() 21 | { 22 | return ex6_9(); 23 | } 24 | 25 | -------------------------------------------------------------------------------- /Chapter06/grade.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "stdafx.h" 4 | #include "grade.h" 5 | #include "vec.h" 6 | #include "student_info.h" 7 | 8 | using std::domain_error; using std::vector; 9 | 10 | double grade(double midterm, double final, double homework) 11 | { 12 | return 0.2 *midterm + 0.4 * final + 0.4 * homework; 13 | } 14 | 15 | double grade(double midterm, double final, const std::vector& hw) 16 | { 17 | if (hw.size() == 0) 18 | throw domain_error("Student has done no homework"); 19 | return grade(midterm, final, median(hw)); 20 | } 21 | 22 | double grade(const Student_info& s) 23 | { 24 | return grade(s.midterm, s.final, s.homework); 25 | } -------------------------------------------------------------------------------- /Chapter06/grade.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_grade_h 2 | #define GUARD_grade_h 3 | 4 | #include 5 | #include "student_info.h" 6 | 7 | double grade(double, double, double); 8 | double grade(double, double, const std::vector&); 9 | double grade(const Student_info&); 10 | 11 | #endif // !GUARD_grade_h 12 | -------------------------------------------------------------------------------- /Chapter06/stdafx.cpp: -------------------------------------------------------------------------------- 1 | // stdafx.cpp : source file that includes just the standard includes 2 | // Chapter06.pch will be the pre-compiled header 3 | // stdafx.obj will contain the pre-compiled type information 4 | 5 | #include "stdafx.h" 6 | 7 | // TODO: reference any additional headers you need in STDAFX.H 8 | // and not in this file 9 | -------------------------------------------------------------------------------- /Chapter06/stdafx.h: -------------------------------------------------------------------------------- 1 | // stdafx.h : include file for standard system include files, 2 | // or project specific include files that are used frequently, but 3 | // are changed infrequently 4 | // 5 | 6 | #pragma once 7 | 8 | #include "targetver.h" 9 | 10 | #include 11 | #include 12 | 13 | 14 | 15 | // TODO: reference additional headers your program requires here 16 | -------------------------------------------------------------------------------- /Chapter06/student_info.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_Student_info 2 | #define GUARD_Student_info 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | struct Student_info 9 | { 10 | std::string name; 11 | std::vector homework; 12 | double midterm; 13 | double final; 14 | }; 15 | 16 | bool compare(const Student_info&, const Student_info&); 17 | std::istream& read(std::istream&, std::vector&); 18 | std::istream& read(std::istream&, Student_info&); 19 | std::istream& read_hw(std::istream&, std::vector&); 20 | bool did_all_hw(const Student_info&); 21 | double grade_aux(const Student_info&); 22 | double optimistic_median(const Student_info&); 23 | double average_grade(const Student_info&); 24 | double median_analysis(const std::vector&); 25 | double average_analysis(const std::vector&); 26 | void write_analysis(std::ostream&, const std::string&, double analysis(const std::vector&), 27 | const std::vector&, const std::vector&); 28 | bool fgrade(const Student_info&); 29 | bool pgrade(const Student_info&); 30 | 31 | #endif // !GUARD_student_info 32 | 33 | -------------------------------------------------------------------------------- /Chapter06/students10.txt: -------------------------------------------------------------------------------- 1 | Geert 50 80 90 45 30 80 2 | Pieter 40 50 30 0 50 50 40 3 | Lotte 40 80 90 45 60 70 4 | Jan 30 20 80 40 50 30 0 5 | Joris 80 90 60 70 60 70 60 6 | Geert 50 80 90 45 30 80 7 | Pieter 40 50 30 40 50 0 40 8 | Lotte 40 80 90 45 60 70 9 | Jan 30 20 80 0 50 30 40 10 | Joris 80 90 60 70 60 70 60 -------------------------------------------------------------------------------- /Chapter06/targetver.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | // Including SDKDDKVer.h defines the highest available Windows platform. 4 | 5 | // If you wish to build your application for a previous Windows platform, include WinSDKVer.h and 6 | // set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h. 7 | 8 | #include 9 | -------------------------------------------------------------------------------- /Chapter06/vec.cpp: -------------------------------------------------------------------------------- 1 | #include "stdafx.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include "vec.h" 9 | 10 | using std::domain_error; using std::sort; using std::vector; 11 | using std::accumulate; 12 | 13 | double median(vector vec) 14 | { 15 | typedef vector::size_type vec_sz; 16 | 17 | vec_sz size = vec.size(); 18 | if (size == 0) 19 | throw domain_error("median of empty vector"); 20 | 21 | sort(vec.begin(), vec.end()); 22 | vec_sz mid = size / 2; 23 | 24 | return size % 2 == 0 ? (vec[mid] + vec[mid - 1]) / 2 : vec[mid]; 25 | } 26 | 27 | double average(const vector& v) 28 | { 29 | return accumulate(v.begin(), v.end(), 0.0) / v.size(); 30 | } 31 | -------------------------------------------------------------------------------- /Chapter06/vec.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_median_h 2 | #define GUARD_median_h 3 | 4 | #include 5 | 6 | double median(std::vector); 7 | double average(const std::vector&); 8 | 9 | #endif // !GUARD_median 10 | -------------------------------------------------------------------------------- /Chapter06/words.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_words_h 2 | #define GUARD_words_h 3 | 4 | #include 5 | #include 6 | 7 | bool space(char); 8 | bool not_space(char); 9 | bool not_url_char(char); 10 | std::vector split(const std::string&); 11 | bool is_palindrome(const std::string&); 12 | std::string::const_iterator url_beg(std::string::const_iterator, std::string::const_iterator); 13 | std::string::const_iterator url_end(std::string::const_iterator, std::string::const_iterator); 14 | std::vector find_urls(const std::string&); 15 | 16 | #endif // !GUARD_words_h 17 | -------------------------------------------------------------------------------- /Chapter07/7_0.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 7-0, 7_0.cpp 3 | 4 | Compile, execute, and test the programs in this chapter. 5 | */ 6 | 7 | #include "stdafx.h" 8 | #include "7_0.h" 9 | 10 | #include 11 | using std::vector; 12 | 13 | #include 14 | using std::string; 15 | 16 | #include 17 | using std::cin; 18 | using std::cout; 19 | using std::endl; 20 | 21 | #include 22 | using std::map; 23 | 24 | #include "words.h" 25 | 26 | int test_xref() 27 | { 28 | map > ret = xref(cin); 29 | 30 | for (map >::const_iterator it = ret.begin(); it != ret.end(); ++it) 31 | { 32 | cout << it->first << " occurs on line(s): "; 33 | 34 | vector::const_iterator line_it = it->second.begin(); 35 | cout << *line_it; 36 | 37 | ++line_it; 38 | 39 | while (line_it != it->second.end()) 40 | { 41 | cout << ", " << *line_it; 42 | ++line_it; 43 | } 44 | 45 | cout << endl; 46 | } 47 | 48 | return 0; 49 | } 50 | 51 | int test_generate_sentence() 52 | { 53 | vector sentence = gen_sentence(read_grammar(cin)); 54 | vector::const_iterator it = sentence.begin(); 55 | 56 | if (!sentence.empty()) 57 | { 58 | cout << *it; 59 | ++it; 60 | } 61 | 62 | while (it != sentence.end()) 63 | { 64 | cout << " " << *it; 65 | ++it; 66 | } 67 | 68 | cout << endl; 69 | return 0; 70 | } 71 | 72 | int ex7_0() 73 | { 74 | test_xref(); 75 | cin.clear(); 76 | test_generate_sentence(); 77 | return 0; 78 | } -------------------------------------------------------------------------------- /Chapter07/7_0.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_7_0_H 2 | #define GUARD_7_0_H 3 | 4 | int ex7_0(); 5 | 6 | #endif // !GUARD_7_0_H -------------------------------------------------------------------------------- /Chapter07/7_1.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeertArien/c-accelerated/c0ae9f66b1733de04f3133db2e9d8af6d555fe6e/Chapter07/7_1.cpp -------------------------------------------------------------------------------- /Chapter07/7_1.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_7_1_H 2 | #define GUARD_7_1_H 3 | 4 | int ex7_1(); 5 | 6 | #endif // !GUARD_7_1_H -------------------------------------------------------------------------------- /Chapter07/7_2.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeertArien/c-accelerated/c0ae9f66b1733de04f3133db2e9d8af6d555fe6e/Chapter07/7_2.cpp -------------------------------------------------------------------------------- /Chapter07/7_2.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_7_2_H 2 | #define GUARD_7_2_H 3 | 4 | int ex7_2(); 5 | 6 | #endif // !GUARD_7_2_H -------------------------------------------------------------------------------- /Chapter07/7_3.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeertArien/c-accelerated/c0ae9f66b1733de04f3133db2e9d8af6d555fe6e/Chapter07/7_3.cpp -------------------------------------------------------------------------------- /Chapter07/7_3.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_7_3_H 2 | #define GUARD_7_3_H 3 | 4 | #include "words.h" 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | int ex7_3(); 12 | std::map > xref_unique(std::istream& in, std::vector find_words(const std::string&) = split); 13 | 14 | #endif // !GUARD_7_3_H -------------------------------------------------------------------------------- /Chapter07/7_4.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_7_4_H 2 | #define GUARD_7_4_H 3 | 4 | int ex7_4(); 5 | 6 | #endif // !GUARD_7_4_H -------------------------------------------------------------------------------- /Chapter07/7_5.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_7_5_H 2 | #define GUARD_7_5_H 3 | 4 | int ex7_5(); 5 | 6 | #endif // !GUARD_7_5_H -------------------------------------------------------------------------------- /Chapter07/7_6.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_7_6_H 2 | #define GUARD_7_6_H 3 | 4 | int ex7_6(); 5 | 6 | #endif // !GUARD_7_6_H -------------------------------------------------------------------------------- /Chapter07/7_7.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_7_7_H 2 | #define GUARD_7_7_H 3 | 4 | int ex7_7(); 5 | 6 | #endif // !GUARD_7_7_H -------------------------------------------------------------------------------- /Chapter07/7_8.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_7_8_H 2 | #define GUARD_7_8_H 3 | 4 | int ex7_8(); 5 | 6 | #endif // !GUARD_7_8_H -------------------------------------------------------------------------------- /Chapter07/7_9.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeertArien/c-accelerated/c0ae9f66b1733de04f3133db2e9d8af6d555fe6e/Chapter07/7_9.cpp -------------------------------------------------------------------------------- /Chapter07/7_9.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_7_9_H 2 | #define GUARD_7_9_H 3 | 4 | int ex7_9(); 5 | 6 | #endif // !GUARD_7_9_H -------------------------------------------------------------------------------- /Chapter07/Chapter07.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Chapter 07, Chapter07.cpp 3 | 4 | Using associative containers. 5 | */ 6 | 7 | #include "stdafx.h" 8 | 9 | #include "7_0.h" 10 | #include "7_1.h" 11 | #include "7_2.h" 12 | #include "7_3.h" 13 | #include "7_4.h" 14 | #include "7_5.h" 15 | #include "7_6.h" 16 | #include "7_7.h" 17 | #include "7_8.h" 18 | #include "7_9.h" 19 | 20 | int main() 21 | { 22 | return ex7_9(); 23 | } -------------------------------------------------------------------------------- /Chapter07/Student_info.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "stdafx.h" 3 | #include "student_info.h" 4 | #include "grade.h" 5 | 6 | using std::cout; using std::cin; 7 | using std::endl; 8 | using std::istream; using std::vector; 9 | 10 | bool compare(const Student_info& x, const Student_info& y) 11 | { 12 | return x.name < y.name; 13 | } 14 | 15 | istream& read(istream& is, Student_info& s) 16 | { 17 | double midterm, final; 18 | vector homework; 19 | 20 | is >> s.name >> midterm >> final; 21 | read_hw(is, homework); 22 | 23 | if (is) 24 | s.grade = grade(midterm, final, homework); 25 | 26 | return is; 27 | } 28 | 29 | istream& read_hw(istream& is, vector& hw) 30 | { 31 | 32 | if (is) 33 | { 34 | is.clear(); 35 | 36 | double x; 37 | while (is >> x) 38 | hw.push_back(x); 39 | 40 | is.clear(); 41 | } 42 | 43 | return is; 44 | } 45 | -------------------------------------------------------------------------------- /Chapter07/grade.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "stdafx.h" 4 | #include "grade.h" 5 | #include "median.h" 6 | #include "student_info.h" 7 | 8 | using std::domain_error; using std::vector; 9 | 10 | double grade(double midterm, double final, double homework) 11 | { 12 | return 0.2 *midterm + 0.4 * final + 0.4 * homework; 13 | } 14 | 15 | double grade(double midterm, double final, const std::vector& hw) 16 | { 17 | if (hw.size() == 0) 18 | throw domain_error("Student has done no homework"); 19 | return grade(midterm, final, median(hw)); 20 | } 21 | 22 | double grade(const Student_info& s) 23 | { 24 | return s.grade; 25 | } -------------------------------------------------------------------------------- /Chapter07/grade.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_grade_h 2 | #define GUARD_grade_h 3 | 4 | #include 5 | #include "student_info.h" 6 | 7 | double grade(double, double, double); 8 | double grade(double, double, const std::vector&); 9 | double grade(const Student_info&); 10 | 11 | #endif // !GUARD_grade_h 12 | -------------------------------------------------------------------------------- /Chapter07/grammar.txt: -------------------------------------------------------------------------------- 1 | cat 2 | dog 3 | table 4 | 5 | 6 | large 7 | brown 8 | absurd 9 | jumps 10 | sits 11 | on the stairs 12 | under the sky 13 | wherever it wants 14 | the 15 | 16 | 17 | 18 | the -> push back 19 | 20 | the -------------------------------------------------------------------------------- /Chapter07/median.cpp: -------------------------------------------------------------------------------- 1 | #include "stdafx.h" 2 | #include 3 | #include 4 | #include 5 | #include "median.h" 6 | 7 | using std::domain_error; using std::sort; using std::vector; 8 | 9 | double median(vector vec) 10 | { 11 | typedef vector::size_type vec_sz; 12 | 13 | vec_sz size = vec.size(); 14 | if (size == 0) 15 | throw domain_error("median of empty vector"); 16 | 17 | sort(vec.begin(), vec.end()); 18 | vec_sz mid = size / 2; 19 | 20 | return size % 2 == 0 ? (vec[mid] + vec[mid - 1]) / 2 : vec[mid]; 21 | } -------------------------------------------------------------------------------- /Chapter07/median.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_median_h 2 | #define GUARD_median_h 3 | 4 | #include 5 | 6 | double median(std::vector vec); 7 | 8 | #endif // !GUARD_median 9 | -------------------------------------------------------------------------------- /Chapter07/numbers.cpp: -------------------------------------------------------------------------------- 1 | #include "stdafx.h" 2 | #include "numbers.h" 3 | 4 | #include 5 | #include 6 | 7 | using std::domain_error; 8 | 9 | int nrand(int n) 10 | { 11 | if (n <= 0 || n > RAND_MAX) 12 | throw domain_error("Argument to nrand is out of range"); 13 | 14 | const int bucket_size = RAND_MAX / n; 15 | int r; 16 | 17 | do r = rand() / bucket_size; 18 | while (r >= n); 19 | 20 | return r; 21 | } -------------------------------------------------------------------------------- /Chapter07/numbers.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_numbers_h 2 | #define GUARD_numbers_h 3 | 4 | int nrand(int); 5 | 6 | #endif // !GUARD_numbers_h 7 | -------------------------------------------------------------------------------- /Chapter07/stdafx.cpp: -------------------------------------------------------------------------------- 1 | // stdafx.cpp : source file that includes just the standard includes 2 | // Chapter07.pch will be the pre-compiled header 3 | // stdafx.obj will contain the pre-compiled type information 4 | 5 | #include "stdafx.h" 6 | 7 | // TODO: reference any additional headers you need in STDAFX.H 8 | // and not in this file 9 | -------------------------------------------------------------------------------- /Chapter07/stdafx.h: -------------------------------------------------------------------------------- 1 | // stdafx.h : include file for standard system include files, 2 | // or project specific include files that are used frequently, but 3 | // are changed infrequently 4 | // 5 | 6 | #pragma once 7 | 8 | #include "targetver.h" 9 | 10 | #include 11 | #include 12 | 13 | 14 | 15 | // TODO: reference additional headers your program requires here 16 | -------------------------------------------------------------------------------- /Chapter07/student_info.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_Student_info 2 | #define GUARD_Student_info 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | struct Student_info { 9 | std::string name; 10 | double grade; 11 | }; 12 | 13 | bool compare(const Student_info&, const Student_info&); 14 | std::istream& read(std::istream&, Student_info&); 15 | std::istream& read_hw(std::istream&, std::vector&); 16 | 17 | #endif // !GUARD_student_info 18 | 19 | -------------------------------------------------------------------------------- /Chapter07/targetver.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | // Including SDKDDKVer.h defines the highest available Windows platform. 4 | 5 | // If you wish to build your application for a previous Windows platform, include WinSDKVer.h and 6 | // set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h. 7 | 8 | #include 9 | -------------------------------------------------------------------------------- /Chapter07/urls.txt: -------------------------------------------------------------------------------- 1 | anna barbara http://www.google.be fantastic http://www.google.be 2 | Boy oh boy http://www.amazon.com great 3 | this is just a test 4 | wald disney is at http://www.disneyland.com not at http://www.google.be you know 5 | this is a really obnoxious sentence ftp://www.telenet.be yay 6 | this is really http://www.amazon.com good 7 | and just once more http://www.amazon.com okay -------------------------------------------------------------------------------- /Chapter07/words.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_words_h 2 | #define GUARD_words_h 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | typedef std::vector Rule; 10 | typedef std::vector Rule_collection; 11 | typedef std::map Grammar; 12 | 13 | std::istream count_words(std::istream, std::ostream); 14 | std::vector split(const std::string&); 15 | std::map > xref(std::istream&, std::vector find_words(const std::string&) = split); 16 | bool space(char); 17 | bool not_space(char); 18 | bool bracketed(const std::string&); 19 | Grammar read_grammar(std::istream&); 20 | std::vector gen_sentence(const Grammar&); 21 | void gen_aux(const Grammar&, const std::string&, std::vector&); 22 | 23 | bool not_url_char(char); 24 | std::string::const_iterator url_beg(std::string::const_iterator, std::string::const_iterator); 25 | std::string::const_iterator url_end(std::string::const_iterator, std::string::const_iterator); 26 | std::vector find_urls(const std::string&); 27 | 28 | #endif // !GUARD_words_h -------------------------------------------------------------------------------- /Chapter08/8_0.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 8-0, 8_0.cpp 3 | 4 | Compile, execute, and test the programs in this chapter. 5 | */ 6 | 7 | #include "stdafx.h" 8 | #include "8_0.h" 9 | 10 | #include 11 | using std::string; 12 | using std::getline; 13 | 14 | #include 15 | using std::vector; 16 | 17 | #include 18 | using std::cin; 19 | using std::cout; 20 | using std::endl; 21 | 22 | #include 23 | using std::copy; 24 | 25 | #include 26 | using std::istream_iterator; 27 | using std::ostream_iterator; 28 | using std::back_inserter; 29 | 30 | #include "vec.h" 31 | #include "words.h" 32 | 33 | int test_split() 34 | { 35 | cout << "Enter lines to split: " << endl; 36 | string s; 37 | while (getline(cin, s)) 38 | split(s, ostream_iterator(cout, "\n")); 39 | return 0; 40 | } 41 | 42 | 43 | void test_stream_iterators() 44 | { 45 | cout << "Enter a few integers: "; 46 | 47 | vector v; 48 | copy(istream_iterator(cin), istream_iterator(), 49 | back_inserter(v)); 50 | cout << endl; 51 | 52 | copy(v.begin(), v.end(), ostream_iterator(cout, " ")); 53 | cout << endl; 54 | } 55 | 56 | void test_median() 57 | { 58 | vector v_double = { 12.5, 16.3, 25.7 }; 59 | cout << median(v_double) << endl; 60 | 61 | vector v_int = { 12, 25, 16, 30 }; 62 | cout << median(v_int) << endl; 63 | } 64 | 65 | int ex8_0() 66 | { 67 | test_median(); 68 | test_stream_iterators(); 69 | cin.clear(); 70 | test_split(); 71 | return 0; 72 | } 73 | -------------------------------------------------------------------------------- /Chapter08/8_0.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_8_0_H 2 | #define GUARD_8_0_H 3 | 4 | int ex8_0(); 5 | 6 | #endif // !GUARD_8_0_H -------------------------------------------------------------------------------- /Chapter08/8_1.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeertArien/c-accelerated/c0ae9f66b1733de04f3133db2e9d8af6d555fe6e/Chapter08/8_1.cpp -------------------------------------------------------------------------------- /Chapter08/8_1.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_8_1_H 2 | #define GUARD_8_1_H 3 | 4 | int ex8_1(); 5 | 6 | #endif // !GUARD_8_1_H -------------------------------------------------------------------------------- /Chapter08/8_2.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeertArien/c-accelerated/c0ae9f66b1733de04f3133db2e9d8af6d555fe6e/Chapter08/8_2.cpp -------------------------------------------------------------------------------- /Chapter08/8_3.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeertArien/c-accelerated/c0ae9f66b1733de04f3133db2e9d8af6d555fe6e/Chapter08/8_3.cpp -------------------------------------------------------------------------------- /Chapter08/8_3.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_8_3_H 2 | #define GUARD_8_3_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | int ex8_3(); 9 | 10 | template 11 | T median_iter(Ran b, Ran e) 12 | { 13 | T count = std::distance(b, e); 14 | 15 | if (count == 0) 16 | throw std::domain_error("median of an empty container"); 17 | 18 | std::sort(b, e); 19 | 20 | T mid = count / 2; 21 | 22 | std::advance(b, mid - 1); 23 | 24 | return count % 2 == 0 ? (*b++ + *b) / 2 : *++b; 25 | } 26 | 27 | #endif // !GUARD_8_3_H -------------------------------------------------------------------------------- /Chapter08/8_4.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeertArien/c-accelerated/c0ae9f66b1733de04f3133db2e9d8af6d555fe6e/Chapter08/8_4.cpp -------------------------------------------------------------------------------- /Chapter08/8_4.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_8_4_H 2 | #define GUARD_8_4_H 3 | 4 | int ex8_4(); 5 | 6 | template 7 | void swap(T &l, T &r) 8 | { 9 | T t = l; 10 | l = r; 11 | r = t; 12 | } 13 | 14 | template 15 | void reversing(Bi begin, Bi end) 16 | { 17 | while (begin != end) 18 | { 19 | --end; 20 | if (begin != end) 21 | swap(*begin++, *end); 22 | // to swap it directly we need to know the 23 | // type of the element the iterator is pointing at 24 | } 25 | } 26 | 27 | #endif // !GUARD_8_4_H -------------------------------------------------------------------------------- /Chapter08/8_5.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_8_5_H 2 | #define GUARD_8_5_H 3 | 4 | int ex8_5(); 5 | 6 | #endif // !GUARD_8_5_H -------------------------------------------------------------------------------- /Chapter08/8_6.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 8-6, 8_6.cpp 3 | 4 | Suppose that m has type map , and that we encounter a call to 5 | copy(m.begin(), m.end(), back_inserter(x)) . What can we say about the 6 | type of x ? What if the call were copy(x.begin(), x.end(), back_inserter(m)) 7 | instead? 8 | */ 9 | 10 | #include "stdafx.h" 11 | #include "8_6.h" 12 | 13 | #include 14 | using std::map; 15 | 16 | #include 17 | using std::string; 18 | 19 | #include 20 | using std::vector; 21 | 22 | #include 23 | using std::back_inserter; 24 | 25 | #include 26 | using std::copy; 27 | 28 | #include 29 | using std::pair; 30 | 31 | int ex8_6() 32 | { 33 | map m = { { 1, "hello" },{ 3, "bye" } }; 34 | 35 | vector> x = { { 1, "thanks" },{ 3, "yo" } }; 36 | 37 | // container that supports output iterator operations, push_back and that holds pairs 38 | copy(m.begin(), m.end(), back_inserter(x)); 39 | 40 | // map doesn't support push_back so back_inserter is an invalid operation 41 | //copy(x.begin(), x.end(), back_inserter(m)); 42 | 43 | return 0; 44 | } -------------------------------------------------------------------------------- /Chapter08/8_6.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_8_6_H 2 | #define GUARD_8_6_H 3 | 4 | int ex8_6(); 5 | 6 | #endif // !GUARD_8_6_H -------------------------------------------------------------------------------- /Chapter08/8_7.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 8-7, 8_7.cpp 3 | 4 | Why doesn't the max function use two template parameters, one for each argument 5 | type? 6 | */ 7 | 8 | #include "stdafx.h" 9 | #include "8_7.h" 10 | 11 | template 12 | O max(const L& left, const R& right) 13 | { 14 | return left > right ? left : right; 15 | } 16 | 17 | // there's no way for the implementation to know what type will be returned when 18 | // two different arguments types are used 19 | // In the example below the return type is specified 20 | int ex8_7() 21 | { 22 | max(5, 4.5); 23 | return 0; 24 | } -------------------------------------------------------------------------------- /Chapter08/8_7.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_8_7_H 2 | #define GUARD_8_7_H 3 | 4 | int ex8_7(); 5 | 6 | #endif // !GUARD_8_7_H -------------------------------------------------------------------------------- /Chapter08/8_8.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeertArien/c-accelerated/c0ae9f66b1733de04f3133db2e9d8af6d555fe6e/Chapter08/8_8.cpp -------------------------------------------------------------------------------- /Chapter08/8_8.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_8_8_H 2 | #define GUARD_8_8_H 3 | 4 | int ex8_8(); 5 | 6 | #endif // !GUARD_8_8_H -------------------------------------------------------------------------------- /Chapter08/Chapter08.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Chapter 08, Chapter08.cpp 3 | 4 | Writing generic functions. 5 | */ 6 | 7 | #include "stdafx.h" 8 | 9 | #include "8_0.h" 10 | #include "8_1.h" 11 | #include "8_2.h" 12 | #include "8_3.h" 13 | #include "8_4.h" 14 | #include "8_5.h" 15 | #include "8_6.h" 16 | #include "8_7.h" 17 | #include "8_8.h" 18 | 19 | int main() 20 | { 21 | return ex8_8(); 22 | } 23 | 24 | -------------------------------------------------------------------------------- /Chapter08/grade.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "stdafx.h" 4 | #include "grade.h" 5 | #include "vec.h" 6 | #include "student_info.h" 7 | 8 | using std::domain_error; using std::vector; 9 | 10 | double grade(double midterm, double final, double homework) 11 | { 12 | return 0.2 *midterm + 0.4 * final + 0.4 * homework; 13 | } 14 | 15 | double grade(double midterm, double final, const std::vector& hw) 16 | { 17 | if (hw.size() == 0) 18 | throw domain_error("Student has done no homework"); 19 | return grade(midterm, final, median(hw)); 20 | } 21 | 22 | double grade(const Student_info& s) 23 | { 24 | return grade(s.midterm, s.final, s.homework); 25 | } -------------------------------------------------------------------------------- /Chapter08/grade.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_grade_h 2 | #define GUARD_grade_h 3 | 4 | #include 5 | #include "student_info.h" 6 | 7 | double grade(double, double, double); 8 | double grade(double, double, const std::vector&); 9 | double grade(const Student_info&); 10 | 11 | #endif // !GUARD_grade_h 12 | -------------------------------------------------------------------------------- /Chapter08/grammar.txt: -------------------------------------------------------------------------------- 1 | cat 2 | dog 3 | table 4 | 5 | 6 | large 7 | brown 8 | absurd 9 | jumps 10 | sits 11 | on the stairs 12 | under the sky 13 | wherever it wants 14 | the 15 | 16 | 17 | 18 | the -> push back 19 | 20 | the -------------------------------------------------------------------------------- /Chapter08/lines.txt: -------------------------------------------------------------------------------- 1 | Peter goes to the bakery. 2 | Peter won the lottery. 3 | Super! This is Super! -------------------------------------------------------------------------------- /Chapter08/numbers.cpp: -------------------------------------------------------------------------------- 1 | #include "stdafx.h" 2 | #include "numbers.h" 3 | 4 | #include 5 | #include 6 | 7 | using std::domain_error; 8 | 9 | int nrand(int n) 10 | { 11 | if (n <= 0 || n > RAND_MAX) 12 | throw domain_error("Argument to nrand is out of range"); 13 | 14 | const int bucket_size = RAND_MAX / n; 15 | int r; 16 | 17 | do r = rand() / bucket_size; 18 | while (r >= n); 19 | 20 | return r; 21 | } -------------------------------------------------------------------------------- /Chapter08/numbers.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_numbers_h 2 | #define GUARD_numbers_h 3 | 4 | int nrand(int); 5 | 6 | #endif // !GUARD_numbers_h 7 | -------------------------------------------------------------------------------- /Chapter08/stdafx.cpp: -------------------------------------------------------------------------------- 1 | // stdafx.cpp : source file that includes just the standard includes 2 | // Chapter08.pch will be the pre-compiled header 3 | // stdafx.obj will contain the pre-compiled type information 4 | 5 | #include "stdafx.h" 6 | 7 | // TODO: reference any additional headers you need in STDAFX.H 8 | // and not in this file 9 | -------------------------------------------------------------------------------- /Chapter08/stdafx.h: -------------------------------------------------------------------------------- 1 | // stdafx.h : include file for standard system include files, 2 | // or project specific include files that are used frequently, but 3 | // are changed infrequently 4 | // 5 | 6 | #pragma once 7 | 8 | #include "targetver.h" 9 | 10 | #include 11 | #include 12 | 13 | 14 | 15 | // TODO: reference additional headers your program requires here 16 | -------------------------------------------------------------------------------- /Chapter08/student_info.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_Student_info 2 | #define GUARD_Student_info 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | struct Student_info 9 | { 10 | std::string name; 11 | std::vector homework; 12 | double midterm; 13 | double final; 14 | }; 15 | 16 | double grade_aux(const Student_info&); 17 | double average_grade(const Student_info&); 18 | double optimistic_median(const Student_info& s); 19 | std::istream& read(std::istream&, std::vector&); 20 | std::vector extract(std::vector&, bool criteria(const Student_info&)); 21 | bool did_all_hw(const Student_info&); 22 | 23 | #endif // !GUARD_student_info 24 | -------------------------------------------------------------------------------- /Chapter08/students10.txt: -------------------------------------------------------------------------------- 1 | Geert 50 80 90 45 30 80 2 | Pieter 40 50 30 0 50 50 40 3 | Lotte 40 80 90 45 60 70 4 | Jan 30 20 80 40 50 30 0 5 | Joris 80 90 60 70 60 70 60 6 | Geert 50 80 90 45 30 80 7 | Pieter 40 50 30 40 50 0 40 8 | Lotte 40 80 90 45 60 70 9 | Jan 30 20 80 0 50 30 40 10 | Joris 80 90 60 70 60 70 60 -------------------------------------------------------------------------------- /Chapter08/targetver.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | // Including SDKDDKVer.h defines the highest available Windows platform. 4 | 5 | // If you wish to build your application for a previous Windows platform, include WinSDKVer.h and 6 | // set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h. 7 | 8 | #include 9 | -------------------------------------------------------------------------------- /Chapter08/vec.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_vec_H 2 | #define GUARD_vec_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | template 10 | T median(std::vector v) 11 | { 12 | typedef typename std::vector::size_type vec_sz; 13 | vec_sz size = v.size(); 14 | if (size == 0) 15 | throw std::domain_error("median of an empty vector"); 16 | std::sort(v.begin(), v.end()); 17 | 18 | vec_sz mid = size / 2; 19 | return size % 2 == 0 ? (v[mid] + v[mid - 1]) / 2 : v[mid]; 20 | } 21 | 22 | inline 23 | double average(const std::vector& v) 24 | { 25 | return std::accumulate(v.begin(), v.end(), 0.0) / v.size(); 26 | } 27 | 28 | #endif // !GUARD_vec_H -------------------------------------------------------------------------------- /Chapter08/words.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_words_H 2 | #define GUARD_words_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | typedef std::vector Rule; 11 | typedef std::vector Rule_collection; 12 | typedef std::map Grammar; 13 | 14 | std::istream count_words(std::istream, std::ostream); 15 | std::vector split(const std::string&); 16 | std::map > xref(std::istream&, std::vector find_words(const std::string&) = split); 17 | bool space(char); 18 | bool not_space(char); 19 | bool bracketed(const std::string&); 20 | Grammar read_grammar(std::istream&); 21 | std::vector gen_sentence(const Grammar&); 22 | void gen_aux(const Grammar&, const std::string&, std::vector&); 23 | 24 | bool not_url_char(char); 25 | std::string::const_iterator url_beg(std::string::const_iterator, std::string::const_iterator); 26 | std::string::const_iterator url_end(std::string::const_iterator, std::string::const_iterator); 27 | std::vector find_urls(const std::string&); 28 | 29 | template // changed 30 | void split(const std::string& str, Out os) { // changed 31 | typedef std::string::const_iterator iter; 32 | iter i = str.begin(); 33 | while (i != str.end()) { 34 | // ignore leading blanks 35 | i = std::find_if(i, str.end(), not_space); 36 | // find end of next word 37 | iter j = std::find_if(i, str.end(), space); 38 | // copy the characters in [i, j) 39 | if (i != str.end()) 40 | *os++ = std::string(i, j); // changed 41 | i = j; 42 | } 43 | } 44 | 45 | #endif // !GUARD_words_H -------------------------------------------------------------------------------- /Chapter09/9_0.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 9-0, 9_0.cpp 3 | 4 | Compile, execute, and test the programs in this chapter. 5 | */ 6 | 7 | #include "stdafx.h" 8 | #include "9_0.h" 9 | 10 | #include 11 | using std::vector; 12 | 13 | #include 14 | using std::string; 15 | 16 | #include 17 | using std::max; 18 | 19 | #include 20 | using std::cin; 21 | using std::cout; 22 | using std::endl; 23 | using std::streamsize; 24 | 25 | #include 26 | using std::setprecision; 27 | 28 | #include 29 | using std::domain_error; 30 | 31 | #include "student_info.h" 32 | 33 | int ex9_0() 34 | { 35 | vector students; 36 | Student_info record; 37 | string::size_type maxlen = 0; 38 | // read and store the data 39 | while (record.read(cin)) { 40 | maxlen = max(maxlen, record.name().size()); 41 | students.push_back(record); 42 | } 43 | // alphabetize the student records 44 | sort(students.begin(), students.end(), compare); 45 | // write the names and grades 46 | for (vector::size_type i = 0; 47 | i != students.size(); ++i) { 48 | cout << students[i].name() 49 | << string(maxlen + 1 - students[i].name().size(), ' '); 50 | try { 51 | double final_grade = students[i].grade(); 52 | streamsize prec = cout.precision(); 53 | cout << setprecision(3) << final_grade 54 | << setprecision(prec) << endl; 55 | } 56 | catch (domain_error e) { 57 | cout << e.what() << endl; 58 | } 59 | } 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /Chapter09/9_0.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_9_0_H 2 | #define GUARD_9_0_H 3 | 4 | int ex9_0(); 5 | 6 | #endif // !GUARD_9_0_H -------------------------------------------------------------------------------- /Chapter09/9_1.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_9_1_H 2 | #define GUARD_9_1_H 3 | 4 | #include 5 | #include 6 | 7 | class Student_info_alt { 8 | 9 | private: 10 | std::string n; 11 | double g; 12 | 13 | public: 14 | Student_info_alt(); 15 | Student_info_alt(std::istream&); 16 | 17 | std::istream& read(std::istream&); 18 | double grade() const { return g; }; 19 | std::string name() const { return n; } 20 | }; 21 | 22 | int ex9_1(); 23 | 24 | #endif // !GUARD_9_1_H -------------------------------------------------------------------------------- /Chapter09/9_2.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_9_2_H 2 | #define GUARD_9_2_H 3 | 4 | #include 5 | #include 6 | 7 | class Student_info_n { 8 | 9 | private: 10 | std::string n; 11 | double g; 12 | 13 | public: 14 | Student_info_n(); 15 | Student_info_n(std::istream&); 16 | 17 | std::istream& read(std::istream&); 18 | double grade() const { return g; }; 19 | std::string name() { return n; } 20 | }; 21 | 22 | int ex9_2(); 23 | 24 | #endif // !GUARD_9_2_H -------------------------------------------------------------------------------- /Chapter09/9_3.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 9-3, 9_3.cpp 3 | 4 | Our grade function was written to throw an exception if a user tried to calculate a 5 | grade for a Student_info object whose values had not yet been read. Users who care 6 | are expected to catch this exception. Write a program that triggers the exception but 7 | does not catch it. Write a program that catches the exception. 8 | */ 9 | 10 | #include "stdafx.h" 11 | #include "9_3.h" 12 | 13 | #include 14 | using std::exception; 15 | 16 | #include 17 | using std::cout; 18 | using std::endl; 19 | 20 | #include "student_info.h" 21 | 22 | int ex9_3() 23 | { 24 | Student_info s; 25 | 26 | try 27 | { 28 | s.grade(); 29 | } 30 | catch (exception e) 31 | { 32 | cout << e.what() << endl; 33 | } 34 | 35 | return 0; 36 | } -------------------------------------------------------------------------------- /Chapter09/9_3.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_9_3_H 2 | #define GUARD_9_3_H 3 | 4 | int ex9_3(); 5 | 6 | #endif // !GUARD_9_3_H -------------------------------------------------------------------------------- /Chapter09/9_4.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 9-4, 9_4.cpp 3 | 4 | Rewrite your program from the previous exercise to use the valid function, 5 | thereby avoiding the exception altogether. 6 | */ 7 | 8 | #include "stdafx.h" 9 | #include "9_4.h" 10 | 11 | #include 12 | using std::cout; 13 | using std::endl; 14 | 15 | #include "student_info.h" 16 | 17 | int ex9_4() 18 | { 19 | Student_info s; 20 | 21 | if (s.valid()) 22 | s.grade(); 23 | else 24 | cout << "Invalid input for student." << endl; 25 | 26 | return 0; 27 | } -------------------------------------------------------------------------------- /Chapter09/9_4.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_9_4_H 2 | #define GUARD_9_4_H 3 | 4 | int ex9_4(); 5 | 6 | #endif // !GUARD_9_4_H -------------------------------------------------------------------------------- /Chapter09/9_5.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_9_5_H 2 | #define GUARD_9_5_H 3 | 4 | #include 5 | #include 6 | 7 | class Student_info_p { 8 | private: 9 | std::string n; 10 | double midterm, final; 11 | 12 | public: 13 | Student_info_p(); 14 | Student_info_p(std::istream&); 15 | 16 | std::istream& read(std::istream&); 17 | double grade() const { return (midterm + final) / 2; }; 18 | std::string name() const { return n; } 19 | bool passing() const { return grade() >= 60; }; 20 | }; 21 | 22 | class Grader 23 | { 24 | private: 25 | std::vector students; 26 | 27 | public: 28 | Grader() {}; 29 | Grader(std::istream&); 30 | 31 | std::ostream& generate_report(std::ostream&) const; 32 | }; 33 | 34 | bool compare_p(const Student_info_p&, const Student_info_p&); 35 | 36 | int ex9_5(); 37 | 38 | #endif // !GUARD_9_5_H -------------------------------------------------------------------------------- /Chapter09/9_6.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_9_6_H 2 | #define GUARD_9_6_H 3 | 4 | #include 5 | #include 6 | 7 | #include "9_5.h" 8 | 9 | class Grader_ext 10 | { 11 | private: 12 | std::vector students; 13 | std::ostream& Grader_ext::generate_report(std::ostream&, 14 | std::vector::const_iterator, 15 | std::vector::const_iterator) const; 16 | 17 | public: 18 | Grader_ext() {}; 19 | Grader_ext(std::istream&); 20 | 21 | std::ostream& generate_report(std::ostream&); 22 | }; 23 | 24 | int ex9_6(); 25 | 26 | #endif // !GUARD_9_6_H -------------------------------------------------------------------------------- /Chapter09/9_7.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeertArien/c-accelerated/c0ae9f66b1733de04f3133db2e9d8af6d555fe6e/Chapter09/9_7.cpp -------------------------------------------------------------------------------- /Chapter09/9_7.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_9_7_H 2 | #define GUARD_9_7_H 3 | 4 | #include 5 | #include 6 | 7 | class Student_info_hw { 8 | 9 | private: 10 | std::string n; 11 | double midterm, final; 12 | std::vector homework; 13 | std::istream& read_hw(std::istream&); 14 | 15 | public: 16 | Student_info_hw(); 17 | Student_info_hw(std::istream&); 18 | 19 | std::istream& read(std::istream&); 20 | double grade() const; 21 | std::string name() const { return n; } 22 | }; 23 | 24 | int ex9_7(); 25 | 26 | #endif // !GUARD_9_7_H -------------------------------------------------------------------------------- /Chapter09/Chapter09.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Chapter 09, Chapter09.cpp 3 | 4 | Defining new types. 5 | */ 6 | 7 | #include "stdafx.h" 8 | 9 | #include "9_0.h" 10 | #include "9_1.h" 11 | #include "9_2.h" 12 | #include "9_3.h" 13 | #include "9_4.h" 14 | #include "9_5.h" 15 | #include "9_6.h" 16 | #include "9_7.h" 17 | 18 | int main() 19 | { 20 | return ex9_7(); 21 | } -------------------------------------------------------------------------------- /Chapter09/stdafx.cpp: -------------------------------------------------------------------------------- 1 | // stdafx.cpp : source file that includes just the standard includes 2 | // Chapter09.pch will be the pre-compiled header 3 | // stdafx.obj will contain the pre-compiled type information 4 | 5 | #include "stdafx.h" 6 | 7 | // TODO: reference any additional headers you need in STDAFX.H 8 | // and not in this file 9 | -------------------------------------------------------------------------------- /Chapter09/stdafx.h: -------------------------------------------------------------------------------- 1 | // stdafx.h : include file for standard system include files, 2 | // or project specific include files that are used frequently, but 3 | // are changed infrequently 4 | // 5 | 6 | #pragma once 7 | 8 | #include "targetver.h" 9 | 10 | #include 11 | #include 12 | 13 | 14 | 15 | // TODO: reference additional headers your program requires here 16 | -------------------------------------------------------------------------------- /Chapter09/student_info.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_student_info_H 2 | #define GUARD_student_info_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | class Student_info { 9 | 10 | private: 11 | std::string n; 12 | double midterm, final; 13 | std::vector homework; 14 | 15 | public: 16 | Student_info(); 17 | Student_info(std::istream&); 18 | 19 | std::istream& read(std::istream&); 20 | double grade() const; 21 | std::string name() const { return n; } 22 | bool valid() const { return !homework.empty(); } 23 | }; 24 | 25 | bool compare(const Student_info&, const Student_info&); 26 | std::istream& read_hw(std::istream&, std::vector&); 27 | double grade(double midterm, double final, const std::vector& hw); 28 | double grade(double midterm, double final, double homework); 29 | double median(std::vector); 30 | 31 | #endif // !GUARD_student_info_H -------------------------------------------------------------------------------- /Chapter09/targetver.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | // Including SDKDDKVer.h defines the highest available Windows platform. 4 | 5 | // If you wish to build your application for a previous Windows platform, include WinSDKVer.h and 6 | // set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h. 7 | 8 | #include 9 | -------------------------------------------------------------------------------- /Chapter10/10_0.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_10_0_H 2 | #define GUARD_10_0_H 3 | 4 | #include 5 | 6 | int ex10_0(); 7 | std::string letter_grade(double); 8 | 9 | #endif // !GUARD_10_0_H -------------------------------------------------------------------------------- /Chapter10/10_1.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeertArien/c-accelerated/c0ae9f66b1733de04f3133db2e9d8af6d555fe6e/Chapter10/10_1.cpp -------------------------------------------------------------------------------- /Chapter10/10_1.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_10_1_H 2 | #define GUARD_10_1_H 3 | 4 | int ex10_1(); 5 | 6 | #endif // !GUARD_10_1_H -------------------------------------------------------------------------------- /Chapter10/10_2.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeertArien/c-accelerated/c0ae9f66b1733de04f3133db2e9d8af6d555fe6e/Chapter10/10_2.cpp -------------------------------------------------------------------------------- /Chapter10/10_2.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_10_2_H 2 | #define GUARD_10_2_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | int ex10_2(); 9 | 10 | template 11 | T median(In begin, const In end) 12 | { 13 | if (begin == end) 14 | throw std::domain_error("median of an empty container"); 15 | 16 | std::vector v; 17 | 18 | while (begin != end) 19 | v.push_back(*begin++); 20 | 21 | std::sort(v.begin(), v.end()); 22 | 23 | std::vector::size_type mid = v.size() / 2; 24 | return v.size() % 2 == 0 ? (v[mid] + v[mid - 1]) / 2 : v[mid]; 25 | } 26 | 27 | #endif // !GUARD_10_2_H -------------------------------------------------------------------------------- /Chapter10/10_3.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 10-3, 10_3.cpp 3 | 4 | Write a test program to verify that the median function operates correctly. 5 | Ensure that calling median does not change the order of the elements in the container. 6 | */ 7 | 8 | #include "stdafx.h" 9 | #include "10_3.h" 10 | 11 | #include "10_2.h" 12 | 13 | #include 14 | using std::vector; 15 | 16 | #include 17 | using std::cout; 18 | using std::endl; 19 | using std::ostream; 20 | 21 | template 22 | ostream& print_vector(const vector v, ostream& os) 23 | { 24 | if (v.size() > 0) 25 | { 26 | os << v[0]; 27 | 28 | for (vector::size_type i = 1; i < v.size(); i++) 29 | os << ", " << v[i]; 30 | 31 | os << endl; 32 | } 33 | 34 | return os; 35 | } 36 | 37 | int ex10_3() 38 | { 39 | vector v_double = { 12.5, 5.25, 25.7, 16.3, 1.26 }; 40 | 41 | print_vector(v_double, cout); 42 | 43 | cout << median::iterator>(v_double.begin(), v_double.end()) << endl; 44 | 45 | print_vector(v_double, cout); 46 | 47 | return 0; 48 | } -------------------------------------------------------------------------------- /Chapter10/10_3.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_10_3_H 2 | #define GUARD_10_3_H 3 | 4 | int ex10_3(); 5 | 6 | #endif // !GUARD_10_3_H -------------------------------------------------------------------------------- /Chapter10/10_4.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 10-4, 10_4.cpp 3 | 4 | Write a class that implements a list that holds strings. 5 | */ 6 | 7 | #include "stdafx.h" 8 | #include "10_4.h" 9 | 10 | #include 11 | using std::string; 12 | 13 | #include 14 | using std::cout; 15 | using std::endl; 16 | 17 | #include 18 | using std::copy; 19 | 20 | String_list::String_list(const String_list& src) { 21 | *this = src; 22 | l = new string[max]; 23 | 24 | for (string::size_type i = 0; i < size; i++) 25 | l[i] = src.l[i]; 26 | } 27 | 28 | String_list::~String_list() 29 | { 30 | delete[] l; 31 | } 32 | 33 | void String_list::resize() 34 | { 35 | max *= 2; 36 | 37 | string* copy = new string[max]; 38 | 39 | for (string::size_type i = 0; i < size; i++) 40 | copy[i] = l[i]; 41 | 42 | delete[] l; 43 | 44 | l = copy; 45 | } 46 | 47 | void String_list::push_back(string str) 48 | { 49 | if (size == max) 50 | resize(); 51 | 52 | l[size++] = str; 53 | } 54 | 55 | int ex10_4() 56 | { 57 | String_list str_list; 58 | 59 | str_list.push_back("test"); 60 | str_list.push_back("more"); 61 | str_list.push_back("testing"); 62 | str_list.push_back("even more"); 63 | str_list.push_back("yup"); 64 | 65 | String_list::iterator iter = str_list.begin(); 66 | 67 | while (iter != str_list.end()) 68 | cout << *iter++ << endl; 69 | 70 | return 0; 71 | } -------------------------------------------------------------------------------- /Chapter10/10_4.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_10_4_H 2 | #define GUARD_10_4_H 3 | 4 | #include 5 | 6 | class String_list { 7 | 8 | private: 9 | 10 | std::string *l; 11 | std::string::size_type size; 12 | std::string::size_type max; 13 | 14 | void resize(); 15 | 16 | public: 17 | 18 | typedef std::string* iterator; 19 | 20 | String_list() : l(new std::string[1]), size(0), max(1) {}; 21 | String_list(const String_list& src); 22 | ~String_list(); 23 | 24 | void push_back(std::string); 25 | std::string* begin() { return l; } 26 | std::string* end() { return l + size; }; 27 | }; 28 | 29 | int ex10_4(); 30 | 31 | #endif // !GUARD_10_4_H -------------------------------------------------------------------------------- /Chapter10/10_5.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 10-5, 10_5.cpp 3 | 4 | Write a bidirectional iterator for your String_list class. 5 | */ 6 | 7 | #include "stdafx.h" 8 | #include "10_5.h" 9 | 10 | #include "10_4.h" 11 | 12 | #include 13 | using std::cout; 14 | using std::endl; 15 | 16 | #include 17 | using std::string; 18 | 19 | int ex10_5() 20 | { 21 | String_list str_list; 22 | 23 | str_list.push_back("one"); 24 | str_list.push_back("two"); 25 | str_list.push_back("three"); 26 | str_list.push_back("four"); 27 | str_list.push_back("five"); 28 | 29 | String_list::iterator iter = str_list.begin(); 30 | 31 | while (iter != str_list.end()) 32 | { 33 | cout << *iter << endl; 34 | *iter++ = "reverse"; 35 | } 36 | 37 | while (iter != str_list.begin()) 38 | cout << *(--iter) << endl; 39 | 40 | return 0; 41 | } -------------------------------------------------------------------------------- /Chapter10/10_5.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_10_5_H 2 | #define GUARD_10_5_H 3 | 4 | int ex10_5(); 5 | 6 | #endif // !GUARD_10_5_H -------------------------------------------------------------------------------- /Chapter10/10_6.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 10-6, 10_6.cpp 3 | 4 | Test the class by rewriting the split function to put its output into a 5 | String_list. 6 | */ 7 | 8 | #include "stdafx.h" 9 | #include "10_6.h" 10 | 11 | #include 12 | using std::string; 13 | 14 | #include 15 | using std::find_if; 16 | 17 | #include 18 | using std::cout; 19 | using std::endl; 20 | 21 | #include "10_4.h" 22 | 23 | bool space(char c) 24 | { 25 | return isspace(c) != 0; 26 | } 27 | 28 | bool not_space(char c) 29 | { 30 | return !isspace(c); 31 | } 32 | 33 | String_list split(const string& str) 34 | { 35 | typedef string::const_iterator iter; 36 | String_list ret; 37 | 38 | iter i = str.begin(); 39 | while (i != str.end()) 40 | { 41 | 42 | i = find_if(i, str.end(), not_space); 43 | 44 | iter j = find_if(i, str.end(), space); 45 | 46 | if (i != str.end()) 47 | ret.push_back(string(i, j)); 48 | i = j; 49 | } 50 | return ret; 51 | } 52 | 53 | int ex10_6() 54 | { 55 | string str = "Please split this amazing string!"; 56 | String_list str_l = split(str); 57 | String_list::iterator iter = str_l.begin(); 58 | 59 | while (iter != str_l.end()) 60 | cout << *iter++ << endl; 61 | 62 | return 0; 63 | } -------------------------------------------------------------------------------- /Chapter10/10_6.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_10_6_H 2 | #define GUARD_10_5_H 3 | 4 | int ex10_6(); 5 | 6 | #endif // !GUARD_10_6_H -------------------------------------------------------------------------------- /Chapter10/Chapter10.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Chapter 10, Chapter10.cpp 3 | 4 | Managing memory and low-level data structures. 5 | */ 6 | 7 | #include "stdafx.h" 8 | 9 | #include "10_0.h" 10 | #include "10_1.h" 11 | #include "10_2.h" 12 | #include "10_3.h" 13 | #include "10_4.h" 14 | #include "10_5.h" 15 | #include "10_6.h" 16 | 17 | int main() 18 | { 19 | ex10_4(); 20 | return 0; 21 | } 22 | 23 | -------------------------------------------------------------------------------- /Chapter10/in.txt: -------------------------------------------------------------------------------- 1 | hello sir 2 | how are you 3 | doing today? -------------------------------------------------------------------------------- /Chapter10/out.txt: -------------------------------------------------------------------------------- 1 | hello sir 2 | how are you 3 | doing today? 4 | -------------------------------------------------------------------------------- /Chapter10/stdafx.cpp: -------------------------------------------------------------------------------- 1 | // stdafx.cpp : source file that includes just the standard includes 2 | // Chapter10.pch will be the pre-compiled header 3 | // stdafx.obj will contain the pre-compiled type information 4 | 5 | #include "stdafx.h" 6 | 7 | // TODO: reference any additional headers you need in STDAFX.H 8 | // and not in this file 9 | -------------------------------------------------------------------------------- /Chapter10/stdafx.h: -------------------------------------------------------------------------------- 1 | // stdafx.h : include file for standard system include files, 2 | // or project specific include files that are used frequently, but 3 | // are changed infrequently 4 | // 5 | 6 | #pragma once 7 | 8 | #include "targetver.h" 9 | 10 | #include 11 | #include 12 | 13 | 14 | 15 | // TODO: reference additional headers your program requires here 16 | -------------------------------------------------------------------------------- /Chapter10/student_info.cpp: -------------------------------------------------------------------------------- 1 | #include "stdafx.h" 2 | #include "student_info.h" 3 | 4 | #include 5 | using std::istream; 6 | 7 | #include 8 | using std::vector; 9 | 10 | #include 11 | using std::domain_error; 12 | 13 | #include 14 | using std::sort; 15 | 16 | Student_info::Student_info() : midterm(0), final(0) { } 17 | 18 | Student_info::Student_info(istream& is) { read(is); } 19 | 20 | istream& Student_info::read(istream& in) 21 | { 22 | in >> n >> midterm >> final; 23 | read_hw(in, homework); 24 | return in; 25 | } 26 | 27 | double Student_info::grade() const 28 | { 29 | return ::grade(midterm, final, homework); 30 | } 31 | 32 | istream& read_hw(istream& is, vector& hw) 33 | { 34 | if (is) 35 | { 36 | is.clear(); 37 | 38 | double x; 39 | while (is >> x) 40 | hw.push_back(x); 41 | 42 | is.clear(); 43 | } 44 | 45 | return is; 46 | } 47 | 48 | double grade(double midterm, double final, const std::vector& hw) 49 | { 50 | if (hw.size() == 0) 51 | throw domain_error("Student has done no homework"); 52 | return grade(midterm, final, median(hw)); 53 | } 54 | 55 | double grade(double midterm, double final, double homework) 56 | { 57 | return 0.2 *midterm + 0.4 * final + 0.4 * homework; 58 | } 59 | 60 | bool compare(const Student_info& x, const Student_info& y) 61 | { 62 | return x.name() < y.name(); 63 | } 64 | 65 | double median(vector vec) 66 | { 67 | typedef vector::size_type vec_sz; 68 | 69 | vec_sz size = vec.size(); 70 | if (size == 0) 71 | throw domain_error("median of empty vector"); 72 | 73 | sort(vec.begin(), vec.end()); 74 | vec_sz mid = size / 2; 75 | 76 | return size % 2 == 0 ? (vec[mid] + vec[mid - 1]) / 2 : vec[mid]; 77 | } -------------------------------------------------------------------------------- /Chapter10/student_info.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_student_info_H 2 | #define GUARD_student_info_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | class Student_info { 9 | 10 | private: 11 | std::string n; 12 | double midterm, final; 13 | std::vector homework; 14 | 15 | public: 16 | Student_info(); 17 | Student_info(std::istream&); 18 | 19 | std::istream& read(std::istream&); 20 | double grade() const; 21 | std::string name() const { return n; } 22 | bool valid() const { return !homework.empty(); } 23 | }; 24 | 25 | bool compare(const Student_info&, const Student_info&); 26 | std::istream& read_hw(std::istream&, std::vector&); 27 | double grade(double midterm, double final, const std::vector& hw); 28 | double grade(double midterm, double final, double homework); 29 | double median(std::vector); 30 | 31 | #endif // !GUARD_student_info_H -------------------------------------------------------------------------------- /Chapter10/targetver.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | // Including SDKDDKVer.h defines the highest available Windows platform. 4 | 5 | // If you wish to build your application for a previous Windows platform, include WinSDKVer.h and 6 | // set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h. 7 | 8 | #include 9 | -------------------------------------------------------------------------------- /Chapter11/11_0.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 11-0, 11_0.cpp 3 | 4 | Compile, execute, and test the programs in this chapter. 5 | */ 6 | 7 | #include "stdafx.h" 8 | #include "11_0.h" 9 | 10 | #include 11 | using std::ostream; 12 | using std::cout; 13 | using std::endl; 14 | 15 | #include "vec.h" 16 | 17 | int ex11_0() 18 | { 19 | Vec v1(5, 5); 20 | Vec v2; 21 | 22 | v2 = v1; 23 | 24 | for (Vec::size_type i = 2; i < v1.size(); i++) 25 | v1[i] = 7; 26 | 27 | for (Vec::size_type i = 0; i < 10; i++) 28 | v2.push_back(10); 29 | 30 | v1.print_vec(cout); 31 | v2.print_vec(cout); 32 | 33 | return 0; 34 | } -------------------------------------------------------------------------------- /Chapter11/11_0.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_11_0_H 2 | #define GUARD_11_0_H 3 | 4 | int ex11_0(); 5 | 6 | #endif // !GUARD_11_0_H -------------------------------------------------------------------------------- /Chapter11/11_5.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_11_5_H 2 | #define GUARD_11_5_H 3 | 4 | int ex11_5(); 5 | 6 | #endif // !GUARD_11_5_H -------------------------------------------------------------------------------- /Chapter11/11_6.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_11_6_H 2 | #define GUARD_11_6_H 3 | 4 | int ex11_6(); 5 | 6 | #endif // !GUARD_11_6_H -------------------------------------------------------------------------------- /Chapter11/11_7.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_11_7_H 2 | #define GUARD_11_7_H 3 | 4 | #include "student_info.h" 5 | #include "vec.h" 6 | 7 | class Grader 8 | { 9 | 10 | private: 11 | Vec students; 12 | std::ostream& Grader::generate_report(std::ostream&, 13 | Vec::const_iterator, 14 | Vec::const_iterator) const; 15 | 16 | public: 17 | Grader() {}; 18 | Grader(std::istream&); 19 | 20 | std::ostream& generate_report(std::ostream&); 21 | }; 22 | 23 | 24 | int ex11_7(); 25 | 26 | #endif // !GUARD_11_7_H -------------------------------------------------------------------------------- /Chapter11/11_8.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 11-8, 11_8.cpp 3 | 4 | Write a simplified version of the standard list class and its associated iterator. 5 | */ 6 | 7 | #include "stdafx.h" 8 | #include "11_8.h" 9 | 10 | #include 11 | using std::cout; 12 | using std::endl; 13 | 14 | #include 15 | using std::string; 16 | 17 | int ex11_8() 18 | { 19 | LList list; 20 | list.push_back(5); 21 | list.push_back(10); 22 | list.push_back(15); 23 | 24 | LList::iterator iter = list.begin(); 25 | 26 | cout << *++iter << endl; 27 | cout << *--iter << endl; 28 | 29 | while (iter != list.end()) 30 | cout << *iter++ << endl; 31 | 32 | LList stringlist(5, "test"); 33 | LList stringlist_copy = stringlist; 34 | LList::iterator listIter = stringlist.begin(); 35 | 36 | *listIter = "first"; 37 | 38 | while (listIter != stringlist.end()) 39 | cout << *listIter++ << endl; 40 | 41 | if (stringlist_copy.size() > 0) { 42 | listIter = stringlist_copy.begin(); 43 | while (listIter != stringlist.end()) 44 | cout << *listIter++ << endl; 45 | } 46 | 47 | return 0; 48 | } -------------------------------------------------------------------------------- /Chapter11/11_9.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeertArien/c-accelerated/c0ae9f66b1733de04f3133db2e9d8af6d555fe6e/Chapter11/11_9.cpp -------------------------------------------------------------------------------- /Chapter11/11_9.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_11_9_H 2 | #define GUARD_11_9_H 3 | 4 | int ex11_9(); 5 | 6 | #endif // !GUARD_11_9_H -------------------------------------------------------------------------------- /Chapter11/Chapter11.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Chapter 11, Chapter11.cpp 3 | 4 | Defining abstract data types 5 | */ 6 | 7 | #include "stdafx.h" 8 | 9 | #include "11_0.h" 10 | #include "11_5.h" 11 | #include "11_6.h" 12 | #include "11_7.h" 13 | #include "11_8.h" 14 | #include "11_9.h" 15 | 16 | int main() 17 | { 18 | ex11_9(); 19 | 20 | return 0; 21 | } 22 | 23 | -------------------------------------------------------------------------------- /Chapter11/stdafx.cpp: -------------------------------------------------------------------------------- 1 | // stdafx.cpp : source file that includes just the standard includes 2 | // Chapter11.pch will be the pre-compiled header 3 | // stdafx.obj will contain the pre-compiled type information 4 | 5 | #include "stdafx.h" 6 | 7 | // TODO: reference any additional headers you need in STDAFX.H 8 | // and not in this file 9 | -------------------------------------------------------------------------------- /Chapter11/stdafx.h: -------------------------------------------------------------------------------- 1 | // stdafx.h : include file for standard system include files, 2 | // or project specific include files that are used frequently, but 3 | // are changed infrequently 4 | // 5 | 6 | #pragma once 7 | 8 | #include "targetver.h" 9 | 10 | #include 11 | #include 12 | 13 | 14 | 15 | // TODO: reference additional headers your program requires here 16 | -------------------------------------------------------------------------------- /Chapter11/student_info.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_student_info_H 2 | #define GUARD_student_info_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | class Student_info { 10 | 11 | private: 12 | std::string n; 13 | double midterm, final; 14 | std::vector homework; 15 | 16 | static int default_constructor_calls; 17 | static int stream_constructor_calls; 18 | static int copy_constructor_calls; 19 | static int assignment_calls; 20 | static int destructor_calls; 21 | 22 | public: 23 | Student_info(); 24 | Student_info(std::istream&); 25 | Student_info(const Student_info&); 26 | 27 | Student_info& operator=(const Student_info&); 28 | ~Student_info(); 29 | 30 | std::istream& read(std::istream&); 31 | double grade() const; 32 | std::string name() const { return n; } 33 | bool valid() const { return !homework.empty(); } 34 | 35 | static void reset_class_info(); 36 | static std::ostream& print_class_info(std::ostream&); 37 | }; 38 | 39 | bool compare(const Student_info&, const Student_info&); 40 | std::istream& read_hw(std::istream&, std::vector&); 41 | double grade(double midterm, double final, const std::vector& hw); 42 | double grade(double midterm, double final, double homework); 43 | double median(std::vector); 44 | std::vector extract_fails(std::vector&); 45 | std::list extract_fails(std::list&); 46 | std::istream& read(std::istream&, std::vector&); 47 | std::istream& read(std::istream&, std::list&); 48 | 49 | #endif // !GUARD_student_info_H -------------------------------------------------------------------------------- /Chapter11/students10.txt: -------------------------------------------------------------------------------- 1 | Geert 50 80 90 45 30 80 2 | Pieter 40 50 30 40 50 50 40 3 | Lotte 40 80 90 45 60 70 4 | Jan 30 20 80 40 50 30 40 5 | Joris 80 90 60 70 60 70 60 6 | Geert 50 80 90 45 30 80 7 | Pieter 40 50 30 40 50 50 40 8 | Lotte 40 80 90 45 60 70 9 | Jan 30 20 80 40 50 30 40 10 | Joris 80 90 60 70 60 70 60 -------------------------------------------------------------------------------- /Chapter11/targetver.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | // Including SDKDDKVer.h defines the highest available Windows platform. 4 | 5 | // If you wish to build your application for a previous Windows platform, include WinSDKVer.h and 6 | // set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h. 7 | 8 | #include 9 | -------------------------------------------------------------------------------- /Chapter12/12_0.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 12-0, 12_0.cpp 3 | 4 | Compile, execute, and test the programs in this chapter. 5 | */ 6 | 7 | #include "stdafx.h" 8 | #include "12_0.h" 9 | 10 | #include 11 | using std::cin; 12 | using std::cout; 13 | using std::endl; 14 | 15 | #include "str.h" 16 | 17 | int ex12_0() 18 | { 19 | Str s("hello"); // construct s 20 | Str t = "hello"; // initialize t 21 | s = "hello"; // assign a new value to s 22 | Str s1 = "test"; 23 | Str s2 = "ing"; 24 | 25 | cout << "Enter a string: "; 26 | cin >> s; // use the input operator to read a string 27 | cout << s << endl; // use the output operator to write a string 28 | s[3]; // use the index operator to access a character 29 | s1 + s2; // use the addition operator to concatenate two strings 30 | 31 | Str name = "Geert"; 32 | Str greeting = "Hello, " + name + "!"; 33 | 34 | Str temp1("Hello, "); // Str::Str(const char*) 35 | Str temp2 = temp1 + name; // operator*(const Str&, const Str&) 36 | Str temp3("!"); // Str::Str(const char*) 37 | greeting = temp2 + temp3; // operator*(const Str&, const Str&) 38 | 39 | return 0; 40 | } -------------------------------------------------------------------------------- /Chapter12/12_0.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_12_0_H 2 | #define GUARD_12_0_H 3 | 4 | int ex12_0(); 5 | 6 | #endif // !GUARD_12_0_H -------------------------------------------------------------------------------- /Chapter12/12_1.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 12-1, 12_1.cpp 3 | 4 | Reimplement the Str class, but choose an implementation strategy that requires 5 | that the class manage the storage itself. For example, you might store an array of char 6 | and a length. Consider what implications this change in design has for copy control. Also 7 | consider the cost of using Vec, (e.g., in storage overhead). 8 | */ 9 | 10 | #include "stdafx.h" 11 | #include "12_1.h" 12 | #include "str_c.h" 13 | 14 | #include 15 | using std::cout; 16 | using std::cin; 17 | using std::endl; 18 | 19 | int ex12_1() 20 | { 21 | Str_c s("hello"); // construct s 22 | Str_c t = "hello"; // initialize t 23 | s = "hello"; // assign a new value to s 24 | Str_c s1 = "test"; 25 | Str_c s2 = "ing"; 26 | 27 | cout << "Enter a string: "; 28 | cin >> s; // use the input operator to read a string 29 | cout << s << endl; // use the output operator to write a string 30 | s[3]; // use the index operator to access a character 31 | s1 + s2; // use the addition operator to concatenate two strings 32 | 33 | Str_c name = "Geert"; 34 | Str_c greeting = "Hello, " + name + "!"; 35 | 36 | Str_c temp1("Hello, "); // Str::Str(const char*) 37 | Str_c temp2 = temp1 + name; // operator*(const Str&, const Str&) 38 | Str_c temp3("!"); // Str::Str(const char*) 39 | greeting = temp2 + temp3; // operator*(const Str&, const Str&) 40 | 41 | return 0; 42 | } -------------------------------------------------------------------------------- /Chapter12/12_1.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_12_1_H 2 | #define GUARD_12_1_H 3 | 4 | int ex12_1(); 5 | 6 | #endif // !GUARD_12_1_H -------------------------------------------------------------------------------- /Chapter12/12_10.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeertArien/c-accelerated/c0ae9f66b1733de04f3133db2e9d8af6d555fe6e/Chapter12/12_10.cpp -------------------------------------------------------------------------------- /Chapter12/12_10.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_12_10_H 2 | #define GUARD_12_10_H 3 | 4 | int ex12_10(); 5 | 6 | #endif // !GUARD_12_10_H -------------------------------------------------------------------------------- /Chapter12/12_11.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeertArien/c-accelerated/c0ae9f66b1733de04f3133db2e9d8af6d555fe6e/Chapter12/12_11.cpp -------------------------------------------------------------------------------- /Chapter12/12_11.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_12_11_H 2 | #define GUARD_12_11_H 3 | 4 | int ex12_11(); 5 | 6 | #endif // !GUARD_12_11_H -------------------------------------------------------------------------------- /Chapter12/12_12.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 12_12, 12_12.cpp 3 | 4 | Define the insert function that takes two iterators for the Vec and Str 5 | classes. 6 | */ 7 | 8 | #include "stdafx.h" 9 | #include "12_12.h" 10 | 11 | #include 12 | using std::cout; 13 | using std::endl; 14 | 15 | #include "str.h" 16 | 17 | int ex12_12() 18 | { 19 | Str greeting = "Hello , how are you?"; 20 | Str name = "Geert"; 21 | 22 | greeting.insert(greeting.begin() + 6, name.begin(), name.end()); 23 | 24 | cout << greeting << endl; 25 | 26 | return 0; 27 | } -------------------------------------------------------------------------------- /Chapter12/12_12.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_12_12_H 2 | #define GUARD_12_12_H 3 | 4 | int ex12_12(); 5 | 6 | #endif // !GUARD_12_12_H -------------------------------------------------------------------------------- /Chapter12/12_13.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 12_13, 12_13.cpp 3 | 4 | Provide an assign function that could be used to assign the values in an array 5 | to a Vec. 6 | */ 7 | 8 | #include "stdafx.h" 9 | #include "12_13.h" 10 | 11 | #include 12 | using std::cout; 13 | using std::endl; 14 | 15 | #include "vec.h" 16 | 17 | int ex12_13() 18 | { 19 | Vec v; 20 | 21 | char data[] = { 'h', 'e', 'l', 'l', 'o' }; 22 | v.assign(data, data + sizeof(data)); 23 | 24 | for (size_t i = 0; i < v.size(); i++) 25 | cout << v[i]; 26 | 27 | cout << endl; 28 | 29 | return 0; 30 | } -------------------------------------------------------------------------------- /Chapter12/12_13.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_12_13_H 2 | #define GUARD_12_13_H 3 | 4 | int ex12_13(); 5 | 6 | #endif // !GUARD_12_13_H -------------------------------------------------------------------------------- /Chapter12/12_14.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 12_14, 12_14.cpp 3 | 4 | Write a program to initialize a Vec from a string. 5 | */ 6 | 7 | #include "stdafx.h" 8 | #include "12_14.h" 9 | 10 | #include 11 | using std::cout; 12 | using std::endl; 13 | 14 | #include 15 | using std::string; 16 | 17 | #include "vec.h" 18 | 19 | int ex12_14() 20 | { 21 | string s = "A lonesome string."; 22 | 23 | Vec v(s.begin(), s.end()); 24 | 25 | for (size_t i = 0; i < v.size(); i++) 26 | cout << v[i]; 27 | 28 | cout << endl; 29 | 30 | return 0; 31 | } -------------------------------------------------------------------------------- /Chapter12/12_14.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_12_14_H 2 | #define GUARD_12_14_H 3 | 4 | int ex12_14(); 5 | 6 | #endif // !GUARD_12_14_H -------------------------------------------------------------------------------- /Chapter12/12_2.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 12-2, 12_2.cpp 3 | 4 | Implement the c_str, data, and copy functions. 5 | */ 6 | 7 | #include "stdafx.h" 8 | #include "12_2.h" 9 | 10 | #include 11 | using std::cout; 12 | using std::endl; 13 | 14 | #include "str_c.h" 15 | 16 | int ex12_2() 17 | { 18 | Str_c str = "Aviation industry"; 19 | const char *data = str.data(); 20 | 21 | for (size_t i = 0; i < str.size(); i++) 22 | cout << data[i]; 23 | cout << endl; 24 | 25 | const char *c = str.c_str(); 26 | 27 | cout << c << endl; 28 | 29 | char copied[8]; 30 | str.copy(copied, sizeof(copied)); 31 | 32 | for (size_t i = 0; i < sizeof(copied); i++) 33 | cout << copied[i]; 34 | cout << endl; 35 | 36 | return 0; 37 | } -------------------------------------------------------------------------------- /Chapter12/12_2.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_12_2_H 2 | #define GUARD_12_2_H 3 | 4 | int ex12_2(); 5 | 6 | #endif // !GUARD_12_2_H -------------------------------------------------------------------------------- /Chapter12/12_3.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 12-3, 12_3.cpp 3 | 4 | Define the relational operators for Str. In doing so, you will want to know that 5 | the header defines a function called strcmp, which compares two character 6 | pointers. The function returns a negative integer if the null-terminated character array 7 | denoted by the first pointer is less than the second, zero if the two strings are equal, or 8 | a positive value if the first string is greater than the second. 9 | */ 10 | 11 | #include "stdafx.h" 12 | #include "12_3.h" 13 | 14 | #include 15 | using std::cout; 16 | using std::endl; 17 | 18 | #include "str_c.h" 19 | 20 | int ex12_3() 21 | { 22 | Str_c short_str = "audio"; 23 | Str_c default_str1 = "Aviation"; 24 | Str_c default_str2 = "aviation"; 25 | Str_c long_str = "aviation industry"; 26 | 27 | cout << short_str << " is smaller than " << default_str2 << " ? " << (short_str < default_str2) << endl; 28 | cout << long_str << " is smaller or equal to " << default_str2 << " ? " << (long_str <= default_str2) << endl; 29 | cout << short_str << " is bigger than " << long_str << " ? " << (short_str > long_str) << endl; 30 | cout << long_str << " is bigger or equal to " << long_str << " ? " << (long_str >= long_str) << endl; 31 | 32 | return 0; 33 | } -------------------------------------------------------------------------------- /Chapter12/12_3.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_12_3_H 2 | #define GUARD_12_3_H 3 | 4 | int ex12_3(); 5 | 6 | #endif // !GUARD_12_3_H -------------------------------------------------------------------------------- /Chapter12/12_4.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 12-4, 12_4.cpp 3 | 4 | Define the equality and inequality operators for Str. 5 | */ 6 | 7 | #include "stdafx.h" 8 | #include "12_4.h" 9 | 10 | #include 11 | using std::cout; 12 | using std::endl; 13 | 14 | #include "str_c.h" 15 | 16 | int ex12_4() 17 | { 18 | Str_c str1 = "Aviation"; 19 | Str_c str2 = "aviation"; 20 | Str_c str3 = "Aviation"; 21 | 22 | cout << str1 << " is equal to " << str2 << " ? " << (str1 == str2) << endl; 23 | cout << str2 << " is not equal to " << str3 << " ? " << (str2 != str3) << endl; 24 | cout << str1 << " is equal to " << str3 << " ? " << (str1 == str3) << endl; 25 | 26 | return 0; 27 | } -------------------------------------------------------------------------------- /Chapter12/12_4.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_12_4_H 2 | #define GUARD_12_4_H 3 | 4 | int ex12_4(); 5 | 6 | #endif // !GUARD_12_4_H -------------------------------------------------------------------------------- /Chapter12/12_5.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 12-5, 12_5.cpp 3 | 4 | Implement concatenation for Str so as not to rely on conversions from const 5 | char*. 6 | */ 7 | 8 | #include "stdafx.h" 9 | #include "12_5.h" 10 | 11 | #include 12 | using std::cout; 13 | using std::endl; 14 | 15 | #include "str_c.h" 16 | 17 | int ex12_5() 18 | { 19 | Str_c name = "Geert"; 20 | Str_c greeting = "Hello, " + name; 21 | 22 | cout << greeting << endl; 23 | 24 | Str_c goodbye = name + ", bye!"; 25 | 26 | cout << goodbye << endl; 27 | 28 | Str_c complete = "Hello, " + name + " and goodbye!"; 29 | 30 | cout << complete << endl; 31 | 32 | return 0; 33 | } -------------------------------------------------------------------------------- /Chapter12/12_5.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_12_5_H 2 | #define GUARD_12_5_H 3 | 4 | int ex12_5(); 5 | 6 | #endif // !GUARD_12_5_H -------------------------------------------------------------------------------- /Chapter12/12_6.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 12-6, 12_6.cpp 3 | 4 | Give Str an operation that will let us implicitly use a Str object as a condition. 5 | The test should fail if the Str is empty, and should succeed otherwise. 6 | */ 7 | 8 | #include "stdafx.h" 9 | #include "12_6.h" 10 | 11 | #include 12 | using std::cin; 13 | using std::cout; 14 | using std::endl; 15 | 16 | #include "str_c.h" 17 | 18 | int ex12_6() 19 | { 20 | Str_c name = "Geert"; 21 | 22 | if (name) 23 | cout << "Hello " << name << "!" << endl; 24 | 25 | Str_c nickname; 26 | 27 | if (nickname) 28 | cout << "Hello " << nickname << "!" << endl; 29 | 30 | return 0; 31 | } -------------------------------------------------------------------------------- /Chapter12/12_6.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_12_6_H 2 | #define GUARD_12_6_H 3 | 4 | int ex12_6(); 5 | 6 | #endif // !GUARD_12_6_H -------------------------------------------------------------------------------- /Chapter12/12_7.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 12_7, 12_7.cpp 3 | 4 | The standard string class provides random-access iterators to manipulate the 5 | string's characters. Add iterators and the iterator operations begin and end to your 6 | Str class. 7 | */ 8 | 9 | #include "stdafx.h" 10 | #include "12_7.h" 11 | 12 | #include 13 | using std::cout; 14 | using std::endl; 15 | 16 | #include "str_c.h" 17 | 18 | int ex12_7() 19 | { 20 | Str_c greeting = "Hello buddy!"; 21 | Str_c::const_iterator iter = greeting.begin(); 22 | 23 | while (iter != greeting.end()) 24 | cout << *iter++; 25 | 26 | cout << endl; 27 | 28 | return 0; 29 | } -------------------------------------------------------------------------------- /Chapter12/12_7.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_12_7_H 2 | #define GUARD_12_7_H 3 | 4 | int ex12_7(); 5 | 6 | #endif // !GUARD_12_7_H -------------------------------------------------------------------------------- /Chapter12/12_8.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 12_8, 12_8.cpp 3 | 4 | Add the getline function to the Str class. 5 | */ 6 | 7 | #include "stdafx.h" 8 | #include "12_8.h" 9 | 10 | #include 11 | using std::cin; 12 | using std::cout; 13 | using std::endl; 14 | 15 | #include "str_c.h" 16 | 17 | int ex12_8() 18 | { 19 | Str_c input; 20 | 21 | while (getline(std::cin, input)) 22 | cout << input << endl; 23 | 24 | return 0; 25 | } -------------------------------------------------------------------------------- /Chapter12/12_8.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_12_8_H 2 | #define GUARD_12_8_H 3 | 4 | int ex12_8(); 5 | 6 | #endif // !GUARD_12_8_H -------------------------------------------------------------------------------- /Chapter12/12_9.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 12_9, 12_9.cpp 3 | 4 | Use class ostream_iterator to reimplement the Str output operator. Why 5 | didn't we ask you to reimplement the input operator using class istream_iterator? 6 | */ 7 | 8 | #include "stdafx.h" 9 | #include "12_9.h" 10 | 11 | #include 12 | using std::cout; 13 | using std::endl; 14 | 15 | #include "str_c.h" 16 | 17 | int ex12_9() 18 | { 19 | Str_c s = "Fantastic news!"; 20 | cout << s << endl; 21 | 22 | return 0; 23 | } -------------------------------------------------------------------------------- /Chapter12/12_9.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_12_9_H 2 | #define GUARD_12_9_H 3 | 4 | int ex12_9(); 5 | 6 | #endif // !GUARD_12_9_H -------------------------------------------------------------------------------- /Chapter12/Chapter12.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Chapter 12, Chapter12.cpp 3 | 4 | Making class objects act like values. 5 | */ 6 | 7 | #include "stdafx.h" 8 | 9 | #include "12_0.h" 10 | #include "12_1.h" 11 | #include "12_2.h" 12 | #include "12_3.h" 13 | #include "12_4.h" 14 | #include "12_5.h" 15 | #include "12_6.h" 16 | #include "12_7.h" 17 | #include "12_8.h" 18 | #include "12_9.h" 19 | #include "12_10.h" 20 | #include "12_11.h" 21 | #include "12_12.h" 22 | #include "12_13.h" 23 | #include "12_14.h" 24 | 25 | int main() 26 | { 27 | ex12_14(); 28 | 29 | return 0; 30 | } -------------------------------------------------------------------------------- /Chapter12/stdafx.cpp: -------------------------------------------------------------------------------- 1 | // stdafx.cpp : source file that includes just the standard includes 2 | // Chapter12.pch will be the pre-compiled header 3 | // stdafx.obj will contain the pre-compiled type information 4 | 5 | #include "stdafx.h" 6 | 7 | // TODO: reference any additional headers you need in STDAFX.H 8 | // and not in this file 9 | -------------------------------------------------------------------------------- /Chapter12/stdafx.h: -------------------------------------------------------------------------------- 1 | // stdafx.h : include file for standard system include files, 2 | // or project specific include files that are used frequently, but 3 | // are changed infrequently 4 | // 5 | 6 | #pragma once 7 | 8 | #include "targetver.h" 9 | 10 | #include 11 | #include 12 | 13 | 14 | 15 | // TODO: reference additional headers your program requires here 16 | -------------------------------------------------------------------------------- /Chapter12/str.cpp: -------------------------------------------------------------------------------- 1 | #include "stdafx.h" 2 | #include "str.h" 3 | 4 | #include 5 | using std::ostream; 6 | using std::istream; 7 | 8 | ostream& operator<<(ostream& os, const Str& s) 9 | { 10 | for (Str::size_type i = 0; i != s.size(); ++i) 11 | os << s[i]; 12 | return os; 13 | } 14 | 15 | istream& operator >> (istream& is, Str& s) 16 | { 17 | // obliterate existing value(s) 18 | s.data.clear(); 19 | // read and discard leading whitespace 20 | char c; 21 | // istream::get() checks inputstream for eof 22 | while (is.get(c) && isspace(c)) 23 | ; // nothing to do, except testing the condition 24 | // if still something to read, do so until next whitespace character 25 | if (is) { 26 | do s.data.push_back(c); // compile error!, data is private 27 | while (is.get(c) && !isspace(c)); 28 | // if we read whitespace, then put it back on the stream 29 | if (is) 30 | is.unget(); 31 | } 32 | return is; 33 | } 34 | 35 | Str operator+(const Str& s, const Str& t) { 36 | Str r = s; 37 | r += t; 38 | return r; 39 | } -------------------------------------------------------------------------------- /Chapter12/targetver.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | // Including SDKDDKVer.h defines the highest available Windows platform. 4 | 5 | // If you wish to build your application for a previous Windows platform, include WinSDKVer.h and 6 | // set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h. 7 | 8 | #include 9 | -------------------------------------------------------------------------------- /Chapter13/13_0.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_13_0_H 2 | #define GUARD_13_0_H 3 | 4 | int ex13_0(); 5 | 6 | #endif // !GUARD_13_0_H -------------------------------------------------------------------------------- /Chapter13/13_1.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 13-1, 13_1.cpp 3 | 4 | Annotate the Core and Grad constructors to write the constructor's name and 5 | argument list when the constructor is executed. For example, you should add a 6 | statement such as 7 | 8 | cerr << "Grad::Grad(istream&)" << endl; 9 | 10 | to the Grad constructor taking an istream& parameter. Then write a small program 11 | that exercises each constructor. Predict beforehand what the output will be. Revise your 12 | program and predictions until your predictions match what is actually written. 13 | */ 14 | 15 | #include "stdafx.h" 16 | #include "13_1.h" 17 | 18 | #include 19 | using std::cin; 20 | using std::cout; 21 | using std::endl; 22 | 23 | #include "student_info.h" 24 | 25 | int ex13_1() 26 | { 27 | // set preprocessor macro "ANNOTATE" in student_info.h to 1 28 | 29 | Core core1; 30 | Grad grad1; 31 | 32 | cout << "Enter Core student info:" << endl; 33 | Core core2(cin); 34 | 35 | cout << "Enter Grad student info:" << endl; 36 | Grad grad2(cin); 37 | 38 | return 0; 39 | } -------------------------------------------------------------------------------- /Chapter13/13_1.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_13_1_H 2 | #define GUARD_13_1_H 3 | 4 | int ex13_1(); 5 | 6 | #endif // !GUARD_13_1_H -------------------------------------------------------------------------------- /Chapter13/13_2.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 13-2, 13_2.cpp 3 | 4 | Given the Core and Grad classes defined in this chapter, indicate which function 5 | is called for each of these invocations: 6 | 7 | Core* p1 = new Core; 8 | Core* p2 = new Grad; 9 | Core s1; 10 | Grad s2; 11 | 12 | p1->grade(); 13 | p1->name(); 14 | 15 | p2->grade(); 16 | p2->name(); 17 | 18 | s1.grade(); 19 | s1.name(); 20 | 21 | s2.name(); 22 | s2.grade(); 23 | 24 | Check whether you are correct by adding output statements to the name and grade 25 | functions that indicate which function is being executed. 26 | */ 27 | 28 | #include "stdafx.h" 29 | #include "13_2.h" 30 | 31 | #include 32 | using std::cin; 33 | 34 | #include "student_info.h" 35 | 36 | int ex13_2() 37 | { 38 | // set preprocessor macro "ANNOTATE" in student_info.h to 1 39 | 40 | Core* p1 = new Core; 41 | Core* p2 = new Grad; 42 | Core s1; 43 | Grad s2; 44 | 45 | p1->read(cin); 46 | p2->read(cin); 47 | s1.read(cin); 48 | s2.read(cin); 49 | 50 | p1->grade(); 51 | p1->name(); 52 | 53 | p2->grade(); 54 | p2->name(); 55 | 56 | s1.grade(); 57 | s1.name(); 58 | 59 | s2.name(); 60 | s2.grade(); 61 | 62 | delete p1; 63 | delete p2; 64 | 65 | return 0; 66 | } -------------------------------------------------------------------------------- /Chapter13/13_2.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_13_2_H 2 | #define GUARD_13_2_H 3 | 4 | int ex13_2(); 5 | 6 | #endif // !GUARD_13_2_H -------------------------------------------------------------------------------- /Chapter13/13_3.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 13-3, 13_3.cpp 3 | 4 | The class that we built in Chapter 9 included a valid member that allowed users 5 | to check whether the object held values for a student record or not. Add that 6 | functionality to the inheritance-based system of classes. 7 | */ 8 | 9 | #include "stdafx.h" 10 | #include "13_3.h" 11 | 12 | #include 13 | using std::cin; 14 | using std::cout; 15 | using std::endl; 16 | 17 | #include "student_info.h" 18 | 19 | int ex13_3() 20 | { 21 | Grad grad(cin); 22 | 23 | if (grad.valid()) 24 | cout << "Grade: " << grad.grade() << endl; 25 | 26 | return 0; 27 | } -------------------------------------------------------------------------------- /Chapter13/13_3.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_13_3_H 2 | #define GUARD_13_3_H 3 | 4 | int ex13_3(); 5 | 6 | #endif // !GUARD_13_3_H -------------------------------------------------------------------------------- /Chapter13/13_4.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeertArien/c-accelerated/c0ae9f66b1733de04f3133db2e9d8af6d555fe6e/Chapter13/13_4.cpp -------------------------------------------------------------------------------- /Chapter13/13_4.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_13_4_H 2 | #define GUARD_13_4_H 3 | 4 | int ex13_4(); 5 | 6 | #endif // !GUARD_13_4_H -------------------------------------------------------------------------------- /Chapter13/13_5.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeertArien/c-accelerated/c0ae9f66b1733de04f3133db2e9d8af6d555fe6e/Chapter13/13_5.cpp -------------------------------------------------------------------------------- /Chapter13/13_5.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_13_5_H 2 | #define GUARD_13_5_H 3 | 4 | int ex13_5(); 5 | 6 | #endif // !GUARD_13_5_H -------------------------------------------------------------------------------- /Chapter13/13_6.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 13-6, 13_6.cpp 3 | 4 | Add a class to the system to represent students taking the course for pass/fail 5 | credit. Assume that such students need not do the homework, but might do so. If they 6 | do, the homework should participate in determining whether they passed or failed, 7 | according to the normal formula. If they did no homework, then the grade is the 8 | average of their midterm and final grades. A passing grade is 60 or higher. 9 | */ 10 | 11 | #include "stdafx.h" 12 | #include "13_6.h" 13 | 14 | #include 15 | using std::cin; 16 | using std::cout; 17 | using std::endl; 18 | 19 | double Credit::grade() const 20 | { 21 | if (homework.empty()) 22 | return (midterm + final) / 2.0; 23 | else 24 | return Core::grade(); 25 | } 26 | 27 | int ex13_6() 28 | { 29 | cout << "Enter a Credit student: " << endl; 30 | Credit credit(cin); 31 | cout << "Grade: " << credit.grade() << endl; 32 | cout << "Passed? " << credit.passed() << endl; 33 | 34 | return 0; 35 | } -------------------------------------------------------------------------------- /Chapter13/13_6.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_13_6_H 2 | #define GUARD_13_6_H 3 | 4 | #include "student_info.h" 5 | 6 | class Credit : public Core { 7 | 8 | public: 9 | 10 | Credit() {} 11 | Credit(std::istream& is) : Core(is) {} 12 | 13 | double grade() const; 14 | bool passed() const { return grade() >= 60; } 15 | 16 | bool valid() const { return true; } 17 | bool requirements_met() const { return true; } 18 | 19 | protected: 20 | 21 | Credit* clone() const { return new Credit(*this); } 22 | }; 23 | 24 | int ex13_6(); 25 | 26 | #endif // !GUARD_13_6_H -------------------------------------------------------------------------------- /Chapter13/13_7.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 13-7, 13_7.cpp 3 | 4 | Add a class to the system to represent students auditing the course. 5 | */ 6 | 7 | #include "stdafx.h" 8 | #include "13_7.h" 9 | 10 | #include 11 | using std::cin; 12 | using std::cout; 13 | using std::endl; 14 | using std::istream; 15 | 16 | istream& Audit::read(istream& in) 17 | { 18 | in >> n; 19 | return in; 20 | } 21 | 22 | int ex13_7() 23 | { 24 | cout << "Enter an Audit student: " << endl; 25 | Audit audit(cin); 26 | cout << "Grade: " << audit.grade() << endl; 27 | cout << "Requirements met? " << audit.requirements_met() << endl; 28 | 29 | return 0; 30 | } -------------------------------------------------------------------------------- /Chapter13/13_7.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_13_7_H 2 | #define GUARD_13_7_H 3 | 4 | #include 5 | 6 | #include "student_info.h" 7 | 8 | class Audit : public Core { 9 | 10 | public: 11 | 12 | Audit() {} 13 | Audit(std::istream& is) { read(is); } 14 | 15 | std::istream& read(std::istream&); 16 | 17 | double grade() const { return 0.0; }; 18 | 19 | bool valid() const { return true; } 20 | bool requirements_met() const { return true; } 21 | 22 | protected: 23 | 24 | Audit* clone() const { return new Audit(*this); } 25 | }; 26 | 27 | int ex13_7(); 28 | 29 | #endif // !GUARD_13_7_H -------------------------------------------------------------------------------- /Chapter13/13_8.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 13-8, 13_8.cpp 3 | 4 | Write a program to generate a grade report that can handle all four kinds of 5 | students. 6 | */ 7 | 8 | #include "stdafx.h" 9 | #include "13_8.h" 10 | 11 | #include 12 | using std::vector; 13 | 14 | #include 15 | using std::string; 16 | 17 | #include 18 | using std::max; 19 | 20 | #include 21 | using std::cin; 22 | using std::cout; 23 | using std::endl; 24 | using std::streamsize; 25 | 26 | #include 27 | using std::setprecision; 28 | 29 | #include 30 | using std::domain_error; 31 | 32 | #include"student_info.h" 33 | 34 | int ex13_8() 35 | { 36 | vector students; 37 | Student_info record; 38 | string::size_type maxlen = 0; 39 | 40 | cout << "Enter students (U = Core, G = Grad, C = Credit, A = Audit)" << endl; 41 | 42 | // read and store the data 43 | while (record.read(cin)) { 44 | maxlen = max(maxlen, record.name().size()); 45 | students.push_back(record); 46 | } 47 | 48 | // alphabetize the student records 49 | sort(students.begin(), students.end(), Student_info::compare); 50 | 51 | // write the names and grades 52 | for (vector::size_type i = 0; 53 | i != students.size(); ++i) { 54 | cout << students[i].name() 55 | << string(maxlen + 1 - students[i].name().size(), ' '); 56 | try { 57 | double final_grade = students[i].grade(); 58 | streamsize prec = cout.precision(); 59 | cout << setprecision(3) << final_grade 60 | << setprecision(prec) << endl; 61 | } 62 | catch (domain_error e) { 63 | cout << e.what() << endl; 64 | } 65 | } 66 | return 0; 67 | } -------------------------------------------------------------------------------- /Chapter13/13_8.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_13_8_H 2 | #define GUARD_13_8_H 3 | 4 | int ex13_8(); 5 | 6 | #endif // !GUARD_13_8_H -------------------------------------------------------------------------------- /Chapter13/Chapter13.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Chapter 13, Chapter13.cpp 3 | 4 | Using inheritance and dynamic binding. 5 | */ 6 | 7 | #include "stdafx.h" 8 | 9 | #include "13_0.h" 10 | #include "13_1.h" 11 | #include "13_2.h" 12 | #include "13_3.h" 13 | #include "13_4.h" 14 | #include "13_5.h" 15 | #include "13_6.h" 16 | #include "13_7.h" 17 | #include "13_8.h" 18 | 19 | int main() 20 | { 21 | ex13_8(); 22 | 23 | return 0; 24 | } 25 | 26 | -------------------------------------------------------------------------------- /Chapter13/stdafx.cpp: -------------------------------------------------------------------------------- 1 | // stdafx.cpp : source file that includes just the standard includes 2 | // Chapter13.pch will be the pre-compiled header 3 | // stdafx.obj will contain the pre-compiled type information 4 | 5 | #include "stdafx.h" 6 | 7 | // TODO: reference any additional headers you need in STDAFX.H 8 | // and not in this file 9 | -------------------------------------------------------------------------------- /Chapter13/stdafx.h: -------------------------------------------------------------------------------- 1 | // stdafx.h : include file for standard system include files, 2 | // or project specific include files that are used frequently, but 3 | // are changed infrequently 4 | // 5 | 6 | #pragma once 7 | 8 | #include "targetver.h" 9 | 10 | #include 11 | #include 12 | 13 | 14 | 15 | // TODO: reference additional headers your program requires here 16 | -------------------------------------------------------------------------------- /Chapter13/targetver.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | // Including SDKDDKVer.h defines the highest available Windows platform. 4 | 5 | // If you wish to build your application for a previous Windows platform, include WinSDKVer.h and 6 | // set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h. 7 | 8 | #include 9 | -------------------------------------------------------------------------------- /Chapter14/14_0.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_14_0_H 2 | #define GUARD_14_0_H 3 | 4 | int ex14_0(); 5 | 6 | #endif // !GUARD_14_0_H -------------------------------------------------------------------------------- /Chapter14/14_1.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 14-1, 14_1.cpp 3 | 4 | Implement the comparison operation that operates on Ptr. 5 | */ 6 | 7 | #include "stdafx.h" 8 | #include "14_1.h" 9 | 10 | #include 11 | using std::stringstream; 12 | 13 | #include 14 | using std::cout; 15 | using std::endl; 16 | 17 | #include "student_info.h" 18 | #include "ptr.h" 19 | 20 | int ex14_1() 21 | { 22 | stringstream ss1("Jan 15 20 30 40"); 23 | stringstream ss2("Jan 80 90 70 50"); 24 | 25 | Ptr p1 = new Core(ss1); 26 | Ptr p2 = new Core(ss2); 27 | 28 | cout << "Is " << p1->name() << " == " << p2->name() << " ? " << (compare_Core_Ptr(p1, p2) == 0) << endl; 29 | 30 | return 0; 31 | } -------------------------------------------------------------------------------- /Chapter14/14_1.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_14_1_H 2 | #define GUARD_14_1_H 3 | 4 | #include "ptr.h" 5 | #include "student_info.h" 6 | 7 | inline bool compare_Core_Ptr(const Ptr hc1, const Ptr hc2) 8 | { 9 | return compare(*hc1, *hc1); 10 | } 11 | 12 | int ex14_1(); 13 | 14 | #endif // !GUARD_14_1_H -------------------------------------------------------------------------------- /Chapter14/14_2.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_14_2_H 2 | #define GUARD_14_2_H 3 | 4 | int ex14_2(); 5 | 6 | #endif // !GUARD_14_2_H -------------------------------------------------------------------------------- /Chapter14/14_3.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeertArien/c-accelerated/c0ae9f66b1733de04f3133db2e9d8af6d555fe6e/Chapter14/14_3.cpp -------------------------------------------------------------------------------- /Chapter14/14_3.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_14_3_H 2 | #define GUARD_14_3_H 3 | 4 | int ex14_3(); 5 | 6 | #endif // !GUARD_14_3_H -------------------------------------------------------------------------------- /Chapter14/14_4.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 14-4, 14_4.cpp 3 | 4 | Reimplement the Str class to use the final version of Ptr. 5 | */ 6 | 7 | #include "stdafx.h" 8 | #include "14_4.h" 9 | 10 | #include 11 | using std::cin; 12 | using std::cout; 13 | using std::endl; 14 | 15 | #include "str.h" 16 | 17 | int ex14_4() 18 | { 19 | Str s1 = "Hello "; 20 | Str s2; 21 | cout << "Please enter your name: "; 22 | cin >> s2; 23 | Str s3 = s1 + s2 + " how are you?"; 24 | cout << s3 << endl; 25 | 26 | return 0; 27 | } -------------------------------------------------------------------------------- /Chapter14/14_4.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_14_4_H 2 | #define GUARD_14_4_H 3 | 4 | int ex14_4(); 5 | 6 | #endif // !GUARD_14_4_H -------------------------------------------------------------------------------- /Chapter14/14_5.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_14_5_H 2 | #define GUARD_14_5_H 3 | 4 | int ex14_5(); 5 | 6 | #endif // !GUARD_14_5_H -------------------------------------------------------------------------------- /Chapter14/14_6.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_14_6_H 2 | #define GUARD_14_6_H 3 | 4 | #include "ptr_r.h" 5 | #include "student_info.h" 6 | 7 | inline bool compare_Core_Ptr_r(const Ptr_r hc1, const Ptr_r hc2) 8 | { 9 | return compare(*hc1, *hc1); 10 | } 11 | 12 | int ex14_6(); 13 | 14 | #endif // !GUARD_14_6_H -------------------------------------------------------------------------------- /Chapter14/Chapter14.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Chapter 14, Chapter14.cpp 3 | 4 | Managing memory (almost) automatically. 5 | */ 6 | 7 | #include "stdafx.h" 8 | 9 | #include "14_0.h" 10 | #include "14_1.h" 11 | #include "14_2.h" 12 | #include "14_3.h" 13 | #include "14_4.h" 14 | #include "14_5.h" 15 | #include "14_6.h" 16 | 17 | int main() 18 | { 19 | ex14_6(); 20 | 21 | return 0; 22 | } 23 | 24 | -------------------------------------------------------------------------------- /Chapter14/ptr.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeertArien/c-accelerated/c0ae9f66b1733de04f3133db2e9d8af6d555fe6e/Chapter14/ptr.h -------------------------------------------------------------------------------- /Chapter14/ptr_r.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_ptr_r_H 2 | #define GUARD_ptr_r_H 3 | 4 | #include 5 | 6 | #include "ref_counter.h" 7 | 8 | template class Ptr_r { 9 | 10 | public: 11 | 12 | void make_unique() { 13 | if (ref_c.count() != 1) { 14 | --ref_c; 15 | ref_c = ref_c(); 16 | p = p ? clone(p) : 0; // call the global (not member) version of clone 17 | } 18 | } 19 | 20 | Ptr_r() : p(0) { } 21 | Ptr_r(T* t) : p(t) { } 22 | Ptr_r(const Ptr_r& h) : ref_c(h.ref_c), p(h.p) { ++ref_c; } 23 | 24 | Ptr_r& operator=(const Ptr_r&); 25 | ~Ptr_r(); 26 | 27 | operator bool() const { return p != 0; } 28 | 29 | T& operator*() const 30 | { 31 | if (p) 32 | return *p; 33 | throw std::runtime_error("unbound Ref_handle"); 34 | } 35 | 36 | T* operator->() const { 37 | if (p) 38 | return p; 39 | throw std::runtime_error("unbound Ref_handle"); 40 | } 41 | 42 | private: 43 | 44 | T* p; 45 | Ref_counter ref_c; 46 | 47 | }; 48 | 49 | template 50 | Ptr_r& Ptr_r::operator=(const Ptr_r& rhs) 51 | { 52 | if (&rhs != this) 53 | { 54 | // free the left-hand side, destroying pointers if appropriate 55 | if ((--ref_c).count() == 0) { 56 | delete p; 57 | } 58 | // copy in values from the right-hand side 59 | ref_c = rhs.ref_c; 60 | p = rhs.p; 61 | ++ref_c; 62 | } 63 | 64 | return *this; 65 | } 66 | 67 | template 68 | Ptr_r::~Ptr_r() 69 | { 70 | if ((--ref_c).count() == 0) { 71 | delete p; 72 | } 73 | } 74 | 75 | #endif // !GUARD_ptr_r_H -------------------------------------------------------------------------------- /Chapter14/ref_counter.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_ref_counter_H 2 | #define GUARD_ref_counter_H 3 | 4 | class Ref_counter { 5 | 6 | public: 7 | 8 | Ref_counter() : counter(new size_t(1)) { }; 9 | 10 | Ref_counter(size_t s) : counter(new size_t(s)) { }; 11 | 12 | Ref_counter(const Ref_counter& r) : counter(r.counter) { }; 13 | 14 | Ref_counter& operator=(const Ref_counter& rhs) { 15 | if (*counter <= 0) 16 | delete counter; 17 | counter = rhs.counter; 18 | 19 | return *this; 20 | }; 21 | 22 | Ref_counter& operator++() { 23 | (*counter)++; 24 | return *this; 25 | }; 26 | 27 | Ref_counter& operator--() { 28 | (*counter)--; 29 | return *this; 30 | }; 31 | 32 | Ref_counter operator++(int) { 33 | Ref_counter ret(*(this->counter)); 34 | (*counter)++; 35 | return ret; 36 | }; 37 | 38 | Ref_counter operator--(int) { 39 | Ref_counter ret(*(this->counter)); 40 | (*counter)--; 41 | return ret; 42 | }; 43 | 44 | ~Ref_counter() { 45 | if (*counter <= 0) 46 | delete counter; 47 | }; 48 | 49 | size_t count() { return *counter; }; 50 | 51 | 52 | private: 53 | 54 | size_t* counter; 55 | 56 | }; 57 | 58 | #endif // !GUARD_ref_counter_H -------------------------------------------------------------------------------- /Chapter14/stdafx.cpp: -------------------------------------------------------------------------------- 1 | // stdafx.cpp : source file that includes just the standard includes 2 | // Chapter14.pch will be the pre-compiled header 3 | // stdafx.obj will contain the pre-compiled type information 4 | 5 | #include "stdafx.h" 6 | 7 | // TODO: reference any additional headers you need in STDAFX.H 8 | // and not in this file 9 | -------------------------------------------------------------------------------- /Chapter14/stdafx.h: -------------------------------------------------------------------------------- 1 | // stdafx.h : include file for standard system include files, 2 | // or project specific include files that are used frequently, but 3 | // are changed infrequently 4 | // 5 | 6 | #pragma once 7 | 8 | #include "targetver.h" 9 | 10 | #include 11 | #include 12 | 13 | 14 | 15 | // TODO: reference additional headers your program requires here 16 | -------------------------------------------------------------------------------- /Chapter14/str.cpp: -------------------------------------------------------------------------------- 1 | #include "stdafx.h" 2 | #include "str.h" 3 | 4 | #include 5 | using std::ostream; 6 | using std::istream; 7 | 8 | ostream& operator<<(ostream& os, const Str& s) 9 | { 10 | for (Str::size_type i = 0; i != s.size(); ++i) 11 | os << s[i]; 12 | return os; 13 | } 14 | 15 | istream& operator>>(istream& is, Str& s) 16 | { 17 | // obliterate existing value(s) 18 | s.data->clear(); 19 | // read and discard leading whitespace 20 | char c; 21 | // istream::get() checks inputstream for eof 22 | while (is.get(c) && isspace(c)) 23 | ; // nothing to do, except testing the condition 24 | // if still something to read, do so until next whitespace character 25 | if (is) { 26 | do s.data->push_back(c); // compile error!, data is private 27 | while (is.get(c) && !isspace(c)); 28 | // if we read whitespace, then put it back on the stream 29 | if (is) 30 | is.unget(); 31 | } 32 | return is; 33 | } 34 | 35 | Str operator+(const Str& s, const Str& t) { 36 | Str r = s; 37 | r += t; 38 | return r; 39 | } -------------------------------------------------------------------------------- /Chapter14/str.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeertArien/c-accelerated/c0ae9f66b1733de04f3133db2e9d8af6d555fe6e/Chapter14/str.h -------------------------------------------------------------------------------- /Chapter14/student_info_p.cpp: -------------------------------------------------------------------------------- 1 | #include"stdafx.h" 2 | #include "student_info_p.h" 3 | 4 | #include 5 | using std::istream; 6 | 7 | istream& Student_info_p::read(istream& is) 8 | { 9 | char ch; 10 | is >> ch; // get record type 11 | 12 | if (ch == 'U') 13 | cp = new Core(is); 14 | else 15 | cp = new Grad(is); 16 | return is; 17 | } 18 | -------------------------------------------------------------------------------- /Chapter14/student_info_p.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_student_info_p_H 2 | #define GUARD_student_info_p_H 3 | 4 | #include 5 | #include 6 | 7 | #include "student_info.h" 8 | #include "ptr.h" 9 | 10 | class Student_info_p { 11 | 12 | public: 13 | 14 | Student_info_p() { } 15 | Student_info_p(std::istream& is) { read(is); } 16 | // no copy, assign, or destructor: they're no longer needed 17 | 18 | std::istream& read(std::istream&); 19 | 20 | std::string name() const { 21 | if (cp) 22 | return cp->name(); 23 | else throw std::runtime_error("uninitialized Student"); 24 | } 25 | 26 | double grade() const { 27 | if (cp) return cp->grade(); 28 | else throw std::runtime_error("uninitialized Student"); 29 | } 30 | 31 | static bool compare(const Student_info_p& s1, 32 | const Student_info_p& s2) { 33 | return s1.name() < s2.name(); 34 | } 35 | 36 | private: 37 | 38 | Ptr cp; 39 | 40 | }; 41 | 42 | 43 | #endif // !GUARD_student_info_p_H -------------------------------------------------------------------------------- /Chapter14/targetver.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | // Including SDKDDKVer.h defines the highest available Windows platform. 4 | 5 | // If you wish to build your application for a previous Windows platform, include WinSDKVer.h and 6 | // set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h. 7 | 8 | #include 9 | -------------------------------------------------------------------------------- /Chapter15/15_0.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 15-0, 15_0.cpp 3 | 4 | Compile, execute, and test the programs in this chapter. 5 | */ 6 | 7 | #include "stdafx.h" 8 | #include "15_0.h" 9 | 10 | #include 11 | using std::vector; 12 | 13 | #include 14 | using std::string; 15 | 16 | #include 17 | using std::cout; 18 | using std::endl; 19 | 20 | #include "pic.h" 21 | 22 | int ex15_0() 23 | { 24 | vector v1 = { "one test", "two test", "aye caramba" }; 25 | vector v2 = { "one direction", "mama mia", "mamo testo", "lake district mall" }; 26 | 27 | cout << frame(v1) << endl; 28 | 29 | cout << hcat(v1, v2) << endl; 30 | 31 | cout << vcat(frame(v1), frame(v2)) << endl; 32 | 33 | return 0; 34 | } -------------------------------------------------------------------------------- /Chapter15/15_0.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_15_0_H 2 | #define GUARD_15_0_H 3 | 4 | int ex15_0(); 5 | 6 | #endif // !GUARD_15_0_H -------------------------------------------------------------------------------- /Chapter15/15_1.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 15-1, 15_1.cpp 3 | 4 | Test your system by writing a program that executes 5 | 6 | Picture p = // some initial starting picture 7 | Picture q = frame(p); 8 | Picture r = hcat(p, q) ; 9 | Picture s = vcat(q, r); 10 | cout << frame(hcat(s, vcat(r, q))) << endl; 11 | 12 | */ 13 | 14 | #include "stdafx.h" 15 | #include "15_1.h" 16 | 17 | #include 18 | using std::vector; 19 | 20 | #include 21 | using std::string; 22 | 23 | #include 24 | using std::cout; 25 | using std::endl; 26 | 27 | #include "pic.h" 28 | 29 | int ex15_1() 30 | { 31 | vector v1 = { "one test", "two test", "aye caramba" }; 32 | Picture p = v1; 33 | Picture q = frame(p); 34 | Picture r = hcat(p, q); 35 | Picture s = vcat(q, r); 36 | cout << frame(hcat(s, vcat(r, q))) << endl; 37 | 38 | return 0; 39 | } -------------------------------------------------------------------------------- /Chapter15/15_1.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_15_1_H 2 | #define GUARD_15_1_H 3 | 4 | int ex15_1(); 5 | 6 | #endif // !GUARD_15_1_H -------------------------------------------------------------------------------- /Chapter15/15_2.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 15-2, 15_2.cpp 3 | 4 | Reimplement the Frame_Pic class so that the frame uses three different 5 | characters: one for the corners, another for the top and bottom borders, and a third for 6 | the side borders. 7 | */ 8 | 9 | #include "stdafx.h" 10 | #include "15_2.h" 11 | 12 | #include 13 | using std::vector; 14 | 15 | #include 16 | using std::string; 17 | 18 | #include 19 | using std::cout; 20 | using std::endl; 21 | 22 | #include "pic.h" 23 | 24 | int ex15_2() 25 | { 26 | vector v1 = { "one test", "two test", "aye caramba" }; 27 | Picture p = v1; 28 | Picture q = frame(p); 29 | Picture r = hcat(p, q); 30 | Picture s = vcat(q, r); 31 | cout << frame(hcat(s, vcat(r, q))) << endl; 32 | 33 | return 0; 34 | } -------------------------------------------------------------------------------- /Chapter15/15_2.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_15_2_H 2 | #define GUARD_15_2_H 3 | 4 | int ex15_2(); 5 | 6 | #endif // !GUARD_15_2_H -------------------------------------------------------------------------------- /Chapter15/15_3.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 15-3, 15_3.cpp 3 | 4 | Give users the option to specify what characters to use for these border 5 | characters. 6 | */ 7 | 8 | #include "stdafx.h" 9 | #include "15_3.h" 10 | 11 | #include 12 | using std::vector; 13 | 14 | #include 15 | using std::string; 16 | 17 | #include 18 | using std::cout; 19 | using std::endl; 20 | 21 | #include "pic.h" 22 | 23 | int ex15_3() 24 | { 25 | vector v1 = { "one test", "two test", "aye caramba" }; 26 | Picture p = v1; 27 | Picture q = frame(p, '+', '*', '*'); 28 | Picture r = hcat(p, q); 29 | Picture s = vcat(q, r); 30 | cout << frame(hcat(s, vcat(r, q))) << endl; 31 | 32 | return 0; 33 | } -------------------------------------------------------------------------------- /Chapter15/15_3.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_15_3_H 2 | #define GUARD_15_3_H 3 | 4 | int ex15_3(); 5 | 6 | #endif // !GUARD_15_3_H -------------------------------------------------------------------------------- /Chapter15/15_4.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 15-4, 15_4.cpp 3 | 4 | Add an operation to reframe a Picture, which changes the frame characters. 5 | The operation should change all of the frames in the interior picture. 6 | */ 7 | 8 | #include "stdafx.h" 9 | #include "15_4.h" 10 | 11 | #include 12 | using std::vector; 13 | 14 | #include 15 | using std::string; 16 | 17 | #include 18 | using std::cout; 19 | using std::endl; 20 | 21 | #include "pic.h" 22 | 23 | void Frame_Pic::reframe(const char corner, const char top_bot, const char side) 24 | { 25 | this->corner = corner; 26 | this->top_bot = top_bot; 27 | this->side = side; 28 | 29 | p->reframe(corner, top_bot, side); 30 | } 31 | 32 | void VCat_Pic::reframe(const char corner, const char top_bot, const char side) 33 | { 34 | top->reframe(corner, top_bot, side); 35 | bottom->reframe(corner, top_bot, side); 36 | } 37 | 38 | void HCat_Pic::reframe(const char corner, const char top_bot, const char side) 39 | { 40 | left->reframe(corner, top_bot, side); 41 | right->reframe(corner, top_bot, side); 42 | } 43 | 44 | void Picture::reframe(const char corner, const char top_bot, const char side) 45 | { 46 | p->reframe(corner, top_bot, side); 47 | } 48 | 49 | int ex15_4() 50 | { 51 | vector v1 = { "one test", "two test", "aye caramba" }; 52 | Picture p = v1; 53 | Picture q = frame(p); 54 | Picture r = hcat(p, q); 55 | Picture s = vcat(q, r); 56 | Picture t = frame(hcat(s, vcat(r, q))); 57 | cout << t << endl; 58 | 59 | t.reframe('*', '*', '*'); 60 | cout << t << endl; 61 | 62 | return 0; 63 | } -------------------------------------------------------------------------------- /Chapter15/15_4.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_15_4_H 2 | #define GUARD_15_4_H 3 | 4 | int ex15_4(); 5 | 6 | #endif // !GUARD_15_4_H -------------------------------------------------------------------------------- /Chapter15/15_5.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 15-5, 15_5.cpp 3 | 4 | Reimplement HCat_Pic so that when pictures of a different size are 5 | concatenated, the shorter one is centered in the space consumed by the longer one. 6 | That is, if we horizontally concatenate two pictures, one of which is four lines long and 7 | the other is two lines long, the first and last rows of the output picture will be blank on 8 | the side of the shorter picture. What can we now conclude about the necessity of the 9 | tests between row and 0. 10 | */ 11 | 12 | #include "stdafx.h" 13 | #include "15_5.h" 14 | 15 | #include 16 | using std::vector; 17 | 18 | #include 19 | using std::string; 20 | 21 | #include 22 | using std::cout; 23 | using std::endl; 24 | 25 | #include "pic.h" 26 | 27 | int ex15_5() 28 | { 29 | vector v1 = { "one test", "aye caramba" }; 30 | vector v2 = { "one direction", "mama mia", "mamo testo", "lake district mall" }; 31 | 32 | cout << frame(hcat(v1, v2)) << endl; 33 | 34 | cout << hcat(v2, v1) << endl; 35 | 36 | cout << hcat(frame(v1), v2) << endl; 37 | 38 | return 0; 39 | } -------------------------------------------------------------------------------- /Chapter15/15_5.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_15_5_H 2 | #define GUARD_15_5_H 3 | 4 | int ex15_5(); 5 | 6 | #endif // !GUARD_15_5_H -------------------------------------------------------------------------------- /Chapter15/15_6.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 15-6, 15_6.cpp 3 | 4 | The Vec and str classes that we developed in Chapters 11 and 12 are powerful 5 | enough to be used to implement Pictures. Reimplement the material in this chapter to 6 | use Vec instead of vector, and test your implementation. 7 | */ 8 | 9 | #include "stdafx.h" 10 | #include "15_6.h" 11 | 12 | #include 13 | using std::cout; 14 | using std::endl; 15 | 16 | #include "pic_v.h" 17 | 18 | int ex15_6() 19 | { 20 | Vec v1; 21 | v1.push_back("one test"); 22 | v1.push_back("greeting"); 23 | v1.push_back("how are you?"); 24 | 25 | Picture_v p = v1; 26 | Picture_v q = frame(p); 27 | Picture_v r = hcat(p, q); 28 | Picture_v s = vcat(q, r); 29 | cout << frame(hcat(s, vcat(r, q))) << endl; 30 | 31 | return 0; 32 | } -------------------------------------------------------------------------------- /Chapter15/15_6.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_15_6_H 2 | #define GUARD_15_6_H 3 | 4 | int ex15_6(); 5 | 6 | #endif // !GUARD_15_6_H -------------------------------------------------------------------------------- /Chapter15/Chapter15.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Chapter 15, Chapter15.cpp 3 | 4 | Revisiting character pictures. 5 | */ 6 | 7 | #include "stdafx.h" 8 | 9 | #include "15_0.h" 10 | #include "15_1.h" 11 | #include "15_2.h" 12 | #include "15_3.h" 13 | #include "15_4.h" 14 | #include "15_5.h" 15 | #include "15_6.h" 16 | 17 | int main() 18 | { 19 | ex15_6(); 20 | 21 | return 0; 22 | } 23 | 24 | -------------------------------------------------------------------------------- /Chapter15/ptr.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeertArien/c-accelerated/c0ae9f66b1733de04f3133db2e9d8af6d555fe6e/Chapter15/ptr.h -------------------------------------------------------------------------------- /Chapter15/stdafx.cpp: -------------------------------------------------------------------------------- 1 | // stdafx.cpp : source file that includes just the standard includes 2 | // Chapter15.pch will be the pre-compiled header 3 | // stdafx.obj will contain the pre-compiled type information 4 | 5 | #include "stdafx.h" 6 | 7 | // TODO: reference any additional headers you need in STDAFX.H 8 | // and not in this file 9 | -------------------------------------------------------------------------------- /Chapter15/stdafx.h: -------------------------------------------------------------------------------- 1 | // stdafx.h : include file for standard system include files, 2 | // or project specific include files that are used frequently, but 3 | // are changed infrequently 4 | // 5 | 6 | #pragma once 7 | 8 | #include "targetver.h" 9 | 10 | #include 11 | #include 12 | 13 | 14 | 15 | // TODO: reference additional headers your program requires here 16 | -------------------------------------------------------------------------------- /Chapter15/targetver.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | // Including SDKDDKVer.h defines the highest available Windows platform. 4 | 5 | // If you wish to build your application for a previous Windows platform, include WinSDKVer.h and 6 | // set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h. 7 | 8 | #include 9 | -------------------------------------------------------------------------------- /Chapter16/16_0.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Exercise 16-0, 16_0.cpp 3 | 4 | Compile, execute, and test the programs in this chapter. 5 | */ 6 | 7 | #include "stdafx.h" 8 | #include "16_0.h" 9 | 10 | #include 11 | using std::vector; 12 | 13 | #include 14 | using std::string; 15 | 16 | #include 17 | using std::cin; 18 | using std::cout; 19 | using std::endl; 20 | 21 | #include "pic.h" 22 | #include "student_info.h" 23 | 24 | 25 | Picture histogram(const vector& students) { 26 | Picture names; 27 | Picture grades; 28 | // for each student 29 | for (vector::const_iterator it = students.begin(); 30 | it != students.end(); ++it) { 31 | // create vertically concatenated pictures of the names and grades 32 | names = vcat(names, vector(1, it->name())); 33 | grades = vcat(grades, 34 | vector(1, " " + string((int)it->grade() / 5, '='))); 35 | } 36 | // horizontally concatenate the name and grade pictures to combine them 37 | return hcat(names, grades); 38 | } int ex16_0() 39 | { 40 | vector students; 41 | Student_info s; 42 | // read the names and grades (U for undergraduate, G for graduate) 43 | while (s.read(cin)) 44 | students.push_back(s); 45 | // put the students in alphabetical order 46 | sort(students.begin(), students.end(), Student_info::compare); 47 | // write the names and histograms 48 | cout << frame(histogram(students)) << endl; 49 | return 0; 50 | } -------------------------------------------------------------------------------- /Chapter16/16_0.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_16_0_H 2 | #define GUARD_16_0_H 3 | 4 | int ex16_0(); 5 | 6 | #endif // !GUARD_16_0_H -------------------------------------------------------------------------------- /Chapter16/16_1.h: -------------------------------------------------------------------------------- 1 | #ifndef GUARD_16_1_H 2 | #define GUARD_16_1_H 3 | 4 | int ex16_1(); 5 | 6 | #endif // !GUARD_16_1_H -------------------------------------------------------------------------------- /Chapter16/Chapter16.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Accelerated C++, Chapter 16, Chapter16.cpp 3 | 4 | Where do we go from here? 5 | */ 6 | 7 | #include "stdafx.h" 8 | 9 | #include "16_0.h" 10 | #include "16_1.h" 11 | 12 | int main() 13 | { 14 | ex16_1(); 15 | 16 | return 0; 17 | } -------------------------------------------------------------------------------- /Chapter16/ptr.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeertArien/c-accelerated/c0ae9f66b1733de04f3133db2e9d8af6d555fe6e/Chapter16/ptr.h -------------------------------------------------------------------------------- /Chapter16/stdafx.cpp: -------------------------------------------------------------------------------- 1 | // stdafx.cpp : source file that includes just the standard includes 2 | // Chapter16.pch will be the pre-compiled header 3 | // stdafx.obj will contain the pre-compiled type information 4 | 5 | #include "stdafx.h" 6 | 7 | // TODO: reference any additional headers you need in STDAFX.H 8 | // and not in this file 9 | -------------------------------------------------------------------------------- /Chapter16/stdafx.h: -------------------------------------------------------------------------------- 1 | // stdafx.h : include file for standard system include files, 2 | // or project specific include files that are used frequently, but 3 | // are changed infrequently 4 | // 5 | 6 | #pragma once 7 | 8 | #include "targetver.h" 9 | 10 | #include 11 | #include 12 | 13 | 14 | 15 | // TODO: reference additional headers your program requires here 16 | -------------------------------------------------------------------------------- /Chapter16/targetver.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | // Including SDKDDKVer.h defines the highest available Windows platform. 4 | 5 | // If you wish to build your application for a previous Windows platform, include WinSDKVer.h and 6 | // set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h. 7 | 8 | #include 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # C-Accelerated 2 | 3 | Solutions to the end-of-chapter exercise files found in the C++ study book **Accelerated C++**, written by Andrew koeing and Barbara Moo (Published in 2000 by Addison-Wesley). 4 | 5 | ## Getting Started 6 | 7 | This project was created using Visual Studio 2015 and compiled under the Microsoft Visual C++ compiler, version 14.0. In order to run these files under a different compiler it's likely that small changes need to be made to the source files. 8 | 9 | For each chapter in the book a different project/folder has been created. Subsequently each exercise has it's own header and source file. At the top of the source file the exercise is outlined as described in the book. 10 | 11 | This project is under active development. 12 | 13 | ## Contributing 14 | 15 | Feel free to contribute by submitting an issue or pull request if you find any errors in the exercise solutions. 16 | 17 | ## Authors 18 | 19 | * **Geert Arien** - *Initial work* - [GeertArien](https://github.com/geertarien) 20 | 21 | ## License 22 | 23 | * This project is licensed under the MIT License 24 | * All exercises that are referred to in this project are copyrighted by AT&T, Barbara E. Moo and Andrew Koenig 25 | 26 | ## Acknowledgments 27 | 28 | * Many thanks to **Barbara E. Moo** and **Andrew Koenig** for writing this compact but dense C++ study book 29 | * **Johnny Chan** for inspiration to some of the solutions - [Mathalope](http://mathalope.co.uk/) 30 | --------------------------------------------------------------------------------