fix formatting and remove unnecessary includes

This commit is contained in:
Ben Harris 2022-01-15 23:41:00 -05:00
parent 9f551a502b
commit 681e15304e
Signed by: ben
GPG Key ID: 4E0AF802FFF7960C
23 changed files with 513 additions and 446 deletions

View File

@ -1,81 +1,80 @@
//Ben Harris //Ben Harris
//This program converts decimal to dozenal. //This program converts decimal to dozenal.
#include <iostream> //import #include <iostream>
#include <string>
#include <cstdlib> #include <cstdlib>
#include <sstream>
#include "PG1.h" #include "PG1.h"
using namespace std; using namespace std;
//main method runs at execution time //main method runs at execution time
int main (int argc, char **argv) { //signature int main(int argc, char **argv) { //signature
cout << "Enter a number in decimal: "; cout << "Enter a number in decimal: ";
string input; string input;
getline (cin,input); getline(cin, input);
cout << input << " in dozenal is " << dozenal (input) << endl; cout << input << " in dozenal is " << dozenal(input) << endl;
system ("pause"); return 0;
return 0;
} }
//this method divides by 12 and returns the quotient, to be stored //this method divides by 12 and returns the quotient, to be stored
string div12 (string dividend, int &remainder) { string div12(string dividend, int &remainder) {
int save = 0;
string quotient = "";
for (int i = 0 ; i < dividend.length() ; i++){
if(save != 0){ int save = 0;
string quotient = "";
save = (save * 10) + (dividend[i]-'0'); for (int i = 0; i < dividend.length(); i++) {
int j = save/12; //divide by 12
char c = j+'0'; //convert to char if (save != 0) {
quotient = quotient+c;
save = save%12; save = (save * 10) + (dividend[i] - '0');
int j = save / 12; //divide by 12
} char c = j + '0'; //convert to char
else { quotient = quotient + c;
save = dividend[i]-'0'; save = save % 12;
quotient += '0';
} } else {
} save = dividend[i] - '0';
remainder = save; quotient += '0';
return quotient; }
}
remainder = save;
return quotient;
} }
//this method calls the div12 method and stores each remainders as the next digit //this method calls the div12 method and stores each remainders as the next digit
string dozenal (string input) { string dozenal(string input) {
string dozenalNumber = ""; string dozenalNumber = "";
int remainder = 0; 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){ if (remainder == 10) {
remainderChar = 'A'; remainderChar = 'A';
}else if (remainder == 11){ } else if (remainder == 11) {
remainderChar = 'B'; remainderChar = 'B';
}else remainderChar = remainder+'0'; } 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){ if (remainder == 10) {
remainderChar = 'A'; remainderChar = 'A';
}else if (remainder == 11){ } else if (remainder == 11) {
remainderChar = 'B'; remainderChar = 'B';
}else remainderChar = remainder+'0'; } else remainderChar = remainder + '0';
dozenalNumber = remainderChar+dozenalNumber; dozenalNumber = remainderChar + dozenalNumber;
return dozenalNumber; return dozenalNumber;
} }

View File

@ -5,14 +5,14 @@
#define _PG1_ #define _PG1_
#include <iostream> #include <iostream>
#include <string>
#include <cstdlib> #include <cstdlib>
#include <sstream>
using namespace std; using namespace std;
int main (int argc, char **argv); //prototype int main(int argc, char **argv); //prototype
string div12 (string dividend, int &remainder); string div12(string dividend, int &remainder);
string dozenal (string input);
string dozenal(string input);
#endif #endif

View File

