 Good morning. Today we propose to discuss the stack and editor classes. We will do a very quick review of the stack class. Last time we had discussed it. We want to go ahead and build an editor class so that we can have an editor object from which we can actually handle modification of text given as input. Edit it and create an edited output. Last time in fact we have seen an example of such an editor. What is the change? The change now is that first we will not define our own stack class as we had done last time. As I mentioned there is a big library in C++ which provides for large number of classes. A string class was one which we had seen. There are several such classes. Additionally there is a special library called standard template library. The template library defines a set of classes, very important classes, but they define it in form of templates. That means a single template can be used to define objects of different types. We will see what exactly we mean by these objects of different types. Having done that, we will then use the standard stack class of the STL, the standard template library and then we will define and use an editor class. So let us do a review of stack. So you keep elements here and we know that we can take out the top element or we can push an element on the top. So there are basically two possible operations on this. One is called a push where you provide some element and it is that element which will be pushed on top of the stack. Last time we had seen what is the corresponding operation to take out elements? Pop. Last time we had seen that pop returns a value of the element type which you can actually look at. The standard template library pop is however different. It literally pops and forgets it. So it doesn't return anything to you. It just pops. That means it removes an element. Now that is not very useful. After all whatever you have kept in the stack, you would like to get it out yourself. So for that, there is another function which is provided in the standard template library which is called top. This top is a facility to peep into the top of stack. This top is not like our top variable which was pointing to a location. This top is a function and when applied to an object of type stack, it is capable of peeping into this. It will look here and will return. It returns the value of the object at top without disturbing it in any way. So assume that the stack consists of a sort of glass jar. So you can see but the glass jar always has an opaque boundary which keeps moving up as you keep putting things on the stack. So you can see only top of the stack at any time. So the top statement, actually the top function looks at the top of stack and says, hey, this is what you have. As a result, if you want to actually get an element from top of the stack and also remove it from the stack, what will you have to do? So suppose the elements were integer elements, you will say some int p, then you will say p is equal to, let's say I also must have stack s and then I will say s dot. So assuming that there is an existing element on the top of the stack, this will get you that value in p and then immediately you say s dot pop. So when using the standard template library, you have to use this combination always to get out something and also have a value. While pushing, there is a single function, push stack. These functions we don't have to define. We don't have to define the stack class, private members, etc., etc., etc. What? We don't even need to know what are the private members as is the standard practice with object classes. We don't need to know. But we know that we can define a stack of a certain type and then we can push elements and pop elements. The major difference, once again I remind that the pop only removes an element and forgets it, but top permits you to look at that element. So if you want to look at an element and pop it out, you will have to say top something followed by pop. This is again the revisit to the definitional stack is a container in which values can be pushed in or popped out. It has lethal properties. Last in, first out. I had said that it can be implemented using an array, push always at one end, pop always at the same end. Actually the standard template library does not implement stack using an array. It actually uses a structure called dq. It's an opposite of q, which is considered the basic structure on top of which variety of things are implemented including lists, vectors and so on. We shall see some of those things as we proceed further. But once again, we ought to be reminded that we don't care one who about how the class has been implemented. If it is part of the standard library, somebody else has taken the care, we need to know only the interface that is required to access the elements of the class. So once we define a stack class, we can define the elements in our main program and start using. This is the stack class from stl. It's actually stl. This stl stands for standard template library. What is the difference between a standard library such as string class which we had used earlier or any class definition such as we give and a template library? The difference is that in our efforts to define classes or other standard classes that you see, the classes define objects, all of which have some very specific basic elements. Consider the class int pair that we had defined. Int pair was defined to have two private data elements integer 1, integer 2 that is numerator, integer denominator, integer for example. Suppose we wanted to define a pair of two floating point numbers, then I would have to define that class to contain private members as float x, float y something and to distinguish between this and the int pair, I might have to name the class as float pair. Now if I was to add these numbers as one operation, then adding integer numbers or adding floating point numbers is exactly same in concept. But I will still have to define one as int pair, one as float pair. I will have to still define separate operation of add int and float. C++ designers thought that this is unnecessary extra work and people will have to remember a very large number of classes and their interfaces for every thing which could have 3, 4, 5 standard variations which are these standard variations int and float is one variable. Mixing int and float is another variable. I can add integer and float. Similarly, operations plus minus star as far as the basic data types are concerned, these operations apply to any basic data like integer or float and natural conversion occurs. I would like my objects also to use exactly the same operator symbols and I would like to automatically distinguish between addition of two integer numbers and addition of two floating point numbers. So what these C++ designers did is they provided a facility to define a template. A template is like a model. Now that model can be used for integer, that model can be used for float, that model can be used for care by specifying for what type you want to use that model. Such is the standard template library. It consists of class definitions, etc. But all class definitions are given in the form of such templates. We shall of course discuss in greater details what these templates are and how they are prescribed, etc. But for that time being, we will only consider the following. Ordinarily, I would have defined stack. If I have stack as a class, I would have just said stack s. Then s would have become an object of the type class or stack. Since I am going to use the stack library stack and not my stack class, I can specify stack opening less than sign care greater than sign. This tells the template library that look I want to use stack, but this stack will contain elements of the type characters. If I had said stack int s, then s would have become a stack of integers. I could similarly do stack float. If I have an employee class and if I have objects of employee type, I could define a stack of employees, where each employee along with its entire structure becomes one element of the stack. So that is the power of the template. The operations on this stack are exactly as I described some time ago. So I have, for example, a push operation s dot push ch. Notice that I have defined it to be a stack of characters. So obviously I will have to give character as a element. Whatever character element I give as a parameter, actual parameter, it will get pushed on top of stack. s dot top, as I explained, returns the element at top of stack without disturbing it. And s dot pop pops out an element and forgets it. So as I said, I should use that as a combination whenever I want to do certain things. What is the maximum size of the stack? Remember last time when we defined our own stack class, we had said that it would be a character array that was our implementation. And whatever was the maximum number of elements in that character array was the limit of the size of the stack. Now in this case, should we not know what is the limit? So any idea what would be the maximum size of the stack, speculate, what could it depend upon? Even ordinarily in my class definition of stack, when suppose I have defined that class, its functions, etc., and put that hidden from you and you are using it, how would you know what is the limit? It will have to be part of the documentation of the class library that this is the interface, this is the maximum limit. There what was the maximum limit that we had thought of? Remember last class where we had defined a stack, I thought we said 10,000 or something, whatever, whatever 100. So that there is some number. What could that number be in such case? Actually the standard template library uses the dynamic memory allocation facility that is available. I have briefly mentioned what that dynamic allocation facility is and we shall be discussing that in greater details later. Suffice it to say that whenever you want to create objects for which the size is not defined explicitly in the class definition, then what the template library is capable of? The constructor function for that particular template library will ask for memory from the operating system. It may do one of the two things. It may create an initial stack of an arbitrary size, say 100 for example. We do not even know that. Suppose I want to put 100 and first element in the stack. In such a case, the standard template library is capable of determining that I have reached the current limit of memory allocation, go to the operating system and say this man wants to put more objects in his stack. Please give me more memory and it will get that memory and logically connect it with this memory of the stack, whatever is the internal implement. Therefore, technically a stack can be as big as the total memory available freely with the operating system. After operating system is loaded, your program is loaded, etc. However, in practice you will be limiting the use of stack, not because of the intrinsic limit of the standard template library, but because whatever you are going to do with stack would be within the limits of a real life problem that you are solving. For example, if you want to edit text, then you may decide arbitrarily that my editor will handle maximum 10,000 characters for editing, in which case you would like to impose a limit of 10,000 characters. Now, that will have to be imposed outside of the stack class because stack class does not care whether 10,000, 20,000 or whatever. So, the way you use stack, you should limit the usage of stack to less than 10,000 for example, or 20,000 whatever. But as far as this stack library is concerned, it is obvious. Can you notice the similarity between the function definitions and the standard functions that you use? You do not have to do anything to implement top, pop, push. It has been done already. But there is something else that is additionally advantages as compared to the conventional function definition. And that is the private data members are completely hidden. The implementation is completely hidden, which can be hidden even in case of functions. But you have no mechanism to access any private data member directly. So, you do not even know what it is and how it is done. We will now consider building an editor class. What could be an editor class? An editor class is a class whose members can provide editing functionality to you. The private members of the data members of the editor class could be what and what should be the functions available on the editor class. Any idea? Let us try to look at an element of such an editor class, which we have not yet defined. We wish to define. Let us say this is an object called E, which belongs to this editor class. Now, we recall how we had decided to do editing. We said that we will use the two-stack model. We will have the notion of a cursor. Whatever is to the left of the cursor, we will keep from the left stack. Whatever is on the right of the cursor, we will keep on the right stack. Moving cursor will mean moving items from left stack to right stack or right stack to left stack. Deleting will mean deleting at the point of the cursor. Inserting means inserting at the point of the cursor. This was our definition of basic editing functions. If these functions are provided, then we will be able to use these functions to do any editing that we wish to do. So let us look at the private data members that an editor object of a class must have. It must have two stacks. So let us say that this editor object will have something called L-stack and something called R-stack. Agreed? It would have some elements here. It would have some elements here. That is how you perceive an editor object of a class. Now, additionally, if it is editing some text, it must know what is the total size of the text that it is editing so that it knows that characters in one stack plus characters in another stack together is equal to that size. Since it is an editor class, we call that value as t-size. This will be an integer variable which will have a location of itself. One more thing if we are editing, we are always editing with reference to a cursor position and that cursor should be between 0 to size minus 1. In fact, it can even be beyond size. It does not matter really because that means it is actually 0 will mean it is at the beginning of the text. So what would size mean at the end after the last element or before the last element? So I will have a position called cursor. You will notice that I do not need anything else. The private data members of the class called editor class need only be size which is an integer value which is the total size of text that I would like to put inside that. If I have different editor elements, different editors could be editing different sizes of text. This is the total size. Of course, the size value can increase or decrease depending upon how many characters I had. First of all, it depends upon how many characters I receive for editing in the first place. Someday I may receive 10,000. Someday I may receive 5,000. So the size will tell me at any point when I put some value there. Second, the cursor position which will determine how many characters of my total text to be edited lie on the left stack and how many characters lie on the right stack. If cursor position is 0, what would it mean in terms of which stack will be full? The right hand slide stack will have all the characters. This is the definition of all private data elements. Nothing else is required. Now remember this and now we know to try to define that if I want to meaningfully utilize this editor, what are the functions that I should have? Remember always a function call to an object invokes some activity which can initialize elements, change values, give values to us. Now this arbitrary change value give values to us, etc., etc. has to be seen in the context of editing. So what are the functions that we want to have for our final editing? First we want to put some text in the editor class, any editor object. Obviously that will be initializing an editor object. I will initialize it with some text which should be put all of it on the right hand side stack and the cursor should be at the 0th position. Then one obvious requirement is moving the cursor. So I should have a function where I should say move cursor to some position. That in response to that function, the editor object should automatically do push and pop to move the cursor to an appropriate point. Then I should have a function which will insert text at the current cursor position. So I should have a function which will take what is to be inserted and then suppose this is my cursor, it would perhaps insert it on the left side of the stack and move the cursor ahead appropriately and come back. I could have a function which says delete. Now when I have a cursor here and I press the delete key, what happens? The characters to the right of the cursor get deleted. On the other hand, if I press backspace, the characters at the left of the cursor get deleted. Do you notice this difference? The backspace and delete. So we should like to have two similar functions in our case also. A delete function and a backspace function. What should be the parameter for this? How many characters do you want to delete? And obviously one delete will work, deleting characters to the right of the cursor. So another delete will work, deleting characters to the left of the cursor. So this is one function that will require. When I have, is there anything else I need? As far as standard editing is concerned, I have insertion, I have deletion of both kinds, I have movement of cursor anywhere, I have initialization. At the end, I would like to take my editor object of the class. All right, I'm finished done. Please give me back everything else. So just as I have an init function, I would like to have a finish function. A finish function should do what? Take my text, which will be currently of whatever size it is and give it back to me as a string. Of course, at that point when I say finish, the cursor may be anywhere in the text. One simplest thing is to move the cursor to zero for which I would have written a function already, which means it will automatically put everything from left stack to right stack. And then assemble all contents of right stack into a character array exactly opposite of what I did at initials time and come out. This is the kind of thinking that has to go before you design a class. Let us consolidate this thinking by writing down some of these definitions. There is again an element E, I have two stacks here and I have T size and I have what? Cursor. That's all is there in that private determinant. Now I am defining interface functions. The first function would be something like E dot init. What should I give to this E dot init? I should give a string and I should give its initial size. Agreed? So I am now defining what would be the initialization. The editor class initialization will consist of me supplying it with a string. I am calling it a string. Actually I could call it text. So in fact it is better to call it text, which would of course be an array of some kind. Large array, 10,000 elements and this size. Initialization is done. The next one I want is the ability to move. So if I say E dot move cursor, the cursor should move. Originally init would have set the cursor to zero. The move cursor command must have some position, which position to move the cursor to. Notice that while your stacks have no implicit or explicit limit on how many characters you can put in the stack, but your cursor will have to have limits. That will depend on what? On the size of text you are editing. If you are editing a text which is 20 characters long, does it make sense to say move cursor to 27? Now that will have to be checked as the function move cursor implementation for the editor object. The stack has no problem but you would have a problem in editing otherwise. So these limits will have to be checked. But you know now what is to be done if move cursor command is given. You first check those limits. Otherwise move the characters from left stack to right stack or right stack to left stack. Only remember one thing. This time the movement will include looking at top, moving that element and then popping it off. So top plus pop. That is the combination that you have to use. The next inline will come. This will do what? It will insert the text at the cursor position. Now remember I have an implementation dependent feature here. If I decide to put the additional characters to the right, then the cursor position will not change. If I decide to put the additional characters on the left, then the cursor will have to be moved by as many characters I have posted. It depends upon your interpretation but which you must state in your implement that this is how things will be done in your insert. There are two delete functions next that I must use. What is the difference between delete care and backspace, delete text and backspace text? You remember? Yes. So delete text will delete to the right of the cursor. Obviously when I delete text, what will happen internally? By the way anytime you write a function, this is just the interface definition I am writing. Immediately after this, suppose you are designing this class, you should say I have thought of these interfaces but what will actually happen when a particular function is invoked should be written in plain English. For example delete text will delete the text to the right of the cursor. Then you should immediately write the implications, the implications on the private data elements of the class. So when you delete what will happen? There are four things there inside. Two are the stacks, one is cursor, one is the size. For each of these what will happen whenever you execute a function should be documented by right. So can you tell me for example in case of delete text what would happen? There are four elements, T cursor, a T size cursor, L stack and right stack. So tell me what happens to each one of these four when I delete text. If I delete text, then cursor does not change because if this is my cursor I am deleting to the right of the cursor. So cursor position vis-a-vis the left is remain same. But what happens to the size? T size. Totally T size was whatever was in the left stack and whatever was in the right stack total. But I have now deleted three characters or four characters. Shouldn't those many characters be removed from the size? So T size will be equal to T size minus, agreed? You said nothing changes in the stack, is that correct? So cursor remain same, left stack remain same. And what will happen to the right stack? Pop, num care elements from what have we done when we wrote this? We have actually written an English language description of the algorithm. Can you see the advantage of writing such things down? Because when you write this, writing your program is almost a syntactic translation of your English into your program. So remember always how to develop programs or classes. First think of the functionality that the class should have that will permit you to decide what should be the member data elements, private data elements. Next you decide for each of the functionality that you want to be associated with an object of the class, the interface functions that you would need. The interface functions will give you that function. And then for each of the interface functions, write down what should happen when that function is invoked. And then you are ready to define the complete class definition. And once you have completed the class definition, you can put that entire class definition which will include the description of private data members and public functions, that is the prototype, which will include implementation of each of those functions. And that is it. You can put all of this in a header file called say editor dot h. Your main program will be a short program for editing. Isn't it? That is how you would define this. Let me quickly show you. So let us quickly look at part of the code next 10 minutes. I will put the remaining code and the executable main program also on the web. Incidentally, I have created a new package for graphics which works. So the library references for the EZ windows which were not working for some of you. All that you need to do is go to this new link which is provided in the last week's thing, download that tar file in your home directory, extract everything in your home directory. It will create an EZW CS 101 directory for you. And there is a program, there is a shell script called compile. Using that compile step, you can compile all your API main programs and run them. I have put S1, S2, S3, those programs that I have demonstrated also there. There is one particular joker in that pack called S6 dot cpp which for some reason is not working. If any one of you can succeed in making it work, please let me also know. I will also independently try. Anyway, come back to this. So here is a class editor. So we continue to use two stack model for editing. The stack class of STL is used here. Notice that much of the thinking that we did and much of the jotting down that we did on piece of paper is included in brief inside the documentation of the program itself so that anybody who reads the program can understand some basic things. So I am saying the stack class of STL is used here. The member functions of the editor class carry out predefined editor commands explained below. Now this is something new. Remember, I am now extending whatever we thought so far. What is it that we thought? We thought that we should have functions which will initialize, delete text, insert text, backspace test, move cursor and finish. Now how would a end user use these facilities? When you write your main program, in the main program you will collect input from the user. User will most probably tell you two things. Here is my file which contains text to be edited. Isn't that what you do when you say g edit? Do you type in the text to be edited? No, you give it a file. Now if you have a g edit, you move the cursor using your mouse. But if it is a line editor, you would expect user to give some commands. Would you like that user to type in a command, move cursor 25? Would you not like it to make simple for that person? Say m 25. Similarly, instead of saying insert this text, instead of saying insert text, say how are you? I would like to say I, insert and then type the text. Delete text, some five characters. Should I have to say delete text five? Can I not simply say d five? So you are now designing other interface. The first one we will design for the editor class objects. How to interact? The second, how will you interact with the user? The first will be implemented as part of the class definition. The second will be implemented in your main program. So it is your main program which will read a complete text from a file. It will invoke the initialization. Then it will start reading, editing commands from the users. If it says move 25, then your main program will invoke move cursor 25. Do you agree that is how it should be? So that is why this program says that predefined commands which are explained below will be used. M pause moves the cursor to the specified position pause. I add text. Given text is inserted, d num, num characters are deleted to the right of cursor, b num, back space num characters are deleted to the left of cursor, q quit. Please remember in our interface functions we did not think of any quit because there is nothing like quitting. As far as that editor class is concerned, we have a finish operation. That finish operation will return back the edited text to us. It is in the main program as far as the end user is concerned. He says, Baba, I have finished editing. Then you will have to invoke that finish function, get the edited text back, and either display the edited text to him or to put it back in the file in which he had given you the original text. That is your job. So remember always whenever you define classes for actual applications, there are two aspects that you have to remember. One define the interface functions and the private data elements for the classes. And second, define what will be the user interface between an end user of your application and whatever you do. Here what I have documented are the end user interfaces. The documentation for the class functions themselves are partially contained in the class definitions which will now follow. It also says that the commands and their parameters are read from the keyboard. There is a syntax error here. Anyway, I will correct it. This should be on the next line. But I hope you can understand what I am trying to do here. This size or text size, I don't know why I have defined two. I think originally I had defined text size and then in my program I was using size. So I defined that also additionally. It's a typical goofy phatak syndrome. I think you are a better programmer so you will be careful. Include iostream, et cetera, et cetera. Remember these have nothing to do with limitations of my stack sizes because my stack has practically unlimited capacity now since I am going to use the standard template library. But these are the limits which I am imposing on my editor class. So my editor will not be able to handle more than 10,000 characters. Of course, I can make it 1 lakh. I can make it 10 lakhs, 1 million. 1 million characters will be generally more than any textbook that you can have or a storybook that you can have. So you can actually edit the complete book. I am now defining class editor. Remember the private data elements. Stack, care, L stack and R stack. You agree these are the two things that are there. Then int, t size and cursor. You remember these are the two things that we defined. So exactly four private data elements that this editor class has. What are the public functions? Here is the definition of the interface. So do you remember our discussion? The first interface function is int init care text int int size. The text whatever is given and the size. The next is move cursor given a position. The third is insert text given a word of text to be added and the length of that. The fourth is delete text and the fifth is backspace text. But you know the context. In one case you delete to the right of cursor. In the other case you delete to the left of cursor. And int finish. It will collect everything in the text array and put it back. Giving you also the length of the modified text which could be different. Let us very quickly look at a few implementation details of these. Int editor colon colon init. Care text int in size. What I have to do? I have to push everything that I receive on the right stack. So notice how I am pushing it. Starting from in size up to zero. The elements will be put on the right stack and the t size is in size. Cursor is zero. I said return. Nothing else is to be done here. Consider the move cursor. Move cursor is supposed to move the cursor from some position a current position to a new position. First I define an internal variable which is internal to the function by the way. Don't confuse it with the data element which is internal to the object itself. This is an internal function element. It will evaporate the moment function execution is over. But the object will remain as object data element will remain. So what am I doing here? I first of all set new cursor to the position. This is where I have to set the cursor. My current position is where it is at cursor. First of all I check whether the new cursor is within bounds or not. If the new cursor is less than zero I can't move the cursor there. If the new cursor is greater than t size I can't move the cursor there. So cannot move cursor beyond text boundaries I return. Please give another cursor position. If everything is okay I am saying if new cursor greater than cursor. Now what will I have to do? That means let's say the old cursor was five, new cursor is ten. If old cursor was five then there were four characters in the left stack and remaining on the right stack. If the cursor has to go to ten those many characters from right stack have to come and go into the left stack. Notice how I am doing that. From cursor to new cursor I am looking at the top element of r stack and taking it out by looking at it and I push it in the left stack. But that does not remove it from the right stack. So I say r stack pop. Remember that top followed by r stack pop will complete that pop operation. What is this c out r stack dot top? What is this c out less less endl? c out is an output statement. This will produce an end of line and this will produce every character which has been taken out from the top and put it on this side. When you edit does the internal editor show why it is working? Oh I have taken this character out, I have put it there etc. Does it show? Would you like it to be seen? After all what does that ultimate user wants? Given your text, given commands, even the final edited text so why is it there? Speculate. Yes, any suggestion? Come again? For debugging. It is absolutely right. These statements have absolutely no business belonging to the final code that you produce and deliver to people. Internal operations while you are executing internal functions should not be visible. Just as private data members are not visible. Only interfaces are visible. Remember these functions are actually operating upon private data members on the stacks and so on. So end user has no business knowing these things. Of course when I am developing the class definition and then I am developing an editor program which will handle various commands, my program may be save 100, 120 likes and it is quite possible that I could goof up. In fact if the programmer is me it is not possible that I will goof up. I guarantee that I will goof up because I am of that type. I make mistakes. But many of you could make such mistakes inadvertently. How will you know where is the mistake? You will only know given text was this, output text is this. You don't know what has happened and of course then in between you shall have segmentation faults then you won't know what has happened. All such statements are included in the initial version of your program. At the end they should either be commented or they should be removed. Agreed? But you will agree that initially when you are developing programs you should have such statements so that you know whether things are working out fine on and the way to test these is test each function independently by calling it many times. So this is the move cursor if new cursor is greater than cursor. If it is not so, by the way that doesn't end your job. You have to now assign a new value to cursor which will be the new cursor value. Does the size change in move cursor? The size? No. T size will remain what? In fact you might want to add here at the beginning shift characters from one stack to another. Cursor position will change. T stack will remain same and characters will be moved from right stack to left stack. That much of commentary is in order inside this. If this is not so, else you have to shift characters from L stack to L stack from right stack. No, sorry. What is it? It's the other way round, right? See the condition was if new cursor was greater than cursor I was shifting from R to L. So if it is else I should be shifting from L to R. The program is correct. The comment is wrong. Do you notice? What should comment say? Shift characters from left stack to R stack. See this is called a goofy Fartak syndrome. You should never be goofy like that when you write programs. That is where these things such as C out L stack etc. etc. will help in debugging and then you should of course make your comments consistent with your implement. Of course these comments should be written first because they have already been decided. What you have to do has been decided. Anyway. So you again I will remind you. You look at the top and push it and then also pop that element. You have to do both. Insertion text exactly in the similar way. When you insert the text T size plus length will be the new size now. It is quite possible that you had a text of 9900 characters and somebody is trying to insert 200 characters. The self-imposed limits of 10,000 characters for that editor will be exceeded. So you should cross check that. If that is happening you should refuse to insert. If that is not happening however you should push characters from ad-word to the left stack. I am putting it to the left stack. I could have put it on the right stack as well. Is this program correct? Now this is not a goof up error. This is a deliberate error. Find that out. What am I doing? I have said insert some characters. I have given some 10 characters or whatever that is known by length and the characters to be inserted are given in ad-word. I have said I will push characters from ad-word to the left stack, right? So what am I doing here? For i equal to 0 to i less than length I am taking a ith element and pushing it on the left stack. Notice that things which go on to the left stack go in the same order in which I receive them. So I don't have to invert the order or something. The first character will go first, second, third this looks alright. I am of course changing t size to t size plus length. So what would be the error? Please remember if you put characters on the left stack and left stack is the custodian of cursor. Cursor 0 means there was nothing on the left stack. Cursor 5 means there are 5 elements in the left stack. Now the cursor has changed because I have added 3, 5, 10, 20 characters. So what I must add here? Yes, agreed cursor must change. I will just leave it in the slides that I will post there. You can just look at this and make the corrections. I will not go through the further details. I hope you have understood the basic things that you need to do about the functions. Let us very quickly look at some portion of the main program. This is my main program. I am defining a file. I assume that somebody will give the text in a file. So I define a file pointer. I collect the file name from the user. This is my reading of the file name and then I open that file. I take the usual care if the file is not present or it cannot be opened. I give a message. Otherwise I read the entire text in the file. How do I read the entire text? Of course when I opened it, it should be at the beginning but I force it at the beginning. F seek, Fp, 0, seek. F pause, F tell, Fp, size equal to file pause. What are these 3 statements? You cannot know the size of the file. So I can do 2 things. I can keep reading a character till the size ends. Or in one shot I can find the size. I can force that file to the internal index to go to the last position which is what is done by saying F seek, seek end. Go there. Now if I say F tell, it will tell me what is that position and that must be the total size of bytes. So I will write that total size which I will put as size. And now input file size is so much. Why I want to do that ahead of time? Because earlier itself I want to advise the user that look baba my editor is capable of 10,000 character editing only. You have more than that go home or find somebody else. Otherwise of course I will read now and this is what I read. So please notice since I have shifted that internal index of the file I will say rewind Fp. Rewind Fp will put it back at the beginning. And then my standard thing I F read text, 1, size, Fp. Do you remember what this statement will do? It will read 1 byte unit. How many units? Size units. There are size bytes. I could have also said a free text, size, 1. That means I have a magnitude of size bytes to be read 1 unit. Size into 1 or 1 into size is same size units. I will read all the size bytes. Now this is a text in the file. Remember I am not reading a text using C in. If I read a text using C in, the C in statement automatically inserts a backslash 0 at the end. There is no such thing in a file. So I will have to do that myself. Text size plus 1 equal to this. You get the point? Once it is done I initialize the editor object. My edit dot init text, size and that editor function will take care of it. Now I will just show you the series of commands that people will give. Will you agree that the best way to handle some kind of a menu situation where somebody will type M or D or I for delete insert move, I will use a switch statement because one character will come. I will say give an editor command. I will read one character. CH is defined as caravan. Now based on what that character is, I have to do one of the several things. Look at the case statement. Case capital M colon case small M colon. Why? Because somebody may just type M. Thinking move is M. So capital or small, I would not like to trouble. But once he says move cursor to the given position I have already have an internal function to do that. I will of course have to read position to what position he wants to move and what I read next will depend upon what does the command. So here I read the position. All I do is I say my edit dot move cursor pause. Agreed? So I have translated the user command into an internal function call public function call to that editor. And of course I break. Break means I go over to read a new command. The next is insert. So read an insert word. I will read a word to be added. I will say add word length is string length of this add word. Please remember I am reading this word using C in. That means the add word will be a proper string now with a backslash zero inserted by C in. So when I say string length, I will get the correct length of it. All that I need to do is I will send that to my edit using insert text function which I haven't done. I will again say break. Similarly for D in case of delete I have to read a number of characters to be deleted. I will invoke my edit dot delete text. Similarly in case of backspace if someone says B or B delete characters before cursor I will read a number back edit dot backspace. Agreed? So you see the main program becomes pretty simple now because of the way I have defined my interface function. Finally if nothing else is to be done sometime or the other the user will get tired of editing and say quit. And when he says quit I do a variety of things. I say finished editing, extract edited text and quit. So my edit dot finish will assemble the text in an array and give it back to me and here I will say size of edited text is this. See out out text and it will break. Of course after every editor command I execute that command and I wait for the next command. I have to set up an iteration. Do not forget that at the beginning I started with do something. I can implement it using while or I can use it to do. At the end what should I say in while? When I come here I would have come because of one of the breaks. The command because of which I have come here after completing it could be anything insert move initialize whatever. But if it is quit I have to get out. If it is not quit I have to continue. So I have to do all of this while CH is neither capital Q nor small. I continue doing this. Otherwise I will print the final out text and get out. Because I know when I have come here I would have come here only because I have finished. So you got this. This is exactly how you can actually segment your larger conceptualization of an algorithm, define classes, define classes containing objects and make your main activity as simple as possible. Ok. We will stop here. Thank you.