SlideShare a Scribd company logo
1 of 33
Download to read offline
#include
#include
#include
#include
#include "textFile.hpp"
using namespace std;
void displayHelp();
bool isInteger(const string &s);
bool askSave();
int main(int argc, const char * argv[]) {
int resultCode = 0;
// Convert to a vector.
vector args(argv, argv + argc);
// Process arguments, must be 1 argument, otherwise exit with code 1.
if (args.size() < 2) {
cout << "Missing command line argument!" << endl;
cout << "Usage: ./myEditor " << endl;
resultCode = 1;
} else {
string fileName = args[1];
textFile dataFile(fileName);
// Check if the file is ok to use, if it is, lets go.
if (!dataFile.checkFile()) {
resultCode = 2;
} else {
// We can now assume the file is good, so we can work with it.
dataFile.loadFile();
// Display some text to help the user.
displayHelp();
// Now get user input.
bool done = false;
string userInput = "";
while (!done) {
cout << "> ";
getline(cin, userInput);
char cmd = tolower(userInput.c_str()[0]);
char cmd2 = ' ';
if (userInput.size() > 1) {
cmd2 = userInput.c_str()[1];
}
// This check makes sure the command format is correct
// so that commands don't fire off by mistake.
if (strncmp(&cmd, " ", 1) != 0 && strncmp(&cmd2, " ", 1) == 0) {
switch(cmd) {
case 'q': {
done = true;
break;
}
case 'i': {
unsigned long lineIndex = dataFile.textLines() + 1;
string newLine;
if (userInput.size() > 1) {
string cmdArg = userInput.substr(2);
lineIndex = stol(cmdArg);
}
cout << to_string(lineIndex) << "> ";
getline(cin, newLine);
dataFile.insertLine(lineIndex - 1, newLine);
break;
}
case 'd': {
if (userInput.size() > 1) {
unsigned long lineIndex;
string cmdArg = userInput.substr(2);
lineIndex = stol(cmdArg);
dataFile.removeLine(lineIndex - 1);
}
break;
}
case 'l': {
for (int x = 0; x < dataFile.textLines(); x++) {
cout << to_string(x + 1) << "> " << dataFile.getLine(x) << endl;
}
break;
}
case 'h': {
displayHelp();
break;
}
case 's': {
dataFile.saveFile();
break;
}
}
}
}
// Ask about saving the file before exiting.
bool save = askSave();
if (save) {
dataFile.saveFile();
}
cout << "Thank you for using myEditor." << endl;
}
}
return resultCode;
}
/*
Asks the user if they'd like to save or not, returns true if they do. Anything
other than "y" or "Y" will be interpreted as a negative answer.
*/
bool askSave()
{
bool result = false;
string userInput = "";
cout << "Do you wan to save the change? (Y - yes; N - no) ";
getline(cin, userInput);
userInput = tolower(*userInput.c_str());
if (strncmp(userInput.c_str(), "y", 1) == 0) {
result = true;
}
return result;
}
/*
Displays help text.
*/
void displayHelp()
{
cout << "Welcome to my text editor." << endl;
cout << "tTo insert text at the end of the file, type 'I'" << endl;
cout << "followed by the text." << endl;
cout << "tTo insert text at a certain line number, type 'I'" << endl;
cout << "followed by a space and the desired line number." << endl;
cout << "tTo delete a line, type 'D' followed by a space and the" << endl;
cout << "line number." << endl;
cout << "tTo print all the lines, type 'L' and press enter." << endl;
cout << "tTo save the current content, type 'S' and press enter." << endl;
cout << "tTo display this introduction, type 'H' and press" << endl;
cout << "enter." << endl;
cout << "tTo quit, type 'Q' and press enter." << endl;
}
/*
Checks to see if a stirng is an integer, returns true if the string is an
integer, returns false it it is not.
*/
bool isInteger(const string &s)
{
char *p;
strtol(s.c_str(), &p, 10);
return (*p == 0);
}
textFile.hpp
#ifndef textFile_hpp
#define textFile_hpp
#include
#include
#include "simpleList.hpp"
using namespace std;
class textFile {
private:
simpleList lines; // Stores all lines of the file.
string fileName; // The name of the file being accessed.
ifstream inputHandle; // File object for reading data from the file.
ofstream outputHandle; // File object for writing data to the file.
public:
textFile(); // Default Constructor
textFile(string file); // Constructor
~textFile(); // Destructor
unsigned long textLines(); // Size of the text file in # of lines.
string getLine(unsigned long lineNumber); // One line of text from the file.
void setLine(unsigned long lineNumber, string data); // Sets a specific line to a new value.
void insertLine(unsigned long lineNumber, string data); // Inserts a line into the file.
void removeLine(unsigned long lineNumber); // Removes a line from the file.
bool checkFile(); // Checks to see if a file is good.
void loadFile(); // Loads all the data into the object of the text file in question.
void saveFile(); // Saves all the data in the object back to its file.
void setFile(string file); // Allows setting of the file name after object creation.
string getFile(); // Returns the name of the file being worked with.
};
#endif /* textFile_hpp */
textFile.cpp
#include
#include "textFile.hpp"
#include "simpleList.hpp"
using namespace std;
/*
Default constructor for textFile class.
*/
textFile::textFile()
{
fileName = "";
}
/*
Constructor that accepts the file name in the object creation.
*/
textFile::textFile(string file)
{
fileName = file;
}
/*
Destructor function, no action required at this tiem.
*/
textFile::~textFile()
{
// Nothing for here.
}
unsigned long textFile::textLines()
{
return lines.size();
}
string textFile::getLine(unsigned long lineNumber)
{
// Check to make sure we're not going to go out of bounds.
if (lineNumber < lines.size()) {
lines.toHead();
lines.forward(lineNumber);
return lines.current()->data();
} else {
// If the index of lines to return is out of bounds, return an empty
// string.
string noData = "";
return noData;
}
}
void textFile::setLine(unsigned long lineNumber, string data)
{
if (lines.size() > lineNumber) {
lines.toHead();
lines.forward(lineNumber);
lines.current()->data(data);
} else {
lines.toTail();
lines.current()->data(data);
}
}
/*
Inserts a new line into the lines vector with data.
*/
void textFile::insertLine(unsigned long lineNumber, string data)
{
if (lines.size() > lineNumber) {
lines.toHead();
lines.forward(lineNumber);
lines.insert(data);
} else {
// This will add enough lines to get to lineNumber x even if there are
// not enough lines.
unsigned long blanks = lineNumber - lines.size();
if (blanks > 0) {
string blankString = "";
for (unsigned int x; x < blanks; x++) {
lines.append(blankString);
}
}
lines.append(data);
}
}
/*
Removes a line from the lines vector.
*/
void textFile::removeLine(unsigned long lineNumber)
{
lines.toHead();
lines.forward(lineNumber);
lines.remove();
}
/*
Checks to see if the file is good to read/write from.
*/
bool textFile::checkFile()
{
// Open the file as if we want to read it.
inputHandle.open(fileName);
// Check to see if the file access is good.
bool result = inputHandle.good();
// Close the file.
inputHandle.close();
return result;
}
void textFile::loadFile()
{
// Make sure we start with a blank slate, no items in the vector.
if (lines.size() > 0)
{
lines.clear();
}
// Open up the file for reading.
string line;
inputHandle.open(fileName.c_str());
// Read every line until end of file.
while (!inputHandle.eof()) {
getline(inputHandle, line);
lines.append(line);
}
if (line == "") {
lines.toTail();
lines.remove();
}
// Make sure to close the file again.
inputHandle.close();
}
/*
Saves all lines in the lines vector to the file.
*/
void textFile::saveFile()
{
// Open the file for writing, this should erase an exiting file.
outputHandle.open(fileName.c_str());
// Write every line from the lines vector into the file, appending
// a new line to the end of each.
lines.toHead();
while (lines.current()->next() == nullptr) {
outputHandle << lines.current()->data().c_str() << endl;
lines.forward(1);
}
// Make sure to close the file.
outputHandle.close();
}
void textFile::setFile(string file)
{
fileName = file;
}
/*
Returns the name of the file being worked with.
*/
string textFile::getFile()
{
return fileName;
}
simpleList.cpp
#include
#include "node.hpp"
#include "simpleList.hpp"
using namespace std;
template simpleList::simpleList()
{
head = nullptr;
tail = nullptr;
currentPtr = nullptr;
listSize = 0;
}
template simpleList::~simpleList()
{
clear();
}
// Appends data to the end of the list.
template void simpleList::append(dataType newData)
{
node *newNode = new node;
newNode->container = newData;
if (newNode == nullptr) {
cerr << "Error allocating memory, append failed." << endl;
} else {
if (head == nullptr && tail == nullptr) {
head = newNode;
tail = newNode;
} else {
tail->nextPtr = newNode;
newNode->prevPtr = tail;
tail = newNode;
}
listSize++;
}
}
*/
template void simpleList::insert(dataType newData)
{
if (currentPtr == nullptr) {
currentPtr = head;
}
node *newNode = new node;
newNode->container = newData;
if (newNode == nullptr) {
cerr << "Error allocating memory, insert failed." << endl;
} else if (currentPtr == head) {
head = newNode;
currentPtr->prevPtr = newNode;
newNode->nextPtr = currentPtr;
currentPtr = newNode;
listSize++;
} else {
currentPtr->prevPtr->nextPtr = newNode;
newNode->nextPtr = currentPtr;
newNode->prevPtr = currentPtr->prevPtr;
currentPtr->prevPtr = newNode;
currentPtr = newNode;
listSize++;
}
}
template void simpleList::forward(unsigned long distance)
{
for(int x = 0; x < distance; x++) {
if (currentPtr->nextPtr != nullptr) {
currentPtr = currentPtr->nextPtr;
} else {
break;
}
}
}
template void simpleList::backwward(unsigned long distance)
{
for(int x = 0; x < distance; x++) {
if (currentPtr->prevPtr != nullptr) {
currentPtr = currentPtr->prevPtr;
} else {
break;
}
}
}
// Reset the current pointer back to the beginning of the list.
template void simpleList::toHead()
{
currentPtr = head;
}
// Reset the current pointer to the end of the list.
template void simpleList::toTail()
{
currentPtr = tail;
}
template node *simpleList::find(dataType searchData)
{
node *searchPtr = head;
while (searchPtr->nextPtr != nullptr) {
if (searchData == searchPtr->container) {
currentPtr = searchPtr;
return currentPtr;
}
}
return nullptr;
}
template void simpleList::remove()
{
if (currentPtr == nullptr) {
currentPtr = tail;
}
node *toRemove = currentPtr;
if (currentPtr == tail && currentPtr == head) {
tail = nullptr;
head = nullptr;
currentPtr = nullptr;
} else if (currentPtr == tail) {
tail = currentPtr->prevPtr;
tail->nextPtr = nullptr;
currentPtr = tail;
} else if (currentPtr == head) {
head = currentPtr->nextPtr;
head->prevPtr = nullptr;
currentPtr = head;
} else {
currentPtr->prevPtr->nextPtr = currentPtr->nextPtr;
currentPtr->nextPtr->prevPtr = currentPtr->prevPtr;
currentPtr = currentPtr->prevPtr;
}
delete toRemove;
listSize--;
}
// Removes all items from memory.
template void simpleList::clear()
{
if (listSize > 0 && head != nullptr && tail != nullptr) {
currentPtr = head;
while (listSize > 0) {
remove();
}
}
}
// Give the current size of the list.
template unsigned long simpleList::size()
{
return listSize;
}
// Return the current pointer.
template node *simpleList::current()
{
return currentPtr;
}
simpleList.hpp
#ifndef simpleList_hpp
#define simpleList_hpp
#include "node.hpp"
namespace std {
using namespace std;
template
class simpleList {
private:
node *head;
node *tail;
node *currentPtr;
unsigned long listSize;
public:
simpleList();
~simpleList();
void append(dataType newData);
void insert(dataType newData);
void forward(unsigned long distance);
void backwward(unsigned long distance);
void toHead();
void toTail();
node *find(dataType searchData);
void remove();
void clear();
unsigned long size();
node *current();
};
}
#endif /* simpleList_hpp */
node.cpp
#include "node.hpp"
using namespace std;
template node::node()
{
prevPtr = nullptr;
nextPtr = nullptr;
}
template node::node(node *prv, node *nxt)
{
prevPtr = prv;
nextPtr = nxt;
}
template node *node::prev()
{
return prevPtr;
}
template node *node::next()
{
return nextPtr;
}
/*
Returns the data contents of the node.
*/
template dataType node::data()
{
return container;
}
/*
Stores new data in the node, replacing the old data.
*/
template void node::data(dataType newData)
{
container = newData;
}
node.hpp
#ifndef node_hpp
#define node_hpp
namespace std {
template
class node {
template friend class simpleList;
private:
node *prevPtr;
node *nextPtr;
dataType container;
public:
node(); // Create a new, blank, node.
node(node *prv, node *nxt); // Create a new node with pointers.
~node(); // Destroy the node.
node *next(); // Returns the next object pointer
node *prev(); // Returns the previous object pointer
dataType data(); // Returns the nodes data.
void data(dataType); // Stores data in the node.
};
}
#endif /* node_hpp */
data.txt
The first line
The second line
And another line
One more line
what do you try to type here?
I like sugar cookies
This line is boring......
This is the last line
Solution
#include
#include
#include
#include
#include "textFile.hpp"
using namespace std;
void displayHelp();
bool isInteger(const string &s);
bool askSave();
int main(int argc, const char * argv[]) {
int resultCode = 0;
// Convert to a vector.
vector args(argv, argv + argc);
// Process arguments, must be 1 argument, otherwise exit with code 1.
if (args.size() < 2) {
cout << "Missing command line argument!" << endl;
cout << "Usage: ./myEditor " << endl;
resultCode = 1;
} else {
string fileName = args[1];
textFile dataFile(fileName);
// Check if the file is ok to use, if it is, lets go.
if (!dataFile.checkFile()) {
resultCode = 2;
} else {
// We can now assume the file is good, so we can work with it.
dataFile.loadFile();
// Display some text to help the user.
displayHelp();
// Now get user input.
bool done = false;
string userInput = "";
while (!done) {
cout << "> ";
getline(cin, userInput);
char cmd = tolower(userInput.c_str()[0]);
char cmd2 = ' ';
if (userInput.size() > 1) {
cmd2 = userInput.c_str()[1];
}
// This check makes sure the command format is correct
// so that commands don't fire off by mistake.
if (strncmp(&cmd, " ", 1) != 0 && strncmp(&cmd2, " ", 1) == 0) {
switch(cmd) {
case 'q': {
done = true;
break;
}
case 'i': {
unsigned long lineIndex = dataFile.textLines() + 1;
string newLine;
if (userInput.size() > 1) {
string cmdArg = userInput.substr(2);
lineIndex = stol(cmdArg);
}
cout << to_string(lineIndex) << "> ";
getline(cin, newLine);
dataFile.insertLine(lineIndex - 1, newLine);
break;
}
case 'd': {
if (userInput.size() > 1) {
unsigned long lineIndex;
string cmdArg = userInput.substr(2);
lineIndex = stol(cmdArg);
dataFile.removeLine(lineIndex - 1);
}
break;
}
case 'l': {
for (int x = 0; x < dataFile.textLines(); x++) {
cout << to_string(x + 1) << "> " << dataFile.getLine(x) << endl;
}
break;
}
case 'h': {
displayHelp();
break;
}
case 's': {
dataFile.saveFile();
break;
}
}
}
}
// Ask about saving the file before exiting.
bool save = askSave();
if (save) {
dataFile.saveFile();
}
cout << "Thank you for using myEditor." << endl;
}
}
return resultCode;
}
/*
Asks the user if they'd like to save or not, returns true if they do. Anything
other than "y" or "Y" will be interpreted as a negative answer.
*/
bool askSave()
{
bool result = false;
string userInput = "";
cout << "Do you wan to save the change? (Y - yes; N - no) ";
getline(cin, userInput);
userInput = tolower(*userInput.c_str());
if (strncmp(userInput.c_str(), "y", 1) == 0) {
result = true;
}
return result;
}
/*
Displays help text.
*/
void displayHelp()
{
cout << "Welcome to my text editor." << endl;
cout << "tTo insert text at the end of the file, type 'I'" << endl;
cout << "followed by the text." << endl;
cout << "tTo insert text at a certain line number, type 'I'" << endl;
cout << "followed by a space and the desired line number." << endl;
cout << "tTo delete a line, type 'D' followed by a space and the" << endl;
cout << "line number." << endl;
cout << "tTo print all the lines, type 'L' and press enter." << endl;
cout << "tTo save the current content, type 'S' and press enter." << endl;
cout << "tTo display this introduction, type 'H' and press" << endl;
cout << "enter." << endl;
cout << "tTo quit, type 'Q' and press enter." << endl;
}
/*
Checks to see if a stirng is an integer, returns true if the string is an
integer, returns false it it is not.
*/
bool isInteger(const string &s)
{
char *p;
strtol(s.c_str(), &p, 10);
return (*p == 0);
}
textFile.hpp
#ifndef textFile_hpp
#define textFile_hpp
#include
#include
#include "simpleList.hpp"
using namespace std;
class textFile {
private:
simpleList lines; // Stores all lines of the file.
string fileName; // The name of the file being accessed.
ifstream inputHandle; // File object for reading data from the file.
ofstream outputHandle; // File object for writing data to the file.
public:
textFile(); // Default Constructor
textFile(string file); // Constructor
~textFile(); // Destructor
unsigned long textLines(); // Size of the text file in # of lines.
string getLine(unsigned long lineNumber); // One line of text from the file.
void setLine(unsigned long lineNumber, string data); // Sets a specific line to a new value.
void insertLine(unsigned long lineNumber, string data); // Inserts a line into the file.
void removeLine(unsigned long lineNumber); // Removes a line from the file.
bool checkFile(); // Checks to see if a file is good.
void loadFile(); // Loads all the data into the object of the text file in question.
void saveFile(); // Saves all the data in the object back to its file.
void setFile(string file); // Allows setting of the file name after object creation.
string getFile(); // Returns the name of the file being worked with.
};
#endif /* textFile_hpp */
textFile.cpp
#include
#include "textFile.hpp"
#include "simpleList.hpp"
using namespace std;
/*
Default constructor for textFile class.
*/
textFile::textFile()
{
fileName = "";
}
/*
Constructor that accepts the file name in the object creation.
*/
textFile::textFile(string file)
{
fileName = file;
}
/*
Destructor function, no action required at this tiem.
*/
textFile::~textFile()
{
// Nothing for here.
}
unsigned long textFile::textLines()
{
return lines.size();
}
string textFile::getLine(unsigned long lineNumber)
{
// Check to make sure we're not going to go out of bounds.
if (lineNumber < lines.size()) {
lines.toHead();
lines.forward(lineNumber);
return lines.current()->data();
} else {
// If the index of lines to return is out of bounds, return an empty
// string.
string noData = "";
return noData;
}
}
void textFile::setLine(unsigned long lineNumber, string data)
{
if (lines.size() > lineNumber) {
lines.toHead();
lines.forward(lineNumber);
lines.current()->data(data);
} else {
lines.toTail();
lines.current()->data(data);
}
}
/*
Inserts a new line into the lines vector with data.
*/
void textFile::insertLine(unsigned long lineNumber, string data)
{
if (lines.size() > lineNumber) {
lines.toHead();
lines.forward(lineNumber);
lines.insert(data);
} else {
// This will add enough lines to get to lineNumber x even if there are
// not enough lines.
unsigned long blanks = lineNumber - lines.size();
if (blanks > 0) {
string blankString = "";
for (unsigned int x; x < blanks; x++) {
lines.append(blankString);
}
}
lines.append(data);
}
}
/*
Removes a line from the lines vector.
*/
void textFile::removeLine(unsigned long lineNumber)
{
lines.toHead();
lines.forward(lineNumber);
lines.remove();
}
/*
Checks to see if the file is good to read/write from.
*/
bool textFile::checkFile()
{
// Open the file as if we want to read it.
inputHandle.open(fileName);
// Check to see if the file access is good.
bool result = inputHandle.good();
// Close the file.
inputHandle.close();
return result;
}
void textFile::loadFile()
{
// Make sure we start with a blank slate, no items in the vector.
if (lines.size() > 0)
{
lines.clear();
}
// Open up the file for reading.
string line;
inputHandle.open(fileName.c_str());
// Read every line until end of file.
while (!inputHandle.eof()) {
getline(inputHandle, line);
lines.append(line);
}
if (line == "") {
lines.toTail();
lines.remove();
}
// Make sure to close the file again.
inputHandle.close();
}
/*
Saves all lines in the lines vector to the file.
*/
void textFile::saveFile()
{
// Open the file for writing, this should erase an exiting file.
outputHandle.open(fileName.c_str());
// Write every line from the lines vector into the file, appending
// a new line to the end of each.
lines.toHead();
while (lines.current()->next() == nullptr) {
outputHandle << lines.current()->data().c_str() << endl;
lines.forward(1);
}
// Make sure to close the file.
outputHandle.close();
}
void textFile::setFile(string file)
{
fileName = file;
}
/*
Returns the name of the file being worked with.
*/
string textFile::getFile()
{
return fileName;
}
simpleList.cpp
#include
#include "node.hpp"
#include "simpleList.hpp"
using namespace std;
template simpleList::simpleList()
{
head = nullptr;
tail = nullptr;
currentPtr = nullptr;
listSize = 0;
}
template simpleList::~simpleList()
{
clear();
}
// Appends data to the end of the list.
template void simpleList::append(dataType newData)
{
node *newNode = new node;
newNode->container = newData;
if (newNode == nullptr) {
cerr << "Error allocating memory, append failed." << endl;
} else {
if (head == nullptr && tail == nullptr) {
head = newNode;
tail = newNode;
} else {
tail->nextPtr = newNode;
newNode->prevPtr = tail;
tail = newNode;
}
listSize++;
}
}
*/
template void simpleList::insert(dataType newData)
{
if (currentPtr == nullptr) {
currentPtr = head;
}
node *newNode = new node;
newNode->container = newData;
if (newNode == nullptr) {
cerr << "Error allocating memory, insert failed." << endl;
} else if (currentPtr == head) {
head = newNode;
currentPtr->prevPtr = newNode;
newNode->nextPtr = currentPtr;
currentPtr = newNode;
listSize++;
} else {
currentPtr->prevPtr->nextPtr = newNode;
newNode->nextPtr = currentPtr;
newNode->prevPtr = currentPtr->prevPtr;
currentPtr->prevPtr = newNode;
currentPtr = newNode;
listSize++;
}
}
template void simpleList::forward(unsigned long distance)
{
for(int x = 0; x < distance; x++) {
if (currentPtr->nextPtr != nullptr) {
currentPtr = currentPtr->nextPtr;
} else {
break;
}
}
}
template void simpleList::backwward(unsigned long distance)
{
for(int x = 0; x < distance; x++) {
if (currentPtr->prevPtr != nullptr) {
currentPtr = currentPtr->prevPtr;
} else {
break;
}
}
}
// Reset the current pointer back to the beginning of the list.
template void simpleList::toHead()
{
currentPtr = head;
}
// Reset the current pointer to the end of the list.
template void simpleList::toTail()
{
currentPtr = tail;
}
template node *simpleList::find(dataType searchData)
{
node *searchPtr = head;
while (searchPtr->nextPtr != nullptr) {
if (searchData == searchPtr->container) {
currentPtr = searchPtr;
return currentPtr;
}
}
return nullptr;
}
template void simpleList::remove()
{
if (currentPtr == nullptr) {
currentPtr = tail;
}
node *toRemove = currentPtr;
if (currentPtr == tail && currentPtr == head) {
tail = nullptr;
head = nullptr;
currentPtr = nullptr;
} else if (currentPtr == tail) {
tail = currentPtr->prevPtr;
tail->nextPtr = nullptr;
currentPtr = tail;
} else if (currentPtr == head) {
head = currentPtr->nextPtr;
head->prevPtr = nullptr;
currentPtr = head;
} else {
currentPtr->prevPtr->nextPtr = currentPtr->nextPtr;
currentPtr->nextPtr->prevPtr = currentPtr->prevPtr;
currentPtr = currentPtr->prevPtr;
}
delete toRemove;
listSize--;
}
// Removes all items from memory.
template void simpleList::clear()
{
if (listSize > 0 && head != nullptr && tail != nullptr) {
currentPtr = head;
while (listSize > 0) {
remove();
}
}
}
// Give the current size of the list.
template unsigned long simpleList::size()
{
return listSize;
}
// Return the current pointer.
template node *simpleList::current()
{
return currentPtr;
}
simpleList.hpp
#ifndef simpleList_hpp
#define simpleList_hpp
#include "node.hpp"
namespace std {
using namespace std;
template
class simpleList {
private:
node *head;
node *tail;
node *currentPtr;
unsigned long listSize;
public:
simpleList();
~simpleList();
void append(dataType newData);
void insert(dataType newData);
void forward(unsigned long distance);
void backwward(unsigned long distance);
void toHead();
void toTail();
node *find(dataType searchData);
void remove();
void clear();
unsigned long size();
node *current();
};
}
#endif /* simpleList_hpp */
node.cpp
#include "node.hpp"
using namespace std;
template node::node()
{
prevPtr = nullptr;
nextPtr = nullptr;
}
template node::node(node *prv, node *nxt)
{
prevPtr = prv;
nextPtr = nxt;
}
template node *node::prev()
{
return prevPtr;
}
template node *node::next()
{
return nextPtr;
}
/*
Returns the data contents of the node.
*/
template dataType node::data()
{
return container;
}
/*
Stores new data in the node, replacing the old data.
*/
template void node::data(dataType newData)
{
container = newData;
}
node.hpp
#ifndef node_hpp
#define node_hpp
namespace std {
template
class node {
template friend class simpleList;
private:
node *prevPtr;
node *nextPtr;
dataType container;
public:
node(); // Create a new, blank, node.
node(node *prv, node *nxt); // Create a new node with pointers.
~node(); // Destroy the node.
node *next(); // Returns the next object pointer
node *prev(); // Returns the previous object pointer
dataType data(); // Returns the nodes data.
void data(dataType); // Stores data in the node.
};
}
#endif /* node_hpp */
data.txt
The first line
The second line
And another line
One more line
what do you try to type here?
I like sugar cookies
This line is boring......
This is the last line

More Related Content

Similar to #include iostream #include cstring #include vector #i.pdf

in c languageTo determine the maximum string length, we need to .pdf
in c languageTo determine the maximum string length, we need to .pdfin c languageTo determine the maximum string length, we need to .pdf
in c languageTo determine the maximum string length, we need to .pdfstopgolook
 
C++ Language -- Dynamic Memory -- There are 7 files in this project- a.pdf
C++ Language -- Dynamic Memory -- There are 7 files in this project- a.pdfC++ Language -- Dynamic Memory -- There are 7 files in this project- a.pdf
C++ Language -- Dynamic Memory -- There are 7 files in this project- a.pdfaassecuritysystem
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...ssuserd6b1fd
 
This is the main file include itemh include itemList.pdf
This is the main file include itemh include itemList.pdfThis is the main file include itemh include itemList.pdf
This is the main file include itemh include itemList.pdfinfo334223
 
Lecture 15_Strings and Dynamic Memory Allocation.pptx
Lecture 15_Strings and  Dynamic Memory Allocation.pptxLecture 15_Strings and  Dynamic Memory Allocation.pptx
Lecture 15_Strings and Dynamic Memory Allocation.pptxJawadTanvir
 
Lab01Filesbuild.bat@echo offclsset DRIVE_LETTER=1.docx
Lab01Filesbuild.bat@echo offclsset DRIVE_LETTER=1.docxLab01Filesbuild.bat@echo offclsset DRIVE_LETTER=1.docx
Lab01Filesbuild.bat@echo offclsset DRIVE_LETTER=1.docxDIPESH30
 
Programming For Big Data [ Submission DvcScheduleV2.cpp and StaticA.pdf
Programming For Big Data [ Submission DvcScheduleV2.cpp and StaticA.pdfProgramming For Big Data [ Submission DvcScheduleV2.cpp and StaticA.pdf
Programming For Big Data [ Submission DvcScheduleV2.cpp and StaticA.pdfssuser6254411
 
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdfModify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdfarjuncorner565
 
Start with the inclusion of libraries#include iostream .docx
 Start with the inclusion of libraries#include iostream .docx Start with the inclusion of libraries#include iostream .docx
Start with the inclusion of libraries#include iostream .docxMARRY7
 
Rewrite this code so it can use a generic type instead of integers. .pdf
Rewrite this code so it can use a generic type instead of integers. .pdfRewrite this code so it can use a generic type instead of integers. .pdf
Rewrite this code so it can use a generic type instead of integers. .pdfalphaagenciesindia
 
IO redirection in C shellPlease implement input output redirect.pdf
IO redirection in C shellPlease implement input  output redirect.pdfIO redirection in C shellPlease implement input  output redirect.pdf
IO redirection in C shellPlease implement input output redirect.pdfforecastfashions
 
Application-Specific Models and Pointcuts using a Logic Meta Language
Application-Specific Models and Pointcuts using a Logic Meta LanguageApplication-Specific Models and Pointcuts using a Logic Meta Language
Application-Specific Models and Pointcuts using a Logic Meta LanguageESUG
 
Getting the following errorsError 1 error C2436 {ctor} mem.pdf
Getting the following errorsError 1 error C2436 {ctor}  mem.pdfGetting the following errorsError 1 error C2436 {ctor}  mem.pdf
Getting the following errorsError 1 error C2436 {ctor} mem.pdfherminaherman
 
maincpp Build and procees a sorted linked list of Patie.pdf
maincpp   Build and procees a sorted linked list of Patie.pdfmaincpp   Build and procees a sorted linked list of Patie.pdf
maincpp Build and procees a sorted linked list of Patie.pdfadityastores21
 
IN C LANGUAGE- I've been trying to finish this program for the last fe.docx
IN C LANGUAGE- I've been trying to finish this program for the last fe.docxIN C LANGUAGE- I've been trying to finish this program for the last fe.docx
IN C LANGUAGE- I've been trying to finish this program for the last fe.docxGordonpACKellyb
 
I have the following code and I need to know why I am receiving the .pdf
I have the following code and I need to know why I am receiving the .pdfI have the following code and I need to know why I am receiving the .pdf
I have the following code and I need to know why I am receiving the .pdfezzi552
 
In C pls -- Write your name here -- Write the compiler used- Visual st.docx
In C pls -- Write your name here -- Write the compiler used- Visual st.docxIn C pls -- Write your name here -- Write the compiler used- Visual st.docx
In C pls -- Write your name here -- Write the compiler used- Visual st.docxBlake0FxCampbelld
 

Similar to #include iostream #include cstring #include vector #i.pdf (20)

C Programming Project
C Programming ProjectC Programming Project
C Programming Project
 
Data structures
Data structuresData structures
Data structures
 
in c languageTo determine the maximum string length, we need to .pdf
in c languageTo determine the maximum string length, we need to .pdfin c languageTo determine the maximum string length, we need to .pdf
in c languageTo determine the maximum string length, we need to .pdf
 
C++ Language -- Dynamic Memory -- There are 7 files in this project- a.pdf
C++ Language -- Dynamic Memory -- There are 7 files in this project- a.pdfC++ Language -- Dynamic Memory -- There are 7 files in this project- a.pdf
C++ Language -- Dynamic Memory -- There are 7 files in this project- a.pdf
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
 
This is the main file include itemh include itemList.pdf
This is the main file include itemh include itemList.pdfThis is the main file include itemh include itemList.pdf
This is the main file include itemh include itemList.pdf
 
Lecture 15_Strings and Dynamic Memory Allocation.pptx
Lecture 15_Strings and  Dynamic Memory Allocation.pptxLecture 15_Strings and  Dynamic Memory Allocation.pptx
Lecture 15_Strings and Dynamic Memory Allocation.pptx
 
Lab01Filesbuild.bat@echo offclsset DRIVE_LETTER=1.docx
Lab01Filesbuild.bat@echo offclsset DRIVE_LETTER=1.docxLab01Filesbuild.bat@echo offclsset DRIVE_LETTER=1.docx
Lab01Filesbuild.bat@echo offclsset DRIVE_LETTER=1.docx
 
Programming For Big Data [ Submission DvcScheduleV2.cpp and StaticA.pdf
Programming For Big Data [ Submission DvcScheduleV2.cpp and StaticA.pdfProgramming For Big Data [ Submission DvcScheduleV2.cpp and StaticA.pdf
Programming For Big Data [ Submission DvcScheduleV2.cpp and StaticA.pdf
 
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdfModify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
 
Start with the inclusion of libraries#include iostream .docx
 Start with the inclusion of libraries#include iostream .docx Start with the inclusion of libraries#include iostream .docx
Start with the inclusion of libraries#include iostream .docx
 
File Organization & processing Mid term summer 2014 - modelanswer
File Organization & processing Mid term summer 2014 - modelanswerFile Organization & processing Mid term summer 2014 - modelanswer
File Organization & processing Mid term summer 2014 - modelanswer
 
Rewrite this code so it can use a generic type instead of integers. .pdf
Rewrite this code so it can use a generic type instead of integers. .pdfRewrite this code so it can use a generic type instead of integers. .pdf
Rewrite this code so it can use a generic type instead of integers. .pdf
 
IO redirection in C shellPlease implement input output redirect.pdf
IO redirection in C shellPlease implement input  output redirect.pdfIO redirection in C shellPlease implement input  output redirect.pdf
IO redirection in C shellPlease implement input output redirect.pdf
 
Application-Specific Models and Pointcuts using a Logic Meta Language
Application-Specific Models and Pointcuts using a Logic Meta LanguageApplication-Specific Models and Pointcuts using a Logic Meta Language
Application-Specific Models and Pointcuts using a Logic Meta Language
 
Getting the following errorsError 1 error C2436 {ctor} mem.pdf
Getting the following errorsError 1 error C2436 {ctor}  mem.pdfGetting the following errorsError 1 error C2436 {ctor}  mem.pdf
Getting the following errorsError 1 error C2436 {ctor} mem.pdf
 
maincpp Build and procees a sorted linked list of Patie.pdf
maincpp   Build and procees a sorted linked list of Patie.pdfmaincpp   Build and procees a sorted linked list of Patie.pdf
maincpp Build and procees a sorted linked list of Patie.pdf
 
IN C LANGUAGE- I've been trying to finish this program for the last fe.docx
IN C LANGUAGE- I've been trying to finish this program for the last fe.docxIN C LANGUAGE- I've been trying to finish this program for the last fe.docx
IN C LANGUAGE- I've been trying to finish this program for the last fe.docx
 
I have the following code and I need to know why I am receiving the .pdf
I have the following code and I need to know why I am receiving the .pdfI have the following code and I need to know why I am receiving the .pdf
I have the following code and I need to know why I am receiving the .pdf
 
In C pls -- Write your name here -- Write the compiler used- Visual st.docx
In C pls -- Write your name here -- Write the compiler used- Visual st.docxIn C pls -- Write your name here -- Write the compiler used- Visual st.docx
In C pls -- Write your name here -- Write the compiler used- Visual st.docx
 

More from anandatalapatra

P(helium) + p(oxygen) = P(total) = P(helium) = P.pdf
                     P(helium) + p(oxygen) = P(total) = P(helium) = P.pdf                     P(helium) + p(oxygen) = P(total) = P(helium) = P.pdf
P(helium) + p(oxygen) = P(total) = P(helium) = P.pdfanandatalapatra
 
NaCl it has two atoms in a molecule for 1 mole of.pdf
                     NaCl it has two atoms in a molecule for 1 mole of.pdf                     NaCl it has two atoms in a molecule for 1 mole of.pdf
NaCl it has two atoms in a molecule for 1 mole of.pdfanandatalapatra
 
just find the moles and then use PV = nRT to fin.pdf
                     just find the moles and then use PV = nRT to fin.pdf                     just find the moles and then use PV = nRT to fin.pdf
just find the moles and then use PV = nRT to fin.pdfanandatalapatra
 
Water might be the most possible item through which bacteria entered.pdf
Water might be the most possible item through which bacteria entered.pdfWater might be the most possible item through which bacteria entered.pdf
Water might be the most possible item through which bacteria entered.pdfanandatalapatra
 
Total particles = 1000Number of particles entering the small apert.pdf
Total particles = 1000Number of particles entering the small apert.pdfTotal particles = 1000Number of particles entering the small apert.pdf
Total particles = 1000Number of particles entering the small apert.pdfanandatalapatra
 
These both are inflammatory bowel diseases characterized by chronic .pdf
These both are inflammatory bowel diseases characterized by chronic .pdfThese both are inflammatory bowel diseases characterized by chronic .pdf
These both are inflammatory bowel diseases characterized by chronic .pdfanandatalapatra
 
The topological structure is mainly gives the representaion of the n.pdf
The topological structure is mainly gives the representaion of the n.pdfThe topological structure is mainly gives the representaion of the n.pdf
The topological structure is mainly gives the representaion of the n.pdfanandatalapatra
 
The main components of protein family members of RISC complex are.pdf
The main components of protein family members of RISC complex are.pdfThe main components of protein family members of RISC complex are.pdf
The main components of protein family members of RISC complex are.pdfanandatalapatra
 
Solution It is for the auditors and other people involved in the .pdf
Solution It is for the auditors and other people involved in the .pdfSolution It is for the auditors and other people involved in the .pdf
Solution It is for the auditors and other people involved in the .pdfanandatalapatra
 
Ques-1 Answer C. neutrophilsReasonInnate immune system is wit.pdf
Ques-1 Answer C. neutrophilsReasonInnate immune system is wit.pdfQues-1 Answer C. neutrophilsReasonInnate immune system is wit.pdf
Ques-1 Answer C. neutrophilsReasonInnate immune system is wit.pdfanandatalapatra
 
relation between concentration and cell potential is given as Ecel.pdf
relation between concentration and cell potential is given as Ecel.pdfrelation between concentration and cell potential is given as Ecel.pdf
relation between concentration and cell potential is given as Ecel.pdfanandatalapatra
 
Part I Identifying Your Customer’s Needs and Goals Chapter 1 A.pdf
Part I Identifying Your Customer’s Needs and Goals Chapter 1 A.pdfPart I Identifying Your Customer’s Needs and Goals Chapter 1 A.pdf
Part I Identifying Your Customer’s Needs and Goals Chapter 1 A.pdfanandatalapatra
 
PrintTest.java import java.util.Scanner;public class PrintTest.pdf
PrintTest.java import java.util.Scanner;public class PrintTest.pdfPrintTest.java import java.util.Scanner;public class PrintTest.pdf
PrintTest.java import java.util.Scanner;public class PrintTest.pdfanandatalapatra
 
Existence of hydrogen bonding increases boiling p.pdf
                     Existence of hydrogen bonding increases boiling p.pdf                     Existence of hydrogen bonding increases boiling p.pdf
Existence of hydrogen bonding increases boiling p.pdfanandatalapatra
 
JellyBean.javapackage jellybeantester;public class JellyBean {.pdf
JellyBean.javapackage jellybeantester;public class JellyBean {.pdfJellyBean.javapackage jellybeantester;public class JellyBean {.pdf
JellyBean.javapackage jellybeantester;public class JellyBean {.pdfanandatalapatra
 
In present DNA microarray technology, the different test-samples are.pdf
In present DNA microarray technology, the different test-samples are.pdfIn present DNA microarray technology, the different test-samples are.pdf
In present DNA microarray technology, the different test-samples are.pdfanandatalapatra
 
In computer graphics vectors perform the operations are...1.Rotati.pdf
In computer graphics vectors perform the operations are...1.Rotati.pdfIn computer graphics vectors perform the operations are...1.Rotati.pdf
In computer graphics vectors perform the operations are...1.Rotati.pdfanandatalapatra
 
copper iodide are incompatible in oxidising media.pdf
                     copper iodide are incompatible in oxidising media.pdf                     copper iodide are incompatible in oxidising media.pdf
copper iodide are incompatible in oxidising media.pdfanandatalapatra
 
Dirac theoremFor a simple network with n 3 vertices, if each ver.pdf
Dirac theoremFor a simple network with n  3 vertices, if each ver.pdfDirac theoremFor a simple network with n  3 vertices, if each ver.pdf
Dirac theoremFor a simple network with n 3 vertices, if each ver.pdfanandatalapatra
 
Construction Supervisor ResponsibilitiesCompletes construction pr.pdf
Construction Supervisor ResponsibilitiesCompletes construction pr.pdfConstruction Supervisor ResponsibilitiesCompletes construction pr.pdf
Construction Supervisor ResponsibilitiesCompletes construction pr.pdfanandatalapatra
 

More from anandatalapatra (20)

P(helium) + p(oxygen) = P(total) = P(helium) = P.pdf
                     P(helium) + p(oxygen) = P(total) = P(helium) = P.pdf                     P(helium) + p(oxygen) = P(total) = P(helium) = P.pdf
P(helium) + p(oxygen) = P(total) = P(helium) = P.pdf
 
NaCl it has two atoms in a molecule for 1 mole of.pdf
                     NaCl it has two atoms in a molecule for 1 mole of.pdf                     NaCl it has two atoms in a molecule for 1 mole of.pdf
NaCl it has two atoms in a molecule for 1 mole of.pdf
 
just find the moles and then use PV = nRT to fin.pdf
                     just find the moles and then use PV = nRT to fin.pdf                     just find the moles and then use PV = nRT to fin.pdf
just find the moles and then use PV = nRT to fin.pdf
 
Water might be the most possible item through which bacteria entered.pdf
Water might be the most possible item through which bacteria entered.pdfWater might be the most possible item through which bacteria entered.pdf
Water might be the most possible item through which bacteria entered.pdf
 
Total particles = 1000Number of particles entering the small apert.pdf
Total particles = 1000Number of particles entering the small apert.pdfTotal particles = 1000Number of particles entering the small apert.pdf
Total particles = 1000Number of particles entering the small apert.pdf
 
These both are inflammatory bowel diseases characterized by chronic .pdf
These both are inflammatory bowel diseases characterized by chronic .pdfThese both are inflammatory bowel diseases characterized by chronic .pdf
These both are inflammatory bowel diseases characterized by chronic .pdf
 
The topological structure is mainly gives the representaion of the n.pdf
The topological structure is mainly gives the representaion of the n.pdfThe topological structure is mainly gives the representaion of the n.pdf
The topological structure is mainly gives the representaion of the n.pdf
 
The main components of protein family members of RISC complex are.pdf
The main components of protein family members of RISC complex are.pdfThe main components of protein family members of RISC complex are.pdf
The main components of protein family members of RISC complex are.pdf
 
Solution It is for the auditors and other people involved in the .pdf
Solution It is for the auditors and other people involved in the .pdfSolution It is for the auditors and other people involved in the .pdf
Solution It is for the auditors and other people involved in the .pdf
 
Ques-1 Answer C. neutrophilsReasonInnate immune system is wit.pdf
Ques-1 Answer C. neutrophilsReasonInnate immune system is wit.pdfQues-1 Answer C. neutrophilsReasonInnate immune system is wit.pdf
Ques-1 Answer C. neutrophilsReasonInnate immune system is wit.pdf
 
relation between concentration and cell potential is given as Ecel.pdf
relation between concentration and cell potential is given as Ecel.pdfrelation between concentration and cell potential is given as Ecel.pdf
relation between concentration and cell potential is given as Ecel.pdf
 
Part I Identifying Your Customer’s Needs and Goals Chapter 1 A.pdf
Part I Identifying Your Customer’s Needs and Goals Chapter 1 A.pdfPart I Identifying Your Customer’s Needs and Goals Chapter 1 A.pdf
Part I Identifying Your Customer’s Needs and Goals Chapter 1 A.pdf
 
PrintTest.java import java.util.Scanner;public class PrintTest.pdf
PrintTest.java import java.util.Scanner;public class PrintTest.pdfPrintTest.java import java.util.Scanner;public class PrintTest.pdf
PrintTest.java import java.util.Scanner;public class PrintTest.pdf
 
Existence of hydrogen bonding increases boiling p.pdf
                     Existence of hydrogen bonding increases boiling p.pdf                     Existence of hydrogen bonding increases boiling p.pdf
Existence of hydrogen bonding increases boiling p.pdf
 
JellyBean.javapackage jellybeantester;public class JellyBean {.pdf
JellyBean.javapackage jellybeantester;public class JellyBean {.pdfJellyBean.javapackage jellybeantester;public class JellyBean {.pdf
JellyBean.javapackage jellybeantester;public class JellyBean {.pdf
 
In present DNA microarray technology, the different test-samples are.pdf
In present DNA microarray technology, the different test-samples are.pdfIn present DNA microarray technology, the different test-samples are.pdf
In present DNA microarray technology, the different test-samples are.pdf
 
In computer graphics vectors perform the operations are...1.Rotati.pdf
In computer graphics vectors perform the operations are...1.Rotati.pdfIn computer graphics vectors perform the operations are...1.Rotati.pdf
In computer graphics vectors perform the operations are...1.Rotati.pdf
 
copper iodide are incompatible in oxidising media.pdf
                     copper iodide are incompatible in oxidising media.pdf                     copper iodide are incompatible in oxidising media.pdf
copper iodide are incompatible in oxidising media.pdf
 
Dirac theoremFor a simple network with n 3 vertices, if each ver.pdf
Dirac theoremFor a simple network with n  3 vertices, if each ver.pdfDirac theoremFor a simple network with n  3 vertices, if each ver.pdf
Dirac theoremFor a simple network with n 3 vertices, if each ver.pdf
 
Construction Supervisor ResponsibilitiesCompletes construction pr.pdf
Construction Supervisor ResponsibilitiesCompletes construction pr.pdfConstruction Supervisor ResponsibilitiesCompletes construction pr.pdf
Construction Supervisor ResponsibilitiesCompletes construction pr.pdf
 

Recently uploaded

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseAnaAcapella
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 

Recently uploaded (20)

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 

#include iostream #include cstring #include vector #i.pdf

  • 1. #include #include #include #include #include "textFile.hpp" using namespace std; void displayHelp(); bool isInteger(const string &s); bool askSave(); int main(int argc, const char * argv[]) { int resultCode = 0; // Convert to a vector. vector args(argv, argv + argc); // Process arguments, must be 1 argument, otherwise exit with code 1. if (args.size() < 2) { cout << "Missing command line argument!" << endl; cout << "Usage: ./myEditor " << endl; resultCode = 1; } else { string fileName = args[1]; textFile dataFile(fileName); // Check if the file is ok to use, if it is, lets go. if (!dataFile.checkFile()) { resultCode = 2; } else { // We can now assume the file is good, so we can work with it. dataFile.loadFile(); // Display some text to help the user. displayHelp();
  • 2. // Now get user input. bool done = false; string userInput = ""; while (!done) { cout << "> "; getline(cin, userInput); char cmd = tolower(userInput.c_str()[0]); char cmd2 = ' '; if (userInput.size() > 1) { cmd2 = userInput.c_str()[1]; } // This check makes sure the command format is correct // so that commands don't fire off by mistake. if (strncmp(&cmd, " ", 1) != 0 && strncmp(&cmd2, " ", 1) == 0) { switch(cmd) { case 'q': { done = true; break; } case 'i': { unsigned long lineIndex = dataFile.textLines() + 1; string newLine; if (userInput.size() > 1) { string cmdArg = userInput.substr(2); lineIndex = stol(cmdArg); } cout << to_string(lineIndex) << "> "; getline(cin, newLine); dataFile.insertLine(lineIndex - 1, newLine); break; } case 'd': { if (userInput.size() > 1) {
  • 3. unsigned long lineIndex; string cmdArg = userInput.substr(2); lineIndex = stol(cmdArg); dataFile.removeLine(lineIndex - 1); } break; } case 'l': { for (int x = 0; x < dataFile.textLines(); x++) { cout << to_string(x + 1) << "> " << dataFile.getLine(x) << endl; } break; } case 'h': { displayHelp(); break; } case 's': { dataFile.saveFile(); break; } } } } // Ask about saving the file before exiting. bool save = askSave(); if (save) { dataFile.saveFile(); } cout << "Thank you for using myEditor." << endl; } } return resultCode; }
  • 4. /* Asks the user if they'd like to save or not, returns true if they do. Anything other than "y" or "Y" will be interpreted as a negative answer. */ bool askSave() { bool result = false; string userInput = ""; cout << "Do you wan to save the change? (Y - yes; N - no) "; getline(cin, userInput); userInput = tolower(*userInput.c_str()); if (strncmp(userInput.c_str(), "y", 1) == 0) { result = true; } return result; } /* Displays help text. */ void displayHelp() { cout << "Welcome to my text editor." << endl; cout << "tTo insert text at the end of the file, type 'I'" << endl; cout << "followed by the text." << endl; cout << "tTo insert text at a certain line number, type 'I'" << endl; cout << "followed by a space and the desired line number." << endl; cout << "tTo delete a line, type 'D' followed by a space and the" << endl; cout << "line number." << endl; cout << "tTo print all the lines, type 'L' and press enter." << endl; cout << "tTo save the current content, type 'S' and press enter." << endl; cout << "tTo display this introduction, type 'H' and press" << endl; cout << "enter." << endl; cout << "tTo quit, type 'Q' and press enter." << endl; } /* Checks to see if a stirng is an integer, returns true if the string is an
  • 5. integer, returns false it it is not. */ bool isInteger(const string &s) { char *p; strtol(s.c_str(), &p, 10); return (*p == 0); } textFile.hpp #ifndef textFile_hpp #define textFile_hpp #include #include #include "simpleList.hpp" using namespace std; class textFile { private: simpleList lines; // Stores all lines of the file. string fileName; // The name of the file being accessed. ifstream inputHandle; // File object for reading data from the file. ofstream outputHandle; // File object for writing data to the file. public: textFile(); // Default Constructor textFile(string file); // Constructor ~textFile(); // Destructor unsigned long textLines(); // Size of the text file in # of lines. string getLine(unsigned long lineNumber); // One line of text from the file. void setLine(unsigned long lineNumber, string data); // Sets a specific line to a new value. void insertLine(unsigned long lineNumber, string data); // Inserts a line into the file. void removeLine(unsigned long lineNumber); // Removes a line from the file. bool checkFile(); // Checks to see if a file is good. void loadFile(); // Loads all the data into the object of the text file in question. void saveFile(); // Saves all the data in the object back to its file. void setFile(string file); // Allows setting of the file name after object creation. string getFile(); // Returns the name of the file being worked with. };
  • 6. #endif /* textFile_hpp */ textFile.cpp #include #include "textFile.hpp" #include "simpleList.hpp" using namespace std; /* Default constructor for textFile class. */ textFile::textFile() { fileName = ""; } /* Constructor that accepts the file name in the object creation. */ textFile::textFile(string file) { fileName = file; } /* Destructor function, no action required at this tiem. */ textFile::~textFile() { // Nothing for here. } unsigned long textFile::textLines() { return lines.size(); } string textFile::getLine(unsigned long lineNumber) { // Check to make sure we're not going to go out of bounds. if (lineNumber < lines.size()) {
  • 7. lines.toHead(); lines.forward(lineNumber); return lines.current()->data(); } else { // If the index of lines to return is out of bounds, return an empty // string. string noData = ""; return noData; } } void textFile::setLine(unsigned long lineNumber, string data) { if (lines.size() > lineNumber) { lines.toHead(); lines.forward(lineNumber); lines.current()->data(data); } else { lines.toTail(); lines.current()->data(data); } } /* Inserts a new line into the lines vector with data. */ void textFile::insertLine(unsigned long lineNumber, string data) { if (lines.size() > lineNumber) { lines.toHead(); lines.forward(lineNumber); lines.insert(data); } else { // This will add enough lines to get to lineNumber x even if there are // not enough lines. unsigned long blanks = lineNumber - lines.size(); if (blanks > 0) { string blankString = "";
  • 8. for (unsigned int x; x < blanks; x++) { lines.append(blankString); } } lines.append(data); } } /* Removes a line from the lines vector. */ void textFile::removeLine(unsigned long lineNumber) { lines.toHead(); lines.forward(lineNumber); lines.remove(); } /* Checks to see if the file is good to read/write from. */ bool textFile::checkFile() { // Open the file as if we want to read it. inputHandle.open(fileName); // Check to see if the file access is good. bool result = inputHandle.good(); // Close the file. inputHandle.close(); return result; } void textFile::loadFile() { // Make sure we start with a blank slate, no items in the vector. if (lines.size() > 0) { lines.clear(); }
  • 9. // Open up the file for reading. string line; inputHandle.open(fileName.c_str()); // Read every line until end of file. while (!inputHandle.eof()) { getline(inputHandle, line); lines.append(line); } if (line == "") { lines.toTail(); lines.remove(); } // Make sure to close the file again. inputHandle.close(); } /* Saves all lines in the lines vector to the file. */ void textFile::saveFile() { // Open the file for writing, this should erase an exiting file. outputHandle.open(fileName.c_str()); // Write every line from the lines vector into the file, appending // a new line to the end of each. lines.toHead(); while (lines.current()->next() == nullptr) { outputHandle << lines.current()->data().c_str() << endl; lines.forward(1); } // Make sure to close the file. outputHandle.close(); } void textFile::setFile(string file) { fileName = file;
  • 10. } /* Returns the name of the file being worked with. */ string textFile::getFile() { return fileName; } simpleList.cpp #include #include "node.hpp" #include "simpleList.hpp" using namespace std; template simpleList::simpleList() { head = nullptr; tail = nullptr; currentPtr = nullptr; listSize = 0; } template simpleList::~simpleList() { clear(); } // Appends data to the end of the list. template void simpleList::append(dataType newData) { node *newNode = new node; newNode->container = newData; if (newNode == nullptr) { cerr << "Error allocating memory, append failed." << endl; } else { if (head == nullptr && tail == nullptr) { head = newNode;
  • 11. tail = newNode; } else { tail->nextPtr = newNode; newNode->prevPtr = tail; tail = newNode; } listSize++; } } */ template void simpleList::insert(dataType newData) { if (currentPtr == nullptr) { currentPtr = head; } node *newNode = new node; newNode->container = newData; if (newNode == nullptr) { cerr << "Error allocating memory, insert failed." << endl; } else if (currentPtr == head) { head = newNode; currentPtr->prevPtr = newNode; newNode->nextPtr = currentPtr; currentPtr = newNode; listSize++; } else { currentPtr->prevPtr->nextPtr = newNode; newNode->nextPtr = currentPtr; newNode->prevPtr = currentPtr->prevPtr; currentPtr->prevPtr = newNode; currentPtr = newNode; listSize++; } } template void simpleList::forward(unsigned long distance)
  • 12. { for(int x = 0; x < distance; x++) { if (currentPtr->nextPtr != nullptr) { currentPtr = currentPtr->nextPtr; } else { break; } } } template void simpleList::backwward(unsigned long distance) { for(int x = 0; x < distance; x++) { if (currentPtr->prevPtr != nullptr) { currentPtr = currentPtr->prevPtr; } else { break; } } } // Reset the current pointer back to the beginning of the list. template void simpleList::toHead() { currentPtr = head; } // Reset the current pointer to the end of the list. template void simpleList::toTail() { currentPtr = tail; } template node *simpleList::find(dataType searchData) { node *searchPtr = head; while (searchPtr->nextPtr != nullptr) { if (searchData == searchPtr->container) { currentPtr = searchPtr;
  • 13. return currentPtr; } } return nullptr; } template void simpleList::remove() { if (currentPtr == nullptr) { currentPtr = tail; } node *toRemove = currentPtr; if (currentPtr == tail && currentPtr == head) { tail = nullptr; head = nullptr; currentPtr = nullptr; } else if (currentPtr == tail) { tail = currentPtr->prevPtr; tail->nextPtr = nullptr; currentPtr = tail; } else if (currentPtr == head) { head = currentPtr->nextPtr; head->prevPtr = nullptr; currentPtr = head; } else { currentPtr->prevPtr->nextPtr = currentPtr->nextPtr; currentPtr->nextPtr->prevPtr = currentPtr->prevPtr; currentPtr = currentPtr->prevPtr; } delete toRemove; listSize--; } // Removes all items from memory.
  • 14. template void simpleList::clear() { if (listSize > 0 && head != nullptr && tail != nullptr) { currentPtr = head; while (listSize > 0) { remove(); } } } // Give the current size of the list. template unsigned long simpleList::size() { return listSize; } // Return the current pointer. template node *simpleList::current() { return currentPtr; } simpleList.hpp #ifndef simpleList_hpp #define simpleList_hpp #include "node.hpp" namespace std { using namespace std; template class simpleList { private: node *head; node *tail; node *currentPtr; unsigned long listSize; public:
  • 15. simpleList(); ~simpleList(); void append(dataType newData); void insert(dataType newData); void forward(unsigned long distance); void backwward(unsigned long distance); void toHead(); void toTail(); node *find(dataType searchData); void remove(); void clear(); unsigned long size(); node *current(); }; } #endif /* simpleList_hpp */ node.cpp #include "node.hpp" using namespace std; template node::node() { prevPtr = nullptr; nextPtr = nullptr; } template node::node(node *prv, node *nxt) { prevPtr = prv; nextPtr = nxt; } template node *node::prev() { return prevPtr; } template node *node::next()
  • 16. { return nextPtr; } /* Returns the data contents of the node. */ template dataType node::data() { return container; } /* Stores new data in the node, replacing the old data. */ template void node::data(dataType newData) { container = newData; } node.hpp #ifndef node_hpp #define node_hpp namespace std { template class node { template friend class simpleList; private: node *prevPtr; node *nextPtr; dataType container; public: node(); // Create a new, blank, node. node(node *prv, node *nxt); // Create a new node with pointers. ~node(); // Destroy the node. node *next(); // Returns the next object pointer node *prev(); // Returns the previous object pointer dataType data(); // Returns the nodes data.
  • 17. void data(dataType); // Stores data in the node. }; } #endif /* node_hpp */ data.txt The first line The second line And another line One more line what do you try to type here? I like sugar cookies This line is boring...... This is the last line Solution #include #include #include #include #include "textFile.hpp" using namespace std; void displayHelp(); bool isInteger(const string &s); bool askSave(); int main(int argc, const char * argv[]) { int resultCode = 0; // Convert to a vector. vector args(argv, argv + argc); // Process arguments, must be 1 argument, otherwise exit with code 1. if (args.size() < 2) { cout << "Missing command line argument!" << endl; cout << "Usage: ./myEditor " << endl;
  • 18. resultCode = 1; } else { string fileName = args[1]; textFile dataFile(fileName); // Check if the file is ok to use, if it is, lets go. if (!dataFile.checkFile()) { resultCode = 2; } else { // We can now assume the file is good, so we can work with it. dataFile.loadFile(); // Display some text to help the user. displayHelp(); // Now get user input. bool done = false; string userInput = ""; while (!done) { cout << "> "; getline(cin, userInput); char cmd = tolower(userInput.c_str()[0]); char cmd2 = ' '; if (userInput.size() > 1) { cmd2 = userInput.c_str()[1]; } // This check makes sure the command format is correct // so that commands don't fire off by mistake. if (strncmp(&cmd, " ", 1) != 0 && strncmp(&cmd2, " ", 1) == 0) { switch(cmd) { case 'q': { done = true; break;
  • 19. } case 'i': { unsigned long lineIndex = dataFile.textLines() + 1; string newLine; if (userInput.size() > 1) { string cmdArg = userInput.substr(2); lineIndex = stol(cmdArg); } cout << to_string(lineIndex) << "> "; getline(cin, newLine); dataFile.insertLine(lineIndex - 1, newLine); break; } case 'd': { if (userInput.size() > 1) { unsigned long lineIndex; string cmdArg = userInput.substr(2); lineIndex = stol(cmdArg); dataFile.removeLine(lineIndex - 1); } break; } case 'l': { for (int x = 0; x < dataFile.textLines(); x++) { cout << to_string(x + 1) << "> " << dataFile.getLine(x) << endl; } break; } case 'h': { displayHelp(); break; } case 's': { dataFile.saveFile(); break; }
  • 20. } } } // Ask about saving the file before exiting. bool save = askSave(); if (save) { dataFile.saveFile(); } cout << "Thank you for using myEditor." << endl; } } return resultCode; } /* Asks the user if they'd like to save or not, returns true if they do. Anything other than "y" or "Y" will be interpreted as a negative answer. */ bool askSave() { bool result = false; string userInput = ""; cout << "Do you wan to save the change? (Y - yes; N - no) "; getline(cin, userInput); userInput = tolower(*userInput.c_str()); if (strncmp(userInput.c_str(), "y", 1) == 0) { result = true; } return result; } /* Displays help text. */ void displayHelp() {
  • 21. cout << "Welcome to my text editor." << endl; cout << "tTo insert text at the end of the file, type 'I'" << endl; cout << "followed by the text." << endl; cout << "tTo insert text at a certain line number, type 'I'" << endl; cout << "followed by a space and the desired line number." << endl; cout << "tTo delete a line, type 'D' followed by a space and the" << endl; cout << "line number." << endl; cout << "tTo print all the lines, type 'L' and press enter." << endl; cout << "tTo save the current content, type 'S' and press enter." << endl; cout << "tTo display this introduction, type 'H' and press" << endl; cout << "enter." << endl; cout << "tTo quit, type 'Q' and press enter." << endl; } /* Checks to see if a stirng is an integer, returns true if the string is an integer, returns false it it is not. */ bool isInteger(const string &s) { char *p; strtol(s.c_str(), &p, 10); return (*p == 0); } textFile.hpp #ifndef textFile_hpp #define textFile_hpp #include #include #include "simpleList.hpp" using namespace std; class textFile { private: simpleList lines; // Stores all lines of the file. string fileName; // The name of the file being accessed. ifstream inputHandle; // File object for reading data from the file. ofstream outputHandle; // File object for writing data to the file.
  • 22. public: textFile(); // Default Constructor textFile(string file); // Constructor ~textFile(); // Destructor unsigned long textLines(); // Size of the text file in # of lines. string getLine(unsigned long lineNumber); // One line of text from the file. void setLine(unsigned long lineNumber, string data); // Sets a specific line to a new value. void insertLine(unsigned long lineNumber, string data); // Inserts a line into the file. void removeLine(unsigned long lineNumber); // Removes a line from the file. bool checkFile(); // Checks to see if a file is good. void loadFile(); // Loads all the data into the object of the text file in question. void saveFile(); // Saves all the data in the object back to its file. void setFile(string file); // Allows setting of the file name after object creation. string getFile(); // Returns the name of the file being worked with. }; #endif /* textFile_hpp */ textFile.cpp #include #include "textFile.hpp" #include "simpleList.hpp" using namespace std; /* Default constructor for textFile class. */ textFile::textFile() { fileName = ""; } /* Constructor that accepts the file name in the object creation. */ textFile::textFile(string file) { fileName = file; }
  • 23. /* Destructor function, no action required at this tiem. */ textFile::~textFile() { // Nothing for here. } unsigned long textFile::textLines() { return lines.size(); } string textFile::getLine(unsigned long lineNumber) { // Check to make sure we're not going to go out of bounds. if (lineNumber < lines.size()) { lines.toHead(); lines.forward(lineNumber); return lines.current()->data(); } else { // If the index of lines to return is out of bounds, return an empty // string. string noData = ""; return noData; } } void textFile::setLine(unsigned long lineNumber, string data) { if (lines.size() > lineNumber) { lines.toHead(); lines.forward(lineNumber); lines.current()->data(data); } else { lines.toTail(); lines.current()->data(data); } }
  • 24. /* Inserts a new line into the lines vector with data. */ void textFile::insertLine(unsigned long lineNumber, string data) { if (lines.size() > lineNumber) { lines.toHead(); lines.forward(lineNumber); lines.insert(data); } else { // This will add enough lines to get to lineNumber x even if there are // not enough lines. unsigned long blanks = lineNumber - lines.size(); if (blanks > 0) { string blankString = ""; for (unsigned int x; x < blanks; x++) { lines.append(blankString); } } lines.append(data); } } /* Removes a line from the lines vector. */ void textFile::removeLine(unsigned long lineNumber) { lines.toHead(); lines.forward(lineNumber); lines.remove(); } /* Checks to see if the file is good to read/write from. */ bool textFile::checkFile() {
  • 25. // Open the file as if we want to read it. inputHandle.open(fileName); // Check to see if the file access is good. bool result = inputHandle.good(); // Close the file. inputHandle.close(); return result; } void textFile::loadFile() { // Make sure we start with a blank slate, no items in the vector. if (lines.size() > 0) { lines.clear(); } // Open up the file for reading. string line; inputHandle.open(fileName.c_str()); // Read every line until end of file. while (!inputHandle.eof()) { getline(inputHandle, line); lines.append(line); } if (line == "") { lines.toTail(); lines.remove(); } // Make sure to close the file again. inputHandle.close(); } /* Saves all lines in the lines vector to the file. */ void textFile::saveFile() {
  • 26. // Open the file for writing, this should erase an exiting file. outputHandle.open(fileName.c_str()); // Write every line from the lines vector into the file, appending // a new line to the end of each. lines.toHead(); while (lines.current()->next() == nullptr) { outputHandle << lines.current()->data().c_str() << endl; lines.forward(1); } // Make sure to close the file. outputHandle.close(); } void textFile::setFile(string file) { fileName = file; } /* Returns the name of the file being worked with. */ string textFile::getFile() { return fileName; } simpleList.cpp #include #include "node.hpp" #include "simpleList.hpp" using namespace std; template simpleList::simpleList() { head = nullptr; tail = nullptr; currentPtr = nullptr; listSize = 0;
  • 27. } template simpleList::~simpleList() { clear(); } // Appends data to the end of the list. template void simpleList::append(dataType newData) { node *newNode = new node; newNode->container = newData; if (newNode == nullptr) { cerr << "Error allocating memory, append failed." << endl; } else { if (head == nullptr && tail == nullptr) { head = newNode; tail = newNode; } else { tail->nextPtr = newNode; newNode->prevPtr = tail; tail = newNode; } listSize++; } } */ template void simpleList::insert(dataType newData) { if (currentPtr == nullptr) { currentPtr = head; } node *newNode = new node; newNode->container = newData; if (newNode == nullptr) { cerr << "Error allocating memory, insert failed." << endl; } else if (currentPtr == head) {
  • 28. head = newNode; currentPtr->prevPtr = newNode; newNode->nextPtr = currentPtr; currentPtr = newNode; listSize++; } else { currentPtr->prevPtr->nextPtr = newNode; newNode->nextPtr = currentPtr; newNode->prevPtr = currentPtr->prevPtr; currentPtr->prevPtr = newNode; currentPtr = newNode; listSize++; } } template void simpleList::forward(unsigned long distance) { for(int x = 0; x < distance; x++) { if (currentPtr->nextPtr != nullptr) { currentPtr = currentPtr->nextPtr; } else { break; } } } template void simpleList::backwward(unsigned long distance) { for(int x = 0; x < distance; x++) { if (currentPtr->prevPtr != nullptr) { currentPtr = currentPtr->prevPtr; } else { break; } } } // Reset the current pointer back to the beginning of the list. template void simpleList::toHead()
  • 29. { currentPtr = head; } // Reset the current pointer to the end of the list. template void simpleList::toTail() { currentPtr = tail; } template node *simpleList::find(dataType searchData) { node *searchPtr = head; while (searchPtr->nextPtr != nullptr) { if (searchData == searchPtr->container) { currentPtr = searchPtr; return currentPtr; } } return nullptr; } template void simpleList::remove() { if (currentPtr == nullptr) { currentPtr = tail; } node *toRemove = currentPtr; if (currentPtr == tail && currentPtr == head) { tail = nullptr; head = nullptr; currentPtr = nullptr; } else if (currentPtr == tail) { tail = currentPtr->prevPtr; tail->nextPtr = nullptr;
  • 30. currentPtr = tail; } else if (currentPtr == head) { head = currentPtr->nextPtr; head->prevPtr = nullptr; currentPtr = head; } else { currentPtr->prevPtr->nextPtr = currentPtr->nextPtr; currentPtr->nextPtr->prevPtr = currentPtr->prevPtr; currentPtr = currentPtr->prevPtr; } delete toRemove; listSize--; } // Removes all items from memory. template void simpleList::clear() { if (listSize > 0 && head != nullptr && tail != nullptr) { currentPtr = head; while (listSize > 0) { remove(); } } } // Give the current size of the list. template unsigned long simpleList::size() { return listSize; } // Return the current pointer. template node *simpleList::current() { return currentPtr; }
  • 31. simpleList.hpp #ifndef simpleList_hpp #define simpleList_hpp #include "node.hpp" namespace std { using namespace std; template class simpleList { private: node *head; node *tail; node *currentPtr; unsigned long listSize; public: simpleList(); ~simpleList(); void append(dataType newData); void insert(dataType newData); void forward(unsigned long distance); void backwward(unsigned long distance); void toHead(); void toTail(); node *find(dataType searchData); void remove(); void clear(); unsigned long size(); node *current(); }; } #endif /* simpleList_hpp */ node.cpp #include "node.hpp" using namespace std;
  • 32. template node::node() { prevPtr = nullptr; nextPtr = nullptr; } template node::node(node *prv, node *nxt) { prevPtr = prv; nextPtr = nxt; } template node *node::prev() { return prevPtr; } template node *node::next() { return nextPtr; } /* Returns the data contents of the node. */ template dataType node::data() { return container; } /* Stores new data in the node, replacing the old data. */ template void node::data(dataType newData) { container = newData; } node.hpp #ifndef node_hpp #define node_hpp
  • 33. namespace std { template class node { template friend class simpleList; private: node *prevPtr; node *nextPtr; dataType container; public: node(); // Create a new, blank, node. node(node *prv, node *nxt); // Create a new node with pointers. ~node(); // Destroy the node. node *next(); // Returns the next object pointer node *prev(); // Returns the previous object pointer dataType data(); // Returns the nodes data. void data(dataType); // Stores data in the node. }; } #endif /* node_hpp */ data.txt The first line The second line And another line One more line what do you try to type here? I like sugar cookies This line is boring...... This is the last line