代写CSCI 2132作业、代做Software留学生作业、代做C++实验作业、C++编程设计作业代写
Change into your subversion directory on bluenose: cd ~/csci2132/svn/CSID . Create a directory a6 for the current assignment. Change into your assignment directory: cd a6 . Create files inside the a5 directory as instructed in the questions below and put them underSubversion control using svn add <filename> . Only add the files you are asked to add! Once you are done answering all questions in the assignment (or the ones that you are able toanswer—hopefully all), the contents of your a4 directory should look like this:a6unique.c(You will also have executable programs and potentially some data files in this directory, but youshould not add them to SVN.) Submit your work using svn commit -m"Submit Assignment 6" .1In this assignment, you are asked to develop a more powerful version of the uniq Unix tool we discussedearlier in the term. This assignment is broken down into multiple steps to divide the problem intoincremental tasks. This type of incremental development that develops a simple but functional versionof a piece of software first and then incrementally adds features is common in the software industry. Italso helps to break a seemingly overwhelming task into smaller subtasks that suddenly do not seemthat overwhelming at all anymore. In each step, you are expected to modify the solution you developedin the previous step instead of starting from scratch. For full marks, the code you submit should satisfyall the requirements stated in the final step. Solutions that address only a subset of the requirementsearn partial marks.1Recall that the uniq tool reads its input from stdin and sends the lines it reads back to stdout butdrops every line that is identical to the line that immediately precedes it. The final version of theunique tool you are tasked to develop in this assignment has the following features: It drops any line that is identical to a line read before, not only the immediately preceding line,any line that came before it. It must be able to deal with lines of arbitrary lengths. It takes an optimal command-line flag -m. If this flag is provided, then it works in “multi-linemode”. In this mode, any line that ends with a backslash (\) is to be treated as a single linetogether with the line that comes after it. If a whole sequence of lines end with backslashes, thenall of them must be joined to form one logical line. This is the same as escaping line breaks inPython source code, for example.We break this into the following incremental steps:Step 1: Recreate the functionality of uniq. In other words, you should check only adjacent lineswhether they are identical.Step 2: Extend your implementation of Step 1 so it checks for duplicate lines throughout the document,not duplicate adjacent lines. In the interest of an efficient implementation, you should use asorting algorithm to sort the lines of the input, which guarantees that identical lines are storedconsecutively. Now the solution in Step 1 can be used to eliminate duplicate lines. Note that thereis no requirement in this step that the order of the lines in the document should be preserved inthe output. In this step, you will simply use the qsort function provided as part of the C standardlibrary to sort the lines.Step 3: This step adds the last ingredient necessary to meet the first two requirements of the uniquetool listed above. Specifically, you will create the illusion that each input line is considered inorder and output if it is not equal to any line that you have seen before and dropped otherwise.This can be done by applying the solution from Step 2 but, before producing the final output,rearranging the lines that were not dropped by the code in Step 2 in the order they appeared inthe input.1You may be tempted to develop a tool that meets all the requirements in one shot instead of developing the solutionincrementally. If you succeed, this will earn you the same marks as if you had developed the solution incrementally. However,the incremental approach outlined here is only slightly more work overall and each step is a much more manageable add-onto the previous step than implementing all the required functionality in one shot. Also, if you get stuck with satisfying oneof the more advanced requirements, completing the earlier steps of this assignment ensures that you have a solution that atleast earns you partial marks. Therefore, you are encouraged to follow the incremental approach outlined here.2Step 4: In this step, you need to check the command line arguments of the program, check whether-m is the only argument and, depending on the outcome, activate multiline mode. Then, whenreading input lines, you need to ensure that you collect all lines separated by escaped newlinecharacters into a single line record internally. An added complication is that you should considerthe inputsThis is \a lineandThis is a\lineto be the same as far as checking for duplicates goes, but in the output of the program, the linebreaks of the line that is kept should be preserved. For example, for the inputThis is \a lineAnother lineThis is a\linethe output should beThis is \a lineAnother lineTo accomplish this, you need to keep a record of where the line breaks were in the input.Step 5: The final step is to replace the qsort function with your own implementation of Merge Sort.In general, it is a better idea to use functions already provided as part of a library unless theselibrary functions are inadequate for some reason. You are asked to implement your own sortingalgorithm here to make you practice writing recursive functions.The remainder of this assignment states the requirement for each of these five steps in more detailand/or gives some hints that should be helpful in completing each step.3Step 1Implement a C program unique.c that recreates the functionality of the uniq tool by reading the inputline by line and dropping each line that is identical to the line immediately before it. On the inputyour program should outputthe same output that uniq would produce.While not strictly necessary for this step, it is important as a basis for subsequent steps that you readthe entire input and store it as a sequence of lines. Specifically, you should represent the input textas a struct that stores the number of lines in the input and the text as a field of type char **. Thischar ** points to a dynamically allocated array of char pointers. Each of these pointers points to adynamically allocated char array that stores a single input line.You need to deal with two potential complications:1. There is no upper limit on the length of each input line. Normally, this would force you to allocatean array of a certain minimum size for each line and then resize this array if it is not large enoughto hold the current line. Thankfully, the getline function can be used in a fashion that does thiswork for you. Read its manpage to find out how you can get it to return a dynamically allocatedcharacter array that stores the read input line.2. You do not know the number of input lines in advance. Thus, you have to allow the array ofpointers to the individual lines to grow. To do this, you need to allocate a larger array once yourcurrent array is full, copy the elements of the current array to the new array, and finally free thecurrent array and start using the new array. In order to avoid a quadratic running time resultingfrom excessive copying, the new array you allocate should be twice as big as the current array.For checking consecutive input lines that are the same, you can use the strcmp standard library function.Read its manpage to find out how to use it.An important requirement for your program is that it should not leak any memory. Since all memorystill held by a process is released when the process exits, there is technically no effort required on yourpart to meet this requirement. However, it is important for you to practice how to manually manageallocated memory over the course of a longer-running program. Thus, for full marks you need toexplicitly release all memory allocated to your program before the program exits. Specifically, you needto call free on the char array holding each line and on the array of char pointers referring to thesechar arrays. In later steps, you will add a few more dynamically allocated arrays that you will alsohave to release.4Step 2Your strategy in this step should be to sort the lines in the input so identical lines in the input are storedconsecutively. Then you can eliminate consecutive duplicate lines as in Step 1. There is no need topreserve the order of the input lines in the output. For example, on the inputyou are allowed to produce the outputTo accomplish this step, you need two ingredients:1. A function that can sort an array of data items, the array of char pointers representing the inputlines in this case.2. A comparator function that is used by the sorting function to determine the correct order of pairsof input items.For the latter, you should use the strcmp function again, with a twist. For the former, you should usethe qsort sorting function provided by the C standard library again.As you can see in the documentation of the qsort function, it calls the comparator function withpointers to the two data items to be compared as arguments. This creates a small wrinkle here: Theitems we want to sort are of type char *, since this is how we represent each input line. Thus, thearguments passed to the comparator function by qsort are of type char **. strcmp, however, expectsto arguments of type char * (because it is meant to compare two strings, not two pointers to strings).Therefore, you need to write a wrapper for strcmp suitable to be used by qsort. All this wrapper needsto do is dereference its char ** arguments to obtain to char * values that can be passed to strcmp,call strcmp on these two char pointers, and then return the result.To complete this step, all you need to do is to combine the discussed ingredients:1. Sort the lines of text using qsort and your strcmp wrapper.2. Eliminate duplicate lines using your solution developed in Step 1.3. Print the lines that remain as in Step 1.5Step 3Next you need to modify your code so the lines that are included in the output are output in the sameorder as they appear in the input. For the inputthis means that the output should be(Recall that, of all identical lines in the input, the first one should be kept.)The strategy is fairly simple: You sort the input lines and eliminate duplicates as in the previous step.Then you rearrange the remaining lines in their original input order and output the resulting list.To support this, you need to1. Number the lines that you read in the order you read them and change your representation ofeach input line from a simple char * to a struct that stores the line number and this char *.2. Change your sorting step from Step 2 so it sorts the lines by their content and identical lines bytheir line numbers. This is a simple extension of the comparison function for pairs of lines yurdeveloped in Step 2 and ensures that the elimination strategy from Step 1 indeed keeps the firstline in each set of identical lines.3. Restore lines to their original order after eliminating duplicates. You do this by sorting the linesthat were not eliminated a second time, this time only by their line numbers, ignoring theircontents. To do so, you need to implement a second comparison function that compares lines byline number.6Step 4Here, you add the ability to invoke unique with an optional command line flag -m. You should parsethe command line as practiced in Lab 7. unique can be invoked without command line argumentsor with the -m argument. Any other set of command line arguments should be rejected with an errormessage.The -m flag turns on “multi-line mode”. This means that lines that end with a backslash (\) shouldbe treated as if they were one line with the one that comes after it. A sequence of multiple lines thatall end with backslashes should be treated as a single “logical line”. The backslashes should not beincluded in this logical line. Thus, the inputA little \dwarf built\a houseon a hillA little dwarf \built a houseshould be treated as if it were the inputA little dwarf built a houseon a hillA little dwarf built a houseDuplicate elimination transforms this intoA little dwarf built a houseon a hillThe output of your program should preserve the line breaks from the input, that is, this sequence oflines produced by eliminating duplicates should be printed asA little \dwarf built\a houseon a hillbecause this is the way the first A little dwarf ... line was broken in the input.The interesting part in this step is how to treat lines separated by escaped newline characters as singlelines when eliminating duplicates but preserve line breaks in the output. There are two ways you cando this.The first is to eliminate the escaped newline characters from your internal representation of the line.This makes sorting and checking for duplicates straightforward using exactly the same strategy asin Step 3. However, the string does not store any information about where the line breaks were inthe input. Thus, you need to extend your line representation so it includes an array of the line break7positions. When outputting the lines that are not dropped, you can then use the array of these linebreak positions to recreate the line breaks in the output.The second option is to preserve all escaped line breaks in your internal representation of each line.This means that you can simply print each line that is part of the final output without the need for anyspecial handling. Sorting and checking of duplicates, however, requires you to implement your ownstring comparison function, one that ignores escaped line breaks, that is, treats them as if they weren’tpresent at all.Step 5We will discuss Merge Sort in class. Implement this algorithm and use it instead of qsort as yoursorting function. Your mergesort function should be a drop-in replacement for qsort, that is, it shouldhave the prototype:void mergesort(void *array, size_t n_elems, size_t elem_size,int (*cmp)(const void *, const void *));(The description of this step is deceptively short. It is by far the most complicated step of thisassignment.)8因为专业,所以值得信赖。如有需要,请加QQ:99515681 或邮箱:
微信:codinghelp