Friday, 27 May 2022

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

Write a program, based on the program that finds lines containing dates (exercise 6), that finds all dates and reformats them to the ISO yyyy-mm-dd format. The program should take an input file and produce and output file that is identical to the input file except for the changed date formatting.


Had a look for a replace type regex and found the standard libraries regex_replace()
This will replace all matches it finds on the same line in one go. Amazing.

I then went off down a tangent using substring to replace certain parts of the string, then attempted to convert the string to time_t to convert the time_t to a particular format. I then bashed my head against the wall and simply pushed the 3 dates into a vector then concatenated them into whatever format I wanted.

This exercise took me longer than expected as C# regex can simply replace one date format with another in 1 line....if only.

To save some time I decided not to do ALL dates and changed only the mm/dd/yyyy and dd/mm/yyyy formats.

Thursday, 26 May 2022

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

Modify the table-checking program from section 23.9 to see if the number of students is increasing or decreasing over the years in question.


I used the iterators in the map created from the previous exercise to easily check the totals over the years and do a quick print out on whether it was increasing/decreasing.

Wednesday, 25 May 2022

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

Modify the table-checking program from section 23.9 to write a new table where the rows with the same initial digit (indication the year: first grades start with 1) are merged.


I honestly was really puzzled by this one. I guess he meant do exactly the same but while checking the lines create a new file where years are merged? So instead of seperate lines for 1A and 1B they get merged into 1? I hope that's what he meant because that's what I did.

I tried to keep things relatively simple. I originally thought of pushing back the table into a vector then erasing lines to create the table but then remembered this chapter was also about maps, not just regex. Maps can store a unique key which was perfect for checking to see if the current line had already been pushed back with the class sizes.

Tuesday, 24 May 2022

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

Using eof(), it is possible to determine which line of a table is the last. Use that to (try to) simplify the table-checking program from section 23.9. Be sure to test your program with files that end with empty lines after the table and with files that don't end with a newline at all.


from_string() also has to be created for the code to work. This is found on page 853 but I added it to my running version of std_lib_facilities.

This was an annoying one to read as it's hard to determine how many tab/spaces there are in the printed book. I also had to type up the table as he doesn't provide it and made sure it matched the patterns he gave.

I also misunderstood the question and thought that he was suggesting that you could use eof() to determine newlines?? My brain goes to strange places. Anyway, I added a simple check that looks for new lines anywhere to skip them.




Monday, 23 May 2022

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

Modify the program from section 23.8.7 so that it takes as inputs a pattern and a file name. Its output should be the numbered lines (line-number: line) that contain a match of the pattern. If no matches are found, no output should be produced.


This didn't require much changing from the code given; just adding in the lines to get a filename and open it.