IIT Bombay Data Structures and Algorithms Prof Ajit

IIT Bombay Data Structures and Algorithms Prof. Ajit A. Diwan Prof. Ganesh Ramakrishnan Prof. Deepak B. Phatak Department of Computer Science and Engineering IIT Bombay Session: Text Editor using 2 Stacks (Program) Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 1

Functions IIT Bombay 1. 2. 3. 4. 5. 6. 7. 8. insert. Character delete. Character back. Space. Character move. Cursor move. Left move. Right replace examine. Top Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 2

Program IIT Bombay Class ‘text. Editor’ with member variables and its member functions class text. Editor { private: stack<char> left. Stack; //Left stack<char> right. Stack; //Right stack public: void insert. Character(character); bool delete. Character(); bool back. Space. Character(); void move. Cursor(int position); void move. Left(int position); void move. Right(int position); void replace(char find. What, char replace. With); void examine. Top(); }; //End of class Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 3

Program IIT Bombay void text. Editor: : examine. Top(){ if(left. Stack. empty()) cout << "left. Stack: emptyt"; else cout << "left. Stack: " << left. Stack. top() << ", " << left. Stack. size() << "tt"; if(right. Stack. empty()) cout << "right. Stack: emptyn"; else cout << "right. Stack: " << right. Stack. top() << ", " << right. Stack. size() << endl; } //End of function Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 4

Program IIT Bombay void text. Editor: : insert. Character(character){ left. Stack. push(character); } //End of function bool text. Editor: : delete. Character(){ if (right. Stack. empty()) return false; else right. Stack. pop(); return true; }//End of function Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay bool text. Editor: : back. Space. Character(){ if (left. Stack. empty()) return false; else left. Stack. pop(); return true; }//End of function 5

Program IIT Bombay void text. Editor: : move. Cursor(int position){ int left. Size, right. Size, count; left. Size = left. Stack. size(); right. Size = right. Stack. size(); if (position < left. Size) move. Left(position); else { count = position - left. Size; move. Right(count); } }//End of function Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay void text. Editor: : move. Left(int position){ int left. Size; left. Size = left. Stack. size(); while(position!=left. Size) { right. Stack. push(left. Stack. top()); left. Stack. pop(); left. Size = left. Stack. size(); } }//End of function 6

Program IIT Bombay void text. Editor: : move. Right(int count){ int right. Size, i=1; right. Size = right. Stack. size(); if (count > right. Size) cout << "Cannot move the cursor, right, to the specified position"; else { while(i<=count) { left. Stack. push(right. Stack. top()); right. Stack. pop(); i++; } //End of while } //End of else }//End of function Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 7

Program IIT Bombay void text. Editor: : replace(char find. What, char replace. With){ int count=1, original. Cursor. Poistion = left. Stack. size(); move. Cursor(0); //Move characters from left stack to right stack //Move characters from right stack to left stack and examine while(!right. Stack. empty()) { if(right. Stack. top()==find. What) { delete. Character(); insert. Character(replace. With); } else move. Cursor(count); count++; } //End of while move. Cursor(original. Cursor. Poistion); //Retain the original cursor position } //End of function Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 8
![Program IIT Bombay int main() { int i=0; char text[]="Data. Structures. And. Algorithms"; text. Program IIT Bombay int main() { int i=0; char text[]="Data. Structures. And. Algorithms"; text.](http://slidetodoc.com/presentation_image_h2/fbc3b32803802413eb35576742187e02/image-9.jpg)
Program IIT Bombay int main() { int i=0; char text[]="Data. Structures. And. Algorithms"; text. Editor txt; while(text[i]!='