Homework 5 solution

$24.99

Original Work ?
Category: You will Instantly receive a download link for .ZIP solution file upon Payment

Description

5/5 - (1 vote)

1. Encryption: 10 Points
File: encryption.cpp For this assignment you’ll write a simple substitution cipher known as ROT13. This method works by shifting the letters of the source text by 13 letters to produce the encrypted output, successfully garbling the output but providing no real security. The signature for the function is given below, you’ll take in a std::string & and should “encrypt” it in place. The signature of the function you should write is: void encrypt(std::string &str); You shouldn’t need to perform any memory allocation to solve this problem. You may also find the ASCII table useful for this assignment. Your encryption function should also support upper and lower case letters and keep them distinct from each other, for example ‘A’ should encrypt to ‘N’ and ‘a’ should encrypt
1
to ‘n’. The input to your program will always just be lowercase and uppercase letters so don’t worry about handling numbers or punctuation or so on.
Figure 1: Example ROT13 encryption from Wikipedia Example Input: 5 HelLozzZ AbCdEfgHijklMnOpQrStuVwXYZGHAwdaowjwa HiHowAreYoU FunKadElic SupErcaLifRagiliSticExpiAlidOcious Expected Output: Case 0: UryYbmmM Case 1: NoPqRstUvwxyZaBcDeFghIjKLMTUNjqnbjwjn Case 2: UvUbjNerLbH Case 3: ShaXnqRyvp Case 4: FhcRepnYvsEntvyvFgvpRkcvNyvqBpvbhf
2
2. Parsing Integers: 30 Points
File: parse_int.cpp In this problem you will write a function to parse an integer from a string that is robust (including overflow) to invalid integers. You cannot use any functions provided by the standard library for parsing an integer from a string, including std::cin to an integer variable. Your function will receive the string as a const std::string & and write the parsed integer to an integer out parameter. You’ll need to use an out parameter because the function will return a bool (true or false) to signal if parsing was successful or not. The signature of the function you’re writing should be: bool parse_int(const std::string &str, int &val); Your parser should be able to handle positive and negative numbers along with slightly odd styles, e.g. +40 should parse as positive 40. You should read the string from std::cin into a std::string. In the case that you encounter something that is not a digit (recall the functions in the cctype header) you should stop parsing and return false to indicate parsing failed. In main you’ll then see if parse_int returned true or false, if it returns true simply print out the number, if it returns false print out ‘Parsing failed’. How can we determine the value of an integer from a char? Recall that we saw that ASCII characters were just numbers, so we can subtract them from each other, for example: ‘2’ – ‘0’ = 2. Example Input 7 1542 -9852 +92 10 hello! 10oops123 123456789012354444444444444444 Expected Output: Case 0: 1542 Case 1: -9852 Case 2: 92 Case 3: 10 Case 4:
3
Parsing failed Case 5: Parsing failed Case 6: Parsing failed
3. Label Generator: 20 points
File: label_generator.cpp For certain applications, it is useful to be able to generate a series of names that form a sequential pattern. For example, if you were writing a program to number figures in a paper, having some mechanism to return the sequence of strings “Figure 1”, “Figure 2”, “Figure 3”, and so on, would be very handy. However, you might also need to label points in a geometric diagram, in which case you would want a similar but independent set of labels for points such as “P0”, “P1”, “P2”, and so forth. If you think about this problem more generally, the tool you need is a label generator that allows the client to define arbitrary sequences of labels, each of whichconsistsofaprefixstring(“Figure”or“P”fortheexamplesinthepreceding paragraph) coupled with an integer used as a sequence number. Because the client may want different sequences to be active simultaneously, it makes sense to define the label generator as an abstract type called LabelGenerator. To initialize a new generator, the client provides the prefix string and the initial index as arguments to the LabelGenerator constructor. Once the generator has been created, the client can return new labels in the sequence by calling next_label() on the LabelGenerator. The constructor’s function signature should be: LabelGenerator(const std::string &prefix, int start); The signature of next_label should be: std::string next_label(); Your implementation should match these function signatures, you should not changethem. Asahintyourlabelprintingshouldbehavesimilartothefollowing code: LabelGenerator point_numbers(“P”, 0); for (int i = 0; i < 3; i++) { std::cout << point_numbers.next_label() << " "; } should print P0 P1 P2. 4 Input and output Theinputforeachcasewillconsistoftwolines. Onthefirstlineisaprefixstring which may contain spaces (including leading or trailing spaces). On the second line are two integers which are the initial and the final values of a sequence. Your program will print the sequence of labels on one line, adjacent terms separated by a space. To turn an integer into a string, you can use the std::to_string() functiondefinedin