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,11 +1,10 @@
//Ben Harris
//This program converts decimal to dozenal.
#include <iostream> //import
#include <string>
#include <iostream>
#include <cstdlib>
#include <sstream>
#include "PG1.h"
using namespace std;
//main method runs at execution time
@ -14,9 +13,9 @@ int main (int argc, char **argv) { //signature
string input;
getline(cin, input);
cout << input << " in dozenal is " << dozenal(input) << endl;
system ("pause");
return 0;
}
//this method divides by 12 and returns the quotient, to be stored
string div12(string dividend, int &remainder) {
@ -33,8 +32,7 @@ string div12 (string dividend, int &remainder) {
quotient = quotient + c;
save = save % 12;
}
else {
} else {
save = dividend[i] - '0';
quotient += '0';
}
@ -42,6 +40,7 @@ string div12 (string dividend, int &remainder) {
remainder = save;
return quotient;
}
//this method calls the div12 method and stores each remainders as the next digit
string dozenal(string input) {

View File

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

View File

@ -3,13 +3,13 @@
//then checks for poker hands excluding straight, flush, and straight flush
#include <iostream>
#include <string>
#include <cstdlib>
#include "time.h"
#include "PG2.h"
using namespace std;
int main (int argc, char **argv){//main method
int main(int argc, char **argv) {
// initialize deck and randomize it
srand((unsigned) time(0));
int *deck = new int[52];
@ -48,26 +48,33 @@ string getSuitName(int card){//returns the suit of the input card
string suits[4] = {"Hearts", "Clubs", "Diamonds", "Spades"};
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 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];
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++) {
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++) {
if (toinsert == d[i])return true;
}return false;
}
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++) {

View File

@ -7,13 +7,21 @@
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int main(int argc, char **argv);
string getSuitName(int card);
string getRankName(int card);
void swapTwo(int a, int b, int *d);
void shuffle(int *d);
bool inArray(int *d, int toinsert, int sz);
bool dupeinarray(int *d, int dupetocheck, int sz);
#endif

View File

@ -2,22 +2,25 @@
//List.cpp defines the methods for a List
#include <iostream>
#include <string>
#include <cstdlib>
#include "List.h"
#include "Node.h"
using namespace std;
List::List() {
head = NULL;
}
List::~List() {
delete head;
}
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;
@ -30,5 +33,6 @@ void List::print(){
while (n != NULL) {
if (n->getcount() == maxcnt) cout << n->getvalue() << endl;
n = n->getnext();
}cout << endl;
}
cout << endl;
}

View File

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

View File

@ -2,9 +2,9 @@
//Node.cpp contains the code for each node
#include <iostream>
#include <string>
#include <cstdlib>
#include "Node.h"
using namespace std;
Node::Node(string s, Node *n) {
@ -12,20 +12,25 @@ Node::Node(string s, Node *n){
value = s;
next = n;
}
Node::~Node() {
delete next;
cnt = 0;
//farewell
}
string Node::getvalue() {
return value;
}
int Node::getcount() {
return cnt;
}
Node *Node::getnext() {
return next;
}
void Node::add(string s) {
if (value == s)cnt++;
else if (next == NULL) next = new Node(s, NULL);

View File

@ -7,6 +7,7 @@
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class Node {
@ -20,7 +21,8 @@ public:
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.
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

View File

@ -3,10 +3,9 @@
//most commonly entered words, printing all in the case of a tie.
#include <iostream>
#include <string>
#include <cstdlib>
#include "PG3.h"
#include "List.h"
using namespace std;
int main(int argc, char **argv) {

View File

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

View File

@ -2,19 +2,21 @@
//List.cpp defines the methods for a List
#include <iostream>
#include <string>
#include <cstdlib>
#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;

View File

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

View File

@ -2,22 +2,26 @@
//Node.cpp contains the code for each node
#include <iostream>
#include <string>
#include <cstdlib>
#include <cctype>
#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) {
@ -27,7 +31,10 @@ Node * Node::add(string s, string l){
if (lvalue == l) return this;
if (l < lvalue) return new Node(s, this);
if (!next){ next = new Node(s, NULL); return this; }
if (!next) {
next = new Node(s, NULL);
return this;
}
next = next->add(s, l);
return this;

View File

@ -8,9 +8,11 @@
#include <string>
#include <cstdlib>
#include "List.h"
using namespace std;
class List;
class Node {
private: // Each Node contains a value, count, and a pointer to the next Node in the list.
string value;
@ -23,7 +25,8 @@ public:
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.
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
};

View File

@ -2,11 +2,10 @@
//This program manages a library of books by title, stored in a linked list
#include<iostream>
#include<string>
#include<cstdlib>
#include<cctype>
#include "PG4.h"
#include "List.h"
using namespace std;
int main(int argc, char **argv) {
@ -18,10 +17,14 @@ int main(int argc, char **argv){
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, 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 (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;
@ -36,5 +39,6 @@ int main(int argc, char **argv){
}
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<string>
#include<cstdlib>
using namespace std;
int main(int argc, char **argv); // Main...

BIN
PG4/pg4

Binary file not shown.

View File

@ -8,14 +8,18 @@
#include <string>
#include <cstdlib>
#include "ArrayLLN.h"
using namespace std;
template <class T> class ArrayLL{
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
@ -23,6 +27,7 @@ public:
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());
@ -30,11 +35,13 @@ public:
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
@ -42,8 +49,7 @@ public:
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.
} 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,11 +9,14 @@
#include <cstdlib>
#include "PG5.h"
#include "ArrayLL.h"
using namespace std;
template <class T> class ArrayLL;
template<class T>
class ArrayLL;
template <class T> class ArrayLLN{
template<class T>
class ArrayLLN {
private:
T contents;
ArrayLLN<T> *next;
@ -23,13 +26,15 @@ public:
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
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);

View File

@ -4,10 +4,9 @@
// 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
@ -22,7 +21,8 @@ int main(int argc, char **argv){ // PG5 main
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));
@ -69,4 +69,7 @@ int main(int argc, char **argv){ // PG5 main
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<cstdlib>
#include "ArrayLL.h"
using namespace std;
int main(int argc, char **argv); // Main...

View File

@ -3,11 +3,10 @@
// 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 <string>
#include <cstdlib>
#include <algorithm>
#include <fstream>
#include "PG6.h"
using namespace std;
int main(int argc, char **argv) {
@ -30,7 +29,8 @@ int main(int argc, char **argv){
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 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));
@ -39,12 +39,11 @@ int main(int argc, char **argv){
}
cout << "Sort successful." << endl;
f.close();
}
else cout << "Error: file could not be opened or does not exist." << endl;
system("pause");
} 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
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);

View File

@ -9,10 +9,13 @@
#include <string>
#include <cstdlib>
#include <fstream>
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