cs201/PG5/PG5-4.out

389 lines
9.0 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 = NULL; }
~ArrayLL(){ delete head; }
int length(){
int cnt = 0;
for (ArrayLLN<T>*P = head; P != nullptr; cnt++, P = P->getnext());
return cnt;
}
T remove(int i){
for (int cnt = 0; cnt < i; cnt++, head = head->getnext());
T tempcontents = head->getcontents();
head = head->removeself();
return tempcontents;
}
T& operator[] (const int i){
ArrayLLN<T>* P = head;
for (int cnt = 0; cnt < i; cnt++, P = P->getnext());
return P->getcontents();
}
void insert(int i, T stuff){
if (i == 0) head = new ArrayLLN<T>(stuff, head); // insert at head, with head as next pointer
else if (i>0 && i < length()){
ArrayLLN<T> *P = head, *Q = nullptr;
for (int j = 0; j < i-1; j++, 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 (i == length())
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 ArrayLLN{
private:
T contents;
ArrayLLN<T> *next;
public:
ArrayLLN(T t, ArrayLLN<T>* n){
contents = t;
next = n;
}
~ArrayLLN(){delete next;}
void setcontents(T s){contents = s;}
T& getcontents(){return contents;}
ArrayLLN<T> * getnext(){return next;}
void setnext(ArrayLLN<T>* s){next = s;}
ArrayLLN<T> * removeself(){
ArrayLLN<T>*temp = next;
this->setnext(nullptr);
delete this;
return temp;
}
void addback(T stuff){
if (next == NULL) next = new ArrayLLN<T>(stuff, nullptr);
else next->addback(stuff);
}
};
#endif
Listing PG5.cpp...
//Ben Harris
//PG5 Main
#include <iostream>
#include <string>
#include <cstdlib>
#include "PG5.h"
#include "ArrayLL.h"
using namespace std;
int main(int argc, char **argv){
string input = "";
cout << "cLibrary Version 1.0 -- type help for a list of commands" << endl;
ArrayLL<string> list;
while (converttolower(input).substr(0, 4) != "exit"){
cout << "Enter command: ";
getline(cin, input);
string lower = converttolower(input);
if (lower.substr(0, 3) == "add"){
bool found = false;
string lv = 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]) == lv){
cout << "That title already exists in the library." << endl;
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]) > lv){
break;
}
}
cout << j << endl;
if (!found) list.insert(j, input.substr(5, input.length() - 6));
}
if (lower.substr(0, 5) == "print"){
cout << endl;
//sorts(list, 0, list.length());
for (int i = 0; i < list.length(); i++)
cout << list[i] << endl;
cout << endl << "There are " << list.length() << " items in the library." << endl;
}
if (lower.substr(0, 6) == "remove"){
string lv = lower.substr(8, input.length() - 9);
int l = list.length();
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, 5) == "clear"){
// list = {};
// cout << endl << "Library cleared." << endl << endl;
//}
if (lower.substr(0, 4) == "help" || lower.substr(0, 4) == "halp"){
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 << "clear \t\t\t--clears the entire library" << endl;
cout << "exit \t\t\t--exits the program (also clears the library)" << endl << endl;
}
}
//delete list;
return 0;
}
void sorts(ArrayLL<string> arr, int left, int right){
int i = left, j = right;
string tmp;
string pivot = arr[(left + right) / 2];
while (i <= j){
while (arr[i] < pivot) i++;
while (arr[j] > pivot) j--;
if (i <= j)
{
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
i++;
j--;
}
}
if (left < j)sorts(arr, left, j);
if (i < right)sorts(arr, i, right);
}
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.
void sorts(ArrayLL<string> arr, int left, int right); // Quick sort ArrayLL
#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\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 1.0 -- type help for a list of commands
Enter command:
There are 0 items in the library.
Enter command: Enter command: 0
Enter command: 1
Enter command: 0
Enter command: 2
Enter command: 3
Enter command: 5
Enter command: 4
Enter command: 1
Enter command: 2
Enter command: 3
Enter command: 0
Enter command: 6
Enter command: 1
Enter command: 3
Enter command: 4
Enter command: 5
Enter command: 6
Enter command: 7
Enter command: 8
Enter command: 19
Enter command: 18
Enter command: 1
Enter command: 18
Enter command: 1
Enter command: 11
Enter command: 10
Enter command: That title already exists in the library.
16
Enter command: That title already exists in the library.
22
Enter command: That title already exists in the library.
4
Enter command: That title already exists in the library.
16
Enter command: That title already exists in the library.
19
Enter command:
A
b
c
D
F
G
H
J
K
m
L
n
e
o
p
i
S
r
t
u
v
q
X
w
y
z
There are 26 items 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: Enter command: Enter command: "i" was removed from the library.
Enter command: Enter command:
S
r
t
u
v
q
X
w
y
z
There are 10 items in the library.
Enter command: 3
Enter command: 4
Enter command: 0
Enter command: 3
Enter command: 1
Enter command: 0
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: "The Hitchhiker's Guide To The Galaxy" was removed from the library.
Enter command:
The Restaurant At The End Of The Universe
t
u
v
q
X
w
y
z
There are 9 items in the library.
Enter command: "The Restaurant At The End Of The Universe" was removed from the library.
"t" was removed from the library.
"u" was removed from the library.
"v" was removed from the library.
"q" was removed from the library.
"X" was removed from the library.
"w" was removed from the library.
"y" was removed from the library.
"z" was removed from the library.
Enter command:
There are 0 items in the library.
Enter command:
C:\Users\apoe\Desktop\Grading Folder>exit
Wrong answers. Doesn't look like everything gets added.
25/50.