@ -3,76 +3,83 @@
//then checks for poker hands excluding straight, flush, and straight flush //then checks for poker hands excluding straight, flush, and straight flush
#include <iostream> #include <iostream>
#include <string>
#include <cstdlib> #include <cstdlib>
#include "time.h" #include "time.h"
#include "PG2.h" #include "PG2.h"
using namespace std; using namespace std;
int main (int argc, char **argv){//main method int main(int argc, char **argv) {
//initialize deck and randomize it // initialize deck and randomize it
srand((unsigned)time(0)); srand((unsigned) time(0));
int *deck = new int[52]; int *deck = new int[52];
for(int i=0;i<52;i++){ for (int i = 0; i < 52; i++) {
deck[i]=i; deck[i] = i;
} }
shuffle(deck); shuffle(deck);
//start to deal the hands // start to deal the hands
for(int j = 0; j<10;j++){//deals ten hands for (int j = 0; j < 10; j++) {//deals ten hands
int rankcount[13] = {0}; int rankcount[13] = {0};
for(int i = j*5;i<j*5+5;i++){//print, then check each hand for duplicates for (int i = j * 5; i < j * 5 + 5; i++) {//print, then check each hand for duplicates
rankcount[deck[i]/4]++; rankcount[deck[i] / 4]++;
cout << getRankName(deck[i]) << " of " << getSuitName(deck[i]) << endl; cout << getRankName(deck[i]) << " of " << getSuitName(deck[i]) << endl;
} }
cout<<endl; cout << endl;
if (inArray(rankcount, 2, 13) && inArray(rankcount, 3, 13))cout << "FULL HOUSE"; if (inArray(rankcount, 2, 13) && inArray(rankcount, 3, 13))cout << "FULL HOUSE";
else if (dupeinarray(rankcount, 2, 13))cout << "TWO PAIR"; else if (dupeinarray(rankcount, 2, 13))cout << "TWO PAIR";
else if (inArray(rankcount, 4, 13))cout << "FOUR OF A KIND"; else if (inArray(rankcount, 4, 13))cout << "FOUR OF A KIND";
else if (inArray(rankcount, 3, 13))cout << "THREE OF A KIND"; else if (inArray(rankcount, 3, 13))cout << "THREE OF A KIND";
else if(inArray(rankcount,2,13))cout<<"ONE PAIR"; else if (inArray(rankcount, 2, 13))cout << "ONE PAIR";
else cout<<"NOTHING"; else cout << "NOTHING";
cout<<endl; cout << endl;
cout<<endl; cout << endl;
} }
//remove systempause before turning in //remove systempause before turning in
//system("pause"); //system("pause");
delete[] deck; delete[] deck;
return 0; return 0;
} }
string getSuitName(int card){//returns the suit of the input card string getSuitName(int card) {//returns the suit of the input card
string suits[4] = {"Hearts","Clubs","Diamonds","Spades"}; string suits[4] = {"Hearts", "Clubs", "Diamonds", "Spades"};
return suits[card%4]; return suits[card % 4];
} }
string getRankName(int card){//returns the rank of the input card
string ranks[13] = {"Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King","Ace"}; string getRankName(int card) {//returns the rank of the input card
return ranks[card/4]; string ranks[13] = {"Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King",
"Ace"};
return ranks[card / 4];
} }
void swapTwo(int a, int b,int *d){//swaps two ints a and b within an array d
d[b] = d[a]+d[b]; void swapTwo(int a, int b, int *d) {//swaps two ints a and b within an array d
d[a] = d[b]-d[a]; d[b] = d[a] + d[b];
d[b] = d[b]-d[a]; d[a] = d[b] - d[a];
d[b] = d[b] - d[a];
} }
void shuffle(int *d){//swaps each element with a random index at or before the current index, shuffling the deck
for(int i = 1; i<52;i++){ void shuffle(int *d) {//swaps each element with a random index at or before the current index, shuffling the deck
int r = rand()%(i+1); for (int i = 1; i < 52; i++) {
swapTwo(i,r,d); int r = rand() % (i + 1);
} swapTwo(i, r, d);
}
} }
bool inArray(int *d,int toinsert, int sz){//returns true if toinsert is found in array d; checks only up to size
for(int i = 0; i<sz;i++){ bool inArray(int *d, int toinsert, int sz) {//returns true if toinsert is found in array d; checks only up to size
if(toinsert == d[i])return true; for (int i = 0; i < sz; i++) {
}return false; if (toinsert == d[i])return true;
}
return false;
} }
bool dupeinarray(int *d, int dupetocheck, int sz){//checks for duplicate dupetocheck in array d
int cnt = 0; bool dupeinarray(int *d, int dupetocheck, int sz) {//checks for duplicate dupetocheck in array d
for (int i = 0; i < sz; i++){ int cnt = 0;
if (d[i] == dupetocheck)cnt++; for (int i = 0; i < sz; i++) {
} if (d[i] == dupetocheck)cnt++;
if (cnt > 1)return true; }
else return false; if (cnt > 1)return true;
else return false;
} }

View File

@ -7,13 +7,21 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <cstdlib> #include <cstdlib>
using namespace std; using namespace std;
int main (int argc, char **argv); int main(int argc, char **argv);
string getSuitName(int card); string getSuitName(int card);
string getRankName(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); 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); bool dupeinarray(int *d, int dupetocheck, int sz);
#endif #endif

