讲解:C Week 03 Lab Exercisephp

Introduction需要提交的文件为:DLList.c testList.c截图1和截图2是对task1的测试截图3是task2的输出结果Task2结果说明:1、 用DLListBefore向空链表中插入一个元素2、 删除这个元素3、 用DLListAfter向空链表中插入一个元素4、 用DLListBefore向链表中插入一个元素5、 用DLListAfter向链表中插入一个元素6、 依次删除所有元素Requirement2017/8/11 COMP2521 17s2 - Week 03 Lab Exercise1/6COMP2521 17s2Week 03 Lab ExerciseData Structures and AlgorithmsMyEd and DLLsObjectivesto illustrate file manipulationto implement a doubly­linked list ADTto design comprehensive test­casesto build a simple line­based text editorAdminMarks 5=outstanding, 4=very good, 3=adequate, 2=sub­standard, 1=hopelessDemo in the Week 03 Lab or in the Week 04 LabSubmit give cs2521 lab03 DLList.c testList.c or via WebCMSDeadline must be submitted by 11:59pm on Sunday 13 AugustAimsThis lab aims to re­examine an idea that we have looked at a few times in lectures (without being explicit aboutit): command interpreters. More importantly, it aims to give you practice manipulating a useful data type:doubly­linked lists. Finally, it aims to demonstrate how files are manipulated in Linux. These ideas are tiedtogether in a simple text editor, a program in the style. of the classic Unix text editor ed , an editor which roamedthe earth contemporaneously with the dinosaurs.BackgroundA text editor deals with a file of text, reads it, allows the user to modify it, and then writes it back. This impliesthat the editor has an internal representation that it deals with in between the reading and the writing. For thisexample, we consider that the internal representation is a sequence of lines, represented by a doubly­linked listof strings. Each node in the list represents one line in the file being edited.Since a common operation is to “move around” the file, looking for lines to change, a doubly­linked list with anotion of a current node provides a convenient way to do this. The editor itself is implemented as a loop which:prints a prompt, reads a command, carries out the command (possibly changing the state of the list), and thenrepeats. Many of the programs that we build for exploring data structures during this course will have a similarstructure. ( The fact that it prints a prompt is an improvement on the real ed editor, which doesn’t. Not prompting the user may beminimalist, but isn’t great interface design).Doubly­linked ListsDoubly­linked lists are a variation on “standard” linked lists where each node has a pointer to the previous nodeas well as a pointer to the next node. The following diagram shows the differences:2017/8/11 COMP2521 17s2 - Week 03 Lab Exercise2/6As shown in the diagram, our version of doubly­linked lists also has a notion of a “current” node, and currentcan move backwards and forwards along the list. Our doubly­linked list does insertions either immediatelybefore or immediately after the current node. Deletion always causes the current node to be removed from thelist.In this lab, we’ll use a doubly­linked list ADT called DLList whose interface is given in the file/home/cs2521/web/17s2/labs/week03/code/DLList.hThe representation of DLList is a doubly­linked list of DLListNode s, accessed via a record containing thefollowing four fields:nitems : a count of the number of nodes/items in the listfirst : a pointer to the first node in the listlast : a pointer to the last node in the listcurr : a pointer to the “current” node in the listSeveral important conditions must hold at all times during the lifetime of a DLList . Technically, theseconditions are called invariants, and are checked for in the function validDLList() .Whenever the list is non­empty, it must have a defined current node. It is not possible for the currentnode pointer to be NULL if there is at least one node in the list.The counter nitems must always contain a value which is the same as the number of nodes in the list.The book­keeping operations (e.g. newDLList() , getDLList() , etc.) are relatively straightforward, sowe’ll describe only the operations relevant for this lab in detail.char DLListCurrent(DLList L)Return a pointer to the string value for the current node. It is an error to call this function if the listcontains no nodes.int DLListMove(DLList L, int n)Move the current pointer either backwards or forwards n positions from the current position. Moveforwards in n is positive, and move backwards if n is negative. If current reaches either end of the listbefore it has moved n positions, it stops at the end node. If the movement stops with current at either thefirst or last node, return a value of 1; otherwise, return a value of 0.int DLListMoveTo(DLList L, int n)Set the current pointer to point to the n th node in the list, where nodes are indexed starting from 1. If n issmaller than 1, the current node will be set to the first node. If n is larger than the number of nodes inthe list, the current node will be set to the last node. The return value has the same behaviour as thereturn value of DLListMove() .void DLListBefore(List, char )2017/8/11 COMP2521 17s2 - Week 03 Lab Exercise3/6Insert a new node in the list immediately before the current node. The new node becomes the currentnode. If the node is inserted before the first node, it also becomes the new first node.void DLListAfter(List, char *)Insert a new node in the list immediately after the current node. The new node becomes the currentnode. If the node is inserted after the last node, it also becomes the new last node.void DLListDelete(List)Remove the current node from the list (and free its memory, including the memory used to hold thestring). The current node becomes the node immediately following the node that was removed. If theremoved node is the last node (i.e. the node at the end of the list), then the current node is set to the newlast node. If the node removed was the only node in the list, then current becomes NULL. IfDLListDelete() is called with an empty list, it should simply return without making any changes tothe list.Note that the supplied DLList ADT implements a doubly­linked list of strings. The strings are created outsidethe list nodes, as in the diagram below:The storage used by the strings needs to be managed separately to the storage used by the list nodes,although still as part of the implementation of the DLList ADT.myed : a Text EditorText editors are programs that allow you to manipulate text files interactively; they provide a user interfacewhere you can load a file, make changes to it, and then save an updated version of the file. Text editors likemyed are seriously retro and hark back to the 60’s, when screen­based editors like vi and emacs didn’t exist,and graphical editors like gedit weren’t even a gleam in some inventor’s eye (except maybe Doug Engelbart).The myed program provides an extremely simple command­line interface where you enter one­lettercommands to manipulate the loaded file. Despite its primitive interface, myed can actually make changes totext files. Here’s an example session with the editor (using the normal notational conventions: what the systemprints is in this font , what you type is in this font , comments are in small grey font ).$ .C代写 Week 03 Lab Exercise代做留学生php语言/myed text // start the editor, loading a file called “text”> % // show all of the lines in the filethis is // the current line is the first linea small filecontaining a few linesof boring text> . // show the current linethis is> i // insert a line before the first linenew first line // you type the new line here> % // show all of the lines again, so you can see the changesnew first line // note that the new line is now the current linethis isa small filecontaining a few linesof boring text2017/8/11 COMP2521 17s2 - Week 03 Lab Exercise4/6> n // move to the next linethis is> n // move to the next linea small file> n // move to the next linecontaining a few lines> -2 // move back two linesthis is> w // save a copy of the modified file> q // quit the editor$ ls // list the files in the current directory… text text.new … // should see the original file and the new version$ cat text.new // look at the contents of the new text filenew first linethis isa small filecontaining a few linesof boring text$The best way to get a feeling for how the editor works is to play with it. There’s a working version in the classdirectory, which you can execute on the CSE lab machines via the command:$ /home/cs2521/web/17s2/labs/week03/myed FileNameChoose any old text file you like as the filename. Once you have an editor prompt, the command ? will tell youwhat other commands are available.If you play with the supplied myed , I can guarantee that you will run the % command a lot. Editing is betterwhen you get immediate feedback on the changes you’ve made, which led to the development of screen­basededitors like vi and emacs soon after the novelty of editing in the ed style. wore off (and once graphicaldisplays were available). If you’re keen to experience the original ed , it’s still available under Linux.Setting UpSet up a directory for this lab under your cs2521/labs directory, change into that directory, and run thefollowing command:$ unzip /home/cs2521/web/17s2/labs/week03/lab03.zipIf you’re working at home, download lab03.zip , unzip it there, and then work on your local machine.If you’ve done the above correctly, you should now find the following files in the directory:Makefile a set of dependencies used to control compilationDLList.h interface definition for the List ADTDLList.c (partial) implementation for the List ADTtestList.c main program for testing the List ADTmyed.c main program for a simple line­based text editortext a simple text file that you can use to test myedOnce you’ve got these files, the first thing to do is to run the command$ makeThis will produce messages about compiling with gcc and will put two executable files in your directory (alongwith some .o files).testL2017/8/11 COMP2521 17s2 - Week 03 Lab Exercise5/6This is the executable for the testList.c program. As supplied, it runs a number of simple tests onthe DLList datatype. You can use it by running ./testL , typing in a few lines of text and then usingcontrl­D to end the input; the program should display all of the lines you typed and not produce anyassert errors. You will need to add more extensive tests as part of this Lab.myedThis is the executable for the myed.c program, which implements a very simple line­based text editor. Ittakes as argument a file name, reads this file into a doubly­linked list of lines, and then supports variousactions on the lines. See the myed.c file for details on what commands are available, or type the ?command inside the editor to get help.Since myed is a text editor, it needs to be invoked by giving it a file to edit. The lab.zip archivecontains a small text file (called text ) that you can use with the editor. Invoke the editor using acommand like:./myed textTask 1The myed program is complete, but doesn’t work because some of the functions in DLList.c are notcomplete. You should complete these functions so that the behaviour of your myed program is the same as thebehaviour of the sample solution:$ /home/cs2521/web/17s2/labs/week03/myed textYou can execute the sample solution on the CSE workstations (only), by typing the full path name exactly asabove. It would be a good idea to play with the sample solution at least briefly to get an idea of how it issupposed to work.Note that since the two myed programs read from stdin and write to stdout , one automatic way of testingthem would be to devise a set of editing scripts (files containing sequences of myed commands), and thenapply each script. with both your myed and the sample myed and using diff to ensure that they both producethe same output, e.g.$ ./myed text out1$ /home/cs2521/web/17s2/labs/week03/myed text out2$ diff out1 out2In order to establish the correctness of your myed , you should develop a number of editing scripts, each onebeing more devious than the previous in attempting to crash your editor (and the sample one, if you can … ifyou do work out how to crash the sample editor, let me know, and I’ll fix it).Task 2Implementing the DLList functions to make myed work serves two purposes: (a) getting a complete editor,(b) myed providing a simple test harness for the DLList ADT. However, the way that myed exercises theADT is by no means complete, and more testing is needed.The testList.c program provides a skeleton in which to build a more serious testing framework. Atpresent, it is minimal; it simply uses the getDLList() function to read some strings from standard input intoa List , displays the List and then does a sanity check on the representation. You should expandtestList.c to provide a more complete set of tests to exercise the DLList functions.Note that writing a detailed test suite for all functions in the ADT is quite time consuming. You should at leastwrite comprehensive test cases for the three functions that you implemented. This means that you should havea test for each of the possible states in which each function might be invoked (e.g. empty list, list with onenode, etc.). At the least, each test should:show the state of the list before the operation (including curr and nitems )indicate what operation is about to be performedinvoke the operationdisplay the state of the list after the operation2017/8/11 COMP2521 17s2 - Week 03 Lab Exercise6/6run the validDLList() check on the listYou can simply use visual inspection of the output to check whether the operations have worked correctly, and,of course, check with validDLList() after each operation. It is not necessary to write automatic checks ofthe precise state, since this would require writing multiple functions almost as complex as validDLList() .If you wish to modify putDLList() to give a better display of the DLList state (e.g. use short lines anddisplay all of them on a single output line), that would be useful (but not essential).SubmissionYou need to submit two files: DLList.c and testList.c . Submit these either via the give command in aterminal window or from WebCMS, and then show your tutor, who’ll give you feedback on your coding style,your test quality, and award a mark.Have fun, jas转自:http://ass.3daixie.com/2019030627560482.html

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,222评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,455评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,720评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,568评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,696评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,879评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,028评论 3 409
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,773评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,220评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,550评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,697评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,360评论 4 332
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,002评论 3 315
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,782评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,010评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,433评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,587评论 2 350

推荐阅读更多精彩内容