Thursday, 5 March 2020

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

In this exercise I am using Visual Studio Community 2017 and the header file "std_lib_facilities.h" which can be found here:

http://www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h

Chapter 11 // Exercise 11

Write a function vector<string> split(const string& s, const string& w) that
returns a vector of whitespace-separated substrings from the argument s, where
whitespace is defined as "ordinary whitespace" plus the characters in w.
Again, on this one I'm assuming that he means add a break at whitespace and the string w? Eg if w is "break" and the line "I like to eat donuts on my break everyday" is entered; the following would be entered in the vector:
[0] I
[1]
[2] like
[3]
[4] to
[5]
[6] eat
[7]
[8] donuts
[9]
[10] on
[11]
[12] my
[13]
[14] everyday




Wednesday, 4 March 2020

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

In this exercise I am using Visual Studio Community 2017 and the header file "std_lib_facilities.h" which can be found here:

http://www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h

Chapter 11 // Exercise 10

 Write a function vector<string> split(const string& s) that returns a vector of whitespace-separated substrings from the argument s.
On this one I'm assuming he just meant "read in the entire thing push back single words and spaces". So that's what I did. It's a vector that goes "word", " ", "word", " ", etc.

Tuesday, 3 March 2020

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

In this exercise I am using Visual Studio Community 2017 and the header file "std_lib_facilities.h" which can be found here:

http://www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h

Chapter 11 // Exercise 9

Split the binary I/O program from section 11.3.2 into two; one program that
converts an ordinary text file into binary and one program that reads binary
and converts it to text. Test these programs by comparing a text file with
what you get by converting it to binary and back.
When just running the code given in the book on a random file, I noticed that when reading it out to a new file, it cut off the last two characters. They weren't special ones just 'e' and '.'...After staring at the code and the book for a good 15 minutes I realised that I truly am an idiot because the program in the book is designed to read in integers...

After changing int to char it worked as expected, however I'm now wondering what was so different about 'e' compared to the other letters.




Monday, 2 March 2020

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

In this exercise I am using Visual Studio Community 2017 and the header file "std_lib_facilities.h" which can be found here:

http://www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h

Chapter 11 // Exercise 8

         Use the program from the previous exercise to make a dictionary (as an alternative to the approach in section 11.7). Run the result on a multi-page text file, look at the result, and see if you can improve the program to make a better dictionary.
Make a dictionary? Is this guy having a giraffe? Also what is a multi-page text file? A txt document doesn't have pages. Also, having read over section 17 again I think he just wants us to run the previous program on a text file and remove all punctuation and contractions, whilst displaying each word on a new line. I can do that easy however that's not a dictionary. A dictionary has definitions of what each word means.

Also, most dictionaries allow some forms of contractions; but they do tend to differ on what is allowed depending on the country. The same applies to hyphenated words as some are common enough to be included in dictionaries. So I decided to allow contractions, hyphens and remove any other form of punctuation and numbers and then output each word on a separate line after being sorted. I started getting a bit pedantic about things as well like removing duplicated words.

Sunday, 1 March 2020

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

In this exercise I am using Visual Studio Community 2017 and the header file "std_lib_facilities.h" which can be found here:

http://www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h

Chapter 11 // Exercise 7

Modify the program from the previous exercise so that it replaces don't with
do not, can't with cannot, etc.; leaves hyphens within words intact (so that
we get " do not use the as-if rule "); and converts all characters to lower 
case.
Eurgh. He just had to say 'etc'  on the contraction words. There are 17 common words that use the 't contraction and only one of them doesn't have a space (cannot). I'm super lazy, so there is no way I'm checking to see if the second to last character is a single quote and then checking if that word is equal to 1 of 17 contractions. 

I ended up putting the contractions and their counterparts into a text file, then reading them into vectors at the start of the program. When a user comes along and then inputs 'don't' it will then just compare the string against the vector of contractions, if there is a match, the index will be used to return the matching 'normal phrase'. You can find the contractions.txt in the main section of Chapter 11 on the git.

That wasn't even the hardest part. I spent the longest ensuring that hyphens were only removed when not wrapped in quotes and were by themselves.