├── Chapter 3. Generic Programming ├── TestFile_3.4.odd ├── TestFile_3.4.even ├── TestFile_3.3.txt ├── TestFile_3.3.map ├── TestFile_3.2.txt ├── TestFile_3.2.vector ├── TestFile_3.1.txt ├── TestFile_3.1.map ├── 3.4.cpp ├── 3.2.cpp ├── 3.1.cpp └── 3.3.cpp ├── Chapter 1. Basic C++ Programming ├── TestFile_1.7.txt ├── 1.3.cpp ├── 1.1.cpp ├── 1.2.cpp ├── 1.5.cpp ├── 1.4.cpp ├── 1.8.cpp ├── 1.6.cpp └── 1.7.cpp ├── Chapter 6. Programming With Templates ├── 6.2.cpp ├── 6.1.h └── Matrix.h ├── Chapter 5. Object-Oriented Programming ├── 5.3.cpp ├── 5.4.cpp ├── Peekback_Stack_5.2.h ├── Stack_5.1.h ├── 5.2.cpp ├── 5.1.cpp ├── LIFO_Stack.h ├── Stack_5.2.h └── Peekback_Stack_5.1.h ├── Chapter 4. Object-Based Programming ├── Stack_4.1.h ├── Stack_4.2.h ├── Matrix.h ├── 4.1.cpp ├── 4.3.h ├── 4.2.cpp ├── UserProfile.h ├── 4.5.cpp └── 4.4.cpp ├── README.md ├── Chapter 7. Exception Handling ├── 7.1.cpp ├── 7.2.cpp └── 7.3.cpp └── Chapter 2. Procedural Programming ├── 2.2.cpp ├── 2.1.cpp ├── 2.3.cpp ├── 2.4.cpp ├── 2.6.cpp └── 2.5.cpp /Chapter 3. Generic Programming/TestFile_3.4.odd: -------------------------------------------------------------------------------- 1 | 3 5 1 -------------------------------------------------------------------------------- /Chapter 3. Generic Programming/TestFile_3.4.even: -------------------------------------------------------------------------------- 1 | 2 2 | 4 3 | -------------------------------------------------------------------------------- /Chapter 1. Basic C++ Programming/TestFile_1.7.txt: -------------------------------------------------------------------------------- 1 | Hello everyone! 2 | I am your friend, Chester! 3 | Nice to meet you! 4 | -------------------------------------------------------------------------------- /Chapter 6. Programming With Templates/6.2.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chester39/EssentialCpp/HEAD/Chapter 6. Programming With Templates/6.2.cpp -------------------------------------------------------------------------------- /Chapter 3. Generic Programming/TestFile_3.3.txt: -------------------------------------------------------------------------------- 1 | Lippman Stanley 2 | Stroustrup Bjarne Cplusplus 3 | Deitel Pual Harvey 4 | Chester Chamberlain Janine Jessie 5 | Villey 6 | Koenig Andrew Barbara Moo 7 | -------------------------------------------------------------------------------- /Chapter 1. Basic C++ Programming/1.3.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Essential C++ 3 | // Stanley Lippman 4 | // Chen Chen @ November 26th, 2014 5 | // 6 | 7 | int my_main(int argc, char *argv[]) {} //The program will not work without main(). 8 | -------------------------------------------------------------------------------- /Chapter 5. Object-Oriented Programming/5.3.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Essential C++ 3 | // Stanley Lippman 4 | // Chen Chen @ December 29th, 2014 5 | // 6 | 7 | Reflects an is-a relationship: 8 | (a) 9 | (c) 10 | (d) 11 | (f) 12 | (g) 13 | 14 | 15 | Not reflects an is-a relationship: 16 | (b) 17 | (e) 18 | (h) 19 | (i) -------------------------------------------------------------------------------- /Chapter 3. Generic Programming/TestFile_3.3.map: -------------------------------------------------------------------------------- 1 | The family has 1 children: Villey 2 | The Chester family has 3 children: Chamberlain Janine Jessie 3 | The Deitel family has 2 children: Pual Harvey 4 | The Koenig family has 3 children: Andrew Barbara Moo 5 | The Lippman family has 1 children: Stanley 6 | The Stroustrup family has 2 children: Bjarne Cplusplus 7 | -------------------------------------------------------------------------------- /Chapter 5. Object-Oriented Programming/5.4.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Essential C++ 3 | // Stanley Lippman 4 | // Chen Chen @ December 29th, 2014 5 | // 6 | 7 | Library Material 8 | book 9 | audio book 10 | rental book 11 | CD-ROM book 12 | record 13 | children's puppet 14 | video 15 | video game 16 | Sega video game 17 | Sony Playstation video game 18 | Nintendo video game -------------------------------------------------------------------------------- /Chapter 1. Basic C++ Programming/1.1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Essential C++ 3 | // Stanley Lippman 4 | // Chen Chen @ November 26th, 2014 5 | // 6 | 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | int main(int argc, char *argv[]) 13 | { 14 | string userName; 15 | cout << "Please enter your first name: "; 16 | cin >> userName; 17 | cout << endl << "Hello, " << userName << "... and goodbye!" << endl; 18 | return 0; 19 | } -------------------------------------------------------------------------------- /Chapter 1. Basic C++ Programming/1.2.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Essential C++ 3 | // Stanley Lippman 4 | // Chen Chen @ November 26th, 2014 5 | // 6 | 7 | //#include 8 | 9 | string userName; //string is unrecognized. 10 | 11 | 12 | //using namespace std; 13 | 14 | cout << "Please enter your first name: "; //cout is unrecognized. 15 | cin >> userName; //cin is unrecognized. 16 | cout << endl << "Hello, " << userName << "... and goodbye!" << endl; //cout is unrecognized. -------------------------------------------------------------------------------- /Chapter 1. Basic C++ Programming/1.5.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Essential C++ 3 | // Stanley Lippman 4 | // Chen Chen @ November 26th, 2014 5 | // 6 | 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | int main(int argc, char *argv[]) 13 | { 14 | string userName; 15 | cout << "Please enter your name: "; 16 | cin >> userName; 17 | if (userName.size() <= 2) 18 | cout << "Your name is too short!" << endl; 19 | else 20 | cout << "Nice name, hello " << userName << "!" << endl; 21 | return 0; 22 | } -------------------------------------------------------------------------------- /Chapter 3. Generic Programming/TestFile_3.2.txt: -------------------------------------------------------------------------------- 1 | Stanley B. Lippman is a computer scientist and author. He is most widely known as an author of C++ Primer book, which is currently published as 5th edition. He has worked with Bjarne Stroustrup in Bell Laboratories during early stages of C++ development.[1] In 2001, Stanley Lippman became an Architect for Visual C++.[1] In 2007, he joined Emergent Game Technologies.[2]. He then worked for NASA, Pixar and is now working at 2kQubits according to his LinkedIn page. He is to be distinguished from Stan Lippmann, a physicist and perennial political candidate, from Seattle. 2 | -------------------------------------------------------------------------------- /Chapter 1. Basic C++ Programming/1.4.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Essential C++ 3 | // Stanley Lippman 4 | // Chen Chen @ November 26th, 2014 5 | // 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | using namespace std; 12 | 13 | int main(int argc, char *argv[]) 14 | { 15 | vector vecName(2); 16 | cout << "Please enter your first name: "; 17 | cin >> vecName[0]; 18 | cout << "Please enter your last name: "; 19 | cin >> vecName[1]; 20 | for (vector::iterator itr = vecName.begin(), vecEnd = vecName.end(); itr != vecEnd; ++itr) 21 | cout << *itr << " "; 22 | return 0; 23 | } -------------------------------------------------------------------------------- /Chapter 1. Basic C++ Programming/1.8.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Essential C++ 3 | // Stanley Lippman 4 | // Chen Chen @ November 27th, 2014 5 | // 6 | 7 | const char *MessageToUser(int numTries) 8 | { 9 | const int num = 5; 10 | static const char *userMessage[num] = { 11 | "Go on, make a guess.", 12 | "Oops! Nice guess but not quite it.", 13 | "Oh. Sorry. Wrong again.", 14 | "Ah, this is harder than it looks, no?", 15 | "It must be getting pretty frustrating by now!" 16 | }; 17 | if (numTries < 0) 18 | numTries = 0; 19 | else if (numTries >= num) 20 | numTries = num - 1; 21 | return userMessage[numTries]; 22 | } -------------------------------------------------------------------------------- /Chapter 3. Generic Programming/TestFile_3.2.vector: -------------------------------------------------------------------------------- 1 | a a is B. as 2 | is to he is In 3 | He is He to be 4 | as an of of in 5 | an He He is at 6 | In 5th and has C++ 7 | for for and now his 8 | C++ and most Bell then 9 | from Game from Stan with 10 | Pixar known book, which 2001, 11 | 2007, NASA, early page. became 12 | joined widely Bjarne author Primer 13 | during Visual worked stages worked 14 | working Lippman C++.[1] Lippman author. 15 | Stanley Stanley LinkedIn edition. Seattle. 16 | computer Emergent 2kQubits according Architect 17 | published currently scientist Lippmann, physicist 18 | perennial political Stroustrup candidate, Laboratories 19 | distinguished development.[1] Technologies.[2]. -------------------------------------------------------------------------------- /Chapter 1. Basic C++ Programming/1.6.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Essential C++ 3 | // Stanley Lippman 4 | // Chen Chen @ November 27th, 2014 5 | // 6 | 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | int main(int argc, char *argv[]) 13 | { 14 | int number, sum = 0; 15 | vector vecNumber; 16 | cout << "Please enter some integers: "; 17 | while (cin >> number) 18 | vecNumber.push_back(number); 19 | for (vector::iterator itr = vecNumber.begin(), vecEnd = vecNumber.end(); itr != vecEnd; ++itr) 20 | sum += *itr; 21 | cout << "The sum is " << sum << endl; 22 | double average = sum / vecNumber.size(); 23 | cout << "The average is " << average << endl; 24 | return 0; 25 | } -------------------------------------------------------------------------------- /Chapter 4. Object-Based Programming/Stack_4.1.h: -------------------------------------------------------------------------------- 1 | // 2 | // Essential C++ 3 | // Stanley Lippman 4 | // Chen Chen @ December 7th, 2014 5 | // 6 | 7 | #ifndef STACK_H_ 8 | #define STACK_H_ 9 | 10 | #include 11 | #include 12 | 13 | using namespace std; 14 | 15 | class Stack 16 | { 17 | public: 18 | bool Empty() const 19 | { 20 | return vecStack.empty(); 21 | } 22 | bool Full() const 23 | { 24 | return vecStack.size() >= vecStack.max_size(); 25 | } 26 | int Size() const 27 | { 28 | return vecStack.size(); 29 | } 30 | bool Push(const string &elem); 31 | bool Pop(string &elem); 32 | bool Peek(string &elem); 33 | 34 | private: 35 | vector vecStack; 36 | }; 37 | 38 | #endif -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Essential C++ 2 | ## By Stanley Lippman 3 | ## Exercises Solutions of Essential C++, of which the author is Chester Chen. 4 | ## November 26th, 2014 - January 13th, 2015 5 | 6 | ### Contents 7 | 8 | - [Chapter 1. Basic C++ Programming](Chapter 1. Basic C++ Programming) 9 | - [Chapter 2. Procedural Programming](Chapter 2. Procedural Programming) 10 | - [Chapter 3. Generic Programming](Chapter 3. Generic Programming) 11 | - [Chapter 4. Object-Based Programming](Chapter 4. Object-Based Programming) 12 | - [Chapter 5. Object-Oriented Programming](Chapter 5. Object-Oriented Programming) 13 | - [Chapter 6. Programming With Templates](Chapter 6. Programming With Templates) 14 | - [Chapter 7. Exception Handling](Chapter 7. Exception Handling) -------------------------------------------------------------------------------- /Chapter 3. Generic Programming/TestFile_3.1.txt: -------------------------------------------------------------------------------- 1 | Essential C++ offers a fast-track to learning and working with C++. This book is specifically designed to bring you up to speed in a short amount of time. It focuses on the elements of C++ programming that you are most likely to encounter and examines features and techniques that help solve real-world programming challenges. Essential C++ presents the basics of C++ in the context of procedural, generic, object-based, and object-oriented programming. It is organized around a series of increasingly complex programming problems, and language features are introduced as solutions to these problems. In this way you will not only learn about the functions and structure of C++, but will understand their purpose and rationale. 2 | -------------------------------------------------------------------------------- /Chapter 7. Exception Handling/7.1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Essential C++ 3 | // Stanley Lippman 4 | // Chen Chen @ January 12th, 2014 5 | // 6 | 7 | ifstream infile(file_name); //Type do not match. 8 | 9 | ifstream infile(file_name.c_str()); 10 | if (!infile.is_open()) //Open failed or not. 11 | return 0; 12 | 13 | ifstream infile(file_name.c_str()); 14 | if (!infile.is_open()) 15 | return 0; 16 | int elem_cnt; 17 | infile >> elem_cnt; 18 | if (!infile.is_open()) //Open failed or not. 19 | return 0; 20 | 21 | ifstream infile(file_name.c_str()); 22 | if (!infile.is_open()) 23 | return 0; 24 | int elem_cnt; 25 | infile >> elem_cnt; 26 | if (!infile.is_open()) 27 | return 0; 28 | int *pi = allocate_array(elem_cnt); 29 | if (!pi) //Memory allocation succeed or not. 30 | return 0; -------------------------------------------------------------------------------- /Chapter 4. Object-Based Programming/Stack_4.2.h: -------------------------------------------------------------------------------- 1 | // 2 | // Essential C++ 3 | // Stanley Lippman 4 | // Chen Chen @ December 7th, 2014 5 | // 6 | 7 | #ifndef STACK_H_ 8 | #define STACK_H_ 9 | 10 | #include 11 | #include 12 | 13 | using namespace std; 14 | 15 | class Stack 16 | { 17 | public: 18 | bool Empty() const 19 | { 20 | return vecStack.empty(); 21 | } 22 | bool Full() const 23 | { 24 | return vecStack.size() >= vecStack.max_size(); 25 | } 26 | int Size() const 27 | { 28 | return vecStack.size(); 29 | } 30 | bool Push(const string &elem); 31 | bool Pop(string &elem); 32 | bool Peek(string &elem); 33 | bool Find(const string &elem) const; 34 | int Count(const string &elem) const; 35 | 36 | private: 37 | vector vecStack; 38 | }; 39 | 40 | #endif -------------------------------------------------------------------------------- /Chapter 5. Object-Oriented Programming/Peekback_Stack_5.2.h: -------------------------------------------------------------------------------- 1 | // 2 | // Essential C++ 3 | // Stanley Lippman 4 | // Chen Chen @ December 29th, 2014 5 | // 6 | 7 | #ifndef PEEKBACK_STACK_H_ 8 | #define PEEKBACK_STACK_H_ 9 | 10 | #include "Stack.h" 11 | 12 | class Peekback_Stack : public Stack 13 | { 14 | public: 15 | Peekback_Stack(int room = 0) : Stack(room) {} 16 | ~Peekback_Stack() {} 17 | virtual bool Peek(int num, string &elem); 18 | }; 19 | 20 | bool Peekback_Stack::Peek(int num, string &elem) 21 | { 22 | if (Empty()) { 23 | cerr << "Sorry, the stack is empty." << endl; 24 | return false; 25 | } 26 | if (num < 0 || num >= Size()) { 27 | cerr << "Sorry, the number is wrong." << endl; 28 | return false; 29 | } 30 | elem = vecStack[num]; 31 | return true; 32 | } 33 | 34 | #endif -------------------------------------------------------------------------------- /Chapter 5. Object-Oriented Programming/Stack_5.1.h: -------------------------------------------------------------------------------- 1 | // 2 | // Essential C++ 3 | // Stanley Lippman 4 | // Chen Chen @ December 29th, 2014 5 | // 6 | 7 | #ifndef STACK_H_ 8 | #define STACK_H_ 9 | 10 | #include 11 | #include 12 | 13 | using namespace std; 14 | 15 | class Stack 16 | { 17 | public: 18 | virtual ~Stack() {} 19 | virtual bool Pop(string &elem) = 0; 20 | virtual bool Push(const string &elem) = 0; 21 | virtual bool Peek(int num, string &elem) = 0; 22 | virtual bool Empty() const = 0; 23 | virtual bool Full() const = 0; 24 | virtual int Size() const = 0; 25 | virtual int Top() const = 0; 26 | virtual void Print(ostream &os = cout) const = 0; 27 | }; 28 | 29 | ostream &operator<<(ostream &os, const Stack &myStack) 30 | { 31 | myStack.Print(); 32 | return os; 33 | } 34 | 35 | #endif -------------------------------------------------------------------------------- /Chapter 6. Programming With Templates/6.1.h: -------------------------------------------------------------------------------- 1 | // 2 | // Essential C++ 3 | // Stanley Lippman 4 | // Chen Chen @ December 29th, 2014 5 | // 6 | 7 | #ifndef EXAMPLE_H_ 8 | #define EXAMPLE_H_ 9 | 10 | template 11 | class Example 12 | { 13 | public: 14 | Example(const Type &min, const Type &max); 15 | Example(const Type *array, int size); 16 | Type opeartor[](int index); 17 | bool opeartor==(const Example &) const; 18 | bool Insert(const Type *, int); 19 | bool Insert(Type &); 20 | Type Min() const 21 | { 22 | return _min; 23 | } 24 | Type Max() const 25 | { 26 | return _max; 27 | } 28 | void Min(const Type &); 29 | void Max(const Type &); 30 | int Count(const Type &value) const; 31 | 32 | private: 33 | int _size; 34 | Type *parray; 35 | Type _min; 36 | Type _max; 37 | }; 38 | 39 | #endif -------------------------------------------------------------------------------- /Chapter 1. Basic C++ Programming/1.7.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Essential C++ 3 | // Stanley Lippman 4 | // Chen Chen @ November 27th, 2014 5 | // 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | using namespace std; 14 | 15 | int main(int argc, char *argv[]) 16 | { 17 | ifstream readFile("TestFile_1.7.txt"); 18 | if (! readFile.is_open()) 19 | cerr << "Sorry, the file fails to read!" << endl; 20 | string word; 21 | vector vecWord; 22 | while (readFile >> word) 23 | vecWord.push_back(word); 24 | cout << "Original text: "; 25 | for (vector::iterator itr = vecWord.begin(), vecEnd = vecWord.end(); itr != vecEnd; ++itr) 26 | cout << *itr << " "; 27 | sort(vecWord.begin(),vecWord.end()); 28 | cout << endl << "Sorted text: "; 29 | for (vector::iterator itr = vecWord.begin(), vecEnd = vecWord.end(); itr != vecEnd; ++itr) 30 | cout << *itr << " "; 31 | return 0; 32 | } -------------------------------------------------------------------------------- /Chapter 2. Procedural Programming/2.2.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Essential C++ 3 | // Stanley Lippman 4 | // Chen Chen @ November 30th, 2014 5 | // 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | using namespace std; 12 | 13 | bool PentaElem(vector &ivec, int pos) 14 | { 15 | if (pos <= 0 || pos > 100) { 16 | cerr << "Wrong position! "; 17 | return false; 18 | } 19 | for (int ix = 1; ix <= pos; ++ix) 20 | ivec.push_back(ix * (3 * ix - 1) / 2); 21 | return true; 22 | } 23 | 24 | void DisplayElem(const vector &ivec, const string &title) 25 | { 26 | cout << title << endl; 27 | for (vector::iterator itr = ivec.begin(), vecEnd = ivec.end(); itr != vecEnd; ++itr) 28 | cout << *itr << " "; 29 | } 30 | 31 | int main(int argc, char *argv[]) 32 | { 33 | vector vecPentagonal; 34 | int pos; 35 | cout << "Please enter a position: "; 36 | cin >> pos; 37 | const string title("Pentagonal Numeric Series"); 38 | if (PentaElem(vecPentagonal, pos)) 39 | DisplayElem(vecPentagonal, title); 40 | return 0; 41 | } -------------------------------------------------------------------------------- /Chapter 3. Generic Programming/TestFile_3.1.map: -------------------------------------------------------------------------------- 1 | C++-4 2 | C++,-1 3 | C++.-1 4 | Essential-2 5 | In-1 6 | It-2 7 | This-1 8 | about-1 9 | amount-1 10 | are-2 11 | around-1 12 | as-1 13 | basics-1 14 | book-1 15 | bring-1 16 | challenges.-1 17 | complex-1 18 | context-1 19 | designed-1 20 | elements-1 21 | encounter-1 22 | examines-1 23 | fast-track-1 24 | features-2 25 | focuses-1 26 | functions-1 27 | generic,-1 28 | help-1 29 | in-2 30 | increasingly-1 31 | introduced-1 32 | is-2 33 | language-1 34 | learn-1 35 | learning-1 36 | likely-1 37 | most-1 38 | not-1 39 | object-based,-1 40 | object-oriented-1 41 | offers-1 42 | on-1 43 | only-1 44 | organized-1 45 | presents-1 46 | problems,-1 47 | problems.-1 48 | procedural,-1 49 | programming-3 50 | programming.-1 51 | purpose-1 52 | rationale.-1 53 | real-world-1 54 | series-1 55 | short-1 56 | solutions-1 57 | solve-1 58 | specifically-1 59 | speed-1 60 | structure-1 61 | techniques-1 62 | that-2 63 | their-1 64 | these-1 65 | this-1 66 | time.-1 67 | understand-1 68 | up-1 69 | way-1 70 | will-2 71 | with-1 72 | working-1 73 | -------------------------------------------------------------------------------- /Chapter 5. Object-Oriented Programming/5.2.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Essential C++ 3 | // Stanley Lippman 4 | // Chen Chen @ December 29th, 2014 5 | // 6 | 7 | #include "Stack.h" 8 | #include "Peekback_Stack.h" 9 | 10 | using namespace std; 11 | 12 | void Peek(Stack &myStack, int num) 13 | { 14 | string str; 15 | if (myStack.Peek(num, str)) 16 | cout << "Peek: " << str << endl; 17 | else 18 | cerr << "Peek failed!" << endl; 19 | } 20 | 21 | int main(int argc, char *argv[]) 22 | { 23 | Stack lifoStack; 24 | Peekback_Stack peekbackStack; 25 | string str; 26 | cout << "Please enter some strings: "; 27 | while (cin >> str && !lifoStack.Full()) 28 | lifoStack.Push(str); 29 | cout << "Peek() of LIFO_Stack: " << endl; 30 | Peek(lifoStack, lifoStack.Top() - 1); 31 | cout << lifoStack; 32 | while (!lifoStack.Empty()) { 33 | string temp; 34 | if (lifoStack.Pop(temp)) 35 | peekbackStack.Push(temp); 36 | } 37 | cout << "Peek() of Peekback_Stack: " << endl; 38 | Peek(peekbackStack, peekbackStack.Top() - 1); 39 | cout << peekbackStack; 40 | return 0; 41 | } -------------------------------------------------------------------------------- /Chapter 7. Exception Handling/7.2.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Essential C++ 3 | // Stanley Lippman 4 | // Chen Chen @ January 12th, 2014 5 | // 6 | 7 | int *alloc_and_init(string file_name) 8 | { 9 | ifstream infile(file_name.c_str()); 10 | if (!infile) 11 | return 0; 12 | int elem_cnt; 13 | infile >> elem_cnt; 14 | if (!infile) 15 | return 0; 16 | try { 17 | int *pi = allocate_array(elem_cnt); 18 | int elem; 19 | int index = 0; 20 | while (infile >> elem && index < elem_cnt) 21 | pi[index++] = elem; 22 | sort_array(pi, elem_cnt); 23 | register_data(pi); 24 | } 25 | catch (const noMem &memFail) { 26 | cerr << "alloc_and_init(): allocate_array failure!" << memFail.what() << endl; 27 | return 0; 28 | } 29 | catch (int &sortFail) { 30 | cerr << "alloc_and_init(): sort_array failure!" << "thrown integer value: " << sortFail << endl; 31 | return 0; 32 | } 33 | catch (string ®isterFail) { 34 | cerr << "alloc_and_init(): register_data failure!" << "thrown string value: " << registerFail << endl; 35 | return 0; 36 | } 37 | return pi; 38 | } -------------------------------------------------------------------------------- /Chapter 2. Procedural Programming/2.1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Essential C++ 3 | // Stanley Lippman 4 | // Chen Chen @ November 30th, 2014 5 | // 6 | 7 | #include 8 | 9 | using namespace std; 10 | 11 | bool FibonElem(int pos, int &elem) 12 | { 13 | 14 | if (pos <= 0 || pos > 1024) { 15 | cerr << "Wrong position! "; 16 | elem = 0; 17 | return false; 18 | } 19 | elem = 1; 20 | int n_2 = 1, n_1 = 1; 21 | for (int ix = 3; ix <= pos; ++ix) { 22 | elem = n_2 + n_1; 23 | n_2 = n_1; 24 | n_1 = elem; 25 | } 26 | return true; 27 | } 28 | 29 | int main(int argc, char *argv[]) 30 | { 31 | int pos; 32 | char continueFibon = 'y'; 33 | while (continueFibon == 'y') { 34 | cout << "Please enter a position: "; 35 | cin >> pos; 36 | int elem; 37 | if (FibonElem(pos, elem)) 38 | cout << "Element # " << pos << " is " << elem << endl; 39 | else 40 | cout << "Sorry, could not calculate this element # " << pos << endl; 41 | continueFibon = 'n'; 42 | cout << "Would you like to try again(y/n)? "; 43 | cin >> continueFibon; 44 | } 45 | return 0; 46 | } -------------------------------------------------------------------------------- /Chapter 5. Object-Oriented Programming/5.1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Essential C++ 3 | // Stanley Lippman 4 | // Chen Chen @ December 29th, 2014 5 | // 6 | 7 | #include "LIFO_Stack.h" 8 | #include "Peekback_Stack.h" 9 | 10 | using namespace std; 11 | 12 | void Peek(Stack &myStack, int num) 13 | { 14 | string str; 15 | if (myStack.Peek(num, str)) 16 | cout << "Peek: " << str << endl; 17 | else 18 | cerr << "Peek failed!" << endl; 19 | } 20 | 21 | int main(int argc, char *argv[]) 22 | { 23 | LIFO_Stack lifoStack; 24 | Peekback_Stack peekbackStack; 25 | string str; 26 | cout << "Please enter some strings: "; 27 | while (cin >> str && !lifoStack.Full()) 28 | lifoStack.Push(str); 29 | cout << "Peek() of LIFO_Stack: " << endl; 30 | Peek(lifoStack, lifoStack.Top() - 1); 31 | cout << lifoStack; 32 | while (!lifoStack.Empty()) { 33 | string temp; 34 | if (lifoStack.Pop(temp)) 35 | peekbackStack.Push(temp); 36 | } 37 | cout << "Peek() of Peekback_Stack: " << endl; 38 | Peek(peekbackStack, peekbackStack.Top() - 1); 39 | cout << peekbackStack; 40 | return 0; 41 | } -------------------------------------------------------------------------------- /Chapter 7. Exception Handling/7.3.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Essential C++ 3 | // Stanley Lippman 4 | // Chen Chen @ January 12th, 2014 5 | // 6 | 7 | bool Stack::Pop(string &elem) 8 | { 9 | if (Empty()) { 10 | throw PopEmpty(); 11 | return false; 12 | } 13 | elem = vecStack[--topStack]; 14 | vecStack.pop_back(); 15 | return true; 16 | } 17 | 18 | bool Stack::Push(const string &elem) 19 | { 20 | if (Full()) { 21 | throw PushFull(); 22 | return false; 23 | } 24 | vecStack.push_back(elem); 25 | ++topStack; 26 | return true; 27 | } 28 | 29 | class StackException : public logic_error 30 | { 31 | public: 32 | StackException(const char *what) :myWhat(what) {} 33 | const char *What() const 34 | { 35 | return myWhat.c_str(); 36 | } 37 | 38 | protected: 39 | string myWhat; 40 | }; 41 | 42 | class PopEmpty : public StackException 43 | { 44 | public: 45 | PopEmpty() : StackException("Sorry, the stack is empty.") {} 46 | }; 47 | 48 | class PushFull : public StackException 49 | { 50 | public: 51 | PushFull() : StackException("Sorry, the stack is full.") {} 52 | }; 53 | -------------------------------------------------------------------------------- /Chapter 4. Object-Based Programming/Matrix.h: -------------------------------------------------------------------------------- 1 | // 2 | // Essential C++ 3 | // Stanley Lippman 4 | // Chen Chen @ December 23rd, 2014 5 | // 6 | 7 | #ifndef MATRIX_H_ 8 | #define MATRIX_H_ 9 | 10 | #include 11 | 12 | using namespace std; 13 | 14 | class Matrix 15 | { 16 | friend Matrix operator+(const Matrix &, const Matrix &); 17 | friend Matrix operator*(const Matrix &, const Matrix &); 18 | 19 | public: 20 | Matrix(const float *); 21 | Matrix(float = 0.0, float = 0.0, float = 0.0, float = 0.0, 22 | float = 0.0, float = 0.0, float = 0.0, float = 0.0, 23 | float = 0.0, float = 0.0, float = 0.0, float = 0.0, 24 | float = 0.0, float = 0.0, float = 0.0, float = 0.0); 25 | int Rows() const 26 | { 27 | return 4; 28 | } 29 | int Cols() const 30 | { 31 | return 4; 32 | } 33 | float operator()(int row, int colmun) const 34 | { 35 | return myMatrix[row][colmun]; 36 | } 37 | float &operator()(int row, int colmun) 38 | { 39 | return myMatrix[row][colmun]; 40 | } 41 | void operator+=(const Matrix &); 42 | ostream &Print(ostream &) const; 43 | 44 | private: 45 | float myMatrix[4][4]; 46 | }; 47 | 48 | #endif -------------------------------------------------------------------------------- /Chapter 3. Generic Programming/3.4.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Essential C++ 3 | // Stanley Lippman 4 | // Chen Chen @ December 5th, 2014 5 | // 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | using namespace std; 14 | 15 | class EvenNum 16 | { 17 | public: 18 | bool operator()(int num) 19 | { 20 | return num % 2 ? false : true; 21 | } 22 | }; 23 | 24 | int main(int argc, char *argv[]) 25 | { 26 | ofstream writeOddFile("TestFile_3.4.odd"); 27 | ofstream writeEvenFile("TestFile_3.4.even"); 28 | if (!writeOddFile.is_open() || !writeEvenFile.is_open()) { 29 | cerr << "Sorry, the file fails to read!" << endl; 30 | return -1; 31 | } 32 | vector vecInput; 33 | cout << "Please enter some numbers: "; 34 | istream_iterator inItr(cin), eos; 35 | copy(inItr, eos, back_inserter(vecInput)); 36 | vector::iterator itr = partition(vecInput.begin(), vecInput.end(), EvenNum()); 37 | ostream_iterator oddItr(writeOddFile, " "), evenItr(writeEvenFile, "\n"); 38 | copy(vecInput.begin(), itr, evenItr); 39 | copy(itr, vecInput.end(), oddItr); 40 | return 0; 41 | } -------------------------------------------------------------------------------- /Chapter 2. Procedural Programming/2.3.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Essential C++ 3 | // Stanley Lippman 4 | // Chen Chen @ November 30th, 2014 5 | // 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | using namespace std; 12 | 13 | inline bool JudgeElem(const vector &ivec, int pos) 14 | { 15 | if (pos <= 0 || pos > 100) { 16 | cerr << "Wrong position! "; 17 | return false; 18 | } 19 | return true; 20 | } 21 | 22 | void PentaElem(vector &ivec, int pos) 23 | { 24 | for (int ix = 1; ix <= pos; ++ix) 25 | ivec.push_back(ix * (3 * ix - 1) / 2); 26 | } 27 | 28 | void DisplayElem(const vector &ivec, const string &title) 29 | { 30 | cout << title << endl; 31 | for (vector::iterator itr = ivec.begin(), vecEnd = ivec.end(); itr != vecEnd; ++itr) 32 | cout << *itr << " "; 33 | } 34 | 35 | int main(int argc, char *argv[]) 36 | { 37 | vector vecPentagonal; 38 | int pos; 39 | cout << "Please enter a position: "; 40 | cin >> pos; 41 | const string title("Pentagonal Numeric Series"); 42 | if (JudgeElem(vecPentagonal, pos)) { 43 | PentaElem(vecPentagonal, pos); 44 | DisplayElem(vecPentagonal, title); 45 | } 46 | return 0; 47 | } -------------------------------------------------------------------------------- /Chapter 2. Procedural Programming/2.4.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Essential C++ 3 | // Stanley Lippman 4 | // Chen Chen @ November 30th, 2014 5 | // 6 | 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | inline bool JudgeElem(int pos) 13 | { 14 | if (pos <= 0 || pos > 100) { 15 | cerr << "Wrong position! "; 16 | return false; 17 | } 18 | return true; 19 | } 20 | 21 | const vector *PentaFunc(int pos) 22 | { 23 | static vector ivec; 24 | if ((JudgeElem(pos)) && (pos > ivec.size())) 25 | for (int ix = ivec.size(); ix <= pos; ++ix) 26 | ivec.push_back(ix * (3 * ix - 1) / 2); 27 | return &ivec; 28 | } 29 | 30 | bool PentaElem(int pos, int &elem) 31 | { 32 | const vector *ivec = PentaFunc(pos); 33 | if (!JudgeElem(pos)) { 34 | cerr << "Wrong position! "; 35 | elem = 0; 36 | return false; 37 | } 38 | elem = (*ivec)[pos - 1]; 39 | return true; 40 | } 41 | 42 | int main(int argc, char *argv[]) 43 | { 44 | int pos, elem; 45 | cout << "Please enter a position: "; 46 | cin >> pos; 47 | if (JudgeElem(pos)) { 48 | PentaElem(pos + 1, elem); 49 | cout << "Element # " << pos << " is " << elem << endl; 50 | } 51 | return 0; 52 | } -------------------------------------------------------------------------------- /Chapter 3. Generic Programming/3.2.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Essential C++ 3 | // Stanley Lippman 4 | // Chen Chen @ December 5th, 2014 5 | // 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | using namespace std; 14 | 15 | class LessThan 16 | { 17 | public: 18 | bool operator()(const string &s1, const string &s2) 19 | { 20 | return s1.size() < s2.size(); 21 | } 22 | }; 23 | 24 | void InitVector(vector &svec, ifstream &ifile) 25 | { 26 | string word; 27 | while (ifile >> word) 28 | svec.push_back(word); 29 | } 30 | 31 | void DisplayVector(vector &svec, ofstream &ofile) 32 | { 33 | int i = 1; 34 | for (vector::iterator itr = svec.begin(), vecEnd = svec.end(); itr != vecEnd; ++itr) 35 | ofile << *itr << ((i++ % 5) ? ' ' : '\n'); 36 | } 37 | 38 | int main(int argc, char *argv[]) 39 | { 40 | ifstream readFile("TestFile_3.2.txt"); 41 | ofstream writeFile("TestFile_3.2.vector"); 42 | if (!readFile.is_open() || !writeFile.is_open()) { 43 | cerr << "Sorry, the file fails to read!" << endl; 44 | return -1; 45 | } 46 | vector vecWord; 47 | InitVector(vecWord, readFile); 48 | sort(vecWord.begin(), vecWord.end(), LessThan()); 49 | DisplayVector(vecWord, writeFile); 50 | return 0; 51 | } -------------------------------------------------------------------------------- /Chapter 4. Object-Based Programming/4.1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Essential C++ 3 | // Stanley Lippman 4 | // Chen Chen @ December 7th, 2014 5 | // 6 | 7 | #include 8 | #include "Stack.h" 9 | 10 | using namespace std; 11 | 12 | bool Stack::Push(const string &elem) 13 | { 14 | if (Full()) 15 | return false; 16 | vecStack.push_back(elem); 17 | return true; 18 | } 19 | 20 | bool Stack::Pop(string &elem) 21 | { 22 | if (Empty()) 23 | return false; 24 | elem = vecStack.back(); 25 | vecStack.pop_back(); 26 | return true; 27 | } 28 | 29 | bool Stack::Peek(string &elem) 30 | { 31 | if (Empty()) 32 | return false; 33 | elem = vecStack.back(); 34 | return true; 35 | } 36 | 37 | int main(int argc, char *argv[]) 38 | { 39 | Stack testStack; 40 | string word; 41 | cout << "Please enter some strings: "; 42 | while (cin >> word && !testStack.Full()) 43 | testStack.Push(word); 44 | if (testStack.Empty()) 45 | cerr << "Sorry, the stack is empty." << endl; 46 | if (testStack.Peek(word)) 47 | cout << "There are " << testStack.Size() << " strings in the stack." << endl; 48 | cout << "The reverse order: "; 49 | while (testStack.Size()) 50 | if (testStack.Pop(word)) 51 | cout << word << " "; 52 | cout << endl << "There are " << testStack.Size() << " strings in the stack." << endl; 53 | return 0; 54 | } -------------------------------------------------------------------------------- /Chapter 2. Procedural Programming/2.6.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Essential C++ 3 | // Stanley Lippman 4 | // Chen Chen @ November 30th, 2014 5 | // 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | template 15 | Type max(Type x1, Type x2) 16 | { 17 | return x1 > x2 ? x1 : x2; 18 | } 19 | 20 | template 21 | vecType max(const vector &ivec) 22 | { 23 | return *max_element(ivec.begin(), ivec.end()); 24 | } 25 | 26 | template 27 | arrayType max(const arrayType *array, int size) 28 | { 29 | return *max_element(array, array + size); 30 | } 31 | 32 | int main(int argc, char *argv[]) 33 | { 34 | int intArray[] = { 5, 10, 15, 20, 25 }; 35 | vector intVec(intArray, intArray + 5); 36 | double douArray[] = { 5.1, 5.5, 10.3, 15.6, 20.8 }; 37 | vector douVec(douArray, douArray + 5); 38 | string strArray[] = { 39 | "we", 40 | "were", 41 | "going", 42 | "to", 43 | "school" 44 | }; 45 | vector strVec(strArray, strArray + 5); 46 | int intMax = max(max(intVec), max(intArray, 5)); 47 | double douMax = max(max(douVec), max(douArray, 5)); 48 | string strMax = max(max(strVec), max(strArray, 5)); 49 | cout << "The max of integers is " << intMax << endl; 50 | cout << "The max of doubles is " << douMax << endl; 51 | cout << "The max of strings is " << strMax << endl; 52 | return 0; 53 | } -------------------------------------------------------------------------------- /Chapter 4. Object-Based Programming/4.3.h: -------------------------------------------------------------------------------- 1 | // 2 | // Essential C++ 3 | // Stanley Lippman 4 | // Chen Chen @ December 7th, 2014 5 | // 6 | 7 | #ifndef GLOBALDATA_H_ 8 | #define GLOBALDATA_H_ 9 | 10 | #include 11 | 12 | using namespace std; 13 | 14 | class GlobalData 15 | { 16 | public: 17 | GlobalData(); 18 | ~GlobalData(); 19 | static string ProgramName() 20 | { 21 | return program_name; 22 | } 23 | static string VersionStamp() 24 | { 25 | return version_stamp; 26 | } 27 | static int VersionNumber() 28 | { 29 | return version_number; 30 | } 31 | static int TestsRun() 32 | { 33 | return tests_run; 34 | } 35 | static int TestsPassed() 36 | { 37 | return tests_passed; 38 | } 39 | static void ProgramName(const string &strName) 40 | { 41 | program_name = strName; 42 | } 43 | static void VersionStamp(const string &strStamp) 44 | { 45 | version_stamp = strStamp; 46 | } 47 | static void VersionNumber(const int &intNumber) 48 | { 49 | version_number = intNumber; 50 | } 51 | static void TestsRun(const int &intRun) 52 | { 53 | tests_run = intRun; 54 | } 55 | static void TestsPassed(const int &intPassed) 56 | { 57 | tests_passed = intPassed; 58 | } 59 | 60 | private: 61 | static string program_name; 62 | static string version_stamp; 63 | static int version_number; 64 | static int tests_run; 65 | static int tests_passed; 66 | }; 67 | 68 | string GlobalData::program_name; 69 | string GlobalData::version_stamp; 70 | int GlobalData::version_number; 71 | int GlobalData::tests_run; 72 | int GlobalData::tests_passed; 73 | 74 | #endif -------------------------------------------------------------------------------- /Chapter 5. Object-Oriented Programming/LIFO_Stack.h: -------------------------------------------------------------------------------- 1 | // 2 | // Essential C++ 3 | // Stanley Lippman 4 | // Chen Chen @ December 29th, 2014 5 | // 6 | 7 | #ifndef LIFO_STACK_H_ 8 | #define LIFO_STACK_H_ 9 | 10 | #include 11 | #include "Stack.h" 12 | 13 | class LIFO_Stack : public Stack 14 | { 15 | public: 16 | LIFO_Stack(int room = 0) : topStack(0) 17 | { 18 | if (room) 19 | vecStack.reserve(room); 20 | } 21 | ~LIFO_Stack() {} 22 | bool Empty() const 23 | { 24 | return vecStack.empty(); 25 | } 26 | bool Full() const 27 | { 28 | return vecStack.size() >= vecStack.max_size(); 29 | } 30 | int Size() const 31 | { 32 | return vecStack.size(); 33 | } 34 | int Top() const 35 | { 36 | return topStack; 37 | } 38 | bool Peek(int num, string &elem) 39 | { 40 | return false; 41 | } 42 | bool Pop(string &elem); 43 | bool Push(const string &elem); 44 | void Print(ostream &os = cout) const; 45 | 46 | private: 47 | vector vecStack; 48 | int topStack; 49 | }; 50 | 51 | bool LIFO_Stack::Pop(string &elem) 52 | { 53 | if (Empty()) { 54 | cerr << "Sorry, the stack is empty." << endl; 55 | return false; 56 | } 57 | elem = vecStack[--topStack]; 58 | vecStack.pop_back(); 59 | return true; 60 | } 61 | 62 | bool LIFO_Stack::Push(const string &elem) 63 | { 64 | if (Full()) { 65 | cerr << "Sorry, the stack is full." << endl; 66 | return false; 67 | } 68 | vecStack.push_back(elem); 69 | ++topStack; 70 | return true; 71 | } 72 | 73 | void LIFO_Stack::Print(ostream &os) const 74 | { 75 | for (vector::const_reverse_iterator itr = vecStack.rbegin(), vecEnd = vecStack.rend(); itr != vecEnd; ++itr) 76 | os << *itr << " "; 77 | os << endl; 78 | } 79 | 80 | #endif -------------------------------------------------------------------------------- /Chapter 4. Object-Based Programming/4.2.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Essential C++ 3 | // Stanley Lippman 4 | // Chen Chen @ December 7th, 2014 5 | // 6 | 7 | #include 8 | #include 9 | #include "Stack.h" 10 | 11 | using namespace std; 12 | 13 | bool Stack::Push(const string &elem) 14 | { 15 | if (Full()) 16 | return false; 17 | vecStack.push_back(elem); 18 | return true; 19 | } 20 | 21 | bool Stack::Pop(string &elem) 22 | { 23 | if (Empty()) 24 | return false; 25 | elem = vecStack.back(); 26 | vecStack.pop_back(); 27 | return true; 28 | } 29 | 30 | bool Stack::Peek(string &elem) 31 | { 32 | if (Empty()) 33 | return false; 34 | elem = vecStack.back(); 35 | return true; 36 | } 37 | 38 | bool Stack::Find(const string &elem) const 39 | { 40 | return ::find(vecStack.begin(), vecStack.end(), elem) != vecStack.end(); 41 | } 42 | 43 | int Stack::Count(const string &elem) const 44 | { 45 | return ::count(vecStack.begin(), vecStack.end(), elem); 46 | } 47 | 48 | int main(int argc, char *argv[]) 49 | { 50 | Stack testStack; 51 | string word; 52 | cerr << "Please enter some strings: "; 53 | while (cin >> word && !testStack.Full()) 54 | testStack.Push(word); 55 | if (testStack.Empty()) 56 | cout << "Sorry, the stack is empty." << endl; 57 | if (testStack.Peek(word)) 58 | cout << "There are " << testStack.Size() << " strings in the stack." << endl; 59 | cin.clear(); 60 | cout << "Please enter the word you want to query: "; 61 | cin >> word; 62 | bool testFind = testStack.Find(word); 63 | int testCount = testFind ? testStack.Count(word) : 0; 64 | if (testFind) 65 | cout << "The word " << word << " is in the stack, and it occurs " << testCount << " times." << endl; 66 | else 67 | cout << "The word " << word << " is not in the stack." << endl; 68 | return 0; 69 | } -------------------------------------------------------------------------------- /Chapter 6. Programming With Templates/Matrix.h: -------------------------------------------------------------------------------- 1 | // 2 | // Essential C++ 3 | // Stanley Lippman 4 | // Chen Chen @ January 12th, 2014 5 | // 6 | 7 | #ifndef MATRIX_H_ 8 | #define MATRIX_H_ 9 | 10 | #include 11 | 12 | using namespace std; 13 | 14 | template 15 | class Matrix 16 | { 17 | friend Matrix operator+(const Matrix &, const Matrix &); 18 | friend Matrix operator*(const Matrix &, const Matrix &); 19 | 20 | public: 21 | Matrix(const Matrix &temp) 22 | { 23 | myRows = temp.myRows; 24 | myColmuns = temp.myColmuns; 25 | int size = myColmuns * myRows; 26 | myMatrix = new Type[size]; 27 | for (int i = 0; i < size; ++i) 28 | myMatrix[i] = temp.myMatrix[i]; 29 | } 30 | Matrix(int rows, int colmuns) : myRows(rows), myColmuns(colmuns) 31 | { 32 | int size = myColmuns * myColmuns; 33 | myMatrix = new Type[size]; 34 | for (int i = 0; i < size; ++i) 35 | myMatrix[i] = Type(); 36 | } 37 | ~Matrix() 38 | { 39 | delete [] myMatrix; 40 | } 41 | int Rows() const 42 | { 43 | return myRows; 44 | } 45 | int Cols() const 46 | { 47 | return myColmuns; 48 | } 49 | Type &operator()(int row, int colmun) 50 | { 51 | return myMatrix[row * Cols() + colmun]; 52 | } 53 | const Type &operator()(int row, int colmun) const 54 | { 55 | return myMatrix[row * Cols() + colmun]; 56 | } 57 | bool SameSize(const Matrix &mat) const 58 | { 59 | return Rows() == mat.Rows() && Cols() == mat.Cols(); 60 | } 61 | bool Comfortable(const Matrix &mat) const 62 | { 63 | return Cols() == mat.Rows; 64 | } 65 | Matrix &operator=(const Matrix &); 66 | void operator+=(const Matrix &); 67 | ostream &Print(ostream &) const; 68 | 69 | protected: 70 | int myRows; 71 | int myColmuns; 72 | Type *myMatrix; 73 | }; 74 | 75 | #endif -------------------------------------------------------------------------------- /Chapter 5. Object-Oriented Programming/Stack_5.2.h: -------------------------------------------------------------------------------- 1 | // 2 | // Essential C++ 3 | // Stanley Lippman 4 | // Chen Chen @ December 29th, 2014 5 | // 6 | 7 | #ifndef STACK_H_ 8 | #define STACK_H_ 9 | 10 | #include 11 | #include 12 | #include 13 | 14 | using namespace std; 15 | 16 | class Stack 17 | { 18 | public: 19 | Stack(int room = 0) : topStack(0) 20 | { 21 | if (room) 22 | vecStack.reserve(room); 23 | } 24 | virtual ~Stack() {} 25 | bool Empty() const 26 | { 27 | return vecStack.empty(); 28 | } 29 | bool Full() const 30 | { 31 | return vecStack.size() >= vecStack.max_size(); 32 | } 33 | int Size() const 34 | { 35 | return vecStack.size(); 36 | } 37 | int Top() const 38 | { 39 | return topStack; 40 | } 41 | virtual bool Peek(int num, string &elem) 42 | { 43 | return false; 44 | } 45 | bool Pop(string &elem); 46 | bool Push(const string &elem); 47 | void Print(ostream &os = cout) const; 48 | 49 | protected: 50 | vector vecStack; 51 | int topStack; 52 | }; 53 | 54 | bool Stack::Pop(string &elem) 55 | { 56 | if (Empty()) { 57 | cerr << "Sorry, the stack is empty." << endl; 58 | return false; 59 | } 60 | elem = vecStack[--topStack]; 61 | vecStack.pop_back(); 62 | return true; 63 | } 64 | 65 | bool Stack::Push(const string &elem) 66 | { 67 | if (Full()) { 68 | cerr << "Sorry, the stack is full." << endl; 69 | return false; 70 | } 71 | vecStack.push_back(elem); 72 | ++topStack; 73 | return true; 74 | } 75 | 76 | void Stack::Print(ostream &os /* = cout */) const 77 | { 78 | for (vector::const_reverse_iterator itr = vecStack.rbegin(), vecEnd = vecStack.rend(); itr != vecEnd; ++itr) 79 | os << *itr << " "; 80 | os << endl; 81 | } 82 | 83 | ostream &operator<<(ostream &os, const Stack &myStack) 84 | { 85 | myStack.Print(); 86 | return os; 87 | } 88 | 89 | #endif -------------------------------------------------------------------------------- /Chapter 2. Procedural Programming/2.5.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Essential C++ 3 | // Stanley Lippman 4 | // Chen Chen @ November 30th, 2014 5 | // 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | int max(int x1, int x2) 15 | { 16 | return x1 > x2 ? x1 : x2; 17 | } 18 | 19 | double max(double x1, double x2) 20 | { 21 | return x1 > x2 ? x1 : x2; 22 | } 23 | 24 | string max(string x1, string x2) 25 | { 26 | return x1 > x2 ? x1 : x2; 27 | } 28 | 29 | int max(const vector &ivec) 30 | { 31 | return *max_element(ivec.begin(), ivec.end()); 32 | } 33 | 34 | double max(const vector &ivec) 35 | { 36 | return *max_element(ivec.begin(), ivec.end()); 37 | } 38 | 39 | string max(const vector &vec) 40 | { 41 | return *max_element(ivec.begin(), ivec.end()); 42 | } 43 | 44 | int max(const int *array, int size) 45 | { 46 | return *max_element(array, array + size); 47 | } 48 | 49 | double max(const double *array, int size) 50 | { 51 | return *max_element(array, array + size); 52 | } 53 | 54 | string max(const string *array, int size) 55 | { 56 | return *max_element(array, array + size); 57 | } 58 | 59 | int main(int argc, char *argv[]) 60 | { 61 | int intArray[] = { 5, 10, 15, 20, 25 }; 62 | vector intVec(intArray, intArray + 5); 63 | double douArray[] = { 5.1, 5.5, 10.3, 15.6, 20.8 }; 64 | vector douVec(douArray, douArray + 5); 65 | string strArray[] = { 66 | "we", 67 | "were", 68 | "going", 69 | "to", 70 | "school" 71 | }; 72 | vector strVec(strArray, strArray + 5); 73 | int intMax = max(max(intVec), max(intArray, 5)); 74 | double douMax = max(max(douVec), max(douArray, 5)); 75 | string strMax = max(max(strVec), max(strArray, 5)); 76 | cout << "The max of integers is " << intMax << endl; 77 | cout << "The max of doubles is " << douMax << endl; 78 | cout << "The max of strings is " << strMax << endl; 79 | return 0; 80 | } -------------------------------------------------------------------------------- /Chapter 3. Generic Programming/3.1.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Essential C++ 3 | // Stanley Lippman 4 | // Chen Chen @ December 5th, 2014 5 | // 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | using namespace std; 14 | 15 | void InitExclusionSet(set &sset) 16 | { 17 | string exclusionWord[10] = { "a", "an", "or", "the", "and", "but", "to", "you", "I", "of" }; 18 | sset.insert(exclusionWord, exclusionWord + 10); 19 | } 20 | 21 | void ProcessFile(map &simap, const set &sset, ifstream &ifile) 22 | { 23 | string word; 24 | while (ifile >> word) { 25 | if (sset.count(word)) 26 | continue; 27 | simap[word]++; 28 | } 29 | } 30 | 31 | void UserQuery(map &simap) 32 | { 33 | string queryWord; 34 | cout << "Please enter the word you want to query: "; 35 | cin >> queryWord; 36 | int i = 0; 37 | for (map::iterator itr = simap.begin(), mapEnd = simap.end(); itr != mapEnd; ++itr) { 38 | i++; 39 | if (itr->first == queryWord) { 40 | cout << "The word " << itr->first << " has appeared " << itr->second << " times." << endl; 41 | break; 42 | } 43 | } 44 | if (i >= simap.size()) 45 | cout << "Sorry, the word " << queryWord << " is not found." << endl; 46 | } 47 | 48 | void DisplayWordCount(map &simap, ofstream &ofile) 49 | { 50 | for (map::iterator itr = simap.begin(), mapEnd = simap.end(); itr != mapEnd; ++itr) 51 | ofile << itr->first << "-" << itr->second << endl; 52 | } 53 | 54 | int main(int argc, char *argv[]) 55 | { 56 | ifstream readFile("TestFile_3.1.txt"); 57 | ofstream writeFile("TestFile_3.1.map"); 58 | if (!readFile.is_open() || !writeFile.is_open()) { 59 | cerr << "Sorry, the file fails to read!" << endl; 60 | return -1; 61 | } 62 | map mapWord; 63 | set setExclusion; 64 | InitExclusionSet(setExclusion); 65 | ProcessFile(mapWord, setExclusion, readFile); 66 | UserQuery(mapWord); 67 | DisplayWordCount(mapWord, writeFile); 68 | return 0; 69 | } -------------------------------------------------------------------------------- /Chapter 5. Object-Oriented Programming/Peekback_Stack_5.1.h: -------------------------------------------------------------------------------- 1 | // 2 | // Essential C++ 3 | // Stanley Lippman 4 | // Chen Chen @ December 29th, 2014 5 | // 6 | 7 | #ifndef PEEKBACK_STACK_H_ 8 | #define PEEKBACK_STACK_H_ 9 | 10 | #include 11 | #include "Stack.h" 12 | 13 | class Peekback_Stack : public Stack 14 | { 15 | public: 16 | Peekback_Stack(int room = 0) : topStack(0) 17 | { 18 | if (room) 19 | vecStack.reserve(room); 20 | } 21 | ~Peekback_Stack() {} 22 | bool Empty() const 23 | { 24 | return vecStack.empty(); 25 | } 26 | bool Full() const 27 | { 28 | return vecStack.size() >= vecStack.max_size(); 29 | } 30 | int Size() const 31 | { 32 | return vecStack.size(); 33 | } 34 | int Top() const 35 | { 36 | return topStack; 37 | } 38 | bool Pop(string &elem); 39 | bool Push(const string &elem); 40 | bool Peek(int num, string &elem); 41 | void Print(ostream &os = cout) const; 42 | 43 | private: 44 | vector vecStack; 45 | int topStack; 46 | }; 47 | 48 | bool Peekback_Stack::Pop(string &elem) 49 | { 50 | if (Empty()) { 51 | cerr << "Sorry, the stack is empty." << endl; 52 | return false; 53 | } 54 | elem = vecStack[--topStack]; 55 | vecStack.pop_back(); 56 | return true; 57 | } 58 | 59 | bool Peekback_Stack::Push(const string &elem) 60 | { 61 | if (Full()) { 62 | cerr << "Sorry, the stack is full." << endl; 63 | return false; 64 | } 65 | vecStack.push_back(elem); 66 | ++topStack; 67 | return true; 68 | } 69 | 70 | bool Peekback_Stack::Peek(int num, string &elem) 71 | { 72 | if (Empty()) { 73 | cerr << "Sorry, the stack is empty." << endl; 74 | return false; 75 | } 76 | if (num < 0 || num >= Size()) { 77 | cerr << "Sorry, the number is wrong." << endl; 78 | return false; 79 | } 80 | elem = vecStack[num]; 81 | return true; 82 | } 83 | 84 | void Peekback_Stack::Print(ostream &os /* = cout */) const 85 | { 86 | for (vector::const_reverse_iterator itr = vecStack.rbegin(), vecEnd = vecStack.rend(); itr != vecEnd; ++itr) 87 | os << *itr << " "; 88 | os << endl; 89 | } 90 | 91 | #endif -------------------------------------------------------------------------------- /Chapter 4. Object-Based Programming/UserProfile.h: -------------------------------------------------------------------------------- 1 | // 2 | // Essential C++ 3 | // Stanley Lippman 4 | // Chen Chen @ December 22nd, 2014 5 | // 6 | 7 | #ifndef USERPROFILE_H_ 8 | #define USERPROFILE_H_ 9 | 10 | #include 11 | #include 12 | 13 | using namespace std; 14 | 15 | class UserProfile 16 | { 17 | public: 18 | enum userLevel { 19 | Beginner, 20 | Intermediate, 21 | Advanced, 22 | Guru 23 | }; 24 | UserProfile(); 25 | UserProfile(string login, userLevel = Beginner); 26 | string UserLogin() const 27 | { 28 | return userLogin; 29 | } 30 | string UserName() const 31 | { 32 | return userName; 33 | } 34 | int LoginCount() const 35 | { 36 | return timesLogged; 37 | } 38 | int GuessCount() const 39 | { 40 | return timesGuessed; 41 | } 42 | int GuessCorrect() const 43 | { 44 | return correctGuessed; 45 | } 46 | void ResetLogin(const string &val) 47 | { 48 | userLogin = val; 49 | } 50 | void UserName(const string &val) 51 | { 52 | userName = val; 53 | } 54 | void ResetLevel(const string &str); 55 | void ResetLevel(userLevel newLevel) 56 | { 57 | nowLevel = newLevel; 58 | } 59 | void ResetLoginCount(int val) 60 | { 61 | timesLogged = val; 62 | } 63 | void ResetGuessCount(int val) 64 | { 65 | timesGuessed = val; 66 | } 67 | void ResetGuessCorrect(int val) 68 | { 69 | correctGuessed = val; 70 | } 71 | void BumpLoginCount(int num = 1) 72 | { 73 | timesLogged += num; 74 | } 75 | void BumpGuessCount(int num = 1) 76 | { 77 | timesGuessed += num; 78 | } 79 | void BumpGuessCorrect(int num = 1) 80 | { 81 | correctGuessed += num; 82 | } 83 | bool operator==(const UserProfile &temp); 84 | bool operator!=(const UserProfile &temp); 85 | double GuessAverage() const; 86 | string UserLevel() const; 87 | 88 | private: 89 | string userLogin; 90 | string userName; 91 | int timesLogged; 92 | int timesGuessed; 93 | int correctGuessed; 94 | userLevel nowLevel; 95 | static map levelMap; 96 | static void InitLevelMap(); 97 | static string GuestLogin(); 98 | }; 99 | 100 | #endif -------------------------------------------------------------------------------- /Chapter 3. Generic Programming/3.3.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Essential C++ 3 | // Stanley Lippman 4 | // Chen Chen @ December 5th, 2014 5 | // 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | using namespace std; 14 | 15 | void InitMap(map > &svmap, ifstream &ifile) 16 | { 17 | string textline; 18 | while (getline(ifile, textline, '\n')) { 19 | string familyName; 20 | vector child; 21 | string::size_type nowPos = 0, prevPos = 0, textSize = textline.size(); 22 | if (!textSize) 23 | continue; 24 | while ((nowPos = textline.find_first_of(' ', nowPos)) != string::npos) { 25 | string::size_type endPos = nowPos - prevPos; 26 | if (!prevPos) 27 | familyName = textline.substr(prevPos, endPos); 28 | else 29 | child.push_back(textline.substr(prevPos, endPos)); 30 | prevPos = ++nowPos; 31 | } 32 | if (prevPos < textSize) 33 | child.push_back(textline.substr(prevPos, nowPos - prevPos)); 34 | if (!svmap.count(familyName)) 35 | svmap[familyName] = child; 36 | else 37 | cerr << "Sorry, we already have a "<< familyName << " family in our map!\n"; 38 | } 39 | } 40 | 41 | void DisplayMap(map > &svmap, ofstream &ofile) 42 | { 43 | for (map >::iterator itr = svmap.begin(), mapEnd = svmap.end(); itr != mapEnd; ++itr) { 44 | ofile << "The " << itr->first << " family "; 45 | if (itr->second.empty()) 46 | ofile << "has no children." << endl; 47 | else { 48 | ofile << "has " << itr->second.size() << " children: "; 49 | for (vector::iterator itrvec = itr->second.begin(), vecEnd = itr->second.end(); itrvec != vecEnd; ++itrvec) 50 | ofile << *itrvec << " "; 51 | ofile << endl; 52 | } 53 | } 54 | } 55 | 56 | void UserQuery(map > &svmap) 57 | { 58 | string queryName; 59 | cout << "Please enter a family name you want to query: "; 60 | cin >> queryName; 61 | int i = 0; 62 | for (map >::iterator itr = svmap.begin(), mapEnd = svmap.end(); itr != mapEnd; ++itr) { 63 | i++; 64 | if (itr->first == queryName) { 65 | cout << "The " << itr->first << " family has " << itr->second.size() << " children: "; 66 | for (vector::iterator itrvec = itr->second.begin(), vecEnd = itr->second.end(); itrvec != vecEnd; ++itrvec) 67 | cout << *itrvec << " "; 68 | break; 69 | } 70 | } 71 | if (i >= svmap.size()) 72 | cout << "Sorry, the " << queryName << " family is not found." << endl; 73 | } 74 | 75 | int main(int argc, char *argv[]) 76 | { 77 | ifstream readFile("TestFile_3.3.txt"); 78 | ofstream writeFile("TestFile_3.3.map"); 79 | if (!readFile.is_open() || !writeFile.is_open()) { 80 | cerr << "Sorry, the file fails to read!" << endl; 81 | return -1; 82 | } 83 | map > mapFamily; 84 | InitMap(mapFamily, readFile); 85 | DisplayMap(mapFamily, writeFile); 86 | UserQuery(mapFamily); 87 | return 0; 88 | } -------------------------------------------------------------------------------- /Chapter 4. Object-Based Programming/4.5.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Essential C++ 3 | // Stanley Lippman 4 | // Chen Chen @ December 23rd, 2014 5 | // 6 | 7 | #include "Matrix.h" 8 | 9 | using namespace std; 10 | 11 | inline ostream &operator<<(ostream &os, const Matrix &mat) 12 | { 13 | return mat.Print(os); 14 | } 15 | 16 | Matrix operator+(const Matrix &mat1, const Matrix &mat2) 17 | { 18 | Matrix result(mat1); 19 | result += mat2; 20 | return result; 21 | } 22 | 23 | Matrix operator*(const Matrix &mat1, const Matrix &mat2) 24 | { 25 | Matrix result; 26 | for (int ix = 0; ix < mat1.Rows(); ++ix) { 27 | for (int jx = 0; jx < mat1.Cols(); ++jx) { 28 | result(ix, jx) = 0; 29 | for (int kx = 0; kx < mat1.Cols(); ++kx) 30 | result(ix, jx) += mat1(ix, kx) * mat2(kx, jx); 31 | } 32 | } 33 | return result; 34 | } 35 | 36 | void Matrix::operator+=(const Matrix &mat) 37 | { 38 | for (int ix = 0; ix < 4; ++ix) 39 | for (int jx = 0; jx < 4; ++jx) 40 | myMatrix[ix][jx] += mat.myMatrix[ix][jx]; 41 | } 42 | 43 | ostream &Matrix::Print(ostream &os) const 44 | { 45 | int num = 0; 46 | for (int ix = 0; ix < 4; ++ix) { 47 | for (int jx = 0; jx < 4; ++jx, ++num) { 48 | if (num && !(num % 8)) 49 | os << endl; 50 | os << myMatrix[ix][jx] << ' '; 51 | } 52 | } 53 | os << endl; 54 | return os; 55 | } 56 | 57 | Matrix::Matrix(const float *array) 58 | { 59 | int arrayIndex = 0; 60 | for (int ix = 0; ix < 4; ++ix) 61 | for (int jx = 0; jx < 4; ++jx) 62 | myMatrix[ix][jx] = array[arrayIndex++]; 63 | 64 | } 65 | 66 | Matrix::Matrix(float a11, float a12, float a13, float a14, 67 | float a21, float a22, float a23, float a24, 68 | float a31, float a32, float a33, float a34, 69 | float a41, float a42, float a43, float a44) 70 | { 71 | myMatrix[0][0] = a11; 72 | myMatrix[0][1] = a12; 73 | myMatrix[0][2] = a13; 74 | myMatrix[0][3] = a14; 75 | myMatrix[1][0] = a21; 76 | myMatrix[1][1] = a22; 77 | myMatrix[1][2] = a23; 78 | myMatrix[1][3] = a24; 79 | myMatrix[2][0] = a31; 80 | myMatrix[2][1] = a32; 81 | myMatrix[2][2] = a33; 82 | myMatrix[2][3] = a34; 83 | myMatrix[3][0] = a41; 84 | myMatrix[3][1] = a42; 85 | myMatrix[3][2] = a43; 86 | myMatrix[3][3] = a44; 87 | } 88 | 89 | int main(int argc, char *argv[]) 90 | { 91 | Matrix mat1; 92 | cout << "Matrix 1: " << endl << mat1 << endl; 93 | float arr1[16] = { 94 | 1.0, 0.0, 0.0, 0.0, 95 | 0.0, 1.0, 0.0, 0.0, 96 | 0.0, 0.0, 1.0, 0.0, 97 | 0.0, 0.0, 0.0, 1.0 98 | }; 99 | Matrix identity(arr1); 100 | cout << "Matrix identity: " << endl << identity << endl; 101 | Matrix mat2(identity); 102 | mat1 = identity; 103 | cout << "Matrix 2: " << endl << mat2 << endl; 104 | cout << "Matrix 1: " << endl << mat1 << endl; 105 | float arr2[16] = { 106 | 1.3f, 0.4f, 2.6f, 8.2f, 107 | 6.2f, 1.7f, 1.3f, 8.3f, 108 | 4.2f, 7.4f, 2.7f, 1.9f, 109 | 6.3f, 8.1f, 5.6f, 6.6f 110 | }; 111 | Matrix mat3(arr2); 112 | cout << "Matrix 3: " << endl << mat3 << endl; 113 | Matrix mat4 = mat3 * identity; 114 | cout << "Matrix 4: " << endl << mat4 << endl; 115 | Matrix mat5 = mat3 + mat4; 116 | cout << "Matrix 5: " << endl << mat5 << endl; 117 | mat3 += mat4; 118 | cout << "Matrix 3: " << endl << mat3 << endl; 119 | return 0; 120 | } -------------------------------------------------------------------------------- /Chapter 4. Object-Based Programming/4.4.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Essential C++ 3 | // Stanley Lippman 4 | // Chen Chen @ December 22nd, 2014 5 | // 6 | 7 | #include 8 | #include 9 | #include "UserProfile.h" 10 | 11 | using namespace std; 12 | 13 | inline UserProfile::UserProfile() : userLogin("guest"), nowLevel(Beginner), timesLogged(1), timesGuessed(0), correctGuessed(0) 14 | { 15 | static int id = 0; 16 | char buffer[16]; 17 | _itoa(id++, buffer, 10); 18 | userLogin += buffer; 19 | } 20 | 21 | inline UserProfile::UserProfile(string login, userLevel level) : userLogin(login), nowLevel(level), timesLogged(1), timesGuessed(0), correctGuessed(0) {} 22 | 23 | inline bool UserProfile::operator==(const UserProfile &temp) 24 | { 25 | if (userLogin == temp.userLogin && userName == temp.userName) 26 | return true; 27 | else 28 | return false; 29 | } 30 | 31 | inline bool UserProfile::operator!=(const UserProfile &temp) 32 | { 33 | return !(*this == temp); 34 | } 35 | 36 | inline double UserProfile::GuessAverage() const 37 | { 38 | return timesGuessed ? (double)correctGuessed / (double)timesGuessed * 100 : 0.0; 39 | } 40 | 41 | inline string UserProfile::UserLevel() const 42 | { 43 | static string levelTable[] = { 44 | "Beginner", 45 | "Intermediate", 46 | "Advanced", 47 | "Guru" 48 | }; 49 | return levelTable[nowLevel]; 50 | } 51 | 52 | map UserProfile::levelMap; 53 | 54 | ostream &operator<<(ostream &os, const UserProfile &rhs) 55 | { 56 | os << "Username: " << rhs.UserLogin() << endl 57 | << "User Level: " << rhs.UserLevel() << endl 58 | << "User Login Times: " << rhs.LoginCount() << endl 59 | << "User Guess Times: " << rhs.GuessCount() << endl 60 | << "User Correct Guess: " << rhs.GuessCorrect() << endl 61 | << "User Guess Average: " << rhs.GuessAverage() << endl; 62 | return os; 63 | } 64 | 65 | inline void UserProfile::ResetLevel(const string &level) 66 | { 67 | if (levelMap.empty()) 68 | InitLevelMap(); 69 | map::iterator itr; 70 | nowLevel = ((itr = levelMap.find(level)) != levelMap.end()) ? itr->second : Beginner; 71 | } 72 | void UserProfile::InitLevelMap() 73 | { 74 | levelMap["Beginner"] = Beginner; 75 | levelMap["Intermediate"] = Intermediate; 76 | levelMap["Advanced"] = Advanced; 77 | levelMap["Guru"] = Guru; 78 | } 79 | 80 | istream &operator>>(istream &is, UserProfile &rhs) 81 | { 82 | string login, level; 83 | is >> login >> level; 84 | int lcount, gcount, gcorrect; 85 | is >> lcount >> gcount >> gcorrect; 86 | rhs.ResetLogin(login); 87 | rhs.ResetLevel(level); 88 | rhs.ResetLoginCount(lcount); 89 | rhs.ResetGuessCount(gcount); 90 | rhs.ResetGuessCorrect(gcorrect); 91 | return is; 92 | } 93 | 94 | int main(int argc, char *argv[]) 95 | { 96 | UserProfile newMan; 97 | cout << newMan; 98 | UserProfile anotherMan; 99 | cout << anotherMan; 100 | UserProfile otherMan("ChesterC", UserProfile::Guru); 101 | cout << otherMan; 102 | otherMan.BumpGuessCount(27); 103 | otherMan.BumpGuessCorrect(25); 104 | otherMan.BumpLoginCount(); 105 | cout << otherMan; 106 | cout << "Please enter user profile data to see if we can read it\n"; 107 | cin >> newMan; 108 | cout << newMan; 109 | return 0; 110 | } --------------------------------------------------------------------------------