View File

@ -2,33 +2,37 @@
//List.cpp defines the methods for a List //List.cpp defines the methods for a List
#include <iostream> #include <iostream>
#include <string>
#include <cstdlib> #include <cstdlib>
#include "List.h" #include "List.h"
#include "Node.h" #include "Node.h"
using namespace std; using namespace std;
List::List(){ List::List() {
head = NULL; head = NULL;
} }
List::~List(){
delete head; List::~List() {
delete head;
} }
void List::add(string s){
if (head == NULL)head = new Node(s, NULL); void List::add(string s) {
else head->add(s); if (head == NULL)head = new Node(s, NULL);
else head->add(s);
} }
void List::print(){
int maxcnt = 0; void List::print() {
Node* n = head; int maxcnt = 0;
while (n != NULL){ Node *n = head;
if (n->getcount() > maxcnt) maxcnt = n->getcount(); while (n != NULL) {
n = n->getnext(); if (n->getcount() > maxcnt) maxcnt = n->getcount();
} n = n->getnext();
cout << endl << "<<Commonest Words>>"<<endl<<endl; }
n = head; cout << endl << "<<Commonest Words>>" << endl << endl;
while (n != NULL){ n = head;
if (n->getcount() == maxcnt) cout << n->getvalue() << endl; while (n != NULL) {
n = n->getnext(); if (n->getcount() == maxcnt) cout << n->getvalue() << endl;
}cout << endl; n = n->getnext();
}
cout << endl;
} }

View File

@ -8,17 +8,21 @@
#include <string> #include <string>
#include <cstdlib> #include <cstdlib>
#include "Node.h" #include "Node.h"
using namespace std; using namespace std;
class Node; class Node;
class List{
class List {
private: // Each list contains only the pointer to its head Node. private: // Each list contains only the pointer to its head Node.
Node *head; Node *head;
public: public:
List(); // List Constructor List(); // List Constructor
~List(); // List Destructor ~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
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. 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 #endif

View File

@ -2,32 +2,37 @@
//Node.cpp contains the code for each node //Node.cpp contains the code for each node
#include <iostream> #include <iostream>
#include <string>
#include <cstdlib> #include <cstdlib>
#include "Node.h" #include "Node.h"
using namespace std; using namespace std;
Node::Node(string s, Node *n){ Node::Node(string s, Node *n) {
cnt = 1; cnt = 1;
value = s; value = s;
next = n; next = n;
} }
Node::~Node(){
delete next; Node::~Node() {
cnt = 0; delete next;
//farewell 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++; void Node::add(string s) {
else if (next == NULL) next = new Node(s, NULL); if (value == s)cnt++;
else next->add(s); else if (next == NULL) next = new Node(s, NULL);
else next->add(s);
} }

View File

@ -7,20 +7,22 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <cstdlib> #include <cstdlib>
using namespace std; using namespace std;
class Node{ class Node {
private: // Each Node contains a vale, count, and a pointer to the next Node in the list. private: // Each Node contains a vale, count, and a pointer to the next Node in the list.
string value; string value;
int cnt; int cnt;
Node *next; Node *next;
public: public:
Node(string s, Node *n); // Node Constructor Node(string s, Node *n); // Node Constructor
~Node(); // Node Destructor ~Node(); // Node Destructor
string getvalue(); // Value accessor string getvalue(); // Value accessor
int getcount(); // Count accessor int getcount(); // Count accessor
Node * getnext(); // Next pointer 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. 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 #endif

View File

@ -3,21 +3,20 @@
//most commonly entered words, printing all in the case of a tie. //most commonly entered words, printing all in the case of a tie.
#include <iostream> #include <iostream>
#include <string>
#include <cstdlib>
#include "PG3.h" #include "PG3.h"
#include "List.h" #include "List.h"
using namespace std; using namespace std;
int main(int argc, char **argv){ int main(int argc, char **argv) {
string input =" "; string input = " ";
List * list = new List(); List *list = new List();
while (input!=""){ while (input != "") {
cout << "Input<<"; cout << "Input<<";
getline(cin, input); getline(cin, input);
if(input!="")list->add(input); if (input != "")list->add(input);
} }
list->print(); list->print();
delete list; delete list;
return 0; return 0;
} }

View File

@ -7,6 +7,7 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <cstdlib> #include <cstdlib>
using namespace std; using namespace std;
int main(int argc, char **argv);// Main method. int main(int argc, char **argv);// Main method.

View File

@ -2,38 +2,40 @@
//List.cpp defines the methods for a List //List.cpp defines the methods for a List
#include <iostream> #include <iostream>
#include <string>
#include <cstdlib> #include <cstdlib>
#include "List.h" #include "List.h"
#include "Node.h" #include "Node.h"
#include "PG4.h" #include "PG4.h"
using namespace std; using namespace std;
List::List(){ List::List() {
head = NULL; 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);
} }
void List::print(){ List::~List() {
delete head;
cout << endl << "Library List" << endl << endl;
if (head) head->print();
cout << endl;
} }
void List::remove(string s){ void List::add(string s, string l) {
if (head) head = head->remove(s, NULL, this); 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; }

View File

@ -8,19 +8,23 @@
#include <string> #include <string>
#include <cstdlib> #include <cstdlib>
#include "Node.h" #include "Node.h"
using namespace std; using namespace std;
class Node; class Node;
class List{
class List {
private: // Each list contains only the pointer to its head Node. private: // Each list contains only the pointer to its head Node.
Node *head; Node *head;
public: public:
List(); // List Constructor List(); // List Constructor
~List(); // List Destructor ~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 add(string s,
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. string l); // Adds a Node to the tail of the list, incrementing the counter if a duplicate string is entered.
void remove(string s); // Calls remove on the nodes that match string s void
void sethead(Node* n); // Head mutator 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 #endif

View File

@ -2,51 +2,58 @@
//Node.cpp contains the code for each node //Node.cpp contains the code for each node
#include <iostream> #include <iostream>
#include <string>
#include <cstdlib> #include <cstdlib>
#include <cctype>
#include "Node.h" #include "Node.h"
#include "List.h" #include "List.h"
#include "PG4.h" #include "PG4.h"
using namespace std; using namespace std;
Node::Node(string s, Node *n){ Node::Node(string s, Node *n) {
value = s; value = s;
next = n; 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;
} }
void Node::print(){ Node::~Node() { delete next; }
cout << value << endl;
if (next) next->print(); 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){ void Node::print() {
string lvalue = converttolower(value); cout << value << endl;
if (next) next = next->remove(s, this, l); if (next) next->print();
if (lvalue.find(s) < value.length() && lvalue.find(s) >= 0){ }
Node *t = next;
next = NULL; Node *Node::remove(string s, Node *prev, List *l) {
delete this; string lvalue = converttolower(value);
return t; if (next) next = next->remove(s, this, l);
} if (lvalue.find(s) < value.length() && lvalue.find(s) >= 0) {
return this; Node *t = next;
next = NULL;
delete this;
return t;
}
return this;
} }

View File

@ -8,24 +8,27 @@
#include <string> #include <string>
#include <cstdlib> #include <cstdlib>
#include "List.h" #include "List.h"
using namespace std; using namespace std;
class List; class List;
class Node{
class Node {
private: // Each Node contains a value, count, and a pointer to the next Node in the list. private: // Each Node contains a value, count, and a pointer to the next Node in the list.
string value; string value;
int cnt; int cnt;
Node *next; Node *next;
public: public:
Node(string s, Node *n); // Node Constructor Node(string s, Node *n); // Node Constructor
~Node(); // Node Destructor ~Node(); // Node Destructor
string getvalue(); // Value accessor string getvalue(); // Value accessor
int getcount(); // Count accessor int getcount(); // Count accessor
Node * getnext(); // Next pointer accessor Node *getnext(); // Next pointer accessor
void setnext(Node* n); // Next pointer mutator 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. Node *add(string s,
void print(); // Tells each node to print itself and its next string l); // Adds a new Node to a list, incrementing the count for a Node if the same string is entered more than once.
Node* remove(string s, Node *prev, List *l); // Remove the nodes with matching string s and returns the new head 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 #endif

View File

@ -2,39 +2,43 @@
//This program manages a library of books by title, stored in a linked list //This program manages a library of books by title, stored in a linked list
#include<iostream> #include<iostream>
#include<string>
#include<cstdlib>
#include<cctype> #include<cctype>
#include "PG4.h" #include "PG4.h"
#include "List.h" #include "List.h"
using namespace std; using namespace std;
int main(int argc, char **argv){ int main(int argc, char **argv) {
string input = " "; string input = " ";
List * list = new List(); List *list = new List();
cout << "cLibrary Version 1.0 -- type help for a list of commands" << endl; cout << "cLibrary Version 1.0 -- type help for a list of commands" << endl;
while (converttolower(input).substr(0,4) != "exit"){ while (converttolower(input).substr(0, 4) != "exit") {
cout << "Enter command: "; cout << "Enter command: ";
getline(cin, input); getline(cin, input);
string lower = converttolower(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, 3) == "add")
if (lower.substr(0, 6) == "remove") list->remove(lower.substr(8, input.length() - 9)); list->add(input.substr(5, input.length() - 6), lower.substr(5, input.length() - 6));
if (lower == "print") list->print(); if (lower.substr(0, 6) == "remove") list->remove(lower.substr(8, input.length() - 9));
if (list && lower == "clear") {delete list; List * list = new List();} if (lower == "print") list->print();
if (lower.substr(0,4) == "help" || lower.substr(0,4) == "halp"){ if (list && lower == "clear") {
cout << endl << "These are the commands that this program accepts: " << endl << endl; delete list;
cout << "add \"book title\" \t--adds a new entry with book title as the name" << endl; List *list = new List();
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; if (lower.substr(0, 4) == "help" || lower.substr(0, 4) == "halp") {
cout << "clear \t\t\t--clears the entire library" << endl; cout << endl << "These are the commands that this program accepts: " << endl << endl;
cout << "exit \t\t\t--exits the program (also clears the library)" << 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;
delete list; cout << "clear \t\t\t--clears the entire library" << endl;
return 0; cout << "exit \t\t\t--exits the program (also clears the library)" << endl << endl;
}
}
delete list;
return 0;
} }
string converttolower(string s){ string converttolower(string s) {
for (int i = 0; i < (int) s.length(); i++) s[i] = tolower(s[i]); return s; for (int i = 0; i < (int) s.length(); i++) s[i] = tolower(s[i]);
return s;
} }

View File

@ -7,6 +7,7 @@
#include<iostream> #include<iostream>
#include<string> #include<string>
#include<cstdlib> #include<cstdlib>
using namespace std; using namespace std;
int main(int argc, char **argv); // Main... int main(int argc, char **argv); // Main...

BIN
PG4/pg4

Binary file not shown.

View File

@ -8,46 +8,52 @@
#include <string> #include <string>
#include <cstdlib> #include <cstdlib>
#include "ArrayLLN.h" #include "ArrayLLN.h"
using namespace std; using namespace std;
template <class T> class ArrayLL{ template<class T>
class ArrayLL {
private: private:
ArrayLLN<T> * head; ArrayLLN<T> *head;
public: public:
ArrayLL(){ head = nullptr; } ArrayLL() { head = nullptr; }
~ArrayLL(){ delete head; }
void sethead(ArrayLLN<T>* t){ head = t; }
int length(){ // returns length of the list ~ArrayLL() { delete head; }
int cnt = 0;
for (ArrayLLN<T>*P = head; P != nullptr; cnt++, P = P->getnext()); void sethead(ArrayLLN<T> *t) { head = t; }
return cnt;
} int length() { // returns length of the list
T remove(int pos){ // removes the node at pos int cnt = 0;
ArrayLLN<T> *P = head, *Q = nullptr; for (ArrayLLN<T> *P = head; P != nullptr; cnt++, P = P->getnext());
for (int cnt = 0; cnt < pos; cnt++, Q = P, P = P->getnext()); return cnt;
T tempcontents = P->getcontents(); }
P->removeself(P, Q, this);
return tempcontents; T remove(int pos) { // removes the node at pos
} ArrayLLN<T> *P = head, *Q = nullptr;
T& operator[] (const int pos){ // overloads [], which returns a reference to the node at pos for (int cnt = 0; cnt < pos; cnt++, Q = P, P = P->getnext());
ArrayLLN<T>* P = head; T tempcontents = P->getcontents();
for (int cnt = 0; cnt < pos; cnt++, P = P->getnext()); P->removeself(P, Q, this);
return P->getcontents(); return tempcontents;
} }
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 T &operator[](const int pos) { // overloads [], which returns a reference to the node at pos
else if (pos > 0 && pos < length()){ // if inserting not at the head or tail ArrayLLN<T> *P = head;
ArrayLLN<T> *P = head, *Q = nullptr; for (int cnt = 0; cnt < pos; cnt++, P = P->getnext());
for (int i = 0; i < pos; i++, Q = P, P = P->getnext()); return P->getcontents();
if (Q) Q->setnext(new ArrayLLN<T>(stuff, P)); }
else P->setnext(new ArrayLLN<T>(stuff, P->getnext()));
} void insert(int pos, T stuff) { // inserts a node at pos with stuff contents
else if (pos == length()) // if inserting at the tail. if (pos == 0) head = new ArrayLLN<T>(stuff, head); // insert at head, with head as next pointer
if (head == NULL) head = new ArrayLLN<T>(stuff, NULL); else if (pos > 0 && pos < length()) { // if inserting not at the head or tail
else head->addback(stuff); ArrayLLN<T> *P = head, *Q = nullptr;
else return; 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;
}
}; };

View File

@ -9,38 +9,43 @@
#include <cstdlib> #include <cstdlib>
#include "PG5.h" #include "PG5.h"
#include "ArrayLL.h" #include "ArrayLL.h"
using namespace std; using namespace std;
template <class T> class ArrayLL; template<class T>
class ArrayLL;
template <class T> class ArrayLLN{ template<class T>
class ArrayLLN {
private: private:
T contents; T contents;
ArrayLLN<T> *next; ArrayLLN<T> *next;
public: 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 t, ArrayLLN<T> *n) { // Constructor
ArrayLLN<T>*temp = curr->getnext(); contents = t;
if (prev) prev->setnext(temp); next = n;
else l->sethead(temp); }
curr->setnext(nullptr);
delete curr;
}
void addback(T stuff){ // adds new node at the end of the list. ~ArrayLLN() { delete next; } // Destructor
if (!next) next = new ArrayLLN<T>(stuff, nullptr); void setcontents(T t) { contents = t; } // contents mutator
else next->addback(stuff); 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);
}
}; };

View File

@ -4,69 +4,72 @@
// The ArrayLL template interfaces with the linkedlist as if it were an array. // The ArrayLL template interfaces with the linkedlist as if it were an array.
#include <iostream> #include <iostream>
#include <string>
#include <cstdlib>
#include "PG5.h" #include "PG5.h"
#include "ArrayLL.h" #include "ArrayLL.h"
using namespace std; using namespace std;
int main(int argc, char **argv){ // PG5 main int main(int argc, char **argv) { // PG5 main
string input = ""; string input = "";
cout << "cLibrary Version 2.0 -- type help for a list of commands" << endl; cout << "cLibrary Version 2.0 -- type help for a list of commands" << endl;
ArrayLL<string> list; ArrayLL<string> 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: "; cout << "Enter command: ";
getline(cin, input); getline(cin, input);
string lower = converttolower(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; bool found = false;
string linputstr = converttolower(input.substr(5, input.length() - 6)); string linputstr = converttolower(input.substr(5, input.length() - 6));
for (int i = 0; i < list.length(); i++) // Make sure no duplicates are entered for (int i = 0; i < list.length(); i++) // Make sure no duplicates are entered
if (converttolower(list[i]) == linputstr) if (converttolower(list[i]) == linputstr)
found = true; found = true;
int j = 0; int j = 0;
for (j = 0; j < list.length(); j++) // Find where in the list that it needs to be inserted for (j = 0; j < list.length(); j++) // Find where in the list that it needs to be inserted
if (converttolower(list[j]) > linputstr) if (converttolower(list[j]) > linputstr)
break; break;
if (!found) list.insert(j, input.substr(5, input.length() - 6));
} 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++){ if (lower.substr(0, 5) == "print") { // prints the entire list, which should be in alphabetical order
string ll = converttolower(list[i]); cout << endl;
if (ll.find(lv) >= 0 && ll.find(lv) < ll.length()){ for (int i = 0; i < list.length(); i++)
cout << "\"" << list.remove(i) << "\"" << " was removed from the library." << endl; cout << list[i] << endl;
i--; 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);
if (lower.substr(0, 4) == "help" || lower.substr(0, 4) == "halp"){ // prints info for each function for (int i = 0; i < list.length(); i++) {
cout << endl << "These are the commands that this program accepts: " << endl << endl; string ll = converttolower(list[i]);
cout << "add \"book title\" \t--adds a new entry with book title as the name" << endl; if (ll.find(lv) >= 0 && ll.find(lv) < ll.length()) {
cout << "remove \"search term\" \t--deletes all books that contain search term" << endl; cout << "\"" << list.remove(i) << "\"" << " was removed from the library." << endl;
cout << "print \t\t\t--prints out the entire library in alphabetical order" << endl; i--;
cout << "exit \t\t\t--exits the program (also clears the library)" << endl << endl; }
} }
} }
return 0;
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;
}

View File

@ -8,6 +8,7 @@
#include<string> #include<string>
#include<cstdlib> #include<cstdlib>
#include "ArrayLL.h" #include "ArrayLL.h"
using namespace std; using namespace std;
int main(int argc, char **argv); // Main... int main(int argc, char **argv); // Main...

View File

@ -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. // 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 <iostream> #include <iostream>
#include <string>
#include <cstdlib> #include <cstdlib>
#include <algorithm>
#include <fstream> #include <fstream>
#include "PG6.h" #include "PG6.h"
using namespace std; using namespace std;
int main(int argc, char **argv){ int main(int argc, char **argv) {
string fn = ""; string fn = "";
streampos fsz; streampos fsz;
cout << "Enter the name of the file that you want to sort." << endl << "File Name: "; cout << "Enter the name of the file that you want to sort." << endl << "File Name: ";
getline(cin, fn); // Get file name from user 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 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()){ if (f.is_open()) {
char * recordsz = new char[4]; char *recordsz = new char[4];
f.read(recordsz, (streamsize) 4); // Read first four bytes f.read(recordsz, (streamsize) 4); // Read first four bytes
int rsz = atoi(recordsz); // Store first four bytes as int record size int rsz = atoi(recordsz); // Store first four bytes as int record size
f.seekg(0, f.end); f.seekg(0, f.end);
fsz = f.tellg(); // Get length of file fsz = f.tellg(); // Get length of file
int recordcnt = ((int)fsz-4) / rsz; // Get # of records int recordcnt = ((int) fsz - 4) / rsz; // Get # of records
f.seekg(0, f.beg); f.seekg(0, f.beg);
// Begin shell sort // Begin shell sort
int d = recordcnt; int d = recordcnt;
while (d > 1){ while (d > 1) {
d = (d == 2) ? 1 : d % 2 == 1 ? (d + 1) / 2 : d % 4 == 0 ? d / 2 + 1 : d / 2 + 2; 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 i = d;
for (int j = i - d; j >= 0 && readstring(f,j,rsz) > readstring(f,j + d,rsz); j -= d){ i < recordcnt; i++) // Use readstring and writestring methods to get and put strings in the file
string t = readstring(f,j,rsz); for (int j = i - d; j >= 0 && readstring(f, j, rsz) > readstring(f, j + d, rsz); j -= d) {
writestring(f, j, rsz, readstring(f,j + d,rsz)); string t = readstring(f, j, rsz);
writestring(f, j+d, rsz, t); writestring(f, j, rsz, readstring(f, j + d, rsz));
} writestring(f, j + d, rsz, t);
} }
cout << "Sort successful." << endl; }
f.close(); cout << "Sort successful." << endl;
} f.close();
else cout << "Error: file could not be opened or does not exist." << endl; } else cout << "Error: file could not be opened or does not exist." << endl;
system("pause");
} }
string readstring(fstream &f, int recordpos, int len){ // This method seeks to the position of recordpos * len, reads len bytes and returns that string string readstring(fstream &f, int recordpos,
char* t = new char[len]; int len) { // This method seeks to the position of recordpos * len, reads len bytes and returns that string
f.seekg(recordpos*len + 4, ios::beg); char *t = new char[len];
f.read(t, len); f.seekg(recordpos * len + 4, ios::beg);
return (string)t; 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 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); f.seekp(recordpos * len + 4, ios::beg);
char * t = new char[s.length()]; char *t = new char[s.length()];
for (unsigned int i = 0; i < s.length(); i++) t[i] = s[i]; for (unsigned int i = 0; i < s.length(); i++) t[i] = s[i];
f.write(t, len); f.write(t, len);
} }

View File

@ -9,10 +9,13 @@
#include <string> #include <string>
#include <cstdlib> #include <cstdlib>
#include <fstream> #include <fstream>
using namespace std; using namespace std;
int main(int argc, char **argv); int main(int argc, char **argv);
string readstring(fstream &f, int recordpos, int len); string readstring(fstream &f, int recordpos, int len);
void writestring(fstream &f, int recordpos, int len, string s); void writestring(fstream &f, int recordpos, int len, string s);
#endif #endif