代写COMP1711作业、代做C/C++,C#编程语言作业、代写C-like留学生作业、代做C/C++实验作业
University of Leeds School of ComputingProcedural Programming COMP1711Semester 1, 2018-2019Assignment 340 MarksTo be submitted before 17/12/2018 at 12:00pmThe BriefYou will write a program to process plain text files containing source code written in C-likeprogramming languages, such as C, C++, and C#.The DetailsThe ProblemWrite a program to process source code files of C-like programming languages by:1. removing all comments from a source code,2. checking that all brackets in a source code are balanced, i.e. for any opening bracket thereexists a closing bracket of the proper type in a proper position.In C-like programming languages there are two types of comments: single line comments which areinitiated with a double slash // and terminated at the end of the current line of code; and multipleline comments which are initiated by /* and terminated with */. The program should remove bothtypes of comments from a source file.Brackets used in C-like programming languages are of three kinds, namely:1. the curly brackets {}, also called braces,2. square brackets [], and3. round brackets (), also called parentheses.For simplicity, in the remainder of this brief, the word bracket will be used to refer to any of thesekinds.In a syntactically well-formed (correct) program, every opening bracket must have a matchingclosing bracket at the correct position. Here are some examples of well-formed and badly formedcode fragments:int foo (int a[], int n) { for (int i = 0; i< (n-a[10]); i++) {// loop body} } // well formedint foo (int a[), int n) { for (int i = 0; i< (n-a[10]); i++) {// loop body } } // badly formed at [)int foo (int a[], int n) { for (int i = 0; i< (n-a[10]); i++) {// loop body} // badly formed, missing one final }int foo (int a[], int n) { for (int i = 0; i< (n-a[10]); i++) {// loop body}}} // badly formed, one extra } at endvoid voo (b(b(c))) { {a[0][i]--; { { {t++;}}}}} // well formedint a]100][100[; // badly formed2Bracket matching can be achieved very easily withthe help of a stack. A stack is a data structure forstoring data items (integer numbers for example) sothat the last item stored (topmost item) is retrievedbefore other items. We say that a stack obeys the LastIn First Out (LIFO) principle. Stacks are oftenencountered in daily life. Imagine the pile (stack) ofplates in a restaurant. Staff put clean plates on top ofthe pile (stack) of plates, while customers can onlyremove plates from the top of the pile. Hence, the lastplate to be placed on top is the first one to beremoved. In programming, placing a data item on top of a stack is called a push operation, andremoving the top item is called a pop operation.Implementing a stackA stack can be implemented with either an array or a linked list. You can use either solution, as longas the array or the linked list is stored in the heap, and there is no pre-defined limit to the number ofelements that it can contain. Linked lists have been studied in class, and we refer to the lectures onpointers for further details on the addition and deletion operations.The rest of this section covers the implementation of stacks with arrays. The array is used to storethe actual data of the stack, as in the following illustration. In addition to the array itself, anadditional integer variable is needed to determine the location of the top item at any time. Thisvariable is called the Stack Pointer (sp). Note that the, in case of arrays, the stack pointer is NOT apointer variable. It is simply an integer variable that contains the index of the top item.When illustrating stacks, we usually draw the array vertically to resemble our intuitive conceptionof stacks in real life. The above illustration shows a stack that can store up to 12 integer numbersbecause it uses an array of maximum size = 12. The illustration shows the stack after 6 numbershave been pushed into it (these are shown in green). As you can see, the value of the stack pointerExisting stack elementsEmpty locations availablefor adding new items(contain junk)3(sp) is 5. This is the index of the top item in this case. Do not confuse the top of the stack (thenumber in position 5) with the top (limit) of the entire array (position 11). Note that array positions‘above’ the top of the stack will have some previous ‘junk’ data (the numbers in blue), but these arenot part of stack data. The stack data in the above example extend from the item in position 0 up to,and including, the topmost item in position 5.Clearing the stackTo clear or initialize a stack, the stack pointer sp is set to a negative value(usually -1). Setting sp to 0 cannot be used to indicate an empty stack,because when sp=0, the topmost element in the stack is in position 0 andthe stack actually contains one item. The illustration to the right shows aclean (empty) stack. Notice that there is no need to clear any previous junkdata in the array, as these will be gradually overwritten with actual stackdata as numbers are pushed into it.The push functionTo push a new item onto a stack, the following two steps are performed:1. The stack pointer (sp) is incremented so that it ‘points’ to (indexes)a new location on top of existing stack data.2. The new item is stored in the array location now pointed at by sp.The illustration shows how to push 35 into a stack. The previous value ofsp was 5, so this is first incremented to 6, and 35 is stored in position 6.The pop functionTo pop an item, the following three steps are performed:1. The value pointed at by sp is copied to a temporary variable.2. The stack pointer (sp) is decremented so that it now points at theelement which was below the top item, but has now become thetop one.3. The value of the temporary variable is returned.The illustration to the right shows how a stack is popped. The top value inposition 6, which is pointed to by sp is first copied into a variable (calledtmp in this example). The sp is then decremented by 1 so it is nowpointing to position 5 which is the new top of stack. Finally the value intmp is returned by the pop function.What is a stack overflow?Stack overflow occurs when we attempt to push a new item into a fullstack, as in the illustration to the right. This is clearly an impossibleoperation. Hence, before attempting to push any new item into a stack wemust first check that the stack is not already full. Since there must be noexplicit limit to your stack, if the next push operation would cause a stack overflow, the memorycontaining the stack must be reallocated on a larger memory area.What is a stack underflow?Stack underflow occurs when we attempt to pop an empty stack as shown inthe illustration to the right. To protect against this, we should always checkthat the stack is not empty before attempting to pop anything out.Checking matching brackets with a stackFor simplicity, in this project we will skip and ignore all characters in a source code except bracketsand comments: we will not check the code for any kind of syntactic correctness other than matchedbrackets.In general, when checking an input string for matching brackets, we can only encounter three casesof mismatched brackets:1. A closing bracket is encountered but there are no unmatched opening brackets to match thisclosing bracket with, such as the last closing brace in this statement “int foo () }”. We willrefer to this case as ‘type 1 error’.2. A closing bracket is encountered but the last opening bracket is of a different type, e.g. “intfoo ( ] {}”. We will refer to this as ‘type 2 error’.3. An opening bracket is encountered but it is never closed, e.g. “int foo ({}“. We will refer tothis as ‘type 3 error’.A stack can be used make sure that all brackets are matched. The following table shows someexamples of the method. For simplicity the stack is shown horizontally with the top of the stack tothe right.Case Input string(The characterbeing read isshown in red)Stack contents(top of stack tothe right)CommentWell-formed string { [ g ] }{ [ g ] }{ [ g ] }{ [ g ] }{ [g ] }{ [ g ] }{ {[{[{ Stack is initially empty‘{‘ pushed on stack‘[‘ pushed on stack‘g’ ignored‘]’ in input matched to ‘[‘ on stack‘}’ in input matched to ‘{‘ on stackStack is finally empty hence string is OKType 1 error [ g ] }[ g ] }[ g ] }[ g ] }[ g ] }Stack is initially empty‘[‘ pushed on stack‘g’ ignored‘]’ in input matched to ‘[‘ on stackError: stack is empty, ‘}’ in input cannot bematched to anythingType 2 error ( [ g ] }( [ g ] }( [ g ] }( [ g ] }( [ g ] }( [ g ] }Stack is initially empty‘(‘ pushed on stack‘[‘ pushed on stack‘g’ ignored‘]’ in input matched to [ on stackError: ‘}’ in input does not match ‘(‘ onstackType 3 error { [ g ]{ [ g ]{ [ g ]{ [ g ]{ [ g ]Stack is initially empty‘{‘ pushed on stack‘[‘ pushed on stack‘g’ ignored‘]’ in input matched to ‘[‘ on stackError: all the string has been read but thestack still contains some bracketsThe TaskWrite a program that opens a plain text file containing C-like source code, reads it, and outputs afile with the same content as the first, except that all comments are removed. Furthermore, theprogram must check that all brackets match, and if they do not, the program should display an errormessage, showing the type of error and the line number where this error was encountered. The inputand output files are passed to the program as command line parameters, as in:./your_executable inputfile.txt outputfile.txtGeneral GuidelinesWrite the program in standard C. If you write your code in any other language, it will NOTbe assessed and you will get a zero mark. This is an individual project, and you are not supposed to work in groups or pairs with otherstudents. Be aware that plagiarism in your code will earn you a zero mark and will have very seriousconsequences. It is much better to submit your own partially finished work, than to fall intothe trap of plagiarism. We use fairly sophisticated software which can identify whether two pieces of code arenearly identical (modifications such as renaming variables and reshuffling the positions offunctions do not deceive it). If two (or more) students have large portions of their filesnearly identical they will be accused of plagiarism or collusion. If found guilty, all partiesinvolved will incur the penalty, regardless of who was the author of the code. For thisreason, never show, or give access to, your code to anyone. Do not help a colleague bysharing your code, or you will both be found guilty of collusion. It is your responsibility to make sure that nobody has access to your code. Lock the sessionif you leave your computer unattended.6Submission Submit a single text file with extension .c through Minerva. Download and check the submitted file, to make sure that it is the correct version. We willnot accept late submissions if you realise you uploaded the wrong file, or the file appears tobe corrupted. Binary files (rather than text), and generally files that cannot be compiled, will earn zeromarks.Writing and testing your programPart of the submission will be automatically evaluated by using the attached test harness. In orderfor the tests to work, your code must contain some predefined functions. You can find thosefunctions along with their documentation, in the file template.c, which should therefore be yourstarting point. Rename this file, and implement the functions in it one by one. To run the tests,unpack the zip file containing the test harness in the same directory where your program is, andcompile it with the following command:gcc -lm -std=c99 –o tests unity.c test.c your_program.cwhere your_program.c is the name of your program file. Each executable can have a singlemain(int argc, char **argv) function, therefore, when compiling the tests, rename your mainfunction into mymain(int argc, char **argv). If the test program compiles correctly, you canthen execute it by running:http://www.daixie0.com/contents/13/2211.html./testsInitially, when all the functions are empty, all the tests will fail. Once you have successfullyimplemented the body of the functions, all the tests will be passed.Once you are satisfied with the tested functions, you can develop the rest of the functionality andthe main function. The functions that are automatically tested, that is the ones you initially found inthe file template.c, must not contain any interactive component. This means that they must not useprintf or scanf and must run without user input. Otherwise, the tests will not run automatically.SupportPlease take advantage of lab sessions to get support from the instructors, especially on being able torun the tests, since this is critical to passing the assignment.Marking SchemeThe program passes the test_stack test (4 marks)The program passes the test_remove_single_line_comment test (4 marks)The program passes the test_remove_multi_line_comment test (4 marks)The program reads and writes the input and output file correctly (3 marks)The program uses the heap correctly, with no memory leak (4 marks)The program correctly checks for matching brackets (5 marks)The program display error messages correctly, including line numbers (5 marks)The program is well structured, clear, and efficient (9 marks)The program uses comments properly (2 marks)
因为专业,所以值得信赖。如有需要,请加QQ:99515681 或邮箱:
微信:codinghelp