Saturday, 21 May 2022

Chapter 23 // Exercise 6 - 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 23 // Exercise 6

Write a program that finds dates in a text file. Write out each line containing at least one date in the format line-number: line. Start with a regular expression for a simple format, e.g., 12/24/2000, and test the program with that. Then, add more formats.


First I copied the text from this page: 
https://help.scribesoft.com/scribe/en/sol/general/datetime.htm into a text file for testing and used https://regex101.com/ to help me create the right patterns.

After that I ended up with 5 different patterns to test various date formats including ISO. I didn't add validation to the patterns as I feel validation should be done once you have the strings, regex is just for searching.

Sunday, 10 April 2022

Chapter 23 // Exercise 5 - 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 23 // Exercise 5

Find a large email message file (thousands of messages) and then time it as written with a multimap and with multimap replaced by an unordered_multimap. Note that our application does not take advantage of the ordering of the multimap.


This one was interesting. I used my random email generator to create a text file with 5555 emails in it.

Then created 2 functions and timed them as he shows on page 1015.

The multimap took 12.591 seconds.
The unordered multimap took 12.48 seconds. 

Saturday, 9 April 2022

Chapter 23 // Exercise 4 - 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 23 // Exercise 4

Find a real email message file (containing real email messages) and modify the email example to extract subject lines from sender names taken as input from the user.


I converted an email from my own account to text (hotmail) and it was very basic with just
From: Name <email>
Sent: DD Month YYYY HH:MM
To: Name <email>
Subject: Subject Message

Message

When a message is forwarded it's exactly the same but Fw: is added after Subject:

So, seeing as how the next exercise needs a text file with thousands of messages I decided to quickly create a "random email generator": 

You can use this to create a text file containing as many email messages as you want for testing.

I then went about changing everything to use wide strings instead so most currency symbols would work. I briefly tried to make emojis work but couldn't be bothered going down that rabbit hole.




Thursday, 7 April 2022

Chapter 23 // Exercise 3 - 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 23 // Exercise 3

Modify the email example from section 23.4 to use regular expressions to find the subject and sender.


So for this I used the site Regex101 to help craft the regex I needed:

This site is great as it gives explanations of what each part of the regex is doing and you can step through matches in the debugger.

For the subject, I assumed that we are finding it only in English and based off the email examples in the section. I got a bit annoyed as apparently std::regex doesn't support the very handy lookbehind feature. So I ended up just searching for "Subject:" then creating a string with everything in the message line after those 9 characters.

I then did the same for the sender.


Friday, 18 March 2022

Chapter 23 // Exercise 2 - 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 23 // Exercise 2

Add a multimap and have it hold subjects. Let the program take an input string from the keyboard and print out every message with that string as its subject.


2 months after starting this chapter I had almost forgotten everything and this took me longer than expected.

The hard part had already been provided by Bjarne though so it was just a case of creating a new multimap that stores messages and subjects and loops through that instead if there is a match.