C++ Basics Cs246, Fall 2020
Total Page:16
File Type:pdf, Size:1020Kb
ASSIGNMENT #2: C++ BASICS CS246, FALL 2020 Assignment #2: C++ basics Due Date 1: Wednesday, October 7, 2020, 5:00 pm Due Date 2: Wednesday, October 21, 2020, 5:00 pm Online Quiz: Wednesday, October 28, 2020, 5:00 pm Topics that must have been completed before starting Due Date 1: 1. Software Testing 2. produceOutputs and runSuite from A1 Topics that must have been completed before starting Due Date 2: 1. C++: Introduction to C++ 2. Preprocessing and Compilation Learning objectives: • C++ I/O (standard, file streams) and output formatting • argv and argc • C++ strings and stringstreams • separate compilation and Makefiles • Questions 1a, 2a, and 3a are due on Due Date 1; questions 1b, 2b, 3b, 4b, 5 and 6 are due on Due Date 2. You must submit the online quiz on Learn by the Quiz date. • On this and subsequent assignments, you will take responsibility for your own testing. 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 test suites for C++ programs that you will later submit by Due Date 2. Test suites will be in a format compatible with that of the latter questions of Assignment 1, so if you did a good job writing your runSuite script, that experience will serve you well here. • Design your test suites with care; they are your primary tool for verifying the correctness of your code. Note that test suite submission zip files are restricted to contain a maximum of 40 tests. The size of each input (.in) file is also restricted to 300 bytes, and each output file (.out) is restricted to 1,000 bytes. This is to encourage you not to combine all of your testing eggs in one basket. • You must use the standard C++ I/O streaming and memory management (MM) facilities on this assignment; you may not use C-style I/O or MM. More concretely, you may #include the following C++ libraries (and no others!) for the current assignment: iostream, fstream, sstream, iomanip, and string. Marmoset will be setup to reject submissions that use C-style I/O or MM, or libraries other than the ones specified above. • We will manually check that you follow a reasonable standard of documentation and style, and to ver- ify any assignment requirements that are not automatically enforced by Marmoset. Code to a standard that you would expect from someone else if you had to maintain their code. Further comments on cod- ing guidelines can be found here: https://www.student.cs.uwaterloo.ca/˜cs246/F20/ codingguidelines.shtml • We have provided some code and sample executables in the subdirectory codeForStudents under the appropriate subdirectories. These executables have been compiled in the CS student environment and will not run anywhere else. Page 1 of 6 ASSIGNMENT #2: C++ BASICS CS246, FALL 2020 • You may not ask public questions on Piazza about what the programs that make up the assignment are supposed to do. A major part of this assignment involves designing test cases, and questions that ask what the programs should do in one case or another will give away potential test cases to the rest of the class. Questions found in violation of this rule will be marked private or deleted; repeat offences could be subject to discipline. Coding Assessment Questions 1 to 4 are part of the coding assessment, and may be publicly discussed on Piazza so long as solutions are neither discussed nor revealed. Question 1 (40% of DD1; 15% of DD2) In this question, you will write a C++ program called lineWrap, whose command-line syntax looks like this:1 lineWrap [-n maxLineLength] [-c censorWord] [inputFile] This program reads in lines of text, and prints them to cout, with at most maxLineLength characters in each printed line, and any remaining text printed afterwards (to a maximum of of maxLineLength again). The default value of maxLineLength is 20, but this can be overridden at the command-line, as we’ll see below. Note that you should preserve all white space in the input, except for the newline characters. For example, if the input file looks like this (here, we’ll use a period instead of actual spaces, to make them more obvious): a234567890b234567890c234567890d234567890e234567890 slings...and. .....arrows...of...outrageous then we expect the output file to look like this: a234567890b234567890 c234567890d234567890 e234567890 slings...and. .....arrows...of...o utrageous Note that the first line of input was 50 characters long, so we had to use three lines of output to print it all; the second input line was 13 characters long, so it fit onto one line of output. You can choose to override the maxLineLength by using the -n option at the command-line; this value must be a positive integer. The program can optionally take a censorWord (a string) as an argument; if one is specified on the command-line, then the censorWord must not appear in the output stream. The removal should be done before you try to print the line, so the output will line up as if the censorWord had never been in the input file in the first place. For example, consider this input file: a23456789gong0b234567890gonggongc234567890d234567890e234567890 ..gong..gogongng.. If there is no censorWord given, then the output should be this a23456789gong0b23456 7890gonggongc2345678 90d234567890e2345678 90 ..gong..gogongng.. 1Note that as per UNIX convention, items in square brackets are optional; if you can call lineWrap with no arguments, it will take input from cin. Page 2 of 6 ASSIGNMENT #2: C++ BASICS CS246, FALL 2020 But if the censorWord is set to gong, then the output should be this. a234567890b234567890 c234567890d234567890 e234567890 ...... The processing of the second line of input shows that (a) whitespace around a censorWord is preserved in the output and (b) if deleting one instance of the censorWord creates another instance of the censorWord, you have to delete that second instance (and third and fourth ...) also. That is, gogongng becomes gong after deleting the gong in the middle, but we’re not done yet; we have to keep deleting the censorWord until no instances remain. If the censorWord is the empty string "", then no censoring should be done; note that a good design may be able to implement this case without any special handling. If an input file name is specified on the command-line, then the input text should be taken from there; otherwise, assume that the input text will come from cin. You need to perform error checking on the command-line arguments: • If the -n option is used, it must be followed by a positive integer. • If the -c option is used, it must be followed by another string i.e., lineWrap -c on its own is illegal. • If an input file name is specified, you must be able to create an istream object successully using that name. Use the following global definitions for error messages:2 const string eBadLineLength = "Error, maxLineLength must be a positive integer"; const string eNoCensorWord = "Error, missing censor word"; const string eCantOpenInputFile = "Error, could not open input file"; If one of these errors arises, print the appropriate message to cerr with nothing else on the line and abort via exit(1). Note that your program should be able to process valid command-line calls with options in any order. That is, the following should all be legal and have the same output: $ lineWrap -n 15 -c gong test1.txt $ lineWrap -c gong test1.txt -n 15 $ lineWrap test1.txt -n 15 -c gong You may assume without having to check that each command-line argument is used at most once; so we don’t care about the output to this, for example: $ lineWrap -c gong test1.txt -n 15 -c fluble -n 35 Feel free to use any element of the C++ APIs for string (http://www.cplusplus.com/reference/string/ string/) and stringstream (http://www.cplusplus.com/reference/sstream/?kw=sstream), even if it has not been discussed “in lecture”; in particular, string::npos might be useful to you. Hint: Try creating the basic program without the command-line options first, then progressively add in the command-line features once you are confident you have solved the simpler problem. a) Due on Due Date 1: Design a test suite for this program. Call your suite file suiteq1.txt. Zip your suite file, together with the associated .in, .out, and .args files, into the file a2q1.zip. Since the .in extension is used for file redirection, and .args is for command-line arguments, the input test files you create should not use either of those extensions. b) Due on Due Date 2: Write the program in C++. Save your solution in a file named a2q1.cc. 2These definitions will also be provided to you in the a2/codeForStudents/q1 subdirectory of the course git repository; please use that file rather than copy/pasting from this PDF to avoid strange formatting errors. Page 3 of 6 ASSIGNMENT #2: C++ BASICS CS246, FALL 2020 Question 2 (30% of DD1; 10% of DD2) You are going to write a slightly simplified version of the GNU UNIX utilities basename in C++; we expect that you will make effective use of the C++ string API in doing so. Basically, basename takes a valid UNIX pathname such as /usr/bin/vim and returns the name of the file with the enclosing directory names removed. For example, $ basename /usr/bin/vim vim More precisely3, given a PATHNAME: basename outputs PATHNAME with any leading directory components removed.