Vous êtes sur la page 1sur 4

CS246Assignment 2 (Fall 2011)

B. Lushman R. Ahmed

Due Date 1: Friday, October 7, 5pm Due Date 2: Friday, October 14, 5pm

Questions 1, 2, 3a, 4, 5a are due on Due Date 1; the remainder of the assignment is due on Due Date 2. Note: On this and subsequent assignments, you will be required to take responsibility for your own testing. As part of that requirement, this assignment is designed to get you into the habit of thinking about testing before you start writing your program. If you look at the deliverables and their due dates, you will notice that there is no C++ code due on Due Date 1. Instead, you will be asked to submit a short shell script, a few short answer questions, and test suites for C++ programs that you will later submit by Due Date 2. Test suites will be in a format compatible with A1Q4. So if you did a good job writing your runSuite script, it will serve you well on this assignment. Be sure to do a good job on your test suites, as they will be your primary tool for verifying the correctness of your submission. For this reason, C++ code due on Due Date 2 will only get one release token per day. We want you to rely on your own pre-written test suite, not Marmoset, to verify correctness. Note: You must use the C++ I/O streaming facilities on this assignment. Marmoset will be programmed to reject submissions that use C-style I/O. 1. Write a shell script called ann that will fetch and display announcements from the CS246 website according to the following specication: Your script should download the course home page from the following URL: http://www.student.cs.uwaterloo.ca/~cs246/index.shtml Find a Unix command that accomplishes this task, and familiarize yourself with this command. Note: this command generates a lot of extraneous output; be sure that your script suppresses this output. Each announcement on the CS246 website is posted within an HTML <li> tag and starts with a posting date, which is surrounded by an HTML <b> tag. You may assume that the announcements are placed on separate lines, and that each announcement occupies exactly one line. The HTML code of an example announcement is given below: <li><b>Fri 20 May:</b> The code block for A2, Q3 has been updated</li> Come up with a regular expression that can be used to recognize lines that contain announcements. (Hint: dont try to be too clever about coming up with a bullet-proof date-matching pattern; just assume that whatever is between the <b> and the </b> is a valid date.) For each announcement, print the date and the announcement body in one line, separated by a - (dash). Here is the expected output for the example given above: Fri 20 May:- The code block for A2, Q3 has been updated

Your ann script should accept the following options: -o k : species that the oldest k announcements should be printed -n k : species that the newest k announcements should be printed if no option is provided, then the newest 3 messages should be printed Ensure that your script doesnt leave any garbage behindat the end of your script, remove the le index.shtml from the current directory, as well as any temporary les you might have created (if any). Hints: The sed command can be used to replace or omit text in a le or stream. For example, the following command will remove the <li> tag and print only the text within the tag: echo "before <li>inside</li> after" | sed s_<li>\(.*\)</li>_\1_ The output will be as follows: before inside after In this example, the text before and after the text matching the pattern is left unchanged; but the part that matches the pattern (i.e., from the <li> to the </li>) has been replaced by just the the text that lies between <li> and </li>. See below for an explanation of this sed command: s_regexp_replacement_ : the s (substitute) command instructs sed to replace text matching regexp with the text given as replacement. The underscore (_) is acting as a delimiter here. <li> : matches the literal <li>. \(.*\) : matches any number of any characters. The surrounding parentheses instruct sed to save the enclosed text for future use in the replacement string replacement. Within replacement, the identier \1 indicates the rst parenthesized text, the identier \2 indicates the second parenthesized text, and so on. </li> : matches the literal </li>. Once you understand the workings of this command, come up with a similar command to pick out the date and body of an announcement, and construct the correct output string. seds -n option can be used to suppress printing of lines that do not match the regular expression. Equivalently, you could pre-lter out non-matching lines using grep or egrep. By now, you should familiar with the use of the head and tail commands for printing the rst or last part of a le or input stream. 2. Note: there is no coding associated with this problem. We would like to write a program called change, which accepts an integer (representing an amount of money, in cents) on standard input. The program then gives the minimal combination of Canadian coins (toonies, loonies, quarters, dimes, nickels, and pennies) whose total value equals the given amount on standard output. For example, if the input is 89 then the outuput is 3 quarters 1 dime 4 pennies

Your task is not to write this program, but to design a test suite for this program. Your test suite must be such that a correct implementation of this program passes all of your tests, but a buggy implementation will fail at least one of your tests. Marmoset will use a correct implementation and several buggy implementations to evaluate your test suite in the manner just described. Your test suite should take the form described in A1P4: each test should provide its input in the le testname.in, and its expected output in the le testname.out. The collection of all testnames should be contained in the le suiteq2.txt. Zip up all of the les that make up your test suite into the le a2q2.zip, and submit to Marmoset. 3. In this problem, you will implement a generalization of the previous problem. You will write a program called genChange (general change) that works for any countrys monetary system (real or ctional). This program accepts, from standard input, the coin denominations that make up the monetary system, and the total value. It then prints a report of the combination of coins needed to make up the total. For example if a particular country has coins with values 1, 3, and 8, and you have 13 units of money, then the input would be as follows: 3 8 3 1 13 The inital 3 means that there are 3 coin types. The next three values are the coin denominations, in decreasing order. The last value is the total. For this input, the output should be 1 x 8 1 x 3 2 x 1 Notes: Most coin systems have the property that you can make change by starting at the highest coin value, taking as many of those as possible, and then moving on to the next coin value, and so on. Although not all combinations of coin denominations have this property, you may assume that the input for genChange will always have this property. There has been talk in Canada of abolishing the penny for years. If this happens, it will be impossible to construct coin totals not divisible by 5. Similarly, in whatever system of denominations you are given, it may not be possible to construct the given total. If that happens, output Impossible (and nothing else) to standard output. You may assume that the number of denominations is at most 10. You may assume that the denominations are given in decreasing order, and no denomination will be listed twice. If a given coin is used 0 times for the given total, do not print it out; your output should contain only those denominations that were actually used, in decreasing order of size. (a) Due on Due Date 1: Design a test suite for this program. Call your suite le suiteq3.txt. Zip your suite le, together with the associated .in and .out les, into the le a2q3.zip. (b) Due on Due Date 2: Write the program in C++. Call your program a2q3.cc.

4. Consider the following C++ program (float.cc, available in the SVN repository): #include <iostream> using namespace std; int main () { float x = 0.0001; float y = 0; for (int i=0; i < 10000; i++) { y += x; } cout << y << endl; return 0; } Compile and run this program, and then answer the following questions: (a) How does the actual behaviour of this program dier from its expected behaviour? (b) How do we account for this dierence? Put your answers to both questions into the le a2q4.txt. 5. In this problem, you will write a program to read a set of lines from standard input using the C++ I/O streaming facilities. Each line will start with a number and will be followed by some words. You may assume that the literals in each line are separated by exactly one space, with no trailing space at the end. Ideally the number at the begining of each line should be equal to the number of words in that line (excluding the number). However, for some lines this does not hold. You should print only the lines for which the starting number does not match the number of words (or literals) in that line. Your output should contain the correct number of words in each line. Sample input: 7 5 6 5 8 there are seven words in this line how many words are there this line is not consistent He is 100 years old another error

Sample output: 5 this line is not consistent 2 another error (a) Due on Due Date 1: Design a test suite for this program. Call your suite le suiteq5.txt. Zip your suite le, together with the associated .in and .out les, into the le a2q5.zip. (b) Due on Due Date 2: Write the program in C++. Call your solution a2q5.cc.

Vous aimerez peut-être aussi