From 681e15304e1f84ed15fdfd2092d6ed0b999fd7dd Mon Sep 17 00:00:00 2001 From: Ben Harris Date: Sat, 15 Jan 2022 23:41:00 -0500 Subject: [PATCH] fix formatting and remove unnecessary includes --- PG1/PG1.cpp | 105 +++++++++++++++++++++---------------------- PG1/PG1.h | 10 ++--- PG2/PG2.cpp | 119 ++++++++++++++++++++++++++----------------------- PG2/PG2.h | 14 ++++-- PG3/List.cpp | 46 ++++++++++--------- PG3/List.h | 16 ++++--- PG3/Node.cpp | 43 ++++++++++-------- PG3/Node.h | 22 ++++----- PG3/PG3.cpp | 25 +++++------ PG3/PG3.h | 1 + PG4/List.cpp | 46 ++++++++++--------- PG4/List.h | 20 +++++---- PG4/Node.cpp | 79 +++++++++++++++++--------------- PG4/Node.h | 29 ++++++------ PG4/PG4.cpp | 58 +++++++++++++----------- PG4/PG4.h | 1 + PG4/pg4 | Bin 24814 -> 0 bytes PG5/ArrayLL.h | 76 ++++++++++++++++--------------- PG5/ArrayLLN.h | 55 ++++++++++++----------- PG5/PG5.cpp | 105 ++++++++++++++++++++++--------------------- PG5/PG5.h | 1 + PG6/PG6.cpp | 85 +++++++++++++++++------------------ PG6/PG6.h | 3 ++ 23 files changed, 513 insertions(+), 446 deletions(-) delete mode 100644 PG4/pg4 diff --git a/PG1/PG1.cpp b/PG1/PG1.cpp index 13115ca..adc0897 100644 --- a/PG1/PG1.cpp +++ b/PG1/PG1.cpp @@ -1,81 +1,80 @@ //Ben Harris //This program converts decimal to dozenal. -#include //import -#include +#include #include -#include #include "PG1.h" + using namespace std; //main method runs at execution time -int main (int argc, char **argv) { //signature - cout << "Enter a number in decimal: "; - string input; - getline (cin,input); - cout << input << " in dozenal is " << dozenal (input) << endl; - system ("pause"); - return 0; +int main(int argc, char **argv) { //signature + cout << "Enter a number in decimal: "; + string input; + getline(cin, input); + cout << input << " in dozenal is " << dozenal(input) << endl; + return 0; } + //this method divides by 12 and returns the quotient, to be stored -string div12 (string dividend, int &remainder) { - - int save = 0; - string quotient = ""; - - for (int i = 0 ; i < dividend.length() ; i++){ +string div12(string dividend, int &remainder) { - if(save != 0){ + int save = 0; + string quotient = ""; - save = (save * 10) + (dividend[i]-'0'); - int j = save/12; //divide by 12 - char c = j+'0'; //convert to char - quotient = quotient+c; - save = save%12; - - } - else { - save = dividend[i]-'0'; - quotient += '0'; - } - } - remainder = save; - return quotient; + for (int i = 0; i < dividend.length(); i++) { + + if (save != 0) { + + save = (save * 10) + (dividend[i] - '0'); + int j = save / 12; //divide by 12 + char c = j + '0'; //convert to char + quotient = quotient + c; + save = save % 12; + + } else { + save = dividend[i] - '0'; + quotient += '0'; + } + } + remainder = save; + return quotient; } + //this method calls the div12 method and stores each remainders as the next digit -string dozenal (string input) { +string dozenal(string input) { - string dozenalNumber = ""; - int remainder = 0; + string dozenalNumber = ""; + int remainder = 0; - while( atoi(div12(input,remainder).c_str()) != 0 ) { + while (atoi(div12(input, remainder).c_str()) != 0) { - input = div12(input,remainder); + input = div12(input, remainder); - char remainderChar; + char remainderChar; - if(remainder == 10){ - remainderChar = 'A'; - }else if (remainder == 11){ - remainderChar = 'B'; - }else remainderChar = remainder+'0'; + if (remainder == 10) { + remainderChar = 'A'; + } else if (remainder == 11) { + remainderChar = 'B'; + } else remainderChar = remainder + '0'; - dozenalNumber = remainderChar+dozenalNumber; + dozenalNumber = remainderChar + dozenalNumber; - } + } - input = div12(input,remainder); + input = div12(input, remainder); - char remainderChar = remainder; + char remainderChar = remainder; - if(remainder == 10){ - remainderChar = 'A'; - }else if (remainder == 11){ - remainderChar = 'B'; - }else remainderChar = remainder+'0'; + if (remainder == 10) { + remainderChar = 'A'; + } else if (remainder == 11) { + remainderChar = 'B'; + } else remainderChar = remainder + '0'; - dozenalNumber = remainderChar+dozenalNumber; + dozenalNumber = remainderChar + dozenalNumber; - return dozenalNumber; + return dozenalNumber; } diff --git a/PG1/PG1.h b/PG1/PG1.h index 9bc9020..0f99bb9 100644 --- a/PG1/PG1.h +++ b/PG1/PG1.h @@ -5,14 +5,14 @@ #define _PG1_ #include -#include #include -#include + using namespace std; -int main (int argc, char **argv); //prototype -string div12 (string dividend, int &remainder); -string dozenal (string input); +int main(int argc, char **argv); //prototype +string div12(string dividend, int &remainder); + +string dozenal(string input); #endif diff --git a/PG2/PG2.cpp b/PG2/PG2.cpp index aab5a62..73ed08d 100644 --- a/PG2/PG2.cpp +++ b/PG2/PG2.cpp @@ -3,76 +3,83 @@ //then checks for poker hands excluding straight, flush, and straight flush #include -#include #include #include "time.h" #include "PG2.h" + using namespace std; -int main (int argc, char **argv){//main method - //initialize deck and randomize it - srand((unsigned)time(0)); - int *deck = new int[52]; - for(int i=0;i<52;i++){ - deck[i]=i; - } - shuffle(deck); +int main(int argc, char **argv) { + // initialize deck and randomize it + srand((unsigned) time(0)); + int *deck = new int[52]; + for (int i = 0; i < 52; i++) { + deck[i] = i; + } + shuffle(deck); - //start to deal the hands - for(int j = 0; j<10;j++){//deals ten hands + // start to deal the hands + for (int j = 0; j < 10; j++) {//deals ten hands - int rankcount[13] = {0}; - for(int i = j*5;i 1)return true; - else return false; + +bool dupeinarray(int *d, int dupetocheck, int sz) {//checks for duplicate dupetocheck in array d + int cnt = 0; + for (int i = 0; i < sz; i++) { + if (d[i] == dupetocheck)cnt++; + } + if (cnt > 1)return true; + else return false; } diff --git a/PG2/PG2.h b/PG2/PG2.h index 75ec351..b248138 100644 --- a/PG2/PG2.h +++ b/PG2/PG2.h @@ -7,13 +7,21 @@ #include #include #include + using namespace std; -int main (int argc, char **argv); +int main(int argc, char **argv); + string getSuitName(int card); + string getRankName(int card); -void swapTwo(int a, int b,int *d); + +void swapTwo(int a, int b, int *d); + void shuffle(int *d); -bool inArray(int *d,int toinsert, int sz); + +bool inArray(int *d, int toinsert, int sz); + bool dupeinarray(int *d, int dupetocheck, int sz); + #endif diff --git a/PG3/List.cpp b/PG3/List.cpp index c91c0ea..594eed4 100644 --- a/PG3/List.cpp +++ b/PG3/List.cpp @@ -2,33 +2,37 @@ //List.cpp defines the methods for a List #include -#include #include #include "List.h" #include "Node.h" + using namespace std; -List::List(){ - head = NULL; +List::List() { + head = NULL; } -List::~List(){ - delete head; + +List::~List() { + delete head; } -void List::add(string s){ - if (head == NULL)head = new Node(s, NULL); - else head->add(s); + +void List::add(string s) { + if (head == NULL)head = new Node(s, NULL); + else head->add(s); } -void List::print(){ - int maxcnt = 0; - Node* n = head; - while (n != NULL){ - if (n->getcount() > maxcnt) maxcnt = n->getcount(); - n = n->getnext(); - } - cout << endl << "<>"<getcount() == maxcnt) cout << n->getvalue() << endl; - n = n->getnext(); - }cout << endl; + +void List::print() { + int maxcnt = 0; + Node *n = head; + while (n != NULL) { + if (n->getcount() > maxcnt) maxcnt = n->getcount(); + n = n->getnext(); + } + cout << endl << "<>" << endl << endl; + n = head; + while (n != NULL) { + if (n->getcount() == maxcnt) cout << n->getvalue() << endl; + n = n->getnext(); + } + cout << endl; } diff --git a/PG3/List.h b/PG3/List.h index 35c2add..aefe505 100644 --- a/PG3/List.h +++ b/PG3/List.h @@ -8,17 +8,21 @@ #include #include #include "Node.h" + using namespace std; class Node; -class List{ + +class List { private: // Each list contains only the pointer to its head Node. - Node *head; + Node *head; public: - List(); // List Constructor - ~List(); // List Destructor - void add(string s); // Adds a Node to the tail of the list, incrementing the counter if a duplicate string is entered. - void print(); // Loops through the array twice: once to find the highest count, then a second time to print each Node that has that count value. + List(); // List Constructor + ~List(); // List Destructor + void + add(string s); // Adds a Node to the tail of the list, incrementing the counter if a duplicate string is entered. + void + print(); // Loops through the array twice: once to find the highest count, then a second time to print each Node that has that count value. }; #endif diff --git a/PG3/Node.cpp b/PG3/Node.cpp index deadef1..fd61564 100644 --- a/PG3/Node.cpp +++ b/PG3/Node.cpp @@ -2,32 +2,37 @@ //Node.cpp contains the code for each node #include -#include #include #include "Node.h" + using namespace std; -Node::Node(string s, Node *n){ - cnt = 1; - value = s; - next = n; +Node::Node(string s, Node *n) { + cnt = 1; + value = s; + next = n; } -Node::~Node(){ - delete next; - cnt = 0; - //farewell + +Node::~Node() { + delete next; + cnt = 0; + //farewell } -string Node::getvalue(){ - return value; + +string Node::getvalue() { + return value; } -int Node::getcount(){ - return cnt; + +int Node::getcount() { + return cnt; } -Node * Node::getnext(){ - return next; + +Node *Node::getnext() { + return next; } -void Node::add(string s){ - if (value == s)cnt++; - else if (next == NULL) next = new Node(s, NULL); - else next->add(s); + +void Node::add(string s) { + if (value == s)cnt++; + else if (next == NULL) next = new Node(s, NULL); + else next->add(s); } diff --git a/PG3/Node.h b/PG3/Node.h index ce7f0bc..589acaf 100644 --- a/PG3/Node.h +++ b/PG3/Node.h @@ -7,20 +7,22 @@ #include #include #include + using namespace std; -class Node{ +class Node { private: // Each Node contains a vale, count, and a pointer to the next Node in the list. - string value; - int cnt; - Node *next; + string value; + int cnt; + Node *next; public: - Node(string s, Node *n); // Node Constructor - ~Node(); // Node Destructor - string getvalue(); // Value accessor - int getcount(); // Count accessor - Node * getnext(); // Next pointer accessor - void add(string s); // Adds a new Node to a list, incrementing the count for a Node if the same string is entered more than once. + Node(string s, Node *n); // Node Constructor + ~Node(); // Node Destructor + string getvalue(); // Value accessor + int getcount(); // Count accessor + Node *getnext(); // Next pointer accessor + void + add(string s); // Adds a new Node to a list, incrementing the count for a Node if the same string is entered more than once. }; #endif diff --git a/PG3/PG3.cpp b/PG3/PG3.cpp index 7938281..bdc93d7 100644 --- a/PG3/PG3.cpp +++ b/PG3/PG3.cpp @@ -3,21 +3,20 @@ //most commonly entered words, printing all in the case of a tie. #include -#include -#include #include "PG3.h" #include "List.h" + using namespace std; -int main(int argc, char **argv){ - string input =" "; - List * list = new List(); - while (input!=""){ - cout << "Input<<"; - getline(cin, input); - if(input!="")list->add(input); - } - list->print(); - delete list; - return 0; +int main(int argc, char **argv) { + string input = " "; + List *list = new List(); + while (input != "") { + cout << "Input<<"; + getline(cin, input); + if (input != "")list->add(input); + } + list->print(); + delete list; + return 0; } diff --git a/PG3/PG3.h b/PG3/PG3.h index cbf32b2..7e826ff 100644 --- a/PG3/PG3.h +++ b/PG3/PG3.h @@ -7,6 +7,7 @@ #include #include #include + using namespace std; int main(int argc, char **argv);// Main method. diff --git a/PG4/List.cpp b/PG4/List.cpp index d4e7d99..5bf3345 100644 --- a/PG4/List.cpp +++ b/PG4/List.cpp @@ -2,38 +2,40 @@ //List.cpp defines the methods for a List #include -#include #include #include "List.h" #include "Node.h" #include "PG4.h" + using namespace std; -List::List(){ - head = NULL; -} -List::~List(){ - delete head; -} -void List::add(string s, string l){ - if (head && head->getvalue() == l) { - cout << "already in library. aborted." << endl; - return; - } - if (!head || converttolower(head->getvalue()) > l) head = new Node(s, head); - else head = head->add(s, l); +List::List() { + head = NULL; } -void List::print(){ - - cout << endl << "Library List" << endl << endl; - if (head) head->print(); - cout << endl; +List::~List() { + delete head; } -void List::remove(string s){ - if (head) head = head->remove(s, NULL, this); +void List::add(string s, string l) { + if (head && head->getvalue() == l) { + cout << "already in library. aborted." << endl; + return; + } + if (!head || converttolower(head->getvalue()) > l) head = new Node(s, head); + else head = head->add(s, l); } -void List::sethead(Node * n){ head = n; } +void List::print() { + + cout << endl << "Library List" << endl << endl; + if (head) head->print(); + cout << endl; +} + +void List::remove(string s) { + if (head) head = head->remove(s, NULL, this); +} + +void List::sethead(Node *n) { head = n; } diff --git a/PG4/List.h b/PG4/List.h index 89b8808..6da75dc 100644 --- a/PG4/List.h +++ b/PG4/List.h @@ -8,19 +8,23 @@ #include #include #include "Node.h" + using namespace std; class Node; -class List{ + +class List { private: // Each list contains only the pointer to its head Node. - Node *head; + Node *head; public: - List(); // List Constructor - ~List(); // List Destructor - void add(string s,string l); // Adds a Node to the tail of the list, incrementing the counter if a duplicate string is entered. - void print(); // Loops through the array twice: once to find the highest count, then a second time to print each Node that has that count value. - void remove(string s); // Calls remove on the nodes that match string s - void sethead(Node* n); // Head mutator + List(); // List Constructor + ~List(); // List Destructor + void add(string s, + string l); // Adds a Node to the tail of the list, incrementing the counter if a duplicate string is entered. + void + print(); // Loops through the array twice: once to find the highest count, then a second time to print each Node that has that count value. + void remove(string s); // Calls remove on the nodes that match string s + void sethead(Node *n); // Head mutator }; #endif diff --git a/PG4/Node.cpp b/PG4/Node.cpp index 1a7fe86..7765722 100644 --- a/PG4/Node.cpp +++ b/PG4/Node.cpp @@ -2,51 +2,58 @@ //Node.cpp contains the code for each node #include -#include #include -#include #include "Node.h" #include "List.h" #include "PG4.h" + using namespace std; -Node::Node(string s, Node *n){ - value = s; - next = n; -} -Node::~Node(){delete next;} -string Node::getvalue(){return value;} -int Node::getcount(){return cnt;} -Node * Node::getnext(){return next;} -void Node::setnext(Node *n){ next = n; } - -Node * Node::add(string s, string l){ - - string lvalue = converttolower(value); - - if (lvalue == l) return this; - if (l < lvalue) return new Node(s, this); - - if (!next){ next = new Node(s, NULL); return this; } - - next = next->add(s,l); - return this; +Node::Node(string s, Node *n) { + value = s; + next = n; } -void Node::print(){ - cout << value << endl; - if (next) next->print(); +Node::~Node() { delete next; } + +string Node::getvalue() { return value; } + +int Node::getcount() { return cnt; } + +Node *Node::getnext() { return next; } + +void Node::setnext(Node *n) { next = n; } + +Node *Node::add(string s, string l) { + + string lvalue = converttolower(value); + + if (lvalue == l) return this; + if (l < lvalue) return new Node(s, this); + + if (!next) { + next = new Node(s, NULL); + return this; + } + + next = next->add(s, l); + return this; } -Node* Node::remove(string s, Node *prev, List *l){ - string lvalue = converttolower(value); - if (next) next = next->remove(s, this, l); - if (lvalue.find(s) < value.length() && lvalue.find(s) >= 0){ - Node *t = next; - next = NULL; - delete this; - return t; - } - return this; +void Node::print() { + cout << value << endl; + if (next) next->print(); +} + +Node *Node::remove(string s, Node *prev, List *l) { + string lvalue = converttolower(value); + if (next) next = next->remove(s, this, l); + if (lvalue.find(s) < value.length() && lvalue.find(s) >= 0) { + Node *t = next; + next = NULL; + delete this; + return t; + } + return this; } diff --git a/PG4/Node.h b/PG4/Node.h index 3a99295..19c6b22 100644 --- a/PG4/Node.h +++ b/PG4/Node.h @@ -8,24 +8,27 @@ #include #include #include "List.h" + using namespace std; class List; -class Node{ + +class Node { private: // Each Node contains a value, count, and a pointer to the next Node in the list. - string value; - int cnt; - Node *next; + string value; + int cnt; + Node *next; public: - Node(string s, Node *n); // Node Constructor - ~Node(); // Node Destructor - string getvalue(); // Value accessor - int getcount(); // Count accessor - Node * getnext(); // Next pointer accessor - void setnext(Node* n); // Next pointer mutator - Node * add(string s,string l); // Adds a new Node to a list, incrementing the count for a Node if the same string is entered more than once. - void print(); // Tells each node to print itself and its next - Node* remove(string s, Node *prev, List *l); // Remove the nodes with matching string s and returns the new head + Node(string s, Node *n); // Node Constructor + ~Node(); // Node Destructor + string getvalue(); // Value accessor + int getcount(); // Count accessor + Node *getnext(); // Next pointer accessor + void setnext(Node *n); // Next pointer mutator + Node *add(string s, + string l); // Adds a new Node to a list, incrementing the count for a Node if the same string is entered more than once. + void print(); // Tells each node to print itself and its next + Node *remove(string s, Node *prev, List *l); // Remove the nodes with matching string s and returns the new head }; #endif diff --git a/PG4/PG4.cpp b/PG4/PG4.cpp index 9f2c834..f22d57a 100644 --- a/PG4/PG4.cpp +++ b/PG4/PG4.cpp @@ -2,39 +2,43 @@ //This program manages a library of books by title, stored in a linked list #include -#include -#include #include #include "PG4.h" #include "List.h" + using namespace std; -int main(int argc, char **argv){ - string input = " "; - List * list = new List(); - cout << "cLibrary Version 1.0 -- type help for a list of commands" << endl; - while (converttolower(input).substr(0,4) != "exit"){ +int main(int argc, char **argv) { + string input = " "; + List *list = new List(); + cout << "cLibrary Version 1.0 -- type help for a list of commands" << endl; + while (converttolower(input).substr(0, 4) != "exit") { - cout << "Enter command: "; - getline(cin, input); - string lower = converttolower(input); - if (lower.substr(0, 3) == "add") list->add(input.substr(5, input.length() - 6),lower.substr(5, input.length() -6)); - if (lower.substr(0, 6) == "remove") list->remove(lower.substr(8, input.length() - 9)); - if (lower == "print") list->print(); - if (list && lower == "clear") {delete list; List * list = new List();} - 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; + cout << "Enter command: "; + getline(cin, input); + string lower = converttolower(input); + if (lower.substr(0, 3) == "add") + list->add(input.substr(5, input.length() - 6), lower.substr(5, input.length() - 6)); + if (lower.substr(0, 6) == "remove") list->remove(lower.substr(8, input.length() - 9)); + if (lower == "print") list->print(); + if (list && lower == "clear") { + delete list; + List *list = new List(); + } + 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; } -string converttolower(string s){ - for (int i = 0; i < (int) s.length(); i++) s[i] = tolower(s[i]); return s; +string converttolower(string s) { + for (int i = 0; i < (int) s.length(); i++) s[i] = tolower(s[i]); + return s; } diff --git a/PG4/PG4.h b/PG4/PG4.h index 4e91566..85eef4f 100644 --- a/PG4/PG4.h +++ b/PG4/PG4.h @@ -7,6 +7,7 @@ #include #include #include + using namespace std; int main(int argc, char **argv); // Main... diff --git a/PG4/pg4 b/PG4/pg4 deleted file mode 100644 index beaf801f940279446255ca1bb7efc842dd79b64e..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 24814 zcmeHve|%KcweOyp3JOw=oA+I7?>%$o zWXAUP)6e~j4LNJCpL^}K_g?4MoPDdecCFoJ6S~;NYC+g|p@&KGC`4-&ql#7`W(bd% zC(aYoKyl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_N&6t{m-@{5)HE+zkrYL|br3EsV+yE0rSe23+gvI#tyg+2!-KI)e>zg@(1 zlL1w|EDL?3vSW>B5OnucGon|?TgPP}i~V~*r~YoLZc4NE%`EbdD|(kD59i=|pXR^5 z57NeFfv(3?(+Dn7^dXC#wM@?!H>mksqm-`6V&}#z^xL!0?_+wtIHBez{dStlL!i?< zxkk|!OS1T#lBa!csL*fJsl=I{;krH89gReL@I?!@;WMtNt~Y9H{0-QdFtq`p5p6eu zv;|=VAE3ys3QZ)#Xz4VzwskfK+KiTHXLrO1^z;ha-H$%_Xen87>B?oWoI%K-r?T>i zs=?ov2>DdDrKi1pJ5)@VK@W4&MC;a-mjt`8dmJ_atgSGNa3??SHLfc!GiWb4dGS9?`eWi3>CnvEKRx1TevyRNnjCb5|y@241m z__mPIwavSW!w(qN~s`ZjYT5wv@3B1vyFxFhVp>lmqwV;R7a;rU#9BrTI z@$f!-YC}ERBXY`ikjb4BvA3arsiC%Kqc*3lZ zUs1s{k5cG!**A7<#x9M`a$8Q?h_miW|1z2q_y)k+yQn zut{kLUWP^0#+p1)WU^V#pUcUuLwPb$!>#x>&0EKVo}5wM4VNODb@X8LddrYBLml+a z%Ls3&*%ayaN5Kq74B8eJv&*~EFyu^W_O37t`goDH2gMe!%M5F}0EzaGCE)c}Q&8n> zOHFD-ppIQ5o_W?Gy;uTbAPPDYQp~V&A%v!BQ&fvA29Nh{wUT9A^bV@2jEs{~q|<1{ zX@NG@)x(4}!NX+QY1U#;E-uPz>Xf6BN-}}dEc&lbywr-s_Ol&P&YyUPn3golX%0+T z0&>p&BX)kZ^ixHI1E=Phl1Swd9)}F;61sfm;~jgm>`crOe5#N;!CH)C`&=3ozj>cq ztA~H@F1r>xA%15|jp=QMEdm+#3ifzRThi*;V_JOJKG #include #include "ArrayLLN.h" + using namespace std; -template class ArrayLL{ +template +class ArrayLL { private: - ArrayLLN * head; + ArrayLLN *head; public: - ArrayLL(){ head = nullptr; } - ~ArrayLL(){ delete head; } - void sethead(ArrayLLN* t){ head = t; } + ArrayLL() { head = nullptr; } - int length(){ // returns length of the list - int cnt = 0; - for (ArrayLLN*P = head; P != nullptr; cnt++, P = P->getnext()); - return cnt; - } - T remove(int pos){ // removes the node at pos - ArrayLLN *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* 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(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 *P = head, *Q = nullptr; - for (int i = 0; i < pos; i++, Q = P, P = P->getnext()); - if (Q) Q->setnext(new ArrayLLN(stuff, P)); - else P->setnext(new ArrayLLN(stuff, P->getnext())); - } - else if (pos == length()) // if inserting at the tail. - if (head == NULL) head = new ArrayLLN(stuff, NULL); - else head->addback(stuff); - else return; - } + ~ArrayLL() { delete head; } + + void sethead(ArrayLLN *t) { head = t; } + + int length() { // returns length of the list + int cnt = 0; + for (ArrayLLN *P = head; P != nullptr; cnt++, P = P->getnext()); + return cnt; + } + + T remove(int pos) { // removes the node at pos + ArrayLLN *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 *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(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 *P = head, *Q = nullptr; + for (int i = 0; i < pos; i++, Q = P, P = P->getnext()); + if (Q) Q->setnext(new ArrayLLN(stuff, P)); + else P->setnext(new ArrayLLN(stuff, P->getnext())); + } else if (pos == length()) // if inserting at the tail. + if (head == NULL) head = new ArrayLLN(stuff, NULL); + else head->addback(stuff); + else return; + } }; diff --git a/PG5/ArrayLLN.h b/PG5/ArrayLLN.h index 8be4726..a84dbf7 100644 --- a/PG5/ArrayLLN.h +++ b/PG5/ArrayLLN.h @@ -9,38 +9,43 @@ #include #include "PG5.h" #include "ArrayLL.h" + using namespace std; -template class ArrayLL; +template +class ArrayLL; -template class ArrayLLN{ +template +class ArrayLLN { private: - T contents; - ArrayLLN *next; + T contents; + ArrayLLN *next; public: - - ArrayLLN(T t, ArrayLLN* 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 * getnext(){return next;} // next ptr accessor - void setnext(ArrayLLN* n){next = n;} // next ptr mutator - void removeself(ArrayLLN* curr, ArrayLLN* prev, ArrayLL* l){ // deletes curr using prev, sets l's head if !prev - ArrayLLN*temp = curr->getnext(); - if (prev) prev->setnext(temp); - else l->sethead(temp); - curr->setnext(nullptr); - delete curr; - } + ArrayLLN(T t, ArrayLLN *n) { // Constructor + contents = t; + next = n; + } - void addback(T stuff){ // adds new node at the end of the list. - if (!next) next = new ArrayLLN(stuff, nullptr); - else next->addback(stuff); - } + ~ArrayLLN() { delete next; } // Destructor + void setcontents(T t) { contents = t; } // contents mutator + T &getcontents() { return contents; } // contents accessor + ArrayLLN *getnext() { return next; } // next ptr accessor + void setnext(ArrayLLN *n) { next = n; } // next ptr mutator + + void + removeself(ArrayLLN *curr, ArrayLLN *prev, ArrayLL *l) { // deletes curr using prev, sets l's head if !prev + ArrayLLN *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(stuff, nullptr); + else next->addback(stuff); + } }; diff --git a/PG5/PG5.cpp b/PG5/PG5.cpp index ddeb067..a15d199 100644 --- a/PG5/PG5.cpp +++ b/PG5/PG5.cpp @@ -4,69 +4,72 @@ // The ArrayLL template interfaces with the linkedlist as if it were an array. #include -#include -#include #include "PG5.h" #include "ArrayLL.h" + using namespace std; -int main(int argc, char **argv){ // PG5 main +int main(int argc, char **argv) { // PG5 main - string input = ""; - cout << "cLibrary Version 2.0 -- type help for a list of commands" << endl; - ArrayLL list; + string input = ""; + cout << "cLibrary Version 2.0 -- type help for a list of commands" << endl; + ArrayLL list; - while (converttolower(input).substr(0, 4) != "exit"){ // quit program when exit command is found + while (converttolower(input).substr(0, 4) != "exit") { // quit program when exit command is found - cout << "Enter command: "; - getline(cin, input); - string lower = converttolower(input); + 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) + 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)); + 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; + 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)); + 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 (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 (!found) list.insert(j, input.substr(5, input.length() - 6)); - 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; + } + + 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;} +string converttolower(string s) { + for (unsigned int i = 0; i < s.length(); i++) s[i] = tolower(s[i]); + return s; +} diff --git a/PG5/PG5.h b/PG5/PG5.h index d8fcc7e..e3aef07 100644 --- a/PG5/PG5.h +++ b/PG5/PG5.h @@ -8,6 +8,7 @@ #include #include #include "ArrayLL.h" + using namespace std; int main(int argc, char **argv); // Main... diff --git a/PG6/PG6.cpp b/PG6/PG6.cpp index b642ea2..4678e1b 100644 --- a/PG6/PG6.cpp +++ b/PG6/PG6.cpp @@ -3,57 +3,56 @@ // This program reads a file, uses the first four bytes as an integer record size and sorts the rest of the file in recordsz chunks. #include -#include #include -#include #include #include "PG6.h" + using namespace std; -int main(int argc, char **argv){ - string fn = ""; - streampos fsz; - cout << "Enter the name of the file that you want to sort." << endl << "File Name: "; - getline(cin, fn); // Get file name from user - fstream f (fn, ios::in | ios::out | ios::binary); // Open file from input location for read and write and as binary - if (f.is_open()){ +int main(int argc, char **argv) { + string fn = ""; + streampos fsz; + cout << "Enter the name of the file that you want to sort." << endl << "File Name: "; + getline(cin, fn); // Get file name from user + fstream f(fn, ios::in | ios::out | ios::binary); // Open file from input location for read and write and as binary + if (f.is_open()) { - char * recordsz = new char[4]; - f.read(recordsz, (streamsize) 4); // Read first four bytes - int rsz = atoi(recordsz); // Store first four bytes as int record size - f.seekg(0, f.end); - fsz = f.tellg(); // Get length of file - int recordcnt = ((int)fsz-4) / rsz; // Get # of records - f.seekg(0, f.beg); - - // Begin shell sort - int d = recordcnt; - while (d > 1){ - d = (d == 2) ? 1 : d % 2 == 1 ? (d + 1) / 2 : d % 4 == 0 ? d / 2 + 1 : d / 2 + 2; - for (int i = d; i < recordcnt; i++) // Use readstring and writestring methods to get and put strings in the file - for (int j = i - d; j >= 0 && readstring(f,j,rsz) > readstring(f,j + d,rsz); j -= d){ - string t = readstring(f,j,rsz); - writestring(f, j, rsz, readstring(f,j + d,rsz)); - writestring(f, j+d, rsz, t); - } - } - cout << "Sort successful." << endl; - f.close(); - } - else cout << "Error: file could not be opened or does not exist." << endl; - system("pause"); + char *recordsz = new char[4]; + f.read(recordsz, (streamsize) 4); // Read first four bytes + int rsz = atoi(recordsz); // Store first four bytes as int record size + f.seekg(0, f.end); + fsz = f.tellg(); // Get length of file + int recordcnt = ((int) fsz - 4) / rsz; // Get # of records + f.seekg(0, f.beg); + + // Begin shell sort + int d = recordcnt; + while (d > 1) { + d = (d == 2) ? 1 : d % 2 == 1 ? (d + 1) / 2 : d % 4 == 0 ? d / 2 + 1 : d / 2 + 2; + for (int i = d; + i < recordcnt; i++) // Use readstring and writestring methods to get and put strings in the file + for (int j = i - d; j >= 0 && readstring(f, j, rsz) > readstring(f, j + d, rsz); j -= d) { + string t = readstring(f, j, rsz); + writestring(f, j, rsz, readstring(f, j + d, rsz)); + writestring(f, j + d, rsz, t); + } + } + cout << "Sort successful." << endl; + f.close(); + } else cout << "Error: file could not be opened or does not exist." << endl; } -string readstring(fstream &f, int recordpos, int len){ // This method seeks to the position of recordpos * len, reads len bytes and returns that string - char* t = new char[len]; - f.seekg(recordpos*len + 4, ios::beg); - f.read(t, len); - return (string)t; +string readstring(fstream &f, int recordpos, + int len) { // This method seeks to the position of recordpos * len, reads len bytes and returns that string + char *t = new char[len]; + f.seekg(recordpos * len + 4, ios::beg); + f.read(t, len); + return (string) t; } -void writestring(fstream &f, int recordpos, int len, string s){ // Writes string s to f at position recordpos*len - f.seekp(recordpos*len + 4, ios::beg); - char * t = new char[s.length()]; - for (unsigned int i = 0; i < s.length(); i++) t[i] = s[i]; - f.write(t, len); +void writestring(fstream &f, int recordpos, int len, string s) { // Writes string s to f at position recordpos*len + f.seekp(recordpos * len + 4, ios::beg); + char *t = new char[s.length()]; + for (unsigned int i = 0; i < s.length(); i++) t[i] = s[i]; + f.write(t, len); } diff --git a/PG6/PG6.h b/PG6/PG6.h index 809c3d1..866b21b 100644 --- a/PG6/PG6.h +++ b/PG6/PG6.h @@ -9,10 +9,13 @@ #include #include #include + using namespace std; int main(int argc, char **argv); + string readstring(fstream &f, int recordpos, int len); + void writestring(fstream &f, int recordpos, int len, string s); #endif