Question
in c++ Write a program to show how a text editor can manage to undo commands. A stack is the natural way to do this.
in c++
Write a program to show how a text editor can manage to undo commands. A stack is the natural way to do this. You can use the STL stack or the textbook StackType code, though you would have to modify the latter.
Because your stack will contain both Insert and Remove objects, you will need to declare it as a stack of pointers to Commands.
What follows is the recommended way to do this, but as long as you support the desired input/output, it is acceptable.
Because we are focused on working with stacks, we are not going to actually save the text and will always start the program with an empty file. You wont have to define a separate class for the file.
You can represent the file as you like, but it will probably be easier to use a list of strings, where each string is a line of the file since the operations are on lines of the file.
Define a class called Command, which has two virtual functions that do not take parameters and do not return anything
o execute() execute the command
o undo() undo the most recent command
o You can choose to add protected variables to the class
o The functions need to be virtual so the compiler will find the child class versions
Define child classes Insert and Remove that override execute() and undo()
o Insert represents inserting a line of text into the file, while Remove represents removing a line of text from the file.
o Each has a constructor to let you get access to the text.
o Each should also include a way to remember which line the operation applies to
o The Insert class should remember the line to insert, while the Remove class should remember the line that was removed
o execute() should do the insertion/removal
o undo() should undo the effect of the operation
Here is some sample output:
Text editing with undo
Please choose one of the following:
0 to quit
1 to insert one line
2 to remove one line
3 to display the text
4 to undo the most recent insert/remove
1
Enter line to insert: Starting
Enter line number where to insert (0 to insert at start): 0
Please choose one of the following:
0 to quit
1 to insert one line
2 to remove one line
3 to display the text
4 to undo the most recent insert/remove
1
Enter line to insert: with a second line
Enter line number where to insert (0 to insert at start): 1
Please choose one of the following:
0 to quit
1 to insert one line
2 to remove one line
3 to display the text
4 to undo the most recent insert/remove
3
Starting
with a second line
Please choose one of the following:
0 to quit
1 to insert one line
2 to remove one line
3 to display the text
4 to undo the most recent insert/remove
2
Enter line number to remove: 0
Please choose one of the following:
0 to quit
1 to insert one line
2 to remove one line
3 to display the text
4 to undo the most recent insert/remove
3
with a second line
Please choose one of the following:
0 to quit
1 to insert one line
2 to remove one line
3 to display the text
4 to undo the most recent insert/remove
4
Please choose one of the following:
0 to quit
1 to insert one line
2 to remove one line
3 to display the text
4 to undo the most recent insert/remove
3
Starting
with a second line
Please choose one of the following:
0 to quit
1 to insert one line
2 to remove one line
3 to display the text
4 to undo the most recent insert/remove
4
Please choose one of the following:
0 to quit
1 to insert one line
2 to remove one line
3 to display the text
4 to undo the most recent insert/remove
3
Starting
Please choose one of the following:
0 to quit
1 to insert one line
2 to remove one line
3 to display the text
4 to undo the most recent insert/remove
0
Please follow the expected output text. This will allow the grader to grade your assignments faster so you get your grades earlier.
Style: Provide a brief comment to describe the program and each class and function you add, use meaningful names for the classes, functions, variables and constants you define. (For constants, you have to not only define named constants, but actually use those names.)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started