operator<) to a struct,
To do this, you will implement a simple C++ program that can (1) count the total number of times each letter occurs in the strings in a dictionary file, (2) give a score to each letter so that more frequently occurring letters have lower scores than rarer ones, and (3) generate and emit a tile definition file (like the one from the previous lab) based on the letters, their frequencies, and their calculated scores.
In this lab and in the subsequent labs, you are encouraged to re-use and modify code from your previous lab solutions - if you choose do this, please put a completely new copy of any header and source files from your previous lab(s) in the new lab project directory you create. This can avoid some potential for error or confusion within Visual Studio, and also ensures you have a backup copy that you could go back to if a modification you try doesn't pan out.
In the next lab assignment you will use these features to generate permutations of letter tiles, check each permutation against a dictionary, and generate a score for each matching permutation.
Note: the details of this assignment are again intentionally somewhat under-specified, leaving you some room to choose what you think is the best way to implement them, as long as what you do is reasonable and you explain your design decisions in comments in the code and in your Readme.txt file.
argv[0]), and another giving the
rest of the information to convey to the user. As in the lab 1 assignment, the
function should print out the program name and how to run the program using those
arguments, and should return a non-zero integer.
LetterTileDef) that contains a letter tile (e.g., of type
LetterTile if you so named that struct in lab 1), and an
unsigned integer (of type unsigned int) that counts how many occurances of
that tile there are.
In that struct, declare a less than operator that takes a reference
to a const letter tile definition object and returns a bool. Define the
operator (in a separate source file for that struct) so that it returns
true if (and only if) either (1) the current object's count is less than the count of the
object passed in, or (2) the counts are the same but the character in the current object's
letter tile is less than that of the object passed in. In addition to taking
a reference to a const object (which promises that the code will not change the object that was
passed), the operator also should promise not to change the current object by putting the
const keyword after the closing parenthesis of its signature, in both its
declaration and its definition.
vector<string> &)
and a reference to a (non-const) vector of letter tile definitions (e.g., of type
vector<LetterTileDef> &) and returns an integer (0 for success, or
non-zero to indicate it encountered a problem).
The function should first form a single string by concatenating all the strings in the
first vector, and then sort the letters in the resulting string into ascending order using
the STL sort algorithm. For example, if the first vector contained "all" and
"good" then concatenating them would give "allgood" and sorting the concatenated string would
give "adglloo".
The function should then count the number of times each letter appears in the sorted concatenated string, and for each letter push back a letter tile definition (using the letter, a score of 0, and the count that was just obtained) into the second vector that was passed to the function.
The function should then sort the vector of tile definitions (which should order them according to the less-than operator given above) and assign a score to each tile definition so that tile definitions that occur less frequently have higher scores than ones that occur more frequently (you are free to assign the same scores to tile definitions with the same occurrence counts, though that is not strictly required).
const vector<LetterTileDef> & if you named your
struct type LetterTileDef)
and a C-style string (of type char *) that has a default argument value of
"tile_defs.txt" (that is, if the argument is not supplied where the function is called,
that value will be used by default).This function should open a file stream using the C-style string given in the second function parameter as the name of the file to open and write out one line at a time to that file with the letter, score, and count for each letter tile definition in the vector appearing together on a line with spaces in between them. If the file cannot be opened or the function encounters any other problems during its execution it should print out a helpful error message indicating the problem and return a non-zero value; otherwise it should return 0 to indicate success.
Send the zip file containing your lab 2 solution as an e-mail attachment to the course e-mail account (cse332@cec.wustl.edu) by the submission deadline for this assignment. If you need to make changes to your lab solution you are welcome to send a new zip file, and we will grade the latest one received prior to the deadline (according to the time stamp the server puts on the e-mail).
Please ask for help from your professor or teaching assistant if you are uncertain about how to do this extra credit part or if you run into any difficulty with it:
If the string has any character that does not have a corresponding tile definition, or if the number of times any character appears in the string exceeds the count in the corresponding tile definition, the function should return 0. The function should return a negative value if it ran into any other problem during execution. Otherwise, the function should return the sum of the scores for each of the characters appearing in the string.
For example if the letters 'd', 'e', 'g', and 'l' all have scores of 2 and counts of 1, letter 'o' has a score of 1 and a count of 2, and no other characters had definitions, then "good" would score 6, but both "google" ('g' appears more times than its count) and "dove" ('v' does not have a corresponding definition) would score 0.
Hint: even though the vector of strings is const (which means you cannot modify the strings in it, you can create a separate string that contains the same characters, and then do things like sorting the characters in the separate string etc. which can really help in implementing the requirements above).
cout).