Objectives 1 Understand the design implementation usage and
Solution
Solution: See the code below:
----------------------------------------------------------
/**
* This program demostrates a line editor
*/
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
//node of storing a line in linked list
struct LINENODE {
int line_no;
string line_text;
LINENODE *previous_line;
LINENODE *next_line;
};
//pointers for first line and current line
LINENODE *first_line = NULL, *current_line = NULL, *last_line = NULL;
//insert a new line in list after current line or as a first line, if list is empty.
void insert_line(int line_no, string line) {
//new line
LINENODE *LINE = new LINENODE;
LINE->line_no = line_no;
LINE->line_text = line;
LINE->previous_line = NULL;
LINE->next_line = NULL;
//linked list empty
if (first_line == NULL) {
//insert as first line
first_line = LINE;
current_line = LINE;
last_line = NULL;
} else {
//insert after current line
LINE->previous_line = current_line;
LINE->next_line = current_line->next_line;
if (current_line->next_line != NULL)
current_line->next_line->previous_line = LINE;
current_line->next_line = LINE;
current_line = LINE; //updates current line
last_line = current_line; //updates last line
}
}
//inserts a line before a given line
void insert_line(int before_line_no, int line_no, string line) {
//new line
LINENODE *LINE = new LINENODE;
LINE->line_no = line_no;
LINE->line_text = line;
LINE->previous_line = NULL;
LINE->next_line = NULL;
//locate line and insert
LINENODE *curr = first_line;
while (curr != NULL) {
//if line found, break
if (curr->line_no == before_line_no)
break;
curr = curr->next_line;
}
//now insert
LINE->next_line = curr;
LINE->previous_line = curr->previous_line;
if (curr->previous_line != NULL)
curr->previous_line->next_line = LINE;
curr->previous_line = LINE;
current_line = LINE; //updates current line
}
//append a line
void append_line(int line_no, string line) {
//new line
LINENODE *LINE = new LINENODE;
LINE->line_no = line_no;
LINE->line_text = line;
LINE->previous_line = NULL;
LINE->next_line = NULL;
//insert after last line
LINE->previous_line = last_line;
last_line->next_line = LINE;
last_line = LINE; //updates last line
current_line = last_line; //updates current line
}
//list lines of text
void list_lines() {
LINENODE *curr = first_line;
while (curr != NULL) {
cout << curr->line_no << \">\" << curr->line_text << endl;
curr = curr->next_line;
}
}
int main(int argc, char* argv[]) {
//check if sufficient numbers of arguments are there.
if (argc < 2) {
cerr << \"Name of file not specified!\" << endl;
cout << \"Usage: EDIT <filename>\" << endl;
return -1;
}
string filename = argv[argc - 1]; //output filename
string filepath = \"./\";
//output file stream
ofstream outfile((filepath + filename).c_str());
//check if file can be created or opened
if (!outfile) {
cerr << \"Error creating or reading file! Exiting...\" << endl;
return -1;
}
int line_no = 1; //line number
string line; //line to be read
//loop to read lines and do operations
//int line_counter=0;
while (1) {
cout << line_no << \">\";
getline(cin, line);
if (line.length() == 0)
line = \"\";
//cout<<\"len:\"<<line.length()<<endl;
char flag; //flag to be used for an operation
if (line != \"\")
flag = line.at(0);
else
flag = \'Y\'; //Y for empty
//cout<<\"char:\"<<flag<<endl;
switch (flag) {
case \'I\':
int before_line_no;
before_line_no = line.at(2) - \'0\';
cout << \"insert\" << before_line_no << endl;
fflush(stdin);
getline(cin, line);
if (line.length() == 0)
line = \"\";
insert_line(before_line_no, line_no, line);
break;
case \'A\':
fflush(stdin);
getline(cin, line);
if (line.length() == 0)
line = \"\";
append_line(line_no, line);
break;
case \'D\':
cout << \"delete\" << endl;
break;
case \'L\':
list_lines();
break;
case \'E\':
exit(1);
case \'Y\':
insert_line(line_no, line);
break;
default:
insert_line(line_no, line);
break;
}
line_no++;
}
return 0;
}
----------------------------------------------------



