Friday, 10 September 2021

Chapter 20 // Exercise 12 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 20 // Exercise 12

Complete the definition of list from section 20.4.1-2 and get the high() example to run. Allocate a Link to represent one past the end.


This one involves some copying from the book but not all the functions in List need implementing, I'm sure he'll have us do that at some point. When implementing pushFront(), I almost forgot to make the current first's previous pointer point at the new front. It's not needed to make this exercise work but the iterators can go forwards and backwards.

I also initially forgot to clean up memory. It's a good thing Irun _CrtDumpMemoryLeaks() at the end of every program to remind me. For that I just created a destructor in MyList that deletes each link until it reaches nullptr.

I was about to post and realised that I hadn't made a Link that points at one past the end. To implement the Tail, I made it null first, created the Head, then newed up the Tail and made the Head point at the tail. This allows easy access to the last element in the list by just using mTail->mPrev->mValue.


Monday, 30 August 2021

Chapter 20 // Exercise 11 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 20 // Exercise 11

Given a list<int> as a (by-reference) parameter, make a vector<double> and copy the elements of the list into it. Verify that the copy was complete and correct. Then print the elements sorted in increasing value.


This one was pretty simple as std::vector has a handy range constructor (since C++98). Basically you give it an iterator to a start and end position in the container and it will copy over those values into the vector. It's a nice one-liner:
vector<double> doubleVector(list.begin(), list.end());

Sunday, 29 August 2021

Chapter 20 // Exercise 10 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 20 // Exercise 10

Define a version of the word-counting program where the user can specify the set of whitespace characters.


I guess for this one he meant that the user can supply several characters at once so "a, 1, -, 9" are "whitespace" characters. For this I created a simple function called isCustomWhitespace(char c, string s). This used std::strings find() function to find c in s. I could then just use this like I did in the previous exercise with isspace() and isalpha().

Saturday, 28 August 2021

Chapter 20 // Exercise 9 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 20 // Exercise 9

Define a program that counts the number of words in a Document. Provide two versions: one that defines word as " a whitespace-separated sequence of characters" and one that defines word as "a sequence of consecutive alphabetic characters." For example, with the former definition, alpha.numeric and as12b are both single words, whereas with the second definition they are both two words.


This table on character handling functions is extremely useful:

I didn't know that isspace() will count control characters like \n as spaces. It made this exercise a lot simpler.

For the first one I iterated through every character and if the current character was a space then I increased the word count; but only if the previous character was not a space.

For the seconds one I increased the word count if the current character was not an alphabetic character; but only if the previous character was an alphabetic character.

Thursday, 26 August 2021

Chapter 20 // Exercise 8 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 20 // Exercise 8

Define a function that counts the number of characters in a Document.


This one was pretty simple. Instead of iterating through every character in the document, I just added up the sizes of the vectors in the list that makes up a document.