cs201/PG5/PG5-6.out

343 lines
9.4 KiB
Plaintext

Listing ArrayLL.h...
//Ben Harris
//Template for an linked list that can be used as a array.
#ifndef _ARRAYLL_
#define _ARRAYLL_
#include <iostream>
#include <string>
#include <cstdlib>
#include "ArrayLLN.h"
using namespace std;
template <class T> class ArrayLL{
private:
ArrayLLN<T> * head;
public:
ArrayLL(){ head = nullptr; }
~ArrayLL(){ delete head; }
void sethead(ArrayLLN<T>* t){ head = t; }
int length(){ // returns length of the list
int cnt = 0;
for (ArrayLLN<T>*P = head; P != nullptr; cnt++, P = P->getnext());
return cnt;
}
T remove(int pos){ // removes the node at pos
ArrayLLN<T> *P = head, *Q = nullptr;
for (int cnt = 0; cnt < pos; cnt++, Q = P, P = P->getnext());
T tempcontents = P->getcontents();
P->removeself(P, Q, this);
return tempcontents;
}
T& operator[] (const int pos){ // overloads [], which returns a reference to the node at pos
ArrayLLN<T>* P = head;
for (int cnt = 0; cnt < pos; cnt++, P = P->getnext());
return P->getcontents();
}
void insert(int pos, T stuff){ // inserts a node at pos with stuff contents
if (pos == 0) head = new ArrayLLN<T>(stuff, head); // insert at head, with head as next pointer
else if (pos > 0 && pos < length()){ // if inserting not at the head or tail
ArrayLLN<T> *P = head, *Q = nullptr;
for (int i = 0; i < pos; i++, Q = P, P = P->getnext());
if (Q) Q->setnext(new ArrayLLN<T>(stuff, P));
else P->setnext(new ArrayLLN<T>(stuff, P->getnext()));
}
else if (pos == length()) // if inserting at the tail.
if (head == NULL) head = new ArrayLLN<T>(stuff, NULL);
else head->addback(stuff);
else return;
}
};
#endif
Listing ArrayLLN.h...
//Ben Harris
//List Node Template
#ifndef _ARRAYLLN_
#define _ARRAYLLN_
#include <iostream>
#include <string>
#include <cstdlib>
#include "PG5.h"
#include "ArrayLL.h"
using namespace std;
template <class T> class ArrayLL;
template <class T> class ArrayLLN{
private:
T contents;
ArrayLLN<T> *next;
public:
ArrayLLN(T t, ArrayLLN<T>* n){ // Constructor
contents = t;
next = n;
}
~ArrayLLN(){delete next;} // Destructor
void setcontents(T t){contents = t;} // contents mutator
T& getcontents(){return contents;} // contents accessor
ArrayLLN<T> * getnext(){return next;} // next ptr accessor
void setnext(ArrayLLN<T>* n){next = n;} // next ptr mutator
void removeself(ArrayLLN<T>* curr, ArrayLLN<T>* prev, ArrayLL<T>* l){ // deletes curr using prev, sets l's head if !prev
ArrayLLN<T>*temp = curr->getnext();
if (prev) prev->setnext(temp);
else l->sethead(temp);
curr->setnext(nullptr);
delete curr;
}
void addback(T stuff){ // adds new node at the end of the list.
if (!next) next = new ArrayLLN<T>(stuff, nullptr);
else next->addback(stuff);
}
};
#endif
Listing PG5.cpp...
// Ben Harris
// PG5 Main
// This program recreates PG4 using a template of a Linked List.
// The ArrayLL template interfaces with the linkedlist as if it were an array.
#include <iostream>
#include <string>
#include <cstdlib>
#include "PG5.h"
#include "ArrayLL.h"
using namespace std;
int main(int argc, char **argv){ // PG5 main
string input = "";
cout << "cLibrary Version 2.0 -- type help for a list of commands" << endl;
ArrayLL<string> list;
while (converttolower(input).substr(0, 4) != "exit"){ // quit program when exit command is found
cout << "Enter command: ";
getline(cin, input);
string lower = converttolower(input);
if (lower.substr(0, 3) == "add"){ // insert a new node in alphabetical order, not allowing duplicates (case-insensitive)
bool found = false;
string linputstr = converttolower(input.substr(5, input.length() - 6));
for (int i = 0; i < list.length(); i++) // Make sure no duplicates are entered
if (converttolower(list[i]) == linputstr)
found = true;
int j = 0;
for (j = 0; j < list.length(); j++) // Find where in the list that it needs to be inserted
if (converttolower(list[j]) > linputstr)
break;
if (!found) list.insert(j, input.substr(5, input.length() - 6));
}
if (lower.substr(0, 5) == "print"){ // prints the entire list, which should be in alphabetical order
cout << endl;
for (int i = 0; i < list.length(); i++)
cout << list[i] << endl;
cout << endl << "There are " << list.length() << " titles in the library." << endl;
}
if (lower.substr(0, 6) == "remove"){ // removes all the items in the library that contain search string
string lv = lower.substr(8, input.length() - 9);
for (int i = 0; i < list.length(); i++){
string ll = converttolower(list[i]);
if (ll.find(lv) >= 0 && ll.find(lv) < ll.length()){
cout << "\"" << list.remove(i) << "\"" << " was removed from the library." << endl;
i--;
}
}
}
if (lower.substr(0, 4) == "help" || lower.substr(0, 4) == "halp"){ // prints info for each function
cout << endl << "These are the commands that this program accepts: " << endl << endl;
cout << "add \"book title\" \t--adds a new entry with book title as the name" << endl;
cout << "remove \"search term\" \t--deletes all books that contain search term" << endl;
cout << "print \t\t\t--prints out the entire library in alphabetical order" << endl;
cout << "exit \t\t\t--exits the program (also clears the library)" << endl << endl;
}
}
return 0;
}
string converttolower(string s){for (unsigned int i = 0; i < s.length(); i++) s[i] = tolower(s[i]); return s;}
Listing PG5.h...
// Ben Harris
// Header for PG5
#ifndef _PG5_
#define _PG5_
#include<iostream>
#include<string>
#include<cstdlib>
#include "ArrayLL.h"
using namespace std;
int main(int argc, char **argv); // Main...
string converttolower(string s); // Returns a version of the input string all lower case.
#endif
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:\Users\apoe\Desktop\Grading Folder>cppcompileall PG5.exe
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:\Users\apoe\Desktop\Grading Folder>"C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86
Setting environment for using Microsoft Visual Studio 2010 x86 tools.
C:\Users\apoe\Desktop\Grading Folder>cl /Tp "PG5.cpp" /O2 /EHsc /W2 /Za /link /OUT:PG5.exe
PG5.cpp
Microsoft (R) Incremental Linker Version 10.00.40219.01
Copyright (C) Microsoft Corporation. All rights reserved.
/out:PG5.exe
/OUT:PG5.exe
PG5.obj
C:\Users\apoe\Desktop\Grading Folder>Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
C:\Users\apoe\Desktop\Grading Folder>PG5.exe < "C:\Grading\Classes\CS201-01-14F\PG5\PG5-1.in"
cLibrary Version 2.0 -- type help for a list of commands
Enter command:
There are 0 titles in the library.
Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command:
A
b
c
D
e
F
G
H
i
J
K
L
m
n
o
p
q
r
S
t
u
v
w
X
y
z
There are 26 titles in the library.
Enter command: "A" was removed from the library.
Enter command: "b" was removed from the library.
Enter command: "c" was removed from the library.
Enter command: "D" was removed from the library.
Enter command: "e" was removed from the library.
Enter command: "F" was removed from the library.
Enter command: "H" was removed from the library.
Enter command: "i" was removed from the library.
Enter command: "J" was removed from the library.
Enter command:
G
K
L
m
n
o
p
q
r
S
t
u
v
w
X
y
z
There are 17 titles in the library.
Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: Enter command: "And Another Thing..." was removed from the library.
"Life, The Universe, And Everything" was removed from the library.
"So Long, And Thanks For All The Fish" was removed from the library.
Enter command: "G" was removed from the library.
"The Hitchhiker's Guide To The Galaxy" was removed from the library.
Enter command:
K
L
m
Mostly Harmless
n
o
p
q
r
S
t
The Restaurant At The End Of The Universe
u
v
w
X
y
z
There are 18 titles in the library.
Enter command: "K" was removed from the library.
"L" was removed from the library.
"m" was removed from the library.
"Mostly Harmless" was removed from the library.
"n" was removed from the library.
"o" was removed from the library.
"p" was removed from the library.
"q" was removed from the library.
"r" was removed from the library.
"S" was removed from the library.
"t" was removed from the library.
"The Restaurant At The End Of The Universe" was removed from the library.
"u" was removed from the library.
"v" was removed from the library.
"w" was removed from the library.
"X" was removed from the library.
"y" was removed from the library.
"z" was removed from the library.
Enter command:
There are 0 titles in the library.
Enter command:
C:\Users\apoe\Desktop\Grading Folder>exit
50/50.