diff --git a/cs235/exam1/Makefile b/cs235/exam1/Makefile new file mode 100644 index 0000000..ded616d --- /dev/null +++ b/cs235/exam1/Makefile @@ -0,0 +1,26 @@ +CXXFLAGS= -Wall -g -std=c++0x +OBJECTS=main.o node.o polylist.o polyman.o +EXE=main + +all: $(EXE) + + +$(EXE): main.o + $(CXX) $(CXXFLAGS) $(OBJECTS) -o $@ +main.o: main.cpp node.o polylist.o polyman.o +polylist.o: polylist.cpp polylist.h +polyman.o: polyman.cpp polyman.h +node.o: node.cpp node.h + +run: $(EXE) + @./$(EXE) + +clean: + @rm -vf *.o + @rm -vf $(EXE) + +drun: $(EXE) + gdb ./$(EXE) + +valgrind: $(EXE) + valgrind --tool=memcheck --leak-check=yes ./$(EXE) diff --git a/cs235/exam1/PolynomialListInterface.h b/cs235/exam1/PolynomialListInterface.h new file mode 100644 index 0000000..e440187 --- /dev/null +++ b/cs235/exam1/PolynomialListInterface.h @@ -0,0 +1,68 @@ +//YOU MAY NOT MODIFY THIS DOCUMENT +#pragma once +#include + +using namespace std; + +class PolynomialListInterface +{ + +public: + /* + * You must create a Node class that will hold each polynomial segment and will be used to create the + * links in your PolynomialList. + * Your Node will likely have 3 private variables in addition to a Node * next you may have. + * These 3 variables will be for the Coefficient, the Variable, and the Exponent respectively. + * i.e. 2x^7 + * Coefficient = 2 + * Variable = x + * Exponent = 7 + */ + PolynomialListInterface(void){} + virtual ~PolynomialListInterface(void){} + + /* + insert + + A node with the given term should be inserted at the appropriate spot in the list. + A term can be accepted only with Coefficient value > 0, and exponent value non-negative. + If the term is invalid, do not add it to the list. + i.e. If the given term segment is 3x^5 and your list contains 2x^8, x^4, 11x^2 + the node should be added after 2x^8. Your updated list should look like 2x^8, 3x^5, x^4, 11x^2. + + */ + virtual void insert(string term) = 0; + + /* + clear + + Remove all nodes from the list. + */ + virtual void clear() = 0; + + /* + at + + Returns the polynomial of the node at the given index. The list begins at + index 0. + + If the given index is out of range of the list, return "invalid"; + */ + virtual string at(int index) = 0; + + /* + size + + Returns the number of nodes in the list. + */ + virtual int size() = 0; + + /* + * This function should string together all of the nodes and print out the entire polynomial separated with '+' signs. + * If an exponent of 0 exists however, you should print only the Coefficient. + * i.e. list of Nodes = (2x^4)->(4x^2)->(3x^1)->(11x^0) + * printList() = return "2 x ^ 4 + 4 x ^ 2 + 3 x ^ 1 + 11 x"; + */ + virtual string printList() = 0; + +}; diff --git a/cs235/exam1/PolynomialManagerInterface.h b/cs235/exam1/PolynomialManagerInterface.h new file mode 100644 index 0000000..ce14d09 --- /dev/null +++ b/cs235/exam1/PolynomialManagerInterface.h @@ -0,0 +1,84 @@ +//YOU MAY NOT MODIFY THIS DOCUMENT +#ifndef POLYNOMIALMANAGERINTERFACE_H_ +#define POLYNOMIALMANAGERINTERFACE_H_ +#include "PolynomialListInterface.h" +#include + +using namespace std; + +class PolynomialManagerInterface { +public: + PolynomialManagerInterface(){} + virtual ~PolynomialManagerInterface(){} + + /* + * addLists() + * This function goes through the process of adding your two list's together and returning the resulting list. + * Like terms should be added together correctly, creating one Node for the final list. + * If both of the lists are empty, return an empty list. This list must be sorted in correct polynomial order by degree. + * i.e. + * (2x^4)->(4x^2)->(3x^1)->(11x^0), not (2x^4)->(3x^1)->(11x^0)->(4x^2) or anything else. + */ + virtual PolynomialListInterface * addLists() = 0; + + /* + * subtractLists() + * This function goes through the process of subtracting your list two from your list one and returning the resulting list. + * Like terms should be subtracted correctly, creating one Node for the final list. If a resulting coefficient is 0, that node should be removed. + * If both of the lists are empty, return an empty list. This list must be sorted in correct polynomial order by degree. + * i.e. + * (2x^4)->(4x^2)->(3x^1)->(11x^0), not (2x^4)->(3x^1)->(11x^0)->(4x^2) or anything else. + */ + virtual PolynomialListInterface * subtractLists() = 0; + + /* + * fillListOne(string term) + * This adds a single term at a time. + * This function will be called many times while your first list is being filled. + * You should add the new term at the appropriate spot in the list + * which may involve adding it to an existing polynomial list, the list should be ordered by degrees. + * All segments will have at least a variable and an exponent, however the Coefficient may be an implied 1. + * i.e. x^6 is valid, but 2x is not. + * If the term is not valid, it should not be added to the list. + * + */ + virtual void fillListOne(string term) = 0; + + /* + * fillListTwo(string term) + * This adds a single term at a time. + * This function will be called many times while your second list is being filled. + * You should add the new term at the appropriate spot in the list + * which may involve adding it to an existing polynomial list, the list should be ordered by degrees. + * All segments will have at least a variable and an exponent, however the Coefficient may be an implied 1. + * i.e. x^6 is valid, but 2x is not. + * If the term is not valid, it should not be added to the list. + */ + virtual void fillListTwo(string term) = 0; + + /* + * clearListOne() + * This function should empty your first list entirely. + */ + virtual void clearListOne() = 0; + + /* + * clearListTwo() + * This function should empty your second list entirely. + */ + virtual void clearListTwo() = 0; + + /* + * getListOne() + * Returns a pointer to the first list. + */ + virtual PolynomialListInterface * getListOne() = 0; + + /* + * getListTwo() + * Returns a pointer to the second list. + */ + virtual PolynomialListInterface * getListTwo() = 0; +}; + +#endif /* POLYNOMIALMANAGERINTERFACE_H_ */ diff --git a/cs235/exam1/main.cpp b/cs235/exam1/main.cpp new file mode 100644 index 0000000..39d62d6 --- /dev/null +++ b/cs235/exam1/main.cpp @@ -0,0 +1,433 @@ +#include +#include +#include +#include +#include "node.h" +#include "polylist.h" +#include "polyman.h" + +using namespace std; + +int main() { +//node::node(const int exponent, const int coefficient, const char variable, node* next) : + node n(0,0,'x',NULL); + node n1(0,1,'x',NULL); + node n2(1,0,'x',NULL); + node n3(1,1,'x',NULL); + node n4(1,'x',NULL); + node n5(0,'x',NULL); + node n6(1,'x',NULL); + + stringstream s; + + s << n; + assert(s.str() == ""); + s.str(""); + + s << n1; + assert(s.str() == "1"); + s.str(""); + + s << n2; + assert(s.str() == ""); + s.str(""); + + s << n3; + assert(s.str() == "x"); + s.str(""); + + string a1 = "1 x ^ 1"; + vector v = parse_expression(a1); + assert(v[0] == "1"); + assert(v[1] == "x"); + assert(v[3] == "1"); + assert(is_valid(v) == true); + + string a2 = "123 g ^ 152"; + v = parse_expression(a2); + assert(is_valid(v) == true); + + string a3 = "a b c"; + v = parse_expression(a3); + assert(is_valid(v) == false); + + string a4 = ""; + v = parse_expression(a4); + assert(is_valid(v) == false); + + string a5 = "1 x"; + v = parse_expression(a5); + assert(is_valid(v) == true); + + string a6 = "x"; + v = parse_expression(a6); + assert(is_valid(v) == true); + + string a7 = "morethanonecharacter"; + v = parse_expression(a7); + assert(is_valid(v) == false); + + string a8 = "$"; + v = parse_expression(a8); + assert(is_valid(v) == false); + + string a9 = "1 & ^ x"; + v = parse_expression(a9); + assert(is_valid(v) == false); + + string b1 = "123 g ^ %"; + v = parse_expression(b1); + assert(is_valid(v) == false); + + string b2 = "1r32 g ^ 152"; + v = parse_expression(b2); + assert(is_valid(v) == false); + + string b3 = "123 ^ 152"; + v = parse_expression(b3); + assert(is_valid(v) == false); + + string b4 = "123 g ^"; + v = parse_expression(b4); + assert(is_valid(v) == false); + + string b5 = "5 x ^ -2"; + v = parse_expression(b5); + assert(is_valid(v) == false); + + polylist p; + stringstream sr; + + p.insert("x"); + sr << p.printList(); + assert(sr.str() == "1"); + + p.insert("2 x"); + sr.str(""); + sr << p.printList(); + assert(sr.str() == "3"); + + p.insert("-5 x"); + sr.str(""); + sr << p.printList(); + assert(sr.str() == "-2"); + + assert(p.size() == 1); + + p.insert("x ^ 1"); + sr.str(""); + sr << p.printList(); + assert(sr.str() == "x + -2"); + + assert(p.size() == 2); + + p.insert("x ^ 4"); + sr.str(""); + sr << p.printList(); + assert(sr.str() == "x ^ 4 + x + -2"); + + + assert(p.size() == 3); + + p.insert("x ^ 5"); + sr.str(""); + sr << p.printList(); + assert(sr.str() == "x ^ 5 + x ^ 4 + x + -2"); + + p.insert("x ^ -1"); + sr.str(""); + sr << p.printList(); + assert(sr.str() == "x ^ 5 + x ^ 4 + x + -2"); + + p.insert("x ^ 4"); + sr.str(""); + sr << p.printList(); + assert(sr.str() == "x ^ 5 + 2 x ^ 4 + x + -2"); + + p.insert("x ^ 4"); + sr.str(""); + sr << p.printList(); + assert(sr.str() == "x ^ 5 + 3 x ^ 4 + x + -2"); + + p.insert("x ^ 4"); + sr.str(""); + sr << p.printList(); + assert(sr.str() == "x ^ 5 + 4 x ^ 4 + x + -2"); + + p.insert("x ^ 5"); + sr.str(""); + sr << p.printList(); + assert(sr.str() == "2 x ^ 5 + 4 x ^ 4 + x + -2"); + + + assert(p.size() == 4); + + p.insert("x ^ 1"); + sr.str(""); + sr << p.printList(); + assert(sr.str() == "2 x ^ 5 + 4 x ^ 4 + 2 x + -2"); + + p.insert("x"); + sr.str(""); + sr << p.printList(); + assert(sr.str() == "2 x ^ 5 + 4 x ^ 4 + 2 x + -1"); + + p.insert("x ^ 7"); + sr.str(""); + sr << p.printList(); + assert(sr.str() == "x ^ 7 + 2 x ^ 5 + 4 x ^ 4 + 2 x + -1"); + + p.insert("x ^ -7"); + sr.str(""); + sr << p.printList(); + assert(sr.str() == "x ^ 7 + 2 x ^ 5 + 4 x ^ 4 + 2 x + -1"); + + p.insert("x ^ 7"); + sr.str(""); + sr << p.printList(); + assert(sr.str() == "2 x ^ 7 + 2 x ^ 5 + 4 x ^ 4 + 2 x + -1"); + + assert(p.size() == 5); + + p.insert("2 x ^ 3"); + sr.str(""); + sr << p.printList(); + assert(sr.str() == "2 x ^ 7 + 2 x ^ 5 + 4 x ^ 4 + 2 x ^ 3 + 2 x + -1"); + + + p.insert("2 x ^ 3"); + sr.str(""); + sr << p.printList(); + assert(sr.str() == "2 x ^ 7 + 2 x ^ 5 + 4 x ^ 4 + 4 x ^ 3 + 2 x + -1"); + + + p.insert("2 x ^ 2"); + sr.str(""); + sr << p.printList(); + assert(sr.str() == "2 x ^ 7 + 2 x ^ 5 + 4 x ^ 4 + 4 x ^ 3 + 2 x ^ 2 + 2 x + -1"); + + p.insert("2 x ^ 7"); + sr.str(""); + sr << p.printList(); + assert(sr.str() == "4 x ^ 7 + 2 x ^ 5 + 4 x ^ 4 + 4 x ^ 3 + 2 x ^ 2 + 2 x + -1"); + + p.insert("2 x ^ 0"); + sr.str(""); + sr << p.printList(); + assert(sr.str() == "4 x ^ 7 + 2 x ^ 5 + 4 x ^ 4 + 4 x ^ 3 + 2 x ^ 2 + 2 x + 1"); + + p.insert("-4 x ^ 4"); + sr.str(""); + sr << p.printList(); + assert(sr.str() == "4 x ^ 7 + 2 x ^ 5 + 4 x ^ 3 + 2 x ^ 2 + 2 x + 1"); + + p.insert("2 x ^ 3"); + sr.str(""); + sr << p.printList(); + assert(sr.str() == "4 x ^ 7 + 2 x ^ 5 + 6 x ^ 3 + 2 x ^ 2 + 2 x + 1"); + + p.insert("8 x ^ 4"); + sr.str(""); + sr << p.printList(); + assert(sr.str() == "4 x ^ 7 + 2 x ^ 5 + 8 x ^ 4 + 6 x ^ 3 + 2 x ^ 2 + 2 x + 1"); + + p.insert("2 x ^ -3"); + sr.str(""); + sr << p.printList(); + assert(sr.str() == "4 x ^ 7 + 2 x ^ 5 + 8 x ^ 4 + 6 x ^ 3 + 2 x ^ 2 + 2 x + 1"); + + p.insert("-1 x ^ 3"); + sr.str(""); + sr << p.printList(); + assert(sr.str() == "4 x ^ 7 + 2 x ^ 5 + 8 x ^ 4 + 5 x ^ 3 + 2 x ^ 2 + 2 x + 1"); + + p.insert("-2 x ^ 7"); + sr.str(""); + sr << p.printList(); + assert(sr.str() == "2 x ^ 7 + 2 x ^ 5 + 8 x ^ 4 + 5 x ^ 3 + 2 x ^ 2 + 2 x + 1"); + + p.insert("2 x ^ 9"); + sr.str(""); + sr << p.printList(); + assert(sr.str() == "2 x ^ 9 + 2 x ^ 7 + 2 x ^ 5 + 8 x ^ 4 + 5 x ^ 3 + 2 x ^ 2 + 2 x + 1"); + + p.insert("-2 x ^ 9"); + sr.str(""); + sr << p.printList(); + assert(sr.str() == "2 x ^ 7 + 2 x ^ 5 + 8 x ^ 4 + 5 x ^ 3 + 2 x ^ 2 + 2 x + 1"); + + p.insert("10 x ^ 7"); + sr.str(""); + sr << p.printList(); + assert(sr.str() == "12 x ^ 7 + 2 x ^ 5 + 8 x ^ 4 + 5 x ^ 3 + 2 x ^ 2 + 2 x + 1"); + + p.insert("x"); + sr.str(""); + sr << p.printList(); + assert(sr.str() == "12 x ^ 7 + 2 x ^ 5 + 8 x ^ 4 + 5 x ^ 3 + 2 x ^ 2 + 2 x + 2"); + + p.insert("x ^ 7"); + sr.str(""); + sr << p.printList(); + assert(sr.str() == "13 x ^ 7 + 2 x ^ 5 + 8 x ^ 4 + 5 x ^ 3 + 2 x ^ 2 + 2 x + 2"); + + p.insert("-10 x ^ 7"); + sr.str(""); + sr << p.printList(); + assert(sr.str() == "3 x ^ 7 + 2 x ^ 5 + 8 x ^ 4 + 5 x ^ 3 + 2 x ^ 2 + 2 x + 2"); + + p.insert("10 x"); + sr.str(""); + sr << p.printList(); + assert(sr.str() == "3 x ^ 7 + 2 x ^ 5 + 8 x ^ 4 + 5 x ^ 3 + 2 x ^ 2 + 2 x + 12"); + + p.insert("-10 x"); + sr.str(""); + sr << p.printList(); + assert(sr.str() == "3 x ^ 7 + 2 x ^ 5 + 8 x ^ 4 + 5 x ^ 3 + 2 x ^ 2 + 2 x + 2"); + + p.insert("-2 x"); + sr.str(""); + sr << p.printList(); + assert(sr.str() == "3 x ^ 7 + 2 x ^ 5 + 8 x ^ 4 + 5 x ^ 3 + 2 x ^ 2 + 2 x"); + + p.insert("2 x ^ 0"); + sr.str(""); + sr << p.printList(); + assert(sr.str() == "3 x ^ 7 + 2 x ^ 5 + 8 x ^ 4 + 5 x ^ 3 + 2 x ^ 2 + 2 x + 2"); + + p.insert("x ^ 3"); + sr.str(""); + sr << p.printList(); + assert(sr.str() == "3 x ^ 7 + 2 x ^ 5 + 8 x ^ 4 + 6 x ^ 3 + 2 x ^ 2 + 2 x + 2"); + + p.insert(""); + sr.str(""); + sr << p.printList(); + assert(sr.str() == "3 x ^ 7 + 2 x ^ 5 + 8 x ^ 4 + 6 x ^ 3 + 2 x ^ 2 + 2 x + 2"); + + p.insert("-3 x ^ 7"); + sr.str(""); + sr << p.printList(); + assert(sr.str() == "2 x ^ 5 + 8 x ^ 4 + 6 x ^ 3 + 2 x ^ 2 + 2 x + 2"); + + p.insert("-2 x ^ 5"); + sr.str(""); + sr << p.printList(); + assert(sr.str() == "8 x ^ 4 + 6 x ^ 3 + 2 x ^ 2 + 2 x + 2"); + + p.insert("-2 x ^ 2"); + sr.str(""); + sr << p.printList(); + assert(sr.str() == "8 x ^ 4 + 6 x ^ 3 + 2 x + 2"); + + p.insert("-3 x ^ 7"); + sr.str(""); + sr << p.printList(); + assert(sr.str() == "-3 x ^ 7 + 8 x ^ 4 + 6 x ^ 3 + 2 x + 2"); + + p.insert("-3 x ^ 7"); + sr.str(""); + sr << p.printList(); + assert(sr.str() == "-6 x ^ 7 + 8 x ^ 4 + 6 x ^ 3 + 2 x + 2"); + + p.insert("9 x ^ 7"); + sr.str(""); + sr << p.printList(); + assert(sr.str() == "3 x ^ 7 + 8 x ^ 4 + 6 x ^ 3 + 2 x + 2"); + + p.clear(); + sr.str(""); + sr << p.printList(); + assert(sr.str() == ""); + + assert(p.size() == 0); + + p.insert("9 x ^ 7"); + sr.str(""); + sr << p.printList(); + assert(sr.str() == "9 x ^ 7"); + + assert(p.size() == 1); + + p.clear(); + sr.str(""); + sr << p.printList(); + assert(sr.str() == ""); + + assert(p.size() == 0); + + p.insert("x ^ 7"); + sr.str(""); + sr << p.printList(); + assert(sr.str() == "x ^ 7"); + + p.insert("-x ^ 7"); + sr.str(""); + sr << p.printList(); + assert(sr.str() == ""); + + p.clear(); + sr.str(""); + sr << p.printList(); + assert(sr.str() == ""); + + assert(p.size() == 0); + + p.insert("2 x"); + sr.str(""); + sr << p.printList(); + assert(sr.str() == "2"); + + p.clear(); + sr.str(""); + sr << p.printList(); + assert(sr.str() == ""); + + assert(p.size() == 0); + + p.insert("x"); + sr.str(""); + sr << p.printList(); + assert(sr.str() == "1"); + + p.remove(0); + sr.str(""); + sr << p.printList(); + assert(sr.str() == ""); + + p.insert("-x"); + sr.str(""); + sr << p.printList(); + assert(sr.str() == "-1"); + + p.insert("2 x"); + sr.str(""); + sr << p.printList(); + assert(sr.str() == "1"); + + node n7(3, -1, 'x',NULL); + + polyman m; + m.addLists(); + m.subtractLists(); + m.fillListOne("x"); + m.fillListOne("2 x"); + m.fillListOne("3 x ^ 3"); + m.fillListOne("4aqkjx"); + m.fillListOne("-4 x ^ 323"); + m.fillListOne("-2 x ^ 400"); + m.fillListOne("x"); + m.fillListOne("x"); + m.fillListOne("x"); + m.fillListOne("x"); + m.fillListTwo("-6 x"); + m.fillListTwo("-4 x ^ 323"); + m.fillListTwo("-x ^ 3"); + m.fillListTwo("7 x ^ 8"); + m.fillListTwo("4 x ^ 3"); + m.fillListTwo("-x"); + m.fillListTwo("14 x"); + m.addLists(); + m.subtractLists(); +} diff --git a/cs235/exam1/node.cpp b/cs235/exam1/node.cpp new file mode 100644 index 0000000..479b987 --- /dev/null +++ b/cs235/exam1/node.cpp @@ -0,0 +1,42 @@ +//Derek McQuay 647465151 CS 235 Fall 2012 midterm 1 +#include "node.h" + +node::node(const int exponent, const int coefficient, const char variable, node* next) : + exponent(exponent), coefficient(coefficient), variable(variable), next(next) {} + +node::node(const int exponent, const char variable, node* next) : //construtor for when coefficient is assumed 1 + exponent(exponent), coefficient(1), variable(variable), next(next) {} + +ostream & operator<<(ostream & os, node n) { //used to correctly print out each node + if(n.coefficient == 0) { + os << ""; + } + else if(n.coefficient == 1 && n.exponent == 0) { + os << n.coefficient; + } + else if(n.coefficient == -1 && n.exponent == 0) { + os << n.coefficient; + } + else if(n.exponent == 0) { + os << n.coefficient; + } + else if(n.coefficient == -1) { + os << "-" << n.variable << " ^ " << n.exponent; + } + else if(n.coefficient < 0 && n.exponent == 1) { + os << n.coefficient << n.variable; + } + else if(n.coefficient == 1 && n.exponent == 1) { + os << n.variable; + } + else if(n.exponent == 1) { + os << n.coefficient << " " << n.variable; + } + else if(n.coefficient == 1) { + os << n.variable << " ^ " << n.exponent; + } + else { + os << n.coefficient << " " << n.variable << " ^ " << n.exponent; + } + return os; +} diff --git a/cs235/exam1/node.h b/cs235/exam1/node.h new file mode 100644 index 0000000..1f97159 --- /dev/null +++ b/cs235/exam1/node.h @@ -0,0 +1,19 @@ +//Derek McQuay 647465151 CS 235 Fall 2012 midterm 1 +#ifndef __NODE_H__ +#define __NODE_H__ + +#include + +using namespace std; + +class node { + public: + node(const int, const int, const char, node*); + node(const int, const char, node*); + int exponent; + int coefficient; + char variable; + node* next; + friend ostream & operator<<(ostream & os, node n); +}; +#endif diff --git a/cs235/exam1/polylist.cpp b/cs235/exam1/polylist.cpp new file mode 100644 index 0000000..31174a2 --- /dev/null +++ b/cs235/exam1/polylist.cpp @@ -0,0 +1,438 @@ +//Derek McQuay 647465151 CS 235 Fall 2012 midterm 1 +#include "polylist.h" + +polylist::polylist(): head(NULL) {} +polylist::~polylist() {clear();} + +void polylist::insert(string term) { + vector v = parse_expression(term); + if(!is_valid(v)) { + return; + } + if(v.size() == 1) { //expression of the form x + if(v[0][0] == '-') { + insertTail(0, -1, v[0][1]); + } + else { + insertTail(0, 1, v[0][0]); + } + } + if(v.size() == 2) { // expresion of the form 2 x + int exp = atoi(v[0].c_str()); + insertTail(0, exp, v[1][0]); + } + if(v.size() == 3) { // expression of the form x ^ 4 + int exp = atoi(v[2].c_str()); + int after_exp = 0; + if(exp == 0) { + if(v[0][0] == '-') { + insertTail(exp, -1, v[0][0]); + } + else { + insertTail(exp, 1, v[0][0]); + } + } + node* node_ptr = head; + while(node_ptr != NULL) { + if(node_ptr->exponent <= exp) { + after_exp++; + break; + } + after_exp++; + node_ptr = node_ptr->next; + } + string before = at(after_exp - 1); + vector previous_vector = parse_expression(before); + if(previous_vector.size() == 1) { + if(node_ptr == head) { + if(node_ptr == head && size() == 0) { + if(v[0][0] == '-') { + insertHead(exp, -1, v[0][0]); + } + else { + insertHead(exp, 1, v[0][0]); + } + } + else if(node_ptr->exponent == exp) { + node_ptr->coefficient += 1; + if(node_ptr->coefficient == 0) { + remove(exp); + } + } + else { + if(v[0][0] == '-') { + insertHead(exp, -1, v[0][1]); + } + else { + insertHead(exp, 1, v[0][0]); + } + } + } + else { + node_ptr->coefficient += 1; + if(node_ptr->coefficient == 0) { + remove(exp); + } + } + } + if(previous_vector.size() == 2) { + if(v[0][0] == '-') { + insertHead(exp, -1, v[0][1]); + } + else { + insertHead(exp, 1, v[0][0]); + } + } + if(previous_vector.size() == 3) { + int insertionNodeExponent = atoi(previous_vector[2].c_str()); + if(insertionNodeExponent == exp) { + node* node_ptr2 = head; + while(node_ptr2 != NULL) { + if(node_ptr2->exponent == exp) { + if(v[0][0] == '-') { + node_ptr2->coefficient -= 1; + } + else { + node_ptr2->coefficient += 1; + } + if(node_ptr2->coefficient == 0) { + remove(exp); + } + } + node_ptr2 = node_ptr2->next; + } + } + else{ + if(v[0][0] == '-') { + insertAfter(exp, -1, v[0][1], insertionNodeExponent); + } + else { + insertAfter(exp, 1, v[0][0], insertionNodeExponent); + } + } + } + if(previous_vector.size() == 4) { + int insertionNodeExponent = atoi(previous_vector[3].c_str()); + if(insertionNodeExponent == exp) { + node* node_ptr2 = head; + while(node_ptr2 != NULL) { + if(node_ptr2->exponent == exp) { + node_ptr2->coefficient += 1; + if(node_ptr2->coefficient == 0) { + remove(exp); + } + } + node_ptr2 = node_ptr2->next; + } + } + else { + if(v[0][0] == '-') { + insertAfter(exp, -1, v[0][1], insertionNodeExponent); + } + else { + insertAfter(exp, 1, v[0][0], insertionNodeExponent); + } + } + } + } + if(v.size() == 4) { //expression of the form "2 x ^ 3" + int coef = atoi(v[0].c_str()); + int exp = atoi(v[3].c_str()); + int after_exp = 0; + node* node_ptr = head; + if(exp == 0) { + insertTail(exp, coef, v[1][0]); + return; + } + while(node_ptr != NULL) { + if(node_ptr->exponent <= exp) { + break; + } + after_exp++; + node_ptr = node_ptr->next; + } + string before = at(after_exp - 1); + vector previous_vector = parse_expression(before); + if(previous_vector.size() == 1) { + if(node_ptr == head) { + if(node_ptr == head && size() == 0) { + insertHead(exp, coef, v[1][0]); + } + else if(node_ptr->exponent == exp) { + node_ptr->coefficient += coef; + if(node_ptr->coefficient == 0) { + remove(exp); + } + } + else { + insertHead(exp, coef, v[1][0]); + } + } + else { + node_ptr->coefficient += coef; + if(node_ptr->coefficient == 0) { + remove(exp); + } + } + } + if(previous_vector.size() == 2) { + insertHead(exp, coef, v[1][0]); + } + if(previous_vector.size() == 3) { + int insertionNodeExponent = atoi(previous_vector[2].c_str()); + if(node_ptr->exponent == exp) { + node* node_ptr2 = head; + while(node_ptr2 != NULL) { + if(node_ptr2->exponent == exp) { + node_ptr2->coefficient += coef; + if(node_ptr2->coefficient == 0) { + remove(exp); + } + } + node_ptr2 = node_ptr2->next; + } + } + else{ + insertAfter(exp, coef, v[1][0], insertionNodeExponent); + } + } + if(previous_vector.size() == 4) { + int insertionNodeExponent = atoi(previous_vector[3].c_str()); + if(node_ptr == NULL) { + insertTail(exp, coef, v[1][0]); + } + else { + if(node_ptr->exponent == exp) { + node* node_ptr2 = head; + while(node_ptr2 != NULL) { + if(node_ptr2->exponent == exp) { + node_ptr2->coefficient += coef; + if(node_ptr2->coefficient == 0) { + remove(exp); + } + } + node_ptr2 = node_ptr2->next; + } + } + else { + insertAfter(exp, coef, v[1][0], insertionNodeExponent); + } + } + } + } +} + +void polylist::clear() { + node* node_ptr = head; + while(head) { + node_ptr = head; + head = head->next; + delete node_ptr; + } +} + +string polylist::at(int index) { + if(index < 0) { + return "invalid"; + } + if(size() <= index) { + return "invalid"; + } + node* node_ptr = head; + for(int i = 0; i < index; i++){ + node_ptr = node_ptr->next; + } + if(head == NULL) + return "invalid"; + stringstream s; + s << *node_ptr; + return s.str(); +} + +int polylist::size() { + int size_of_list = 0; + node* node_ptr = head; + while(node_ptr != NULL) { + size_of_list++; + node_ptr = node_ptr->next; + } + return size_of_list; +} + +string polylist::printList() { + stringstream s; + if(head == NULL) + return ""; + node* node_ptr = head; + while(node_ptr != NULL) { + if(node_ptr->next == NULL) { + s << *node_ptr; + return s.str(); + } + else { + s << *node_ptr << " + "; + node_ptr = node_ptr->next; + } + } + return s.str(); +} + +void polylist::remove(int exponent) { + node* node_ptr = head; + if(node_ptr->exponent == exponent) { + node* ptr = head; + head = head->next; + delete ptr; + return; + } + while(node_ptr != NULL) { + if(node_ptr->next == NULL) + return; + if(node_ptr->next->exponent == exponent) { + node* ptr = node_ptr->next; + node_ptr->next = node_ptr->next->next; + delete ptr; + } + node_ptr = node_ptr->next; + } +} + +void polylist::insertHead(int exponent, int coefficient, char variable) { + head = new node(exponent, coefficient, variable, head); +} + +void polylist::insertTail(int exponent, int coefficient, char variable) { + if(head == NULL) { + insertHead(exponent, coefficient, variable); + return; + } + node* node_ptr = head; + while(node_ptr != NULL) { + if(node_ptr->next == NULL) { + if(node_ptr->exponent == exponent) { + node_ptr->coefficient += coefficient; + if(node_ptr->coefficient == 0) { + remove(exponent); + } + } + else{ + node_ptr->next = new node(exponent, coefficient, variable, NULL); + break; + } + } + node_ptr = node_ptr->next; + } +} + +void polylist::insertAfter(int exponent, int coefficient, char variable, int insertionNodeExponent) { + node* node_ptr = head; + while(node_ptr != NULL) { + if(node_ptr->exponent == insertionNodeExponent) { + if(head == node_ptr) { + insertHead(exponent, coefficient, variable); + } + else{ + node* temp = new node(exponent, coefficient, variable, NULL); + temp->next = node_ptr->next; + node_ptr->next = temp; + } + } + node_ptr = node_ptr->next; + } +} + +bool is_valid(vector expression) { + if(expression.empty()) { //if expression is size 0 + return false; + } + if(expression.size() == 1) { //if expression is size one. only case if "x" + if(expression[0][0] == '-') { + if(expression[0] == "-x") + return true; + if(!isalpha(expression[0][1]) or expression[0].size() > 1) { + return false; + } + } + else if(!isalpha(expression[0][0]) or expression[0].size() > 1) { + return false; + } + return true; + } + if(expression.size() == 2) { //if expression is size two, only case is of form "2 x" + for(unsigned int i = 0; i < expression[0].size(); i++) { + if(expression[0][0] != '-') { + if(!isdigit(expression[0][i])) { + return false; + } + } + } + if(!isalpha(expression[1][0]) or expression[1].size() > 1) { + return false; + } + return true; + } + if(expression.size() == 3) { //if expression is size three, only case is of form "x ^ 1" + if(expression[0][0] == '-') { + if(expression[0] == "-x") + return true; + if(!isalpha(expression[0][1]) or expression[0].size() > 1) { + return false; + } + } + else if(!isalpha(expression[0][0]) or expression[0].size() > 1) { + return false; + } + if(expression[1] != "^") { + return false; + } + for(unsigned int i = 0; i < expression[2].size(); i++) { + if(!isdigit(expression[2][i])) { + return false; + } + } + return true; + } + if(expression.size() == 4) { //if expression is size four, only case is of form "1 x ^ 1" + for(unsigned int i = 0; i < expression[0].size(); i++) { + if(expression[0][0] != '-') { + if(!isdigit(expression[0][i])) { + return false; + } + } + } + if(!isalpha(expression[1][0]) or expression[1].size() > 1) { + return false; + } + if(expression[2] != "^") { + return false; + } + for(unsigned int i = 0; i < expression[3].size(); i++) { + if(!isdigit(expression[3][i])) { + return false; + } + } + return true; + } + return false; +} + +vector parse_expression(string expression) { //parses expression with " " being the delimeter + vector results; + string s; + for(unsigned int i = 0; i < expression.length(); i++) { + char c = expression[i]; + if(c != ' ') { + s += c; + } + else { + if(s != "") { + results.push_back(s); + s.clear(); + } + } + } + if(s != "") { + results.push_back(s); + } + return results; +} diff --git a/cs235/exam1/polylist.h b/cs235/exam1/polylist.h new file mode 100644 index 0000000..69d4bbd --- /dev/null +++ b/cs235/exam1/polylist.h @@ -0,0 +1,31 @@ +//Derek McQuay 647465151 CS 235 Fall 2012 midterm 1 +#ifndef __POLYLIST_H__ +#define __POLYLIST_H__ +#include "PolynomialListInterface.h" +#include "node.h" +#include +#include +#include +#include +#include + +using namespace std; + +class polylist : public PolynomialListInterface { + public: + polylist(); + ~polylist(); + node* head; + void insert(string term); + void clear(); + string at(int index); + int size(); + string printList(); + void remove(int exponent); + void insertAfter(int exponent, int coefficient, char value, int insertionNodeExponent); + void insertTail(int exponent, int coefficient, char value); + void insertHead(int exponent, int coefficient, char value); +}; +vector parse_expression(string expression); //these are included outside of the class because i used them in my test.cpp +bool is_valid(vector expression); //and wanted them not to be included in the class +#endif diff --git a/cs235/exam1/polyman.cpp b/cs235/exam1/polyman.cpp new file mode 100644 index 0000000..eaef89e --- /dev/null +++ b/cs235/exam1/polyman.cpp @@ -0,0 +1,107 @@ +//Derek McQuay 647465151 CS 235 Fall 2012 midterm 1 +#include "polyman.h" + +PolynomialListInterface * polyman::addLists() { + node * node_ptr1 = l1.head; + node * node_ptr2 = l2.head; + polylist l3; + stringstream exp; + while(node_ptr1 != NULL) { + if(node_ptr1->coefficient != 0) { + exp << node_ptr1->coefficient; + exp << " "; + } + exp << node_ptr1->variable; + if(node_ptr1->exponent != 0) { + exp << " ^ "; + exp << node_ptr1->exponent; + } + l3.insert(exp.str()); + exp.str(""); + node_ptr1 = node_ptr1->next; + } + while(node_ptr2 != NULL) { + if(node_ptr2->coefficient != 0) { + exp << node_ptr2->coefficient; + exp << " "; + } + exp << node_ptr2->variable; + if(node_ptr2->exponent != 0) { + exp << " ^ "; + exp << node_ptr2->exponent; + } + l3.insert(exp.str()); + exp.str(""); + node_ptr2 = node_ptr2->next; + } + polylist * p = NULL; + p = &l3; + return p; +} + +PolynomialListInterface * polyman::subtractLists() { + node * node_ptr1 = l1.head; + node * node_ptr2 = l2.head; + polylist l3; + stringstream exp; + while(node_ptr1 != NULL) { + if(node_ptr1->coefficient != 0) { + exp << node_ptr1->coefficient; + exp << " "; + } + exp << node_ptr1->variable; + if(node_ptr1->exponent != 0) { + exp << " ^ "; + exp << node_ptr1->exponent; + } + l3.insert(exp.str()); + exp.str(""); + node_ptr1 = node_ptr1->next; + } + while(node_ptr2 != NULL) { + if(node_ptr2->coefficient != 0) { + node_ptr2->coefficient = node_ptr2->coefficient * -1; // will cause it to be subtracting + exp << node_ptr2->coefficient; + exp << " "; + } + exp << node_ptr2->variable; + if(node_ptr2->exponent != 0) { + exp << " ^ "; + exp << node_ptr2->exponent; + } + l3.insert(exp.str()); + exp.str(""); + node_ptr2 = node_ptr2->next; + } + polylist * p = NULL; + p = &l3; + return p; +} + +void polyman::fillListOne(string term) { + l1.insert(term); +} + +void polyman::fillListTwo(string term) { + l2.insert(term); +} + +void polyman::clearListOne() { + l1.clear(); +} + +void polyman::clearListTwo() { + l2.clear(); +} + +PolynomialListInterface * polyman::getListOne() { + polylist * p = NULL; + p = &l1; + return p; +} + +PolynomialListInterface * polyman::getListTwo() { + polylist * p = NULL; + p = &l2; + return p; +} diff --git a/cs235/exam1/polyman.h b/cs235/exam1/polyman.h new file mode 100644 index 0000000..5bf9e2b --- /dev/null +++ b/cs235/exam1/polyman.h @@ -0,0 +1,22 @@ +//Derek McQuay 647465151 CS 235 Fall 2012 midterm 1 +#ifndef __POLYMAN_H__ +#define __POLYMAN_H__ +#include "PolynomialManagerInterface.h" +#include "polylist.h" + +using namespace std; + +class polyman : public PolynomialManagerInterface { + public: + polylist l1; + polylist l2; + PolynomialListInterface * addLists(); + PolynomialListInterface * subtractLists(); + void fillListOne(string term); + void fillListTwo(string term); + void clearListOne(); + void clearListTwo(); + PolynomialListInterface * getListOne(); + PolynomialListInterface * getListTwo(); +}; +#endif diff --git a/cs235/final/Makefile b/cs235/final/Makefile new file mode 100644 index 0000000..813bc1e --- /dev/null +++ b/cs235/final/Makefile @@ -0,0 +1,31 @@ +CXXFLAGS= -Wall -g -std=c++0x +OBJECTS=RedBlackTreeFactory.o br_tree.o br_node.o main.o +EXE=main +all: $(EXE) + +$(EXE): $(OBJECTS) + $(CXX) $(CXXFLAGS) $(OBJECTS) -o $@ +test: test.cpp RedBlackTreeFactory.o br_node.o +rtest: test + ./test +RedBlackTreeFactory.o: RedBlackTreeFactory.cpp RedBlackTreeFactory.h +br_node.o: br_node.cpp br_node.h +br_tree.o: br_tree.cpp br_tree.h +main.o: main.cpp + +run: main + ./main + +clean: + @rm -vf *.o + @rm -vf $(EXE) + @rm -vf *.1 + @rm -vf *.0 + @rm -vf test + @rm -rvf *.dSYM + +drun: main + gdb ./main + +valgrind: $(EXE) + valgrind --tool=memcheck --leak-check=yes ./$(EXE) diff --git a/cs235/final/RedBlackNodeInterface.h b/cs235/final/RedBlackNodeInterface.h new file mode 100644 index 0000000..e32f1d2 --- /dev/null +++ b/cs235/final/RedBlackNodeInterface.h @@ -0,0 +1,43 @@ +//YOU MAY NOT MODIFY THIS DOCUMENT +#pragma once + +#include + +using namespace std; + +class RedBlackNodeInterface { + +public: + RedBlackNodeInterface() {} + virtual ~RedBlackNodeInterface() {} + + /* + * Returns the word that is stored in this node + * + * @return the word that is stored in this node. + */ + virtual string getWord() = 0; + + /* + * Returns the color of the node. A red node should return 0 + * and a black node should return 1. + * + * @return the color of the node + */ + virtual int getColor() = 0; + + /* + * Returns the left child of this node or null if it doesn't have one. + * + * @return the left child of this node or null if it doesn't have one. + */ + virtual RedBlackNodeInterface * getLeftChild() = 0; + + /* + * Returns the right child of this node or null if it doesn't have one. + * + * @return the right child of this node or null if it doesn't have one. + */ + virtual RedBlackNodeInterface * getRightChild() = 0; + +}; diff --git a/cs235/final/RedBlackTreeFactory.cpp b/cs235/final/RedBlackTreeFactory.cpp new file mode 100644 index 0000000..4a09392 --- /dev/null +++ b/cs235/final/RedBlackTreeFactory.cpp @@ -0,0 +1,20 @@ +#include "RedBlackTreeFactory.h" +#include "br_tree.h" +//You may add #include statements here + +/* + You will MODIFY THIS DOCUMENT. +*/ +//======================================================================================= +/* + getRedBlackTree() + + Creates and returns an object whose class extends RedBlackTreeInterface. + This should be an object of a class you have created. + + Example: If you made a class called "RedBlackTree", you might say, "return new RedBlackTree();". +*/ +RedBlackTreeInterface * RedBlackTreeFactory::getRedBlackTree() +{ + return new br_tree();//Modify this line +} diff --git a/cs235/final/RedBlackTreeFactory.h b/cs235/final/RedBlackTreeFactory.h new file mode 100644 index 0000000..2ee460d --- /dev/null +++ b/cs235/final/RedBlackTreeFactory.h @@ -0,0 +1,23 @@ +#pragma once +#include "RedBlackTreeInterface.h" + +using namespace std; + +/* + WARNING: It is expressly forbidden to modify any part of this document, including its name +*/ +//======================================================================================= +/* + getRedBlackTree() + + Creates and returns an object whose class extends RedBlackTreeInterface. + This should be an object of a class you have created. + + Example: If you made a class called "RedBlackTree", you might say, "return new RedBlackTree();". +*/ +class RedBlackTreeFactory +{ + public: + static RedBlackTreeInterface * getRedBlackTree(); +}; +//======================================================================================= diff --git a/cs235/final/RedBlackTreeInterface.h b/cs235/final/RedBlackTreeInterface.h new file mode 100644 index 0000000..1974303 --- /dev/null +++ b/cs235/final/RedBlackTreeInterface.h @@ -0,0 +1,78 @@ +//YOU MAY NOT MODIFY THIS DOCUMENT +#pragma once + +#include +#include "RedBlackNodeInterface.h" + +using namespace std; + +class RedBlackTreeInterface { +public: + RedBlackTreeInterface() {} + virtual ~RedBlackTreeInterface() {} + + //Please note that the class that implements this interface must be made + //of objects which implement the NodeInterface + + /* + * Returns the root node for this tree + * + * @return the root node for this tree. + */ + virtual RedBlackNodeInterface * getRootNode() = 0; + + /* + * Attempts to add the given word to the tree + * + * Addition should be consistent with the instructions found in the exam instructions. + */ + virtual void add(string word) = 0; + + /* + * Attempts to add the given string of words to the tree + * + * A string of words consists of alphabetical characters, no punctuation marks, + * and white space as the delimiter separating words. The string should be parsed and + * each word should be added to the tree. + */ + virtual void addPhrase(string words) = 0; + + /* + * Attempts to remove the given word from the tree + * + * Removal should be consistent with the instructions found in the exam instructions. + */ + virtual void remove(string word) = 0; + + /* + * The tree should print in the following format: + * + * Root (value & color) + * Left subtree root (value & color) + * Left left subtree root (value & color) + * Left right subtree root (value & color) + * Right subtree root (value & color) + * Right left subtree root (value & color) + * Right right subtree root (value & color) + * + * For example: + * The tree: + * + * jumps(b) + * brown(r) quick(r) + * The(b) fox(b) over(b) the(b) + * lazy(r) + * + * Will print: + * jumps(b) + * brown(r) + * The(b) + * fox(b) + * quick(r) + * over(b) + * lazy(r); + * the(b) + * + */ + virtual string printTree() = 0; +}; diff --git a/cs235/final/TwoThreeTreeFactory.cpp b/cs235/final/TwoThreeTreeFactory.cpp new file mode 100644 index 0000000..aa42b30 --- /dev/null +++ b/cs235/final/TwoThreeTreeFactory.cpp @@ -0,0 +1,21 @@ +#include "TwoThreeTreeFactory.h" +//You may add #include statements here + +/* + You will MODIFY THIS DOCUMENT. +*/ +//======================================================================================= +/* + getTwoThreeTree() + + Creates and returns an object whose class extends TwoTreeTreeInterface. + This should be an object of a class you have created. + + Example: If you made a class called "TwoThreeTree", you might say, "return new TwoThreeTree();". + + This method should return NULL or 0 if you do not intent to do the extra credit +*/ +TwoThreeTreeInterface * TwoThreeTreeFactory::getTwoThreeTree() +{ + return 0;//Modify this line +} diff --git a/cs235/final/TwoThreeTreeFactory.h b/cs235/final/TwoThreeTreeFactory.h new file mode 100644 index 0000000..a9f9af7 --- /dev/null +++ b/cs235/final/TwoThreeTreeFactory.h @@ -0,0 +1,25 @@ +#pragma once +#include "TwoThreeTreeInterface.h" + +using namespace std; + +/* + WARNING: It is expressly forbidden to modify any part of this document, including its name +*/ +//======================================================================================= +/* + getTwoThreeTree() + + Creates and returns an object whose class extends TwoThreeTreeInterface. + This should be an object of a class you have created. + + Example: If you made a class called "TwoThreeTree", you might say, "return new TwoThreeTree();". + + This method should return NULL or 0 if you do not intent to do the extra credit +*/ +class TwoThreeTreeFactory +{ + public: + static TwoThreeTreeInterface * getTwoThreeTree(); +}; +//======================================================================================= diff --git a/cs235/final/TwoThreeTreeInterface.h b/cs235/final/TwoThreeTreeInterface.h new file mode 100644 index 0000000..f6c94a0 --- /dev/null +++ b/cs235/final/TwoThreeTreeInterface.h @@ -0,0 +1,58 @@ +//YOU MAY NOT MODIFY THIS DOCUMENT +#pragma once + +#include + +using namespace std; + +class TwoThreeTreeInterface { +public: + TwoThreeTreeInterface() {} + virtual ~TwoThreeTreeInterface() {} + + //This class may use a node of your own creation. + + /* + * Attempts to add the given word to the tree + * + * Addition should be consistent with the instructions found in the exam instructions. + */ + virtual void add(string word) = 0; + + /* + * Attempts to add the given string of words to the tree + * + * A string of words consists of alphabetical characters, no punctuation marks, + * and white space as the delimiter separating words. The string should be parsed and + * each word should be added to the tree. + */ + virtual void addPhrase(string words) = 0; + + /* + * Attempts to remove the given word from the tree + * + * Removal should be consistent with the instructions found in the exam instructions. + */ + virtual void remove(string word) = 0; + + /* + * The tree should print in the following format: + * + * Root (value) + * Left subtree root (value) + * Left left subtree root (value) + * Left middle subtree root (value) + * Left right subtree root (value) + * Middle subtree root (value) + * Middle left subtree root (value) + * Middle middle subtree root (value) + * Middle right subtree root (value) + * + * Right subtree root (value) + * Right left subtree root (value) + * Right middle subtree root (value) + * Right right subtree root (value) + * + */ + virtual string printTree() = 0; +}; diff --git a/cs235/final/br_node.cpp b/cs235/final/br_node.cpp new file mode 100644 index 0000000..bf75779 --- /dev/null +++ b/cs235/final/br_node.cpp @@ -0,0 +1,19 @@ +#include "br_node.h" + +br_node::br_node(string data): data(data), color(0), parent(NULL), left(NULL), right(NULL) {} + +string br_node::getWord() { + return data; +} + +int br_node::getColor() { + return color; +} + +RedBlackNodeInterface* br_node::getLeftChild() { + return left; +} + +RedBlackNodeInterface* br_node::getRightChild() { + return right; +} diff --git a/cs235/final/br_node.h b/cs235/final/br_node.h new file mode 100644 index 0000000..12cda0c --- /dev/null +++ b/cs235/final/br_node.h @@ -0,0 +1,21 @@ +#ifndef __BR_NODE_H__ +#define __BR_NODE_H__ + +#include "RedBlackNodeInterface.h" + +using namespace std; + +class br_node : public RedBlackNodeInterface { + public: + br_node(string data); + string data; + int color; //red = 0, black = 1 + br_node* parent; + br_node* left; + br_node* right; + string getWord(); + int getColor(); + RedBlackNodeInterface * getLeftChild(); + RedBlackNodeInterface * getRightChild(); +}; +#endif diff --git a/cs235/final/br_tree.cpp b/cs235/final/br_tree.cpp new file mode 100644 index 0000000..e9e9401 --- /dev/null +++ b/cs235/final/br_tree.cpp @@ -0,0 +1,500 @@ +#include "br_tree.h" + +br_tree::br_tree():root(NULL), h(false){} + +vector parse_expression(string expression) { //parses expression with " " being the delimeter + vector results; + string s; + for(unsigned int i = 0; i < expression.length(); i++) { + char c = expression[i]; + if(c != ' ') { + s += c; + } + else { + if(s != "") { + results.push_back(s); + s.clear(); + } + } + } + if(s != "") { + results.push_back(s); + } + return results; +} + +bool br_tree::in_tree(string data) { + bool found = false; + br_node* current; + br_node* parent; + current = root; + while(current) { + if(current->data == data) { + found = true; + break; + } + else { + parent = current; + if(data > current->data) { + current = current->right; + } + else if(data < current->data) { + current = current->left; + } + } + } + return found; +} + +RedBlackNodeInterface* br_tree::getRootNode() { + return root; +} + +void br_tree::add(string word) { + if(in_tree(word)) { + return; + } + if(root == NULL) { + root = new br_node(word); + root->color = 1; + return; + } + br_node* current; + br_node* cur_parent; + current = root; + while(current) { + cur_parent = current; + if(word > current->data) { + current = current->right; + } + else { + current = current->left; + } + } + if(word < cur_parent->data) { + cur_parent->left = new br_node(word); + cur_parent->left->parent = cur_parent; + br_node* child_added = cur_parent->left; + if(cur_parent->color == 0 && child_added->color == 0) { + balance(cur_parent->left); + } + return; + } + else { + cur_parent->right = new br_node(word); + cur_parent->right->parent = cur_parent; + br_node* child_added = cur_parent->right; + if(cur_parent->color == 0 && child_added->color == 0) { + balance(cur_parent->right); + } + return; + } +} + +void br_tree::balance(br_node* cur) { + if((cur->data == root->data) || (cur->parent->data == root->data)) { + return; + } + if((cur->parent->parent->right != NULL) && (cur->parent->parent->left != NULL)) { + if((cur->parent->parent->left->color == 0) && (cur->parent->parent->right->color == 0)) { + cur->parent->parent->left->color = 1; + cur->parent->parent->right->color = 1; + cur->parent->parent->color = 0; + balance(cur->parent->parent); + return; + } + } + if((cur->parent->color == 0) && (cur->color == 0)) { + if((cur->parent->left != NULL) && (cur->parent->parent->left != NULL)) { + if((cur->parent->left->data == cur->data) && (cur->parent->parent->left->data == cur->parent->data)) { + //case III left + br_node* temp = cur->parent->parent; + if(cur->parent->right == NULL) { + temp->left = cur->parent->right; + } + else { + temp->left = cur->parent->right; + cur->parent->right->parent = temp; + } + cur->parent->right = temp; + if(temp->parent != NULL) { + if(temp->parent->right->data == temp->data) { + temp->parent->right = cur->parent; + } + else { + temp->parent->left = cur->parent; + } + } + else { + root = cur->parent; + } + cur->parent->parent = temp->parent; + temp->parent = cur->parent; + cur->parent->color = 1; + temp->color = 0; + balance(cur->parent); + return; + } + } + if((cur->parent->left != NULL) && (cur->parent->parent->right != NULL)) { + if((cur->parent->left->data == cur->data) && (cur->parent->parent->right->data == cur->parent->data)){ + //case II right + br_node* temp1 = cur->parent->parent; + br_node* temp2 = cur->parent; + if(cur->right == NULL) { + temp2->left = cur->right; + } + else { + temp2->left = cur->right; + cur->right->parent = temp2; + } + cur->right = temp2; + temp2->parent = cur; + cur->parent = temp1; + temp1->right = cur; + balance(temp2); + return; + } + } + if((cur->parent->right != NULL) && (cur->parent->parent->left != NULL)) { + if((cur->parent->right->data == cur->data) && (cur->parent->parent->left->data == cur->parent->data)){ + //case II left + br_node* temp1 = cur->parent->parent; + br_node* temp2 = cur->parent; + if(cur->left == NULL) { + temp2->right = cur->left; + } + else { + temp2->right = cur->left; + cur->left->parent = temp2; + } + cur->left = temp2; + temp2->parent = cur; + cur->parent = temp1; + temp1->left = cur; + balance(temp2); + return; + } + } + if((cur->parent->right != NULL) && (cur->parent->parent->right != NULL)) { + if((cur->parent->right->data == cur->data) && (cur->parent->parent->right->data == cur->parent->data)) { + //case III right + br_node* temp = cur->parent->parent; + if(cur->parent->left == NULL) { + temp->right = cur->parent->left; + } + else { + temp->right = cur->parent->left; + cur->parent->left->parent = temp; + } + cur->parent->left = temp; + if(temp->parent != NULL) { + if(temp->parent->right->data == temp->data) { + temp->parent->right = cur->parent; + } + else { + temp->parent->left = cur->parent; + } + } + else { + root = cur->parent; + } + cur->parent->parent = temp->parent; + temp->parent = cur->parent; + cur->parent->color = 1; + temp->color = 0; + balance(cur->parent); + return; + } + } + } + if(root->color == 0) { + root->color = 1; + return; + } + balance(cur->parent); +} + +void br_tree::addPhrase(string words) { + vector parsed_string = parse_expression(words); + for(unsigned int i = 0; i < parsed_string.size(); i++) { + add(parsed_string[i]); + } +} + +void br_tree::remove(string word) { + if(!(in_tree(word))) { + return; + } + if(root == NULL) { + return; + } + br_node* current; + br_node* parent; + current = root; + while(current) { + if(current->data == word) { + break; + } + else { + parent = current; + if(word > current->data) { + current = current->right; + } + else if(word < current->data) { + current = current->left; + } + } + } + if(current->color == 1) { + h = true; + } + else { + h = false; + } + root = del_leaf(root, word); +} + +br_node* br_tree::del_leaf(br_node* root, string data) { + br_node* temp; + if(root == NULL) { + return root; + } + else { + if(data < root->data) { + root->left = del_leaf(root->left, data); + if(h) { + cout << "2" << endl; + bal_del(root->left); + } + } + else { + if(data > root->data) { + root->right = del_leaf(root->right, data); + if(h) { + cout << "3" << endl; + cout << root->data << endl; + bal_del(root->right); + } + } + else { + temp = root; + if(temp->right == NULL) { + root = temp->left; + delete temp; + } + else { + if(temp->left == NULL) { + root = temp->right; + delete temp; + } + else { + temp->left = del(temp->left, temp); + if(h) { + cout << "4" << endl; + if(h) { + bal_del(temp); + } + } + } + } + } + } + } + return root; +} + +void br_tree::bal_del(br_node* x) { + //cout << "bal_del(" << x->data << ")" << endl; + if(x == NULL) { + return; + } + while(x != root && x->color == 1) { + if(x == x->parent->left) { + br_node* w = x->parent->right; + if(w == NULL) { + return; + } + if(w->right == NULL || w->left == NULL) { + return; + } + if(w != NULL && w->right != NULL && w->left != NULL) { + if(w->color == 0) { + cout << "case 1 first" << endl; + w->color = 1; //Case 1 + x->parent->color = 0; //Case 1 + left_rot(x->parent); //Case 1 + w = x->parent->right; //Case 1 + } + else if(w->left->color == 1 && w->right->color == 1) { + cout << "case 2 first" << endl; + w->color = 1; //Case 2 + x = x->parent; //Case 2 + } + else { + if(w->right->color == 1) { + cout << "case 3 first" << endl; + w->left->color = 1; //Case 3 + w->color = 0; //Case 3 + right_rot(w); //Case 3 + w = x->parent->right; //Case 3 + } + cout << "case 4 first" << endl; + w->color = x->parent->color; //Case 4 + x->parent->color = 1; //Case 4 + w->right->color = 1; //Case 4 + left_rot(x->parent); + x = root; + } + } + } + else { + br_node* w = x->parent->left; + if(w == NULL) { + return; + } + if(w->right == NULL || w->left == NULL) { + return; + } + if(w->color == 0) { + cout << "case 1 second" << endl; + w->color = 1; //Case 1 + x->parent->color = 0; //Case 1 + right_rot(x->parent); //Case 1 + w = x->parent->left; //Case 1 + } + else if(w->right->color == 1 && w->left->color == 1) { + cout << "case 2 second" << endl; + w->color = 0; //Case 2 + x = x->parent; //Case 2 + } + else { + if(w->left->color == 1) { + cout << "case 3 second" << endl; + w->right->color = 1; //Case 3 + w->color = 0; //Case 3 + left_rot(w); //Case 3 + w = x->parent->left; //Case 3 + } + cout << "case 4 second" << endl; + w->color = x->parent->color; //Case 4 + x->parent->color = 1; //Case 4 + w->left->color = 1; //Case 4 + right_rot(x->parent); + x = root; + } + } + } + x->color = 1; +} + +br_node* br_tree::del(br_node* succ, br_node* n) { + br_node* temp = succ; + if(succ->right != NULL) { + succ->right = del(succ->right, n); + } + else { + br_node* current; + br_node* parent; + current = root; + while(current) { + if(current->data == succ->data) { + break; + } + else { + parent = current; + if(succ->data > current->data) { + current = current->right; + } + else if(succ->data < current->data) { + current = current->left; + } + } + } + if(root->left->data == succ->data) { + root->data = succ->data; + n->right = root->right; + succ = succ->left; + delete temp; + return succ; + } + else if(root->right->data == succ->data) { + root->data = succ->data; + n->right = root->right; + succ = succ->right; + delete temp; + return succ; + } + temp = succ; + n->data = succ->data; + succ = succ->left; + delete temp; + } + return succ; +} + +void br_tree::right_rot(br_node* x) { + br_node* y = x->left; + x->left = y->right; + if(y->right != NULL) { + y->right->parent = x; + } + y->parent = x->parent; + if(x->parent == NULL) { + root = y; + } + else if(x == x->parent->right) { + x->parent->right = y; + } + else { + x->parent->left = y; + } + y->right = x; + x->parent = y; +} + +void br_tree::left_rot(br_node* x) { + br_node* y = x->right; + x->right = y->left; + if(y->left != NULL) { + y->left->parent = x; + } + y->parent = x->parent; + if(x->parent == NULL) { + root = y; + } + else if(x == x->parent->left) { + x->parent->left = y; + } + else { + x->parent->right = y; + } + y->left = x; + x->parent = y; +} + +void br_tree::printer(br_node* node, int a) { + a++; + if(node == NULL) { + return; + } + printer(node->right, a); + for(int i = 0; i <= a; i++) { + cout << "\t"; + } + printf("%s %d", node->data.c_str(), node->color); + cout << endl << endl; + printer(node->left, a); +} + +string br_tree::printTree() { + string tree; + if(root == NULL) { + return ""; + } + cout << root->data << endl; + cout << endl; + printer(root, 0); + cout << endl; + return tree; +} diff --git a/cs235/final/br_tree.h b/cs235/final/br_tree.h new file mode 100644 index 0000000..7ba2bbe --- /dev/null +++ b/cs235/final/br_tree.h @@ -0,0 +1,34 @@ +#ifndef __BR_TREE_H__ +#define __BR_TREE_H__ + +#include "br_node.h" +#include "RedBlackTreeInterface.h" +#include +#include +#include +#include + +using namespace std; + +class br_tree : public RedBlackTreeInterface { + public: + br_tree(); + br_node* root; + bool h; + RedBlackNodeInterface * getRootNode(); + bool in_tree(string data); + void add(string word); + void addPhrase(string words); + void remove(string word); + void balance(br_node* cur); + void transfer(br_node* temp1, br_node* temp2); + void bal_del(br_node* x); + br_node* del_leaf(br_node* root, string data); + void right_rot(br_node* x); + void left_rot(br_node* x); + void printer(br_node* node, int a); + br_node* del(br_node* succ, br_node* n); + br_node* min(br_node* temp); + string printTree(); +}; +#endif diff --git a/cs235/final/main.cpp b/cs235/final/main.cpp new file mode 100644 index 0000000..8322e0b --- /dev/null +++ b/cs235/final/main.cpp @@ -0,0 +1,235 @@ +#include +#include +#include +#include +#include +#include "RedBlackTreeFactory.h" +#include "TwoThreeTreeFactory.h" +#include "br_tree.h" + +/* This main is provided to help you test your code. Be aware that the TAs will use a separate + * test driver that will only test your red black tree code. You may modify this code if you wish, + * but do not try to modify the main to get your own code to work. + */ + + +using namespace std; +int getOption(int max, int min); +string getString(); +void exitPause(); + +RedBlackTreeInterface * redBlackTree; +TwoThreeTreeInterface * twoThreeTree; + +int main() +{ + srand(time(NULL)); + br_tree b; + redBlackTree = RedBlackTreeFactory::getRedBlackTree(); + + if(redBlackTree == NULL) { + cout << endl; + cout << "Factory getRedBlackTree() returned NULL!\nExiting Test Driver (press enter to exit)" << endl; + + exitPause(); + return 0; + } + +// twoThreeTree = TwoThreeTreeFactory::getTwoThreeTree(); + + cout << "Final Exam Beginning" << endl; + while(1) + { + int choice = 0; + if(twoThreeTree == NULL) { + cout<< "Please select an option:" + << "\n1. Add a single word to the tree" + << "\n2. Add a list of words to the tree" + << "\n3. Remove a single word from the tree" + << "\n4. Print the tree" + << "\n5. Quit Program" << endl; + choice = getOption(1,5); + } + else { + cout<< "Please select an option:" + << "\n1. Add a single word to the red black tree" + << "\n2. Add a list of words to the red black tree" + << "\n3. Remove a single word from the red black tree" + << "\n4. Print the red black tree" + << "\n5. Add a single word to the two three tree" + << "\n6. Add a list of words to the two three tree" + << "\n7. Remove a single word from the two three tree" + << "\n8. Print the two three tree" + << "\n9. Quit Program" << endl; + choice = getOption(1,9); + } + if(choice == 1) + { + //Insert the string that the user passes to the program into the tree + for(int i = 0; i < 50000; i++) { + stringstream s; + int rand_n = rand() % 50000; + s << rand_n; + b.add(s.str()); + } + cout << b.root << endl; + //cout<< "Enter the word to add to the tree (alphabetical characters, no punctuation marks, no spaces)."<add(insertToTree); + } + else if(choice == 2) + { + //Insert the string that the user passes to the program into the tree + cout<< "Enter the string to add to the tree." + << "\n(alphabetical characters, no punctuation marks, and with white space as the delimiter separating words)" << endl; + string insertToTree; + insertToTree = getString(); + redBlackTree->addPhrase(insertToTree); + } + else if(choice == 3) + { + cout << "here" << endl; + while(b.root) { + cout << "ah" << endl; + stringstream s; + int rand_n = rand() % 50000; + s << rand_n; + b.remove(s.str()); + } + //Remove the string that the user passes to the program into the tree + //cout << "Enter the word to remove from the tree (alphabetical characters, no punctuation marks, no spaces)."<remove(removeFromTree); + } + else if(choice == 4) + { + cout << b.printTree(); + //cout << "Your tree:" << endl; + //cout << "\t" << redBlackTree->printTree() << endl; + } + else if(choice == 5 && twoThreeTree == NULL) + { + break; + } + else if(choice == 5 && twoThreeTree != NULL) + { + //Insert the string that the user passes to the program into the tree + cout<< "Enter the word to add to the tree (alphabetical characters, no punctuation marks, no spaces)."<add(insertToTree); + } + else if(choice == 6) + { + //Insert the string that the user passes to the program into the tree + cout<< "Enter the string to add to the tree." + << "\n(alphabetical characters, no punctuation marks, and with white space as the delimiter separating words)" << endl; + string insertToTree; + insertToTree = getString(); + twoThreeTree->addPhrase(insertToTree); + } + else if(choice == 7) + { + //Remove the string that the user passes to the program into the tree + cout << "Enter the word to remove from the tree (alphabetical characters, no punctuation marks, no spaces)."<remove(removeFromTree); + } + else if(choice == 8) + { + cout << "Your tree:" << endl; + cout << "\t" << twoThreeTree->printTree() << endl; + } + else + { + break; + } + } + cout << "Thanks for Playing!" << endl; + + exitPause(); + return 0; +} + +//------------------------------------------------------------------------------------- +int getOption(int min, int max) +{ + int input = 0; + bool done = false; + while(!done) + { + input = 0; + cin >> input; + cin.ignore(1000,'\n'); + if(cin.fail()) + { + cin.clear(); + cin.ignore(1000,'\n'); + cout << "Error: Invalid" << endl; + } + else if(input < min || input > max) + { + cout << "Error: Invalid number" << endl; + } + else + { + done = true; + } + } + return input; +} + +string getString() +{ + string input = ""; + bool done = false; + while(!done) + { + input = ""; + getline(cin, input); + if(cin.fail()) + { + cin.clear(); + cin.ignore(1000,'\n'); + cout << "Error: Invalid name" << endl; + } + else + { + done = true; + } + } + return input; +} + +string getWord() +{ + string input = ""; + bool done = false; + while(!done) + { + input = ""; + getline(cin, input); + if(cin.fail()) + { + cin.clear(); + cin.ignore(1000,'\n'); + cout << "Error: Invalid name" << endl; + } + else + { + done = true; + } + } + return input; +} + +void exitPause() { + #ifdef ON_WINDOWS + system("pause"); + #else + + #endif +} diff --git a/cs235/final/test.cpp b/cs235/final/test.cpp new file mode 100644 index 0000000..0c09a4b --- /dev/null +++ b/cs235/final/test.cpp @@ -0,0 +1,7 @@ +#include "br_tree.h" + +using namespace std; + +int main() { + br_tree b; +} diff --git a/cs235/lab02/Lab 2 README.pdf b/cs235/lab02/Lab 2 README.pdf new file mode 100755 index 0000000..b3b44dd Binary files /dev/null and b/cs235/lab02/Lab 2 README.pdf differ diff --git a/cs235/lab02/Run Test Driver.bat b/cs235/lab02/Run Test Driver.bat new file mode 100755 index 0000000..79943f5 --- /dev/null +++ b/cs235/lab02/Run Test Driver.bat @@ -0,0 +1,33 @@ +TITLE TA Test Driver + +@echo off + +del Student_code\circle.cpp~ + +rem setting up environment variables needed for VS compiler +call "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86_amd64 + +rem compiling the .obj +cl /EHsc /Fedont_run_me.exe Student_Code\*.cpp ignoreme.lib + +rem if compilation failed, goto error section +if ERRORLEVEL 1 goto error + +rem cleanup unnecessary generated files +del *.obj + +rem run the driver in another window +.\dont_run_me.exe +del dont_run_me.exe +pause + +exit + +:error + +rem remove any generated files +del *.obj + +echo ----------ERROR-------- +pause +exit diff --git a/cs235/lab02/Student_Code/Factory.cpp b/cs235/lab02/Student_Code/Factory.cpp new file mode 100644 index 0000000..a6bd086 --- /dev/null +++ b/cs235/lab02/Student_Code/Factory.cpp @@ -0,0 +1,20 @@ +#include "Factory.h" +#include "circle.h" +//You may add #include statments here + +/* + You will MODIFY THIS DOCUMENT. +*/ +//======================================================================================= +/* + getGame() + + Creates and returns an object whose class extends JosephusInterface. + This should be an object of a class you have created. + + Example: If you made a class called "Circle", you might say, "return new Circle();". +*/ +JosephusInterface * Factory::getGame() +{ + return new circle(); +} diff --git a/cs235/lab02/Student_Code/Factory.h b/cs235/lab02/Student_Code/Factory.h new file mode 100644 index 0000000..da8c81c --- /dev/null +++ b/cs235/lab02/Student_Code/Factory.h @@ -0,0 +1,23 @@ +#pragma once +#include "JosephusInterface.h" + +using namespace std; + +/* + WARNING: It is expressly forbidden to modify any part of this document, including its name +*/ +//======================================================================================= +/* + getGame() + + Creates and returns an object whose class extends JosephusInterface. + This should be an object of a class you have created. + + Example: If you made a class called "Circle", you might say, "return new Circle();". +*/ +class Factory +{ + public: + static JosephusInterface * getGame(); +}; +//======================================================================================= diff --git a/cs235/lab02/Student_Code/JosephusInterface.h b/cs235/lab02/Student_Code/JosephusInterface.h new file mode 100644 index 0000000..8b1c1a8 --- /dev/null +++ b/cs235/lab02/Student_Code/JosephusInterface.h @@ -0,0 +1,54 @@ +//YOU MAY NOT MODIFY THIS DOCUMENT +#pragma once +#include +#include +#include + +using namespace std; + +class JosephusInterface +{ +public: + JosephusInterface(void){} + virtual ~JosephusInterface(void){} + /* + getNames + + Returns a list of names in the order in which the people will be standing for the "game". + Although fewer people may be playing, you must return 20 names here. Do not provide + duplicate names. + + For the sake of the test driver, this method must return the list of 20 names in the + same order every time it is called, and this list of 20 names in this order must be used + to play the "game". + + This method will be called repeatedly. + */ + virtual vector getNames() = 0; + /* + playGame + + Plays a "game" with the first n people from the list (above) counting forward every m. An + explanation for how the "game" works can be found in the exam specs. + + This method should return a list of names in the order in which the lot fell upon them (including + the survivor, who should be last). If n is not between 10 and 20 or if m is non-positive, + return an empty vector. + + This method will be called repeatedly. + */ + virtual vector playGame(int n, int m) = 0; + /* + reportSafeIndex + + Returns the "safe index", the last index/location in the circle that will be chosen when + the "game" is played. The point of this method is to permit someone to cheat the system + by finding the safe location ahead of time. + + If n is not between 10 and 20 or if m is non-positive, return -1. + + This method may be called repeatedly. + */ + virtual int reportSafeIndex(int n, int m) = 0; +}; + diff --git a/cs235/lab02/Student_Code/Makefile b/cs235/lab02/Student_Code/Makefile new file mode 100644 index 0000000..14ac4a0 --- /dev/null +++ b/cs235/lab02/Student_Code/Makefile @@ -0,0 +1,25 @@ +CXXFLAGS= -Wall -g -std=c++0x +OBJECTS=main.o Factory.o circle.o +EXE=main + +all: $(EXE) + + +$(EXE): main.o + $(CXX) $(CXXFLAGS) $(OBJECTS) -o $@ +main.o: main.cpp Factory.o circle.o +Factory.o: Factory.cpp Factory.h +circle.o: circle.cpp circle.h + +run: $(EXE) + @./$(EXE) + +clean: + @rm -vf *.o + @rm -vf $(EXE) + +debug: $(EXE) + gdb ./$(EXE) + +valgrind: $(EXE) + valgrind --tool=memcheck --leak-check=yes ./$(EXE) diff --git a/cs235/lab02/Student_Code/circle.cpp b/cs235/lab02/Student_Code/circle.cpp new file mode 100644 index 0000000..756c0d1 --- /dev/null +++ b/cs235/lab02/Student_Code/circle.cpp @@ -0,0 +1,88 @@ +#include +#include "circle.h" + +ostream & operator<<(ostream & os, vector strings) { + for(unsigned int i = 0; i < strings.size(); i++) + os << strings[i] << ", "; + os << endl; + return os; +} + +vector circle::getNames() { + vector names; + names.push_back("Josephus"); + names.push_back("A"); + names.push_back("B"); + names.push_back("C"); + names.push_back("D"); + names.push_back("E"); + names.push_back("F"); + names.push_back("G"); + names.push_back("H"); + names.push_back("I"); + names.push_back("J"); + names.push_back("K"); + names.push_back("L"); + names.push_back("M"); + names.push_back("N"); + names.push_back("O"); + names.push_back("P"); + names.push_back("Q"); + names.push_back("R"); + names.push_back("S"); + return names; +} +vector circle::playGame(int n, int m) { + vector names = getNames(); + vector temp; + if(n < 10 || n > 20){ + names.clear(); + return names; + } + if(m < 1){ + names.clear(); + return names; + } + for(int i = 0; i < 20 - n; i++) { + names.pop_back(); + } + unsigned int count = 0; + for(int i = 0; i < n; i++) { + int hits = 0; + while(hits < m) { + if(names[count] == ""){ + } + else { + hits++; + if(hits == m) { + temp.push_back(names[count]); + names[count] = ""; + hits = 100; + } + } + count++; + if(count+1 > names.size()) { + count = 0; + } + } + } + return temp; +} + +int circle::reportSafeIndex(int n, int m) { + if(m < 1) { + return -1; + } + else if (n < 10 || n > 20){ + return -1; + } + vector names = getNames(); + vector kill_list = playGame(n, m); + string safe_person_name = kill_list[kill_list.size() - 1]; + for(unsigned int i = 0; i < names.size(); i++) { + if(safe_person_name == names[i]) { + return i; + } + } + return -1; +} diff --git a/cs235/lab02/Student_Code/circle.h b/cs235/lab02/Student_Code/circle.h new file mode 100644 index 0000000..3e580e0 --- /dev/null +++ b/cs235/lab02/Student_Code/circle.h @@ -0,0 +1,19 @@ +#ifndef __CIRCLE_H__ +#define __CIRCLE_H__ + +#include +#include +#include "JosephusInterface.h" + +using namespace std; + +class circle : public JosephusInterface +{ + public: + circle(){} + vector getNames(); + vector playGame(int n, int m); + int reportSafeIndex(int n, int m); +}; + +#endif diff --git a/cs235/lab02/Student_Code/main.cpp b/cs235/lab02/Student_Code/main.cpp new file mode 100644 index 0000000..9930beb --- /dev/null +++ b/cs235/lab02/Student_Code/main.cpp @@ -0,0 +1,1974 @@ +#include +#include +#include "circle.h" +#include "Factory.h" + +using namespace std; + +int main() { + vector names; + circle c; + names = c.getNames(); + assert(names.size() == 20); + + names = c.playGame(20, 1); + vector expected { + "Josephus", + "A", "B", "C", "D", "E", "F", "G", "H", + "I", "J", "K", "L", "M", "N", "O", "P", + "Q", "R", "S", }; + assert(names == expected); + assert(c.reportSafeIndex(20, 1) == 19); + + names = c.playGame(10, 3); + vector expected2 { + "B","E","H","A","F","Josephus","G","D","I","C", + }; + assert(names == expected2); + assert(c.reportSafeIndex(10, 3) == 3); + + names = c.playGame(10, 6); + vector expected3 { + "E","A","H","F","D","G", + "Josephus","I","C","B", + }; + assert(names == expected3); + + // test 3 tests: + assert(c.reportSafeIndex(10, 6) == 2); + assert(c.reportSafeIndex(0, -5) == -1); + assert(c.reportSafeIndex(0, -4) == -1); + assert(c.reportSafeIndex(0, -3) == -1); + assert(c.reportSafeIndex(0, -2) == -1); + assert(c.reportSafeIndex(0, -1) == -1); + assert(c.reportSafeIndex(0, 0) == -1); + assert(c.reportSafeIndex(0, 1) == -1); + assert(c.reportSafeIndex(0, 2) == -1); + assert(c.reportSafeIndex(0, 3) == -1); + assert(c.reportSafeIndex(0, 4) == -1); + assert(c.reportSafeIndex(0, 5) == -1); + assert(c.reportSafeIndex(0, 6) == -1); + assert(c.reportSafeIndex(0, 7) == -1); + assert(c.reportSafeIndex(0, 8) == -1); + assert(c.reportSafeIndex(0, 9) == -1); + assert(c.reportSafeIndex(0, 10) == -1); + assert(c.reportSafeIndex(0, 11) == -1); + assert(c.reportSafeIndex(0, 12) == -1); + assert(c.reportSafeIndex(0, 13) == -1); + assert(c.reportSafeIndex(0, 14) == -1); + assert(c.reportSafeIndex(0, 15) == -1); + assert(c.reportSafeIndex(0, 16) == -1); + assert(c.reportSafeIndex(0, 17) == -1); + assert(c.reportSafeIndex(0, 18) == -1); + assert(c.reportSafeIndex(0, 19) == -1); + assert(c.reportSafeIndex(0, 20) == -1); + assert(c.reportSafeIndex(0, 21) == -1); + assert(c.reportSafeIndex(0, 22) == -1); + assert(c.reportSafeIndex(0, 23) == -1); + assert(c.reportSafeIndex(0, 24) == -1); + assert(c.reportSafeIndex(0, 25) == -1); + assert(c.reportSafeIndex(0, 26) == -1); + assert(c.reportSafeIndex(0, 27) == -1); + assert(c.reportSafeIndex(0, 28) == -1); + assert(c.reportSafeIndex(0, 29) == -1); + assert(c.reportSafeIndex(0, 30) == -1); + assert(c.reportSafeIndex(0, 31) == -1); + assert(c.reportSafeIndex(0, 32) == -1); + assert(c.reportSafeIndex(0, 33) == -1); + assert(c.reportSafeIndex(0, 34) == -1); + assert(c.reportSafeIndex(0, 35) == -1); + assert(c.reportSafeIndex(0, 36) == -1); + assert(c.reportSafeIndex(0, 37) == -1); + assert(c.reportSafeIndex(0, 38) == -1); + assert(c.reportSafeIndex(0, 39) == -1); + assert(c.reportSafeIndex(0, 40) == -1); + assert(c.reportSafeIndex(0, 41) == -1); + assert(c.reportSafeIndex(1, -5) == -1); + assert(c.reportSafeIndex(1, -4) == -1); + assert(c.reportSafeIndex(1, -3) == -1); + assert(c.reportSafeIndex(1, -2) == -1); + assert(c.reportSafeIndex(1, -1) == -1); + assert(c.reportSafeIndex(1, 0) == -1); + assert(c.reportSafeIndex(1, 1) == -1); + assert(c.reportSafeIndex(1, 2) == -1); + assert(c.reportSafeIndex(1, 3) == -1); + assert(c.reportSafeIndex(1, 4) == -1); + assert(c.reportSafeIndex(1, 5) == -1); + assert(c.reportSafeIndex(1, 6) == -1); + assert(c.reportSafeIndex(1, 7) == -1); + assert(c.reportSafeIndex(1, 8) == -1); + assert(c.reportSafeIndex(1, 9) == -1); + assert(c.reportSafeIndex(1, 10) == -1); + assert(c.reportSafeIndex(1, 11) == -1); + assert(c.reportSafeIndex(1, 12) == -1); + assert(c.reportSafeIndex(1, 13) == -1); + assert(c.reportSafeIndex(1, 14) == -1); + assert(c.reportSafeIndex(1, 15) == -1); + assert(c.reportSafeIndex(1, 16) == -1); + assert(c.reportSafeIndex(1, 17) == -1); + assert(c.reportSafeIndex(1, 18) == -1); + assert(c.reportSafeIndex(1, 19) == -1); + assert(c.reportSafeIndex(1, 20) == -1); + assert(c.reportSafeIndex(1, 21) == -1); + assert(c.reportSafeIndex(1, 22) == -1); + assert(c.reportSafeIndex(1, 23) == -1); + assert(c.reportSafeIndex(1, 24) == -1); + assert(c.reportSafeIndex(1, 25) == -1); + assert(c.reportSafeIndex(1, 26) == -1); + assert(c.reportSafeIndex(1, 27) == -1); + assert(c.reportSafeIndex(1, 28) == -1); + assert(c.reportSafeIndex(1, 29) == -1); + assert(c.reportSafeIndex(1, 30) == -1); + assert(c.reportSafeIndex(1, 31) == -1); + assert(c.reportSafeIndex(1, 32) == -1); + assert(c.reportSafeIndex(1, 33) == -1); + assert(c.reportSafeIndex(1, 34) == -1); + assert(c.reportSafeIndex(1, 35) == -1); + assert(c.reportSafeIndex(1, 36) == -1); + assert(c.reportSafeIndex(1, 37) == -1); + assert(c.reportSafeIndex(1, 38) == -1); + assert(c.reportSafeIndex(1, 39) == -1); + assert(c.reportSafeIndex(1, 40) == -1); + assert(c.reportSafeIndex(1, 41) == -1); + assert(c.reportSafeIndex(2, -5) == -1); + assert(c.reportSafeIndex(2, -4) == -1); + assert(c.reportSafeIndex(2, -3) == -1); + assert(c.reportSafeIndex(2, -2) == -1); + assert(c.reportSafeIndex(2, -1) == -1); + assert(c.reportSafeIndex(2, 0) == -1); + assert(c.reportSafeIndex(2, 1) == -1); + assert(c.reportSafeIndex(2, 2) == -1); + assert(c.reportSafeIndex(2, 3) == -1); + assert(c.reportSafeIndex(2, 4) == -1); + assert(c.reportSafeIndex(2, 5) == -1); + assert(c.reportSafeIndex(2, 6) == -1); + assert(c.reportSafeIndex(2, 7) == -1); + assert(c.reportSafeIndex(2, 8) == -1); + assert(c.reportSafeIndex(2, 9) == -1); + assert(c.reportSafeIndex(2, 10) == -1); + assert(c.reportSafeIndex(2, 11) == -1); + assert(c.reportSafeIndex(2, 12) == -1); + assert(c.reportSafeIndex(2, 13) == -1); + assert(c.reportSafeIndex(2, 14) == -1); + assert(c.reportSafeIndex(2, 15) == -1); + assert(c.reportSafeIndex(2, 16) == -1); + assert(c.reportSafeIndex(2, 17) == -1); + assert(c.reportSafeIndex(2, 18) == -1); + assert(c.reportSafeIndex(2, 19) == -1); + assert(c.reportSafeIndex(2, 20) == -1); + assert(c.reportSafeIndex(2, 21) == -1); + assert(c.reportSafeIndex(2, 22) == -1); + assert(c.reportSafeIndex(2, 23) == -1); + assert(c.reportSafeIndex(2, 24) == -1); + assert(c.reportSafeIndex(2, 25) == -1); + assert(c.reportSafeIndex(2, 26) == -1); + assert(c.reportSafeIndex(2, 27) == -1); + assert(c.reportSafeIndex(2, 28) == -1); + assert(c.reportSafeIndex(2, 29) == -1); + assert(c.reportSafeIndex(2, 30) == -1); + assert(c.reportSafeIndex(2, 31) == -1); + assert(c.reportSafeIndex(2, 32) == -1); + assert(c.reportSafeIndex(2, 33) == -1); + assert(c.reportSafeIndex(2, 34) == -1); + assert(c.reportSafeIndex(2, 35) == -1); + assert(c.reportSafeIndex(2, 36) == -1); + assert(c.reportSafeIndex(2, 37) == -1); + assert(c.reportSafeIndex(2, 38) == -1); + assert(c.reportSafeIndex(2, 39) == -1); + assert(c.reportSafeIndex(2, 40) == -1); + assert(c.reportSafeIndex(2, 41) == -1); + assert(c.reportSafeIndex(3, -5) == -1); + assert(c.reportSafeIndex(3, -4) == -1); + assert(c.reportSafeIndex(3, -3) == -1); + assert(c.reportSafeIndex(3, -2) == -1); + assert(c.reportSafeIndex(3, -1) == -1); + assert(c.reportSafeIndex(3, 0) == -1); + assert(c.reportSafeIndex(3, 1) == -1); + assert(c.reportSafeIndex(3, 2) == -1); + assert(c.reportSafeIndex(3, 3) == -1); + assert(c.reportSafeIndex(3, 4) == -1); + assert(c.reportSafeIndex(3, 5) == -1); + assert(c.reportSafeIndex(3, 6) == -1); + assert(c.reportSafeIndex(3, 7) == -1); + assert(c.reportSafeIndex(3, 8) == -1); + assert(c.reportSafeIndex(3, 9) == -1); + assert(c.reportSafeIndex(3, 10) == -1); + assert(c.reportSafeIndex(3, 11) == -1); + assert(c.reportSafeIndex(3, 12) == -1); + assert(c.reportSafeIndex(3, 13) == -1); + assert(c.reportSafeIndex(3, 14) == -1); + assert(c.reportSafeIndex(3, 15) == -1); + assert(c.reportSafeIndex(3, 16) == -1); + assert(c.reportSafeIndex(3, 17) == -1); + assert(c.reportSafeIndex(3, 18) == -1); + assert(c.reportSafeIndex(3, 19) == -1); + assert(c.reportSafeIndex(3, 20) == -1); + assert(c.reportSafeIndex(3, 21) == -1); + assert(c.reportSafeIndex(3, 22) == -1); + assert(c.reportSafeIndex(3, 23) == -1); + assert(c.reportSafeIndex(3, 24) == -1); + assert(c.reportSafeIndex(3, 25) == -1); + assert(c.reportSafeIndex(3, 26) == -1); + assert(c.reportSafeIndex(3, 27) == -1); + assert(c.reportSafeIndex(3, 28) == -1); + assert(c.reportSafeIndex(3, 29) == -1); + assert(c.reportSafeIndex(3, 30) == -1); + assert(c.reportSafeIndex(3, 31) == -1); + assert(c.reportSafeIndex(3, 32) == -1); + assert(c.reportSafeIndex(3, 33) == -1); + assert(c.reportSafeIndex(3, 34) == -1); + assert(c.reportSafeIndex(3, 35) == -1); + assert(c.reportSafeIndex(3, 36) == -1); + assert(c.reportSafeIndex(3, 37) == -1); + assert(c.reportSafeIndex(3, 38) == -1); + assert(c.reportSafeIndex(3, 39) == -1); + assert(c.reportSafeIndex(3, 40) == -1); + assert(c.reportSafeIndex(3, 41) == -1); + assert(c.reportSafeIndex(4, -5) == -1); + assert(c.reportSafeIndex(4, -4) == -1); + assert(c.reportSafeIndex(4, -3) == -1); + assert(c.reportSafeIndex(4, -2) == -1); + assert(c.reportSafeIndex(4, -1) == -1); + assert(c.reportSafeIndex(4, 0) == -1); + assert(c.reportSafeIndex(4, 1) == -1); + assert(c.reportSafeIndex(4, 2) == -1); + assert(c.reportSafeIndex(4, 3) == -1); + assert(c.reportSafeIndex(4, 4) == -1); + assert(c.reportSafeIndex(4, 5) == -1); + assert(c.reportSafeIndex(4, 6) == -1); + assert(c.reportSafeIndex(4, 7) == -1); + assert(c.reportSafeIndex(4, 8) == -1); + assert(c.reportSafeIndex(4, 9) == -1); + assert(c.reportSafeIndex(4, 10) == -1); + assert(c.reportSafeIndex(4, 11) == -1); + assert(c.reportSafeIndex(4, 12) == -1); + assert(c.reportSafeIndex(4, 13) == -1); + assert(c.reportSafeIndex(4, 14) == -1); + assert(c.reportSafeIndex(4, 15) == -1); + assert(c.reportSafeIndex(4, 16) == -1); + assert(c.reportSafeIndex(4, 17) == -1); + assert(c.reportSafeIndex(4, 18) == -1); + assert(c.reportSafeIndex(4, 19) == -1); + assert(c.reportSafeIndex(4, 20) == -1); + assert(c.reportSafeIndex(4, 21) == -1); + assert(c.reportSafeIndex(4, 22) == -1); + assert(c.reportSafeIndex(4, 23) == -1); + assert(c.reportSafeIndex(4, 24) == -1); + assert(c.reportSafeIndex(4, 25) == -1); + assert(c.reportSafeIndex(4, 26) == -1); + assert(c.reportSafeIndex(4, 27) == -1); + assert(c.reportSafeIndex(4, 28) == -1); + assert(c.reportSafeIndex(4, 29) == -1); + assert(c.reportSafeIndex(4, 30) == -1); + assert(c.reportSafeIndex(4, 31) == -1); + assert(c.reportSafeIndex(4, 32) == -1); + assert(c.reportSafeIndex(4, 33) == -1); + assert(c.reportSafeIndex(4, 34) == -1); + assert(c.reportSafeIndex(4, 35) == -1); + assert(c.reportSafeIndex(4, 36) == -1); + assert(c.reportSafeIndex(4, 37) == -1); + assert(c.reportSafeIndex(4, 38) == -1); + assert(c.reportSafeIndex(4, 39) == -1); + assert(c.reportSafeIndex(4, 40) == -1); + assert(c.reportSafeIndex(4, 41) == -1); + assert(c.reportSafeIndex(5, -5) == -1); + assert(c.reportSafeIndex(5, -4) == -1); + assert(c.reportSafeIndex(5, -3) == -1); + assert(c.reportSafeIndex(5, -2) == -1); + assert(c.reportSafeIndex(5, -1) == -1); + assert(c.reportSafeIndex(5, 0) == -1); + assert(c.reportSafeIndex(5, 1) == -1); + assert(c.reportSafeIndex(5, 2) == -1); + assert(c.reportSafeIndex(5, 3) == -1); + assert(c.reportSafeIndex(5, 4) == -1); + assert(c.reportSafeIndex(5, 5) == -1); + assert(c.reportSafeIndex(5, 6) == -1); + assert(c.reportSafeIndex(5, 7) == -1); + assert(c.reportSafeIndex(5, 8) == -1); + assert(c.reportSafeIndex(5, 9) == -1); + assert(c.reportSafeIndex(5, 10) == -1); + assert(c.reportSafeIndex(5, 11) == -1); + assert(c.reportSafeIndex(5, 12) == -1); + assert(c.reportSafeIndex(5, 13) == -1); + assert(c.reportSafeIndex(5, 14) == -1); + assert(c.reportSafeIndex(5, 15) == -1); + assert(c.reportSafeIndex(5, 16) == -1); + assert(c.reportSafeIndex(5, 17) == -1); + assert(c.reportSafeIndex(5, 18) == -1); + assert(c.reportSafeIndex(5, 19) == -1); + assert(c.reportSafeIndex(5, 20) == -1); + assert(c.reportSafeIndex(5, 21) == -1); + assert(c.reportSafeIndex(5, 22) == -1); + assert(c.reportSafeIndex(5, 23) == -1); + assert(c.reportSafeIndex(5, 24) == -1); + assert(c.reportSafeIndex(5, 25) == -1); + assert(c.reportSafeIndex(5, 26) == -1); + assert(c.reportSafeIndex(5, 27) == -1); + assert(c.reportSafeIndex(5, 28) == -1); + assert(c.reportSafeIndex(5, 29) == -1); + assert(c.reportSafeIndex(5, 30) == -1); + assert(c.reportSafeIndex(5, 31) == -1); + assert(c.reportSafeIndex(5, 32) == -1); + assert(c.reportSafeIndex(5, 33) == -1); + assert(c.reportSafeIndex(5, 34) == -1); + assert(c.reportSafeIndex(5, 35) == -1); + assert(c.reportSafeIndex(5, 36) == -1); + assert(c.reportSafeIndex(5, 37) == -1); + assert(c.reportSafeIndex(5, 38) == -1); + assert(c.reportSafeIndex(5, 39) == -1); + assert(c.reportSafeIndex(5, 40) == -1); + assert(c.reportSafeIndex(5, 41) == -1); + assert(c.reportSafeIndex(6, -5) == -1); + assert(c.reportSafeIndex(6, -4) == -1); + assert(c.reportSafeIndex(6, -3) == -1); + assert(c.reportSafeIndex(6, -2) == -1); + assert(c.reportSafeIndex(6, -1) == -1); + assert(c.reportSafeIndex(6, 0) == -1); + assert(c.reportSafeIndex(6, 1) == -1); + assert(c.reportSafeIndex(6, 2) == -1); + assert(c.reportSafeIndex(6, 3) == -1); + assert(c.reportSafeIndex(6, 4) == -1); + assert(c.reportSafeIndex(6, 5) == -1); + assert(c.reportSafeIndex(6, 6) == -1); + assert(c.reportSafeIndex(6, 7) == -1); + assert(c.reportSafeIndex(6, 8) == -1); + assert(c.reportSafeIndex(6, 9) == -1); + assert(c.reportSafeIndex(6, 10) == -1); + assert(c.reportSafeIndex(6, 11) == -1); + assert(c.reportSafeIndex(6, 12) == -1); + assert(c.reportSafeIndex(6, 13) == -1); + assert(c.reportSafeIndex(6, 14) == -1); + assert(c.reportSafeIndex(6, 15) == -1); + assert(c.reportSafeIndex(6, 16) == -1); + assert(c.reportSafeIndex(6, 17) == -1); + assert(c.reportSafeIndex(6, 18) == -1); + assert(c.reportSafeIndex(6, 19) == -1); + assert(c.reportSafeIndex(6, 20) == -1); + assert(c.reportSafeIndex(6, 21) == -1); + assert(c.reportSafeIndex(6, 22) == -1); + assert(c.reportSafeIndex(6, 23) == -1); + assert(c.reportSafeIndex(6, 24) == -1); + assert(c.reportSafeIndex(6, 25) == -1); + assert(c.reportSafeIndex(6, 26) == -1); + assert(c.reportSafeIndex(6, 27) == -1); + assert(c.reportSafeIndex(6, 28) == -1); + assert(c.reportSafeIndex(6, 29) == -1); + assert(c.reportSafeIndex(6, 30) == -1); + assert(c.reportSafeIndex(6, 31) == -1); + assert(c.reportSafeIndex(6, 32) == -1); + assert(c.reportSafeIndex(6, 33) == -1); + assert(c.reportSafeIndex(6, 34) == -1); + assert(c.reportSafeIndex(6, 35) == -1); + assert(c.reportSafeIndex(6, 36) == -1); + assert(c.reportSafeIndex(6, 37) == -1); + assert(c.reportSafeIndex(6, 38) == -1); + assert(c.reportSafeIndex(6, 39) == -1); + assert(c.reportSafeIndex(6, 40) == -1); + assert(c.reportSafeIndex(6, 41) == -1); + assert(c.reportSafeIndex(7, -5) == -1); + assert(c.reportSafeIndex(7, -4) == -1); + assert(c.reportSafeIndex(7, -3) == -1); + assert(c.reportSafeIndex(7, -2) == -1); + assert(c.reportSafeIndex(7, -1) == -1); + assert(c.reportSafeIndex(7, 0) == -1); + assert(c.reportSafeIndex(7, 1) == -1); + assert(c.reportSafeIndex(7, 2) == -1); + assert(c.reportSafeIndex(7, 3) == -1); + assert(c.reportSafeIndex(7, 4) == -1); + assert(c.reportSafeIndex(7, 5) == -1); + assert(c.reportSafeIndex(7, 6) == -1); + assert(c.reportSafeIndex(7, 7) == -1); + assert(c.reportSafeIndex(7, 8) == -1); + assert(c.reportSafeIndex(7, 9) == -1); + assert(c.reportSafeIndex(7, 10) == -1); + assert(c.reportSafeIndex(7, 11) == -1); + assert(c.reportSafeIndex(7, 12) == -1); + assert(c.reportSafeIndex(7, 13) == -1); + assert(c.reportSafeIndex(7, 14) == -1); + assert(c.reportSafeIndex(7, 15) == -1); + assert(c.reportSafeIndex(7, 16) == -1); + assert(c.reportSafeIndex(7, 17) == -1); + assert(c.reportSafeIndex(7, 18) == -1); + assert(c.reportSafeIndex(7, 19) == -1); + assert(c.reportSafeIndex(7, 20) == -1); + assert(c.reportSafeIndex(7, 21) == -1); + assert(c.reportSafeIndex(7, 22) == -1); + assert(c.reportSafeIndex(7, 23) == -1); + assert(c.reportSafeIndex(7, 24) == -1); + assert(c.reportSafeIndex(7, 25) == -1); + assert(c.reportSafeIndex(7, 26) == -1); + assert(c.reportSafeIndex(7, 27) == -1); + assert(c.reportSafeIndex(7, 28) == -1); + assert(c.reportSafeIndex(7, 29) == -1); + assert(c.reportSafeIndex(7, 30) == -1); + assert(c.reportSafeIndex(7, 31) == -1); + assert(c.reportSafeIndex(7, 32) == -1); + assert(c.reportSafeIndex(7, 33) == -1); + assert(c.reportSafeIndex(7, 34) == -1); + assert(c.reportSafeIndex(7, 35) == -1); + assert(c.reportSafeIndex(7, 36) == -1); + assert(c.reportSafeIndex(7, 37) == -1); + assert(c.reportSafeIndex(7, 38) == -1); + assert(c.reportSafeIndex(7, 39) == -1); + assert(c.reportSafeIndex(7, 40) == -1); + assert(c.reportSafeIndex(7, 41) == -1); + assert(c.reportSafeIndex(8, -5) == -1); + assert(c.reportSafeIndex(8, -4) == -1); + assert(c.reportSafeIndex(8, -3) == -1); + assert(c.reportSafeIndex(8, -2) == -1); + assert(c.reportSafeIndex(8, -1) == -1); + assert(c.reportSafeIndex(8, 0) == -1); + assert(c.reportSafeIndex(8, 1) == -1); + assert(c.reportSafeIndex(8, 2) == -1); + assert(c.reportSafeIndex(8, 3) == -1); + assert(c.reportSafeIndex(8, 4) == -1); + assert(c.reportSafeIndex(8, 5) == -1); + assert(c.reportSafeIndex(8, 6) == -1); + assert(c.reportSafeIndex(8, 7) == -1); + assert(c.reportSafeIndex(8, 8) == -1); + assert(c.reportSafeIndex(8, 9) == -1); + assert(c.reportSafeIndex(8, 10) == -1); + assert(c.reportSafeIndex(8, 11) == -1); + assert(c.reportSafeIndex(8, 12) == -1); + assert(c.reportSafeIndex(8, 13) == -1); + assert(c.reportSafeIndex(8, 14) == -1); + assert(c.reportSafeIndex(8, 15) == -1); + assert(c.reportSafeIndex(8, 16) == -1); + assert(c.reportSafeIndex(8, 17) == -1); + assert(c.reportSafeIndex(8, 18) == -1); + assert(c.reportSafeIndex(8, 19) == -1); + assert(c.reportSafeIndex(8, 20) == -1); + assert(c.reportSafeIndex(8, 21) == -1); + assert(c.reportSafeIndex(8, 22) == -1); + assert(c.reportSafeIndex(8, 23) == -1); + assert(c.reportSafeIndex(8, 24) == -1); + assert(c.reportSafeIndex(8, 25) == -1); + assert(c.reportSafeIndex(8, 26) == -1); + assert(c.reportSafeIndex(8, 27) == -1); + assert(c.reportSafeIndex(8, 28) == -1); + assert(c.reportSafeIndex(8, 29) == -1); + assert(c.reportSafeIndex(8, 30) == -1); + assert(c.reportSafeIndex(8, 31) == -1); + assert(c.reportSafeIndex(8, 32) == -1); + assert(c.reportSafeIndex(8, 33) == -1); + assert(c.reportSafeIndex(8, 34) == -1); + assert(c.reportSafeIndex(8, 35) == -1); + assert(c.reportSafeIndex(8, 36) == -1); + assert(c.reportSafeIndex(8, 37) == -1); + assert(c.reportSafeIndex(8, 38) == -1); + assert(c.reportSafeIndex(8, 39) == -1); + assert(c.reportSafeIndex(8, 40) == -1); + assert(c.reportSafeIndex(8, 41) == -1); + assert(c.reportSafeIndex(9, -5) == -1); + assert(c.reportSafeIndex(9, -4) == -1); + assert(c.reportSafeIndex(9, -3) == -1); + assert(c.reportSafeIndex(9, -2) == -1); + assert(c.reportSafeIndex(9, -1) == -1); + assert(c.reportSafeIndex(9, 0) == -1); + assert(c.reportSafeIndex(9, 1) == -1); + assert(c.reportSafeIndex(9, 2) == -1); + assert(c.reportSafeIndex(9, 3) == -1); + assert(c.reportSafeIndex(9, 4) == -1); + assert(c.reportSafeIndex(9, 5) == -1); + assert(c.reportSafeIndex(9, 6) == -1); + assert(c.reportSafeIndex(9, 7) == -1); + assert(c.reportSafeIndex(9, 8) == -1); + assert(c.reportSafeIndex(9, 9) == -1); + assert(c.reportSafeIndex(9, 10) == -1); + assert(c.reportSafeIndex(9, 11) == -1); + assert(c.reportSafeIndex(9, 12) == -1); + assert(c.reportSafeIndex(9, 13) == -1); + assert(c.reportSafeIndex(9, 14) == -1); + assert(c.reportSafeIndex(9, 15) == -1); + assert(c.reportSafeIndex(9, 16) == -1); + assert(c.reportSafeIndex(9, 17) == -1); + assert(c.reportSafeIndex(9, 18) == -1); + assert(c.reportSafeIndex(9, 19) == -1); + assert(c.reportSafeIndex(9, 20) == -1); + assert(c.reportSafeIndex(9, 21) == -1); + assert(c.reportSafeIndex(9, 22) == -1); + assert(c.reportSafeIndex(9, 23) == -1); + assert(c.reportSafeIndex(9, 24) == -1); + assert(c.reportSafeIndex(9, 25) == -1); + assert(c.reportSafeIndex(9, 26) == -1); + assert(c.reportSafeIndex(9, 27) == -1); + assert(c.reportSafeIndex(9, 28) == -1); + assert(c.reportSafeIndex(9, 29) == -1); + assert(c.reportSafeIndex(9, 30) == -1); + assert(c.reportSafeIndex(9, 31) == -1); + assert(c.reportSafeIndex(9, 32) == -1); + assert(c.reportSafeIndex(9, 33) == -1); + assert(c.reportSafeIndex(9, 34) == -1); + assert(c.reportSafeIndex(9, 35) == -1); + assert(c.reportSafeIndex(9, 36) == -1); + assert(c.reportSafeIndex(9, 37) == -1); + assert(c.reportSafeIndex(9, 38) == -1); + assert(c.reportSafeIndex(9, 39) == -1); + assert(c.reportSafeIndex(9, 40) == -1); + assert(c.reportSafeIndex(9, 41) == -1); + assert(c.reportSafeIndex(10, -5) == -1); + assert(c.reportSafeIndex(10, -4) == -1); + assert(c.reportSafeIndex(10, -3) == -1); + assert(c.reportSafeIndex(10, -2) == -1); + assert(c.reportSafeIndex(10, -1) == -1); + assert(c.reportSafeIndex(10, 0) == -1); + assert(c.reportSafeIndex(10, 1) == 9); + assert(c.reportSafeIndex(10, 2) == 4); + assert(c.reportSafeIndex(10, 3) == 3); + assert(c.reportSafeIndex(10, 4) == 4); + assert(c.reportSafeIndex(10, 5) == 2); + assert(c.reportSafeIndex(10, 6) == 2); + assert(c.reportSafeIndex(10, 7) == 8); + assert(c.reportSafeIndex(10, 8) == 0); + assert(c.reportSafeIndex(10, 9) == 6); + assert(c.reportSafeIndex(10, 10) == 7); + assert(c.reportSafeIndex(10, 11) == 6); + assert(c.reportSafeIndex(10, 12) == 9); + assert(c.reportSafeIndex(10, 13) == 4); + assert(c.reportSafeIndex(10, 14) == 6); + assert(c.reportSafeIndex(10, 15) == 5); + assert(c.reportSafeIndex(10, 16) == 6); + assert(c.reportSafeIndex(10, 17) == 2); + assert(c.reportSafeIndex(10, 18) == 4); + assert(c.reportSafeIndex(10, 19) == 1); + assert(c.reportSafeIndex(10, 20) == 0); + assert(c.reportSafeIndex(10, 21) == 9); + assert(c.reportSafeIndex(10, 22) == 9); + assert(c.reportSafeIndex(10, 23) == 9); + assert(c.reportSafeIndex(10, 24) == 0); + assert(c.reportSafeIndex(10, 25) == 5); + assert(c.reportSafeIndex(10, 26) == 8); + assert(c.reportSafeIndex(10, 27) == 4); + assert(c.reportSafeIndex(10, 28) == 5); + assert(c.reportSafeIndex(10, 29) == 4); + assert(c.reportSafeIndex(10, 30) == 5); + assert(c.reportSafeIndex(10, 31) == 1); + assert(c.reportSafeIndex(10, 32) == 2); + assert(c.reportSafeIndex(10, 33) == 1); + assert(c.reportSafeIndex(10, 34) == 1); + assert(c.reportSafeIndex(10, 35) == 9); + assert(c.reportSafeIndex(10, 36) == 2); + assert(c.reportSafeIndex(10, 37) == 8); + assert(c.reportSafeIndex(10, 38) == 8); + assert(c.reportSafeIndex(10, 39) == 6); + assert(c.reportSafeIndex(10, 40) == 7); + assert(c.reportSafeIndex(10, 41) == 7); + assert(c.reportSafeIndex(11, -5) == -1); + assert(c.reportSafeIndex(11, -4) == -1); + assert(c.reportSafeIndex(11, -3) == -1); + assert(c.reportSafeIndex(11, -2) == -1); + assert(c.reportSafeIndex(11, -1) == -1); + assert(c.reportSafeIndex(11, 0) == -1); + assert(c.reportSafeIndex(11, 1) == 10); + assert(c.reportSafeIndex(11, 2) == 6); + assert(c.reportSafeIndex(11, 3) == 6); + assert(c.reportSafeIndex(11, 4) == 8); + assert(c.reportSafeIndex(11, 5) == 7); + assert(c.reportSafeIndex(11, 6) == 8); + assert(c.reportSafeIndex(11, 7) == 4); + assert(c.reportSafeIndex(11, 8) == 8); + assert(c.reportSafeIndex(11, 9) == 4); + assert(c.reportSafeIndex(11, 10) == 6); + assert(c.reportSafeIndex(11, 11) == 6); + assert(c.reportSafeIndex(11, 12) == 10); + assert(c.reportSafeIndex(11, 13) == 6); + assert(c.reportSafeIndex(11, 14) == 9); + assert(c.reportSafeIndex(11, 15) == 9); + assert(c.reportSafeIndex(11, 16) == 0); + assert(c.reportSafeIndex(11, 17) == 8); + assert(c.reportSafeIndex(11, 18) == 0); + assert(c.reportSafeIndex(11, 19) == 9); + assert(c.reportSafeIndex(11, 20) == 9); + assert(c.reportSafeIndex(11, 21) == 8); + assert(c.reportSafeIndex(11, 22) == 9); + assert(c.reportSafeIndex(11, 23) == 10); + assert(c.reportSafeIndex(11, 24) == 2); + assert(c.reportSafeIndex(11, 25) == 8); + assert(c.reportSafeIndex(11, 26) == 1); + assert(c.reportSafeIndex(11, 27) == 9); + assert(c.reportSafeIndex(11, 28) == 0); + assert(c.reportSafeIndex(11, 29) == 0); + assert(c.reportSafeIndex(11, 30) == 2); + assert(c.reportSafeIndex(11, 31) == 10); + assert(c.reportSafeIndex(11, 32) == 1); + assert(c.reportSafeIndex(11, 33) == 1); + assert(c.reportSafeIndex(11, 34) == 2); + assert(c.reportSafeIndex(11, 35) == 0); + assert(c.reportSafeIndex(11, 36) == 5); + assert(c.reportSafeIndex(11, 37) == 1); + assert(c.reportSafeIndex(11, 38) == 2); + assert(c.reportSafeIndex(11, 39) == 1); + assert(c.reportSafeIndex(11, 40) == 3); + assert(c.reportSafeIndex(11, 41) == 4); + assert(c.reportSafeIndex(12, -5) == -1); + assert(c.reportSafeIndex(12, -4) == -1); + assert(c.reportSafeIndex(12, -3) == -1); + assert(c.reportSafeIndex(12, -2) == -1); + assert(c.reportSafeIndex(12, -1) == -1); + assert(c.reportSafeIndex(12, 0) == -1); + assert(c.reportSafeIndex(12, 1) == 11); + assert(c.reportSafeIndex(12, 2) == 8); + assert(c.reportSafeIndex(12, 3) == 9); + assert(c.reportSafeIndex(12, 4) == 0); + assert(c.reportSafeIndex(12, 5) == 0); + assert(c.reportSafeIndex(12, 6) == 2); + assert(c.reportSafeIndex(12, 7) == 11); + assert(c.reportSafeIndex(12, 8) == 4); + assert(c.reportSafeIndex(12, 9) == 1); + assert(c.reportSafeIndex(12, 10) == 4); + assert(c.reportSafeIndex(12, 11) == 5); + assert(c.reportSafeIndex(12, 12) == 10); + assert(c.reportSafeIndex(12, 13) == 7); + assert(c.reportSafeIndex(12, 14) == 11); + assert(c.reportSafeIndex(12, 15) == 0); + assert(c.reportSafeIndex(12, 16) == 4); + assert(c.reportSafeIndex(12, 17) == 1); + assert(c.reportSafeIndex(12, 18) == 6); + assert(c.reportSafeIndex(12, 19) == 4); + assert(c.reportSafeIndex(12, 20) == 5); + assert(c.reportSafeIndex(12, 21) == 5); + assert(c.reportSafeIndex(12, 22) == 7); + assert(c.reportSafeIndex(12, 23) == 9); + assert(c.reportSafeIndex(12, 24) == 2); + assert(c.reportSafeIndex(12, 25) == 9); + assert(c.reportSafeIndex(12, 26) == 3); + assert(c.reportSafeIndex(12, 27) == 0); + assert(c.reportSafeIndex(12, 28) == 4); + assert(c.reportSafeIndex(12, 29) == 5); + assert(c.reportSafeIndex(12, 30) == 8); + assert(c.reportSafeIndex(12, 31) == 5); + assert(c.reportSafeIndex(12, 32) == 9); + assert(c.reportSafeIndex(12, 33) == 10); + assert(c.reportSafeIndex(12, 34) == 0); + assert(c.reportSafeIndex(12, 35) == 11); + assert(c.reportSafeIndex(12, 36) == 5); + assert(c.reportSafeIndex(12, 37) == 2); + assert(c.reportSafeIndex(12, 38) == 4); + assert(c.reportSafeIndex(12, 39) == 4); + assert(c.reportSafeIndex(12, 40) == 7); + assert(c.reportSafeIndex(12, 41) == 9); + assert(c.reportSafeIndex(13, -5) == -1); + assert(c.reportSafeIndex(13, -4) == -1); + assert(c.reportSafeIndex(13, -3) == -1); + assert(c.reportSafeIndex(13, -2) == -1); + assert(c.reportSafeIndex(13, -1) == -1); + assert(c.reportSafeIndex(13, 0) == -1); + assert(c.reportSafeIndex(13, 1) == 12); + assert(c.reportSafeIndex(13, 2) == 10); + assert(c.reportSafeIndex(13, 3) == 12); + assert(c.reportSafeIndex(13, 4) == 4); + assert(c.reportSafeIndex(13, 5) == 5); + assert(c.reportSafeIndex(13, 6) == 8); + assert(c.reportSafeIndex(13, 7) == 5); + assert(c.reportSafeIndex(13, 8) == 12); + assert(c.reportSafeIndex(13, 9) == 10); + assert(c.reportSafeIndex(13, 10) == 1); + assert(c.reportSafeIndex(13, 11) == 3); + assert(c.reportSafeIndex(13, 12) == 9); + assert(c.reportSafeIndex(13, 13) == 7); + assert(c.reportSafeIndex(13, 14) == 12); + assert(c.reportSafeIndex(13, 15) == 2); + assert(c.reportSafeIndex(13, 16) == 7); + assert(c.reportSafeIndex(13, 17) == 5); + assert(c.reportSafeIndex(13, 18) == 11); + assert(c.reportSafeIndex(13, 19) == 10); + assert(c.reportSafeIndex(13, 20) == 12); + assert(c.reportSafeIndex(13, 21) == 0); + assert(c.reportSafeIndex(13, 22) == 3); + assert(c.reportSafeIndex(13, 23) == 6); + assert(c.reportSafeIndex(13, 24) == 0); + assert(c.reportSafeIndex(13, 25) == 8); + assert(c.reportSafeIndex(13, 26) == 3); + assert(c.reportSafeIndex(13, 27) == 1); + assert(c.reportSafeIndex(13, 28) == 6); + assert(c.reportSafeIndex(13, 29) == 8); + assert(c.reportSafeIndex(13, 30) == 12); + assert(c.reportSafeIndex(13, 31) == 10); + assert(c.reportSafeIndex(13, 32) == 2); + assert(c.reportSafeIndex(13, 33) == 4); + assert(c.reportSafeIndex(13, 34) == 8); + assert(c.reportSafeIndex(13, 35) == 7); + assert(c.reportSafeIndex(13, 36) == 2); + assert(c.reportSafeIndex(13, 37) == 0); + assert(c.reportSafeIndex(13, 38) == 3); + assert(c.reportSafeIndex(13, 39) == 4); + assert(c.reportSafeIndex(13, 40) == 8); + assert(c.reportSafeIndex(13, 41) == 11); + assert(c.reportSafeIndex(14, -5) == -1); + assert(c.reportSafeIndex(14, -4) == -1); + assert(c.reportSafeIndex(14, -3) == -1); + assert(c.reportSafeIndex(14, -2) == -1); + assert(c.reportSafeIndex(14, -1) == -1); + assert(c.reportSafeIndex(14, 0) == -1); + assert(c.reportSafeIndex(14, 1) == 13); + assert(c.reportSafeIndex(14, 2) == 12); + assert(c.reportSafeIndex(14, 3) == 1); + assert(c.reportSafeIndex(14, 4) == 8); + assert(c.reportSafeIndex(14, 5) == 10); + assert(c.reportSafeIndex(14, 6) == 0); + assert(c.reportSafeIndex(14, 7) == 12); + assert(c.reportSafeIndex(14, 8) == 6); + assert(c.reportSafeIndex(14, 9) == 5); + assert(c.reportSafeIndex(14, 10) == 11); + assert(c.reportSafeIndex(14, 11) == 0); + assert(c.reportSafeIndex(14, 12) == 7); + assert(c.reportSafeIndex(14, 13) == 6); + assert(c.reportSafeIndex(14, 14) == 12); + assert(c.reportSafeIndex(14, 15) == 3); + assert(c.reportSafeIndex(14, 16) == 9); + assert(c.reportSafeIndex(14, 17) == 8); + assert(c.reportSafeIndex(14, 18) == 1); + assert(c.reportSafeIndex(14, 19) == 1); + assert(c.reportSafeIndex(14, 20) == 4); + assert(c.reportSafeIndex(14, 21) == 7); + assert(c.reportSafeIndex(14, 22) == 11); + assert(c.reportSafeIndex(14, 23) == 1); + assert(c.reportSafeIndex(14, 24) == 10); + assert(c.reportSafeIndex(14, 25) == 5); + assert(c.reportSafeIndex(14, 26) == 1); + assert(c.reportSafeIndex(14, 27) == 0); + assert(c.reportSafeIndex(14, 28) == 6); + assert(c.reportSafeIndex(14, 29) == 9); + assert(c.reportSafeIndex(14, 30) == 0); + assert(c.reportSafeIndex(14, 31) == 13); + assert(c.reportSafeIndex(14, 32) == 6); + assert(c.reportSafeIndex(14, 33) == 9); + assert(c.reportSafeIndex(14, 34) == 0); + assert(c.reportSafeIndex(14, 35) == 0); + assert(c.reportSafeIndex(14, 36) == 10); + assert(c.reportSafeIndex(14, 37) == 9); + assert(c.reportSafeIndex(14, 38) == 13); + assert(c.reportSafeIndex(14, 39) == 1); + assert(c.reportSafeIndex(14, 40) == 6); + assert(c.reportSafeIndex(14, 41) == 10); + assert(c.reportSafeIndex(15, -5) == -1); + assert(c.reportSafeIndex(15, -4) == -1); + assert(c.reportSafeIndex(15, -3) == -1); + assert(c.reportSafeIndex(15, -2) == -1); + assert(c.reportSafeIndex(15, -1) == -1); + assert(c.reportSafeIndex(15, 0) == -1); + assert(c.reportSafeIndex(15, 1) == 14); + assert(c.reportSafeIndex(15, 2) == 14); + assert(c.reportSafeIndex(15, 3) == 4); + assert(c.reportSafeIndex(15, 4) == 12); + assert(c.reportSafeIndex(15, 5) == 0); + assert(c.reportSafeIndex(15, 6) == 6); + assert(c.reportSafeIndex(15, 7) == 4); + assert(c.reportSafeIndex(15, 8) == 14); + assert(c.reportSafeIndex(15, 9) == 14); + assert(c.reportSafeIndex(15, 10) == 6); + assert(c.reportSafeIndex(15, 11) == 11); + assert(c.reportSafeIndex(15, 12) == 4); + assert(c.reportSafeIndex(15, 13) == 4); + assert(c.reportSafeIndex(15, 14) == 11); + assert(c.reportSafeIndex(15, 15) == 3); + assert(c.reportSafeIndex(15, 16) == 10); + assert(c.reportSafeIndex(15, 17) == 10); + assert(c.reportSafeIndex(15, 18) == 4); + assert(c.reportSafeIndex(15, 19) == 5); + assert(c.reportSafeIndex(15, 20) == 9); + assert(c.reportSafeIndex(15, 21) == 13); + assert(c.reportSafeIndex(15, 22) == 3); + assert(c.reportSafeIndex(15, 23) == 9); + assert(c.reportSafeIndex(15, 24) == 4); + assert(c.reportSafeIndex(15, 25) == 0); + assert(c.reportSafeIndex(15, 26) == 12); + assert(c.reportSafeIndex(15, 27) == 12); + assert(c.reportSafeIndex(15, 28) == 4); + assert(c.reportSafeIndex(15, 29) == 8); + assert(c.reportSafeIndex(15, 30) == 0); + assert(c.reportSafeIndex(15, 31) == 14); + assert(c.reportSafeIndex(15, 32) == 8); + assert(c.reportSafeIndex(15, 33) == 12); + assert(c.reportSafeIndex(15, 34) == 4); + assert(c.reportSafeIndex(15, 35) == 5); + assert(c.reportSafeIndex(15, 36) == 1); + assert(c.reportSafeIndex(15, 37) == 1); + assert(c.reportSafeIndex(15, 38) == 6); + assert(c.reportSafeIndex(15, 39) == 10); + assert(c.reportSafeIndex(15, 40) == 1); + assert(c.reportSafeIndex(15, 41) == 6); + assert(c.reportSafeIndex(16, -5) == -1); + assert(c.reportSafeIndex(16, -4) == -1); + assert(c.reportSafeIndex(16, -3) == -1); + assert(c.reportSafeIndex(16, -2) == -1); + assert(c.reportSafeIndex(16, -1) == -1); + assert(c.reportSafeIndex(16, 0) == -1); + assert(c.reportSafeIndex(16, 1) == 15); + assert(c.reportSafeIndex(16, 2) == 0); + assert(c.reportSafeIndex(16, 3) == 7); + assert(c.reportSafeIndex(16, 4) == 0); + assert(c.reportSafeIndex(16, 5) == 5); + assert(c.reportSafeIndex(16, 6) == 12); + assert(c.reportSafeIndex(16, 7) == 11); + assert(c.reportSafeIndex(16, 8) == 6); + assert(c.reportSafeIndex(16, 9) == 7); + assert(c.reportSafeIndex(16, 10) == 0); + assert(c.reportSafeIndex(16, 11) == 6); + assert(c.reportSafeIndex(16, 12) == 0); + assert(c.reportSafeIndex(16, 13) == 1); + assert(c.reportSafeIndex(16, 14) == 9); + assert(c.reportSafeIndex(16, 15) == 2); + assert(c.reportSafeIndex(16, 16) == 10); + assert(c.reportSafeIndex(16, 17) == 11); + assert(c.reportSafeIndex(16, 18) == 6); + assert(c.reportSafeIndex(16, 19) == 8); + assert(c.reportSafeIndex(16, 20) == 13); + assert(c.reportSafeIndex(16, 21) == 2); + assert(c.reportSafeIndex(16, 22) == 9); + assert(c.reportSafeIndex(16, 23) == 0); + assert(c.reportSafeIndex(16, 24) == 12); + assert(c.reportSafeIndex(16, 25) == 9); + assert(c.reportSafeIndex(16, 26) == 6); + assert(c.reportSafeIndex(16, 27) == 7); + assert(c.reportSafeIndex(16, 28) == 0); + assert(c.reportSafeIndex(16, 29) == 5); + assert(c.reportSafeIndex(16, 30) == 14); + assert(c.reportSafeIndex(16, 31) == 13); + assert(c.reportSafeIndex(16, 32) == 8); + assert(c.reportSafeIndex(16, 33) == 13); + assert(c.reportSafeIndex(16, 34) == 6); + assert(c.reportSafeIndex(16, 35) == 8); + assert(c.reportSafeIndex(16, 36) == 5); + assert(c.reportSafeIndex(16, 37) == 6); + assert(c.reportSafeIndex(16, 38) == 12); + assert(c.reportSafeIndex(16, 39) == 1); + assert(c.reportSafeIndex(16, 40) == 9); + assert(c.reportSafeIndex(16, 41) == 15); + assert(c.reportSafeIndex(17, -5) == -1); + assert(c.reportSafeIndex(17, -4) == -1); + assert(c.reportSafeIndex(17, -3) == -1); + assert(c.reportSafeIndex(17, -2) == -1); + assert(c.reportSafeIndex(17, -1) == -1); + assert(c.reportSafeIndex(17, 0) == -1); + assert(c.reportSafeIndex(17, 1) == 16); + assert(c.reportSafeIndex(17, 2) == 2); + assert(c.reportSafeIndex(17, 3) == 10); + assert(c.reportSafeIndex(17, 4) == 4); + assert(c.reportSafeIndex(17, 5) == 10); + assert(c.reportSafeIndex(17, 6) == 1); + assert(c.reportSafeIndex(17, 7) == 1); + assert(c.reportSafeIndex(17, 8) == 14); + assert(c.reportSafeIndex(17, 9) == 16); + assert(c.reportSafeIndex(17, 10) == 10); + assert(c.reportSafeIndex(17, 11) == 0); + assert(c.reportSafeIndex(17, 12) == 12); + assert(c.reportSafeIndex(17, 13) == 14); + assert(c.reportSafeIndex(17, 14) == 6); + assert(c.reportSafeIndex(17, 15) == 0); + assert(c.reportSafeIndex(17, 16) == 9); + assert(c.reportSafeIndex(17, 17) == 11); + assert(c.reportSafeIndex(17, 18) == 7); + assert(c.reportSafeIndex(17, 19) == 10); + assert(c.reportSafeIndex(17, 20) == 16); + assert(c.reportSafeIndex(17, 21) == 6); + assert(c.reportSafeIndex(17, 22) == 14); + assert(c.reportSafeIndex(17, 23) == 6); + assert(c.reportSafeIndex(17, 24) == 2); + assert(c.reportSafeIndex(17, 25) == 0); + assert(c.reportSafeIndex(17, 26) == 15); + assert(c.reportSafeIndex(17, 27) == 0); + assert(c.reportSafeIndex(17, 28) == 11); + assert(c.reportSafeIndex(17, 29) == 0); + assert(c.reportSafeIndex(17, 30) == 10); + assert(c.reportSafeIndex(17, 31) == 10); + assert(c.reportSafeIndex(17, 32) == 6); + assert(c.reportSafeIndex(17, 33) == 12); + assert(c.reportSafeIndex(17, 34) == 6); + assert(c.reportSafeIndex(17, 35) == 9); + assert(c.reportSafeIndex(17, 36) == 7); + assert(c.reportSafeIndex(17, 37) == 9); + assert(c.reportSafeIndex(17, 38) == 16); + assert(c.reportSafeIndex(17, 39) == 6); + assert(c.reportSafeIndex(17, 40) == 15); + assert(c.reportSafeIndex(17, 41) == 5); + assert(c.reportSafeIndex(18, -5) == -1); + assert(c.reportSafeIndex(18, -4) == -1); + assert(c.reportSafeIndex(18, -3) == -1); + assert(c.reportSafeIndex(18, -2) == -1); + assert(c.reportSafeIndex(18, -1) == -1); + assert(c.reportSafeIndex(18, 0) == -1); + assert(c.reportSafeIndex(18, 1) == 17); + assert(c.reportSafeIndex(18, 2) == 4); + assert(c.reportSafeIndex(18, 3) == 13); + assert(c.reportSafeIndex(18, 4) == 8); + assert(c.reportSafeIndex(18, 5) == 15); + assert(c.reportSafeIndex(18, 6) == 7); + assert(c.reportSafeIndex(18, 7) == 8); + assert(c.reportSafeIndex(18, 8) == 4); + assert(c.reportSafeIndex(18, 9) == 7); + assert(c.reportSafeIndex(18, 10) == 2); + assert(c.reportSafeIndex(18, 11) == 11); + assert(c.reportSafeIndex(18, 12) == 6); + assert(c.reportSafeIndex(18, 13) == 9); + assert(c.reportSafeIndex(18, 14) == 2); + assert(c.reportSafeIndex(18, 15) == 15); + assert(c.reportSafeIndex(18, 16) == 7); + assert(c.reportSafeIndex(18, 17) == 10); + assert(c.reportSafeIndex(18, 18) == 7); + assert(c.reportSafeIndex(18, 19) == 11); + assert(c.reportSafeIndex(18, 20) == 0); + assert(c.reportSafeIndex(18, 21) == 9); + assert(c.reportSafeIndex(18, 22) == 0); + assert(c.reportSafeIndex(18, 23) == 11); + assert(c.reportSafeIndex(18, 24) == 8); + assert(c.reportSafeIndex(18, 25) == 7); + assert(c.reportSafeIndex(18, 26) == 5); + assert(c.reportSafeIndex(18, 27) == 9); + assert(c.reportSafeIndex(18, 28) == 3); + assert(c.reportSafeIndex(18, 29) == 11); + assert(c.reportSafeIndex(18, 30) == 4); + assert(c.reportSafeIndex(18, 31) == 5); + assert(c.reportSafeIndex(18, 32) == 2); + assert(c.reportSafeIndex(18, 33) == 9); + assert(c.reportSafeIndex(18, 34) == 4); + assert(c.reportSafeIndex(18, 35) == 8); + assert(c.reportSafeIndex(18, 36) == 7); + assert(c.reportSafeIndex(18, 37) == 10); + assert(c.reportSafeIndex(18, 38) == 0); + assert(c.reportSafeIndex(18, 39) == 9); + assert(c.reportSafeIndex(18, 40) == 1); + assert(c.reportSafeIndex(18, 41) == 10); + assert(c.reportSafeIndex(19, -5) == -1); + assert(c.reportSafeIndex(19, -4) == -1); + assert(c.reportSafeIndex(19, -3) == -1); + assert(c.reportSafeIndex(19, -2) == -1); + assert(c.reportSafeIndex(19, -1) == -1); + assert(c.reportSafeIndex(19, 0) == -1); + assert(c.reportSafeIndex(19, 1) == 18); + assert(c.reportSafeIndex(19, 2) == 6); + assert(c.reportSafeIndex(19, 3) == 16); + assert(c.reportSafeIndex(19, 4) == 12); + assert(c.reportSafeIndex(19, 5) == 1); + assert(c.reportSafeIndex(19, 6) == 13); + assert(c.reportSafeIndex(19, 7) == 15); + assert(c.reportSafeIndex(19, 8) == 12); + assert(c.reportSafeIndex(19, 9) == 16); + assert(c.reportSafeIndex(19, 10) == 12); + assert(c.reportSafeIndex(19, 11) == 3); + assert(c.reportSafeIndex(19, 12) == 18); + assert(c.reportSafeIndex(19, 13) == 3); + assert(c.reportSafeIndex(19, 14) == 16); + assert(c.reportSafeIndex(19, 15) == 11); + assert(c.reportSafeIndex(19, 16) == 4); + assert(c.reportSafeIndex(19, 17) == 8); + assert(c.reportSafeIndex(19, 18) == 6); + assert(c.reportSafeIndex(19, 19) == 11); + assert(c.reportSafeIndex(19, 20) == 1); + assert(c.reportSafeIndex(19, 21) == 11); + assert(c.reportSafeIndex(19, 22) == 3); + assert(c.reportSafeIndex(19, 23) == 15); + assert(c.reportSafeIndex(19, 24) == 13); + assert(c.reportSafeIndex(19, 25) == 13); + assert(c.reportSafeIndex(19, 26) == 12); + assert(c.reportSafeIndex(19, 27) == 17); + assert(c.reportSafeIndex(19, 28) == 12); + assert(c.reportSafeIndex(19, 29) == 2); + assert(c.reportSafeIndex(19, 30) == 15); + assert(c.reportSafeIndex(19, 31) == 17); + assert(c.reportSafeIndex(19, 32) == 15); + assert(c.reportSafeIndex(19, 33) == 4); + assert(c.reportSafeIndex(19, 34) == 0); + assert(c.reportSafeIndex(19, 35) == 5); + assert(c.reportSafeIndex(19, 36) == 5); + assert(c.reportSafeIndex(19, 37) == 9); + assert(c.reportSafeIndex(19, 38) == 0); + assert(c.reportSafeIndex(19, 39) == 10); + assert(c.reportSafeIndex(19, 40) == 3); + assert(c.reportSafeIndex(19, 41) == 13); + assert(c.reportSafeIndex(20, -5) == -1); + assert(c.reportSafeIndex(20, -4) == -1); + assert(c.reportSafeIndex(20, -3) == -1); + assert(c.reportSafeIndex(20, -2) == -1); + assert(c.reportSafeIndex(20, -1) == -1); + assert(c.reportSafeIndex(20, 0) == -1); + assert(c.reportSafeIndex(20, 1) == 19); + assert(c.reportSafeIndex(20, 2) == 8); + assert(c.reportSafeIndex(20, 3) == 19); + assert(c.reportSafeIndex(20, 4) == 16); + assert(c.reportSafeIndex(20, 5) == 6); + assert(c.reportSafeIndex(20, 6) == 19); + assert(c.reportSafeIndex(20, 7) == 2); + assert(c.reportSafeIndex(20, 8) == 0); + assert(c.reportSafeIndex(20, 9) == 5); + assert(c.reportSafeIndex(20, 10) == 2); + assert(c.reportSafeIndex(20, 11) == 14); + assert(c.reportSafeIndex(20, 12) == 10); + assert(c.reportSafeIndex(20, 13) == 16); + assert(c.reportSafeIndex(20, 14) == 10); + assert(c.reportSafeIndex(20, 15) == 6); + assert(c.reportSafeIndex(20, 16) == 0); + assert(c.reportSafeIndex(20, 17) == 5); + assert(c.reportSafeIndex(20, 18) == 4); + assert(c.reportSafeIndex(20, 19) == 10); + assert(c.reportSafeIndex(20, 20) == 1); + assert(c.reportSafeIndex(20, 21) == 12); + assert(c.reportSafeIndex(20, 22) == 5); + assert(c.reportSafeIndex(20, 23) == 18); + assert(c.reportSafeIndex(20, 24) == 17); + assert(c.reportSafeIndex(20, 25) == 18); + assert(c.reportSafeIndex(20, 26) == 18); + assert(c.reportSafeIndex(20, 27) == 4); + assert(c.reportSafeIndex(20, 28) == 0); + assert(c.reportSafeIndex(20, 29) == 11); + assert(c.reportSafeIndex(20, 30) == 5); + assert(c.reportSafeIndex(20, 31) == 8); + assert(c.reportSafeIndex(20, 32) == 7); + assert(c.reportSafeIndex(20, 33) == 17); + assert(c.reportSafeIndex(20, 34) == 14); + assert(c.reportSafeIndex(20, 35) == 0); + assert(c.reportSafeIndex(20, 36) == 1); + assert(c.reportSafeIndex(20, 37) == 6); + assert(c.reportSafeIndex(20, 38) == 18); + assert(c.reportSafeIndex(20, 39) == 9); + assert(c.reportSafeIndex(20, 40) == 3); + assert(c.reportSafeIndex(20, 41) == 14); + assert(c.reportSafeIndex(21, -5) == -1); + assert(c.reportSafeIndex(21, -4) == -1); + assert(c.reportSafeIndex(21, -3) == -1); + assert(c.reportSafeIndex(21, -2) == -1); + assert(c.reportSafeIndex(21, -1) == -1); + assert(c.reportSafeIndex(21, 0) == -1); + assert(c.reportSafeIndex(21, 1) == -1); + assert(c.reportSafeIndex(21, 2) == -1); + assert(c.reportSafeIndex(21, 3) == -1); + assert(c.reportSafeIndex(21, 4) == -1); + assert(c.reportSafeIndex(21, 5) == -1); + assert(c.reportSafeIndex(21, 6) == -1); + assert(c.reportSafeIndex(21, 7) == -1); + assert(c.reportSafeIndex(21, 8) == -1); + assert(c.reportSafeIndex(21, 9) == -1); + assert(c.reportSafeIndex(21, 10) == -1); + assert(c.reportSafeIndex(21, 11) == -1); + assert(c.reportSafeIndex(21, 12) == -1); + assert(c.reportSafeIndex(21, 13) == -1); + assert(c.reportSafeIndex(21, 14) == -1); + assert(c.reportSafeIndex(21, 15) == -1); + assert(c.reportSafeIndex(21, 16) == -1); + assert(c.reportSafeIndex(21, 17) == -1); + assert(c.reportSafeIndex(21, 18) == -1); + assert(c.reportSafeIndex(21, 19) == -1); + assert(c.reportSafeIndex(21, 20) == -1); + assert(c.reportSafeIndex(21, 21) == -1); + assert(c.reportSafeIndex(21, 22) == -1); + assert(c.reportSafeIndex(21, 23) == -1); + assert(c.reportSafeIndex(21, 24) == -1); + assert(c.reportSafeIndex(21, 25) == -1); + assert(c.reportSafeIndex(21, 26) == -1); + assert(c.reportSafeIndex(21, 27) == -1); + assert(c.reportSafeIndex(21, 28) == -1); + assert(c.reportSafeIndex(21, 29) == -1); + assert(c.reportSafeIndex(21, 30) == -1); + assert(c.reportSafeIndex(21, 31) == -1); + assert(c.reportSafeIndex(21, 32) == -1); + assert(c.reportSafeIndex(21, 33) == -1); + assert(c.reportSafeIndex(21, 34) == -1); + assert(c.reportSafeIndex(21, 35) == -1); + assert(c.reportSafeIndex(21, 36) == -1); + assert(c.reportSafeIndex(21, 37) == -1); + assert(c.reportSafeIndex(21, 38) == -1); + assert(c.reportSafeIndex(21, 39) == -1); + assert(c.reportSafeIndex(21, 40) == -1); + assert(c.reportSafeIndex(21, 41) == -1); + assert(c.reportSafeIndex(22, -5) == -1); + assert(c.reportSafeIndex(22, -4) == -1); + assert(c.reportSafeIndex(22, -3) == -1); + assert(c.reportSafeIndex(22, -2) == -1); + assert(c.reportSafeIndex(22, -1) == -1); + assert(c.reportSafeIndex(22, 0) == -1); + assert(c.reportSafeIndex(22, 1) == -1); + assert(c.reportSafeIndex(22, 2) == -1); + assert(c.reportSafeIndex(22, 3) == -1); + assert(c.reportSafeIndex(22, 4) == -1); + assert(c.reportSafeIndex(22, 5) == -1); + assert(c.reportSafeIndex(22, 6) == -1); + assert(c.reportSafeIndex(22, 7) == -1); + assert(c.reportSafeIndex(22, 8) == -1); + assert(c.reportSafeIndex(22, 9) == -1); + assert(c.reportSafeIndex(22, 10) == -1); + assert(c.reportSafeIndex(22, 11) == -1); + assert(c.reportSafeIndex(22, 12) == -1); + assert(c.reportSafeIndex(22, 13) == -1); + assert(c.reportSafeIndex(22, 14) == -1); + assert(c.reportSafeIndex(22, 15) == -1); + assert(c.reportSafeIndex(22, 16) == -1); + assert(c.reportSafeIndex(22, 17) == -1); + assert(c.reportSafeIndex(22, 18) == -1); + assert(c.reportSafeIndex(22, 19) == -1); + assert(c.reportSafeIndex(22, 20) == -1); + assert(c.reportSafeIndex(22, 21) == -1); + assert(c.reportSafeIndex(22, 22) == -1); + assert(c.reportSafeIndex(22, 23) == -1); + assert(c.reportSafeIndex(22, 24) == -1); + assert(c.reportSafeIndex(22, 25) == -1); + assert(c.reportSafeIndex(22, 26) == -1); + assert(c.reportSafeIndex(22, 27) == -1); + assert(c.reportSafeIndex(22, 28) == -1); + assert(c.reportSafeIndex(22, 29) == -1); + assert(c.reportSafeIndex(22, 30) == -1); + assert(c.reportSafeIndex(22, 31) == -1); + assert(c.reportSafeIndex(22, 32) == -1); + assert(c.reportSafeIndex(22, 33) == -1); + assert(c.reportSafeIndex(22, 34) == -1); + assert(c.reportSafeIndex(22, 35) == -1); + assert(c.reportSafeIndex(22, 36) == -1); + assert(c.reportSafeIndex(22, 37) == -1); + assert(c.reportSafeIndex(22, 38) == -1); + assert(c.reportSafeIndex(22, 39) == -1); + assert(c.reportSafeIndex(22, 40) == -1); + assert(c.reportSafeIndex(22, 41) == -1); + assert(c.reportSafeIndex(23, -5) == -1); + assert(c.reportSafeIndex(23, -4) == -1); + assert(c.reportSafeIndex(23, -3) == -1); + assert(c.reportSafeIndex(23, -2) == -1); + assert(c.reportSafeIndex(23, -1) == -1); + assert(c.reportSafeIndex(23, 0) == -1); + assert(c.reportSafeIndex(23, 1) == -1); + assert(c.reportSafeIndex(23, 2) == -1); + assert(c.reportSafeIndex(23, 3) == -1); + assert(c.reportSafeIndex(23, 4) == -1); + assert(c.reportSafeIndex(23, 5) == -1); + assert(c.reportSafeIndex(23, 6) == -1); + assert(c.reportSafeIndex(23, 7) == -1); + assert(c.reportSafeIndex(23, 8) == -1); + assert(c.reportSafeIndex(23, 9) == -1); + assert(c.reportSafeIndex(23, 10) == -1); + assert(c.reportSafeIndex(23, 11) == -1); + assert(c.reportSafeIndex(23, 12) == -1); + assert(c.reportSafeIndex(23, 13) == -1); + assert(c.reportSafeIndex(23, 14) == -1); + assert(c.reportSafeIndex(23, 15) == -1); + assert(c.reportSafeIndex(23, 16) == -1); + assert(c.reportSafeIndex(23, 17) == -1); + assert(c.reportSafeIndex(23, 18) == -1); + assert(c.reportSafeIndex(23, 19) == -1); + assert(c.reportSafeIndex(23, 20) == -1); + assert(c.reportSafeIndex(23, 21) == -1); + assert(c.reportSafeIndex(23, 22) == -1); + assert(c.reportSafeIndex(23, 23) == -1); + assert(c.reportSafeIndex(23, 24) == -1); + assert(c.reportSafeIndex(23, 25) == -1); + assert(c.reportSafeIndex(23, 26) == -1); + assert(c.reportSafeIndex(23, 27) == -1); + assert(c.reportSafeIndex(23, 28) == -1); + assert(c.reportSafeIndex(23, 29) == -1); + assert(c.reportSafeIndex(23, 30) == -1); + assert(c.reportSafeIndex(23, 31) == -1); + assert(c.reportSafeIndex(23, 32) == -1); + assert(c.reportSafeIndex(23, 33) == -1); + assert(c.reportSafeIndex(23, 34) == -1); + assert(c.reportSafeIndex(23, 35) == -1); + assert(c.reportSafeIndex(23, 36) == -1); + assert(c.reportSafeIndex(23, 37) == -1); + assert(c.reportSafeIndex(23, 38) == -1); + assert(c.reportSafeIndex(23, 39) == -1); + assert(c.reportSafeIndex(23, 40) == -1); + assert(c.reportSafeIndex(23, 41) == -1); + assert(c.reportSafeIndex(24, -5) == -1); + assert(c.reportSafeIndex(24, -4) == -1); + assert(c.reportSafeIndex(24, -3) == -1); + assert(c.reportSafeIndex(24, -2) == -1); + assert(c.reportSafeIndex(24, -1) == -1); + assert(c.reportSafeIndex(24, 0) == -1); + assert(c.reportSafeIndex(24, 1) == -1); + assert(c.reportSafeIndex(24, 2) == -1); + assert(c.reportSafeIndex(24, 3) == -1); + assert(c.reportSafeIndex(24, 4) == -1); + assert(c.reportSafeIndex(24, 5) == -1); + assert(c.reportSafeIndex(24, 6) == -1); + assert(c.reportSafeIndex(24, 7) == -1); + assert(c.reportSafeIndex(24, 8) == -1); + assert(c.reportSafeIndex(24, 9) == -1); + assert(c.reportSafeIndex(24, 10) == -1); + assert(c.reportSafeIndex(24, 11) == -1); + assert(c.reportSafeIndex(24, 12) == -1); + assert(c.reportSafeIndex(24, 13) == -1); + assert(c.reportSafeIndex(24, 14) == -1); + assert(c.reportSafeIndex(24, 15) == -1); + assert(c.reportSafeIndex(24, 16) == -1); + assert(c.reportSafeIndex(24, 17) == -1); + assert(c.reportSafeIndex(24, 18) == -1); + assert(c.reportSafeIndex(24, 19) == -1); + assert(c.reportSafeIndex(24, 20) == -1); + assert(c.reportSafeIndex(24, 21) == -1); + assert(c.reportSafeIndex(24, 22) == -1); + assert(c.reportSafeIndex(24, 23) == -1); + assert(c.reportSafeIndex(24, 24) == -1); + assert(c.reportSafeIndex(24, 25) == -1); + assert(c.reportSafeIndex(24, 26) == -1); + assert(c.reportSafeIndex(24, 27) == -1); + assert(c.reportSafeIndex(24, 28) == -1); + assert(c.reportSafeIndex(24, 29) == -1); + assert(c.reportSafeIndex(24, 30) == -1); + assert(c.reportSafeIndex(24, 31) == -1); + assert(c.reportSafeIndex(24, 32) == -1); + assert(c.reportSafeIndex(24, 33) == -1); + assert(c.reportSafeIndex(24, 34) == -1); + assert(c.reportSafeIndex(24, 35) == -1); + assert(c.reportSafeIndex(24, 36) == -1); + assert(c.reportSafeIndex(24, 37) == -1); + assert(c.reportSafeIndex(24, 38) == -1); + assert(c.reportSafeIndex(24, 39) == -1); + assert(c.reportSafeIndex(24, 40) == -1); + assert(c.reportSafeIndex(24, 41) == -1); + assert(c.reportSafeIndex(25, -5) == -1); + assert(c.reportSafeIndex(25, -4) == -1); + assert(c.reportSafeIndex(25, -3) == -1); + assert(c.reportSafeIndex(25, -2) == -1); + assert(c.reportSafeIndex(25, -1) == -1); + assert(c.reportSafeIndex(25, 0) == -1); + assert(c.reportSafeIndex(25, 1) == -1); + assert(c.reportSafeIndex(25, 2) == -1); + assert(c.reportSafeIndex(25, 3) == -1); + assert(c.reportSafeIndex(25, 4) == -1); + assert(c.reportSafeIndex(25, 5) == -1); + assert(c.reportSafeIndex(25, 6) == -1); + assert(c.reportSafeIndex(25, 7) == -1); + assert(c.reportSafeIndex(25, 8) == -1); + assert(c.reportSafeIndex(25, 9) == -1); + assert(c.reportSafeIndex(25, 10) == -1); + assert(c.reportSafeIndex(25, 11) == -1); + assert(c.reportSafeIndex(25, 12) == -1); + assert(c.reportSafeIndex(25, 13) == -1); + assert(c.reportSafeIndex(25, 14) == -1); + assert(c.reportSafeIndex(25, 15) == -1); + assert(c.reportSafeIndex(25, 16) == -1); + assert(c.reportSafeIndex(25, 17) == -1); + assert(c.reportSafeIndex(25, 18) == -1); + assert(c.reportSafeIndex(25, 19) == -1); + assert(c.reportSafeIndex(25, 20) == -1); + assert(c.reportSafeIndex(25, 21) == -1); + assert(c.reportSafeIndex(25, 22) == -1); + assert(c.reportSafeIndex(25, 23) == -1); + assert(c.reportSafeIndex(25, 24) == -1); + assert(c.reportSafeIndex(25, 25) == -1); + assert(c.reportSafeIndex(25, 26) == -1); + assert(c.reportSafeIndex(25, 27) == -1); + assert(c.reportSafeIndex(25, 28) == -1); + assert(c.reportSafeIndex(25, 29) == -1); + assert(c.reportSafeIndex(25, 30) == -1); + assert(c.reportSafeIndex(25, 31) == -1); + assert(c.reportSafeIndex(25, 32) == -1); + assert(c.reportSafeIndex(25, 33) == -1); + assert(c.reportSafeIndex(25, 34) == -1); + assert(c.reportSafeIndex(25, 35) == -1); + assert(c.reportSafeIndex(25, 36) == -1); + assert(c.reportSafeIndex(25, 37) == -1); + assert(c.reportSafeIndex(25, 38) == -1); + assert(c.reportSafeIndex(25, 39) == -1); + assert(c.reportSafeIndex(25, 40) == -1); + assert(c.reportSafeIndex(25, 41) == -1); + assert(c.reportSafeIndex(26, -5) == -1); + assert(c.reportSafeIndex(26, -4) == -1); + assert(c.reportSafeIndex(26, -3) == -1); + assert(c.reportSafeIndex(26, -2) == -1); + assert(c.reportSafeIndex(26, -1) == -1); + assert(c.reportSafeIndex(26, 0) == -1); + assert(c.reportSafeIndex(26, 1) == -1); + assert(c.reportSafeIndex(26, 2) == -1); + assert(c.reportSafeIndex(26, 3) == -1); + assert(c.reportSafeIndex(26, 4) == -1); + assert(c.reportSafeIndex(26, 5) == -1); + assert(c.reportSafeIndex(26, 6) == -1); + assert(c.reportSafeIndex(26, 7) == -1); + assert(c.reportSafeIndex(26, 8) == -1); + assert(c.reportSafeIndex(26, 9) == -1); + assert(c.reportSafeIndex(26, 10) == -1); + assert(c.reportSafeIndex(26, 11) == -1); + assert(c.reportSafeIndex(26, 12) == -1); + assert(c.reportSafeIndex(26, 13) == -1); + assert(c.reportSafeIndex(26, 14) == -1); + assert(c.reportSafeIndex(26, 15) == -1); + assert(c.reportSafeIndex(26, 16) == -1); + assert(c.reportSafeIndex(26, 17) == -1); + assert(c.reportSafeIndex(26, 18) == -1); + assert(c.reportSafeIndex(26, 19) == -1); + assert(c.reportSafeIndex(26, 20) == -1); + assert(c.reportSafeIndex(26, 21) == -1); + assert(c.reportSafeIndex(26, 22) == -1); + assert(c.reportSafeIndex(26, 23) == -1); + assert(c.reportSafeIndex(26, 24) == -1); + assert(c.reportSafeIndex(26, 25) == -1); + assert(c.reportSafeIndex(26, 26) == -1); + assert(c.reportSafeIndex(26, 27) == -1); + assert(c.reportSafeIndex(26, 28) == -1); + assert(c.reportSafeIndex(26, 29) == -1); + assert(c.reportSafeIndex(26, 30) == -1); + assert(c.reportSafeIndex(26, 31) == -1); + assert(c.reportSafeIndex(26, 32) == -1); + assert(c.reportSafeIndex(26, 33) == -1); + assert(c.reportSafeIndex(26, 34) == -1); + assert(c.reportSafeIndex(26, 35) == -1); + assert(c.reportSafeIndex(26, 36) == -1); + assert(c.reportSafeIndex(26, 37) == -1); + assert(c.reportSafeIndex(26, 38) == -1); + assert(c.reportSafeIndex(26, 39) == -1); + assert(c.reportSafeIndex(26, 40) == -1); + assert(c.reportSafeIndex(26, 41) == -1); + assert(c.reportSafeIndex(27, -5) == -1); + assert(c.reportSafeIndex(27, -4) == -1); + assert(c.reportSafeIndex(27, -3) == -1); + assert(c.reportSafeIndex(27, -2) == -1); + assert(c.reportSafeIndex(27, -1) == -1); + assert(c.reportSafeIndex(27, 0) == -1); + assert(c.reportSafeIndex(27, 1) == -1); + assert(c.reportSafeIndex(27, 2) == -1); + assert(c.reportSafeIndex(27, 3) == -1); + assert(c.reportSafeIndex(27, 4) == -1); + assert(c.reportSafeIndex(27, 5) == -1); + assert(c.reportSafeIndex(27, 6) == -1); + assert(c.reportSafeIndex(27, 7) == -1); + assert(c.reportSafeIndex(27, 8) == -1); + assert(c.reportSafeIndex(27, 9) == -1); + assert(c.reportSafeIndex(27, 10) == -1); + assert(c.reportSafeIndex(27, 11) == -1); + assert(c.reportSafeIndex(27, 12) == -1); + assert(c.reportSafeIndex(27, 13) == -1); + assert(c.reportSafeIndex(27, 14) == -1); + assert(c.reportSafeIndex(27, 15) == -1); + assert(c.reportSafeIndex(27, 16) == -1); + assert(c.reportSafeIndex(27, 17) == -1); + assert(c.reportSafeIndex(27, 18) == -1); + assert(c.reportSafeIndex(27, 19) == -1); + assert(c.reportSafeIndex(27, 20) == -1); + assert(c.reportSafeIndex(27, 21) == -1); + assert(c.reportSafeIndex(27, 22) == -1); + assert(c.reportSafeIndex(27, 23) == -1); + assert(c.reportSafeIndex(27, 24) == -1); + assert(c.reportSafeIndex(27, 25) == -1); + assert(c.reportSafeIndex(27, 26) == -1); + assert(c.reportSafeIndex(27, 27) == -1); + assert(c.reportSafeIndex(27, 28) == -1); + assert(c.reportSafeIndex(27, 29) == -1); + assert(c.reportSafeIndex(27, 30) == -1); + assert(c.reportSafeIndex(27, 31) == -1); + assert(c.reportSafeIndex(27, 32) == -1); + assert(c.reportSafeIndex(27, 33) == -1); + assert(c.reportSafeIndex(27, 34) == -1); + assert(c.reportSafeIndex(27, 35) == -1); + assert(c.reportSafeIndex(27, 36) == -1); + assert(c.reportSafeIndex(27, 37) == -1); + assert(c.reportSafeIndex(27, 38) == -1); + assert(c.reportSafeIndex(27, 39) == -1); + assert(c.reportSafeIndex(27, 40) == -1); + assert(c.reportSafeIndex(27, 41) == -1); + assert(c.reportSafeIndex(28, -5) == -1); + assert(c.reportSafeIndex(28, -4) == -1); + assert(c.reportSafeIndex(28, -3) == -1); + assert(c.reportSafeIndex(28, -2) == -1); + assert(c.reportSafeIndex(28, -1) == -1); + assert(c.reportSafeIndex(28, 0) == -1); + assert(c.reportSafeIndex(28, 1) == -1); + assert(c.reportSafeIndex(28, 2) == -1); + assert(c.reportSafeIndex(28, 3) == -1); + assert(c.reportSafeIndex(28, 4) == -1); + assert(c.reportSafeIndex(28, 5) == -1); + assert(c.reportSafeIndex(28, 6) == -1); + assert(c.reportSafeIndex(28, 7) == -1); + assert(c.reportSafeIndex(28, 8) == -1); + assert(c.reportSafeIndex(28, 9) == -1); + assert(c.reportSafeIndex(28, 10) == -1); + assert(c.reportSafeIndex(28, 11) == -1); + assert(c.reportSafeIndex(28, 12) == -1); + assert(c.reportSafeIndex(28, 13) == -1); + assert(c.reportSafeIndex(28, 14) == -1); + assert(c.reportSafeIndex(28, 15) == -1); + assert(c.reportSafeIndex(28, 16) == -1); + assert(c.reportSafeIndex(28, 17) == -1); + assert(c.reportSafeIndex(28, 18) == -1); + assert(c.reportSafeIndex(28, 19) == -1); + assert(c.reportSafeIndex(28, 20) == -1); + assert(c.reportSafeIndex(28, 21) == -1); + assert(c.reportSafeIndex(28, 22) == -1); + assert(c.reportSafeIndex(28, 23) == -1); + assert(c.reportSafeIndex(28, 24) == -1); + assert(c.reportSafeIndex(28, 25) == -1); + assert(c.reportSafeIndex(28, 26) == -1); + assert(c.reportSafeIndex(28, 27) == -1); + assert(c.reportSafeIndex(28, 28) == -1); + assert(c.reportSafeIndex(28, 29) == -1); + assert(c.reportSafeIndex(28, 30) == -1); + assert(c.reportSafeIndex(28, 31) == -1); + assert(c.reportSafeIndex(28, 32) == -1); + assert(c.reportSafeIndex(28, 33) == -1); + assert(c.reportSafeIndex(28, 34) == -1); + assert(c.reportSafeIndex(28, 35) == -1); + assert(c.reportSafeIndex(28, 36) == -1); + assert(c.reportSafeIndex(28, 37) == -1); + assert(c.reportSafeIndex(28, 38) == -1); + assert(c.reportSafeIndex(28, 39) == -1); + assert(c.reportSafeIndex(28, 40) == -1); + assert(c.reportSafeIndex(28, 41) == -1); + assert(c.reportSafeIndex(29, -5) == -1); + assert(c.reportSafeIndex(29, -4) == -1); + assert(c.reportSafeIndex(29, -3) == -1); + assert(c.reportSafeIndex(29, -2) == -1); + assert(c.reportSafeIndex(29, -1) == -1); + assert(c.reportSafeIndex(29, 0) == -1); + assert(c.reportSafeIndex(29, 1) == -1); + assert(c.reportSafeIndex(29, 2) == -1); + assert(c.reportSafeIndex(29, 3) == -1); + assert(c.reportSafeIndex(29, 4) == -1); + assert(c.reportSafeIndex(29, 5) == -1); + assert(c.reportSafeIndex(29, 6) == -1); + assert(c.reportSafeIndex(29, 7) == -1); + assert(c.reportSafeIndex(29, 8) == -1); + assert(c.reportSafeIndex(29, 9) == -1); + assert(c.reportSafeIndex(29, 10) == -1); + assert(c.reportSafeIndex(29, 11) == -1); + assert(c.reportSafeIndex(29, 12) == -1); + assert(c.reportSafeIndex(29, 13) == -1); + assert(c.reportSafeIndex(29, 14) == -1); + assert(c.reportSafeIndex(29, 15) == -1); + assert(c.reportSafeIndex(29, 16) == -1); + assert(c.reportSafeIndex(29, 17) == -1); + assert(c.reportSafeIndex(29, 18) == -1); + assert(c.reportSafeIndex(29, 19) == -1); + assert(c.reportSafeIndex(29, 20) == -1); + assert(c.reportSafeIndex(29, 21) == -1); + assert(c.reportSafeIndex(29, 22) == -1); + assert(c.reportSafeIndex(29, 23) == -1); + assert(c.reportSafeIndex(29, 24) == -1); + assert(c.reportSafeIndex(29, 25) == -1); + assert(c.reportSafeIndex(29, 26) == -1); + assert(c.reportSafeIndex(29, 27) == -1); + assert(c.reportSafeIndex(29, 28) == -1); + assert(c.reportSafeIndex(29, 29) == -1); + assert(c.reportSafeIndex(29, 30) == -1); + assert(c.reportSafeIndex(29, 31) == -1); + assert(c.reportSafeIndex(29, 32) == -1); + assert(c.reportSafeIndex(29, 33) == -1); + assert(c.reportSafeIndex(29, 34) == -1); + assert(c.reportSafeIndex(29, 35) == -1); + assert(c.reportSafeIndex(29, 36) == -1); + assert(c.reportSafeIndex(29, 37) == -1); + assert(c.reportSafeIndex(29, 38) == -1); + assert(c.reportSafeIndex(29, 39) == -1); + assert(c.reportSafeIndex(29, 40) == -1); + assert(c.reportSafeIndex(29, 41) == -1); + assert(c.reportSafeIndex(30, -5) == -1); + assert(c.reportSafeIndex(30, -4) == -1); + assert(c.reportSafeIndex(30, -3) == -1); + assert(c.reportSafeIndex(30, -2) == -1); + assert(c.reportSafeIndex(30, -1) == -1); + assert(c.reportSafeIndex(30, 0) == -1); + assert(c.reportSafeIndex(30, 1) == -1); + assert(c.reportSafeIndex(30, 2) == -1); + assert(c.reportSafeIndex(30, 3) == -1); + assert(c.reportSafeIndex(30, 4) == -1); + assert(c.reportSafeIndex(30, 5) == -1); + assert(c.reportSafeIndex(30, 6) == -1); + assert(c.reportSafeIndex(30, 7) == -1); + assert(c.reportSafeIndex(30, 8) == -1); + assert(c.reportSafeIndex(30, 9) == -1); + assert(c.reportSafeIndex(30, 10) == -1); + assert(c.reportSafeIndex(30, 11) == -1); + assert(c.reportSafeIndex(30, 12) == -1); + assert(c.reportSafeIndex(30, 13) == -1); + assert(c.reportSafeIndex(30, 14) == -1); + assert(c.reportSafeIndex(30, 15) == -1); + assert(c.reportSafeIndex(30, 16) == -1); + assert(c.reportSafeIndex(30, 17) == -1); + assert(c.reportSafeIndex(30, 18) == -1); + assert(c.reportSafeIndex(30, 19) == -1); + assert(c.reportSafeIndex(30, 20) == -1); + assert(c.reportSafeIndex(30, 21) == -1); + assert(c.reportSafeIndex(30, 22) == -1); + assert(c.reportSafeIndex(30, 23) == -1); + assert(c.reportSafeIndex(30, 24) == -1); + assert(c.reportSafeIndex(30, 25) == -1); + assert(c.reportSafeIndex(30, 26) == -1); + assert(c.reportSafeIndex(30, 27) == -1); + assert(c.reportSafeIndex(30, 28) == -1); + assert(c.reportSafeIndex(30, 29) == -1); + assert(c.reportSafeIndex(30, 30) == -1); + assert(c.reportSafeIndex(30, 31) == -1); + assert(c.reportSafeIndex(30, 32) == -1); + assert(c.reportSafeIndex(30, 33) == -1); + assert(c.reportSafeIndex(30, 34) == -1); + assert(c.reportSafeIndex(30, 35) == -1); + assert(c.reportSafeIndex(30, 36) == -1); + assert(c.reportSafeIndex(30, 37) == -1); + assert(c.reportSafeIndex(30, 38) == -1); + assert(c.reportSafeIndex(30, 39) == -1); + assert(c.reportSafeIndex(30, 40) == -1); + assert(c.reportSafeIndex(30, 41) == -1); + assert(c.reportSafeIndex(31, -5) == -1); + assert(c.reportSafeIndex(31, -4) == -1); + assert(c.reportSafeIndex(31, -3) == -1); + assert(c.reportSafeIndex(31, -2) == -1); + assert(c.reportSafeIndex(31, -1) == -1); + assert(c.reportSafeIndex(31, 0) == -1); + assert(c.reportSafeIndex(31, 1) == -1); + assert(c.reportSafeIndex(31, 2) == -1); + assert(c.reportSafeIndex(31, 3) == -1); + assert(c.reportSafeIndex(31, 4) == -1); + assert(c.reportSafeIndex(31, 5) == -1); + assert(c.reportSafeIndex(31, 6) == -1); + assert(c.reportSafeIndex(31, 7) == -1); + assert(c.reportSafeIndex(31, 8) == -1); + assert(c.reportSafeIndex(31, 9) == -1); + assert(c.reportSafeIndex(31, 10) == -1); + assert(c.reportSafeIndex(31, 11) == -1); + assert(c.reportSafeIndex(31, 12) == -1); + assert(c.reportSafeIndex(31, 13) == -1); + assert(c.reportSafeIndex(31, 14) == -1); + assert(c.reportSafeIndex(31, 15) == -1); + assert(c.reportSafeIndex(31, 16) == -1); + assert(c.reportSafeIndex(31, 17) == -1); + assert(c.reportSafeIndex(31, 18) == -1); + assert(c.reportSafeIndex(31, 19) == -1); + assert(c.reportSafeIndex(31, 20) == -1); + assert(c.reportSafeIndex(31, 21) == -1); + assert(c.reportSafeIndex(31, 22) == -1); + assert(c.reportSafeIndex(31, 23) == -1); + assert(c.reportSafeIndex(31, 24) == -1); + assert(c.reportSafeIndex(31, 25) == -1); + assert(c.reportSafeIndex(31, 26) == -1); + assert(c.reportSafeIndex(31, 27) == -1); + assert(c.reportSafeIndex(31, 28) == -1); + assert(c.reportSafeIndex(31, 29) == -1); + assert(c.reportSafeIndex(31, 30) == -1); + assert(c.reportSafeIndex(31, 31) == -1); + assert(c.reportSafeIndex(31, 32) == -1); + assert(c.reportSafeIndex(31, 33) == -1); + assert(c.reportSafeIndex(31, 34) == -1); + assert(c.reportSafeIndex(31, 35) == -1); + assert(c.reportSafeIndex(31, 36) == -1); + assert(c.reportSafeIndex(31, 37) == -1); + assert(c.reportSafeIndex(31, 38) == -1); + assert(c.reportSafeIndex(31, 39) == -1); + assert(c.reportSafeIndex(31, 40) == -1); + assert(c.reportSafeIndex(31, 41) == -1); + assert(c.reportSafeIndex(32, -5) == -1); + assert(c.reportSafeIndex(32, -4) == -1); + assert(c.reportSafeIndex(32, -3) == -1); + assert(c.reportSafeIndex(32, -2) == -1); + assert(c.reportSafeIndex(32, -1) == -1); + assert(c.reportSafeIndex(32, 0) == -1); + assert(c.reportSafeIndex(32, 1) == -1); + assert(c.reportSafeIndex(32, 2) == -1); + assert(c.reportSafeIndex(32, 3) == -1); + assert(c.reportSafeIndex(32, 4) == -1); + assert(c.reportSafeIndex(32, 5) == -1); + assert(c.reportSafeIndex(32, 6) == -1); + assert(c.reportSafeIndex(32, 7) == -1); + assert(c.reportSafeIndex(32, 8) == -1); + assert(c.reportSafeIndex(32, 9) == -1); + assert(c.reportSafeIndex(32, 10) == -1); + assert(c.reportSafeIndex(32, 11) == -1); + assert(c.reportSafeIndex(32, 12) == -1); + assert(c.reportSafeIndex(32, 13) == -1); + assert(c.reportSafeIndex(32, 14) == -1); + assert(c.reportSafeIndex(32, 15) == -1); + assert(c.reportSafeIndex(32, 16) == -1); + assert(c.reportSafeIndex(32, 17) == -1); + assert(c.reportSafeIndex(32, 18) == -1); + assert(c.reportSafeIndex(32, 19) == -1); + assert(c.reportSafeIndex(32, 20) == -1); + assert(c.reportSafeIndex(32, 21) == -1); + assert(c.reportSafeIndex(32, 22) == -1); + assert(c.reportSafeIndex(32, 23) == -1); + assert(c.reportSafeIndex(32, 24) == -1); + assert(c.reportSafeIndex(32, 25) == -1); + assert(c.reportSafeIndex(32, 26) == -1); + assert(c.reportSafeIndex(32, 27) == -1); + assert(c.reportSafeIndex(32, 28) == -1); + assert(c.reportSafeIndex(32, 29) == -1); + assert(c.reportSafeIndex(32, 30) == -1); + assert(c.reportSafeIndex(32, 31) == -1); + assert(c.reportSafeIndex(32, 32) == -1); + assert(c.reportSafeIndex(32, 33) == -1); + assert(c.reportSafeIndex(32, 34) == -1); + assert(c.reportSafeIndex(32, 35) == -1); + assert(c.reportSafeIndex(32, 36) == -1); + assert(c.reportSafeIndex(32, 37) == -1); + assert(c.reportSafeIndex(32, 38) == -1); + assert(c.reportSafeIndex(32, 39) == -1); + assert(c.reportSafeIndex(32, 40) == -1); + assert(c.reportSafeIndex(32, 41) == -1); + assert(c.reportSafeIndex(33, -5) == -1); + assert(c.reportSafeIndex(33, -4) == -1); + assert(c.reportSafeIndex(33, -3) == -1); + assert(c.reportSafeIndex(33, -2) == -1); + assert(c.reportSafeIndex(33, -1) == -1); + assert(c.reportSafeIndex(33, 0) == -1); + assert(c.reportSafeIndex(33, 1) == -1); + assert(c.reportSafeIndex(33, 2) == -1); + assert(c.reportSafeIndex(33, 3) == -1); + assert(c.reportSafeIndex(33, 4) == -1); + assert(c.reportSafeIndex(33, 5) == -1); + assert(c.reportSafeIndex(33, 6) == -1); + assert(c.reportSafeIndex(33, 7) == -1); + assert(c.reportSafeIndex(33, 8) == -1); + assert(c.reportSafeIndex(33, 9) == -1); + assert(c.reportSafeIndex(33, 10) == -1); + assert(c.reportSafeIndex(33, 11) == -1); + assert(c.reportSafeIndex(33, 12) == -1); + assert(c.reportSafeIndex(33, 13) == -1); + assert(c.reportSafeIndex(33, 14) == -1); + assert(c.reportSafeIndex(33, 15) == -1); + assert(c.reportSafeIndex(33, 16) == -1); + assert(c.reportSafeIndex(33, 17) == -1); + assert(c.reportSafeIndex(33, 18) == -1); + assert(c.reportSafeIndex(33, 19) == -1); + assert(c.reportSafeIndex(33, 20) == -1); + assert(c.reportSafeIndex(33, 21) == -1); + assert(c.reportSafeIndex(33, 22) == -1); + assert(c.reportSafeIndex(33, 23) == -1); + assert(c.reportSafeIndex(33, 24) == -1); + assert(c.reportSafeIndex(33, 25) == -1); + assert(c.reportSafeIndex(33, 26) == -1); + assert(c.reportSafeIndex(33, 27) == -1); + assert(c.reportSafeIndex(33, 28) == -1); + assert(c.reportSafeIndex(33, 29) == -1); + assert(c.reportSafeIndex(33, 30) == -1); + assert(c.reportSafeIndex(33, 31) == -1); + assert(c.reportSafeIndex(33, 32) == -1); + assert(c.reportSafeIndex(33, 33) == -1); + assert(c.reportSafeIndex(33, 34) == -1); + assert(c.reportSafeIndex(33, 35) == -1); + assert(c.reportSafeIndex(33, 36) == -1); + assert(c.reportSafeIndex(33, 37) == -1); + assert(c.reportSafeIndex(33, 38) == -1); + assert(c.reportSafeIndex(33, 39) == -1); + assert(c.reportSafeIndex(33, 40) == -1); + assert(c.reportSafeIndex(33, 41) == -1); + assert(c.reportSafeIndex(34, -5) == -1); + assert(c.reportSafeIndex(34, -4) == -1); + assert(c.reportSafeIndex(34, -3) == -1); + assert(c.reportSafeIndex(34, -2) == -1); + assert(c.reportSafeIndex(34, -1) == -1); + assert(c.reportSafeIndex(34, 0) == -1); + assert(c.reportSafeIndex(34, 1) == -1); + assert(c.reportSafeIndex(34, 2) == -1); + assert(c.reportSafeIndex(34, 3) == -1); + assert(c.reportSafeIndex(34, 4) == -1); + assert(c.reportSafeIndex(34, 5) == -1); + assert(c.reportSafeIndex(34, 6) == -1); + assert(c.reportSafeIndex(34, 7) == -1); + assert(c.reportSafeIndex(34, 8) == -1); + assert(c.reportSafeIndex(34, 9) == -1); + assert(c.reportSafeIndex(34, 10) == -1); + assert(c.reportSafeIndex(34, 11) == -1); + assert(c.reportSafeIndex(34, 12) == -1); + assert(c.reportSafeIndex(34, 13) == -1); + assert(c.reportSafeIndex(34, 14) == -1); + assert(c.reportSafeIndex(34, 15) == -1); + assert(c.reportSafeIndex(34, 16) == -1); + assert(c.reportSafeIndex(34, 17) == -1); + assert(c.reportSafeIndex(34, 18) == -1); + assert(c.reportSafeIndex(34, 19) == -1); + assert(c.reportSafeIndex(34, 20) == -1); + assert(c.reportSafeIndex(34, 21) == -1); + assert(c.reportSafeIndex(34, 22) == -1); + assert(c.reportSafeIndex(34, 23) == -1); + assert(c.reportSafeIndex(34, 24) == -1); + assert(c.reportSafeIndex(34, 25) == -1); + assert(c.reportSafeIndex(34, 26) == -1); + assert(c.reportSafeIndex(34, 27) == -1); + assert(c.reportSafeIndex(34, 28) == -1); + assert(c.reportSafeIndex(34, 29) == -1); + assert(c.reportSafeIndex(34, 30) == -1); + assert(c.reportSafeIndex(34, 31) == -1); + assert(c.reportSafeIndex(34, 32) == -1); + assert(c.reportSafeIndex(34, 33) == -1); + assert(c.reportSafeIndex(34, 34) == -1); + assert(c.reportSafeIndex(34, 35) == -1); + assert(c.reportSafeIndex(34, 36) == -1); + assert(c.reportSafeIndex(34, 37) == -1); + assert(c.reportSafeIndex(34, 38) == -1); + assert(c.reportSafeIndex(34, 39) == -1); + assert(c.reportSafeIndex(34, 40) == -1); + assert(c.reportSafeIndex(34, 41) == -1); + assert(c.reportSafeIndex(35, -5) == -1); + assert(c.reportSafeIndex(35, -4) == -1); + assert(c.reportSafeIndex(35, -3) == -1); + assert(c.reportSafeIndex(35, -2) == -1); + assert(c.reportSafeIndex(35, -1) == -1); + assert(c.reportSafeIndex(35, 0) == -1); + assert(c.reportSafeIndex(35, 1) == -1); + assert(c.reportSafeIndex(35, 2) == -1); + assert(c.reportSafeIndex(35, 3) == -1); + assert(c.reportSafeIndex(35, 4) == -1); + assert(c.reportSafeIndex(35, 5) == -1); + assert(c.reportSafeIndex(35, 6) == -1); + assert(c.reportSafeIndex(35, 7) == -1); + assert(c.reportSafeIndex(35, 8) == -1); + assert(c.reportSafeIndex(35, 9) == -1); + assert(c.reportSafeIndex(35, 10) == -1); + assert(c.reportSafeIndex(35, 11) == -1); + assert(c.reportSafeIndex(35, 12) == -1); + assert(c.reportSafeIndex(35, 13) == -1); + assert(c.reportSafeIndex(35, 14) == -1); + assert(c.reportSafeIndex(35, 15) == -1); + assert(c.reportSafeIndex(35, 16) == -1); + assert(c.reportSafeIndex(35, 17) == -1); + assert(c.reportSafeIndex(35, 18) == -1); + assert(c.reportSafeIndex(35, 19) == -1); + assert(c.reportSafeIndex(35, 20) == -1); + assert(c.reportSafeIndex(35, 21) == -1); + assert(c.reportSafeIndex(35, 22) == -1); + assert(c.reportSafeIndex(35, 23) == -1); + assert(c.reportSafeIndex(35, 24) == -1); + assert(c.reportSafeIndex(35, 25) == -1); + assert(c.reportSafeIndex(35, 26) == -1); + assert(c.reportSafeIndex(35, 27) == -1); + assert(c.reportSafeIndex(35, 28) == -1); + assert(c.reportSafeIndex(35, 29) == -1); + assert(c.reportSafeIndex(35, 30) == -1); + assert(c.reportSafeIndex(35, 31) == -1); + assert(c.reportSafeIndex(35, 32) == -1); + assert(c.reportSafeIndex(35, 33) == -1); + assert(c.reportSafeIndex(35, 34) == -1); + assert(c.reportSafeIndex(35, 35) == -1); + assert(c.reportSafeIndex(35, 36) == -1); + assert(c.reportSafeIndex(35, 37) == -1); + assert(c.reportSafeIndex(35, 38) == -1); + assert(c.reportSafeIndex(35, 39) == -1); + assert(c.reportSafeIndex(35, 40) == -1); + assert(c.reportSafeIndex(35, 41) == -1); + assert(c.reportSafeIndex(36, -5) == -1); + assert(c.reportSafeIndex(36, -4) == -1); + assert(c.reportSafeIndex(36, -3) == -1); + assert(c.reportSafeIndex(36, -2) == -1); + assert(c.reportSafeIndex(36, -1) == -1); + assert(c.reportSafeIndex(36, 0) == -1); + assert(c.reportSafeIndex(36, 1) == -1); + assert(c.reportSafeIndex(36, 2) == -1); + assert(c.reportSafeIndex(36, 3) == -1); + assert(c.reportSafeIndex(36, 4) == -1); + assert(c.reportSafeIndex(36, 5) == -1); + assert(c.reportSafeIndex(36, 6) == -1); + assert(c.reportSafeIndex(36, 7) == -1); + assert(c.reportSafeIndex(36, 8) == -1); + assert(c.reportSafeIndex(36, 9) == -1); + assert(c.reportSafeIndex(36, 10) == -1); + assert(c.reportSafeIndex(36, 11) == -1); + assert(c.reportSafeIndex(36, 12) == -1); + assert(c.reportSafeIndex(36, 13) == -1); + assert(c.reportSafeIndex(36, 14) == -1); + assert(c.reportSafeIndex(36, 15) == -1); + assert(c.reportSafeIndex(36, 16) == -1); + assert(c.reportSafeIndex(36, 17) == -1); + assert(c.reportSafeIndex(36, 18) == -1); + assert(c.reportSafeIndex(36, 19) == -1); + assert(c.reportSafeIndex(36, 20) == -1); + assert(c.reportSafeIndex(36, 21) == -1); + assert(c.reportSafeIndex(36, 22) == -1); + assert(c.reportSafeIndex(36, 23) == -1); + assert(c.reportSafeIndex(36, 24) == -1); + assert(c.reportSafeIndex(36, 25) == -1); + assert(c.reportSafeIndex(36, 26) == -1); + assert(c.reportSafeIndex(36, 27) == -1); + assert(c.reportSafeIndex(36, 28) == -1); + assert(c.reportSafeIndex(36, 29) == -1); + assert(c.reportSafeIndex(36, 30) == -1); + assert(c.reportSafeIndex(36, 31) == -1); + assert(c.reportSafeIndex(36, 32) == -1); + assert(c.reportSafeIndex(36, 33) == -1); + assert(c.reportSafeIndex(36, 34) == -1); + assert(c.reportSafeIndex(36, 35) == -1); + assert(c.reportSafeIndex(36, 36) == -1); + assert(c.reportSafeIndex(36, 37) == -1); + assert(c.reportSafeIndex(36, 38) == -1); + assert(c.reportSafeIndex(36, 39) == -1); + assert(c.reportSafeIndex(36, 40) == -1); + assert(c.reportSafeIndex(36, 41) == -1); + assert(c.reportSafeIndex(37, -5) == -1); + assert(c.reportSafeIndex(37, -4) == -1); + assert(c.reportSafeIndex(37, -3) == -1); + assert(c.reportSafeIndex(37, -2) == -1); + assert(c.reportSafeIndex(37, -1) == -1); + assert(c.reportSafeIndex(37, 0) == -1); + assert(c.reportSafeIndex(37, 1) == -1); + assert(c.reportSafeIndex(37, 2) == -1); + assert(c.reportSafeIndex(37, 3) == -1); + assert(c.reportSafeIndex(37, 4) == -1); + assert(c.reportSafeIndex(37, 5) == -1); + assert(c.reportSafeIndex(37, 6) == -1); + assert(c.reportSafeIndex(37, 7) == -1); + assert(c.reportSafeIndex(37, 8) == -1); + assert(c.reportSafeIndex(37, 9) == -1); + assert(c.reportSafeIndex(37, 10) == -1); + assert(c.reportSafeIndex(37, 11) == -1); + assert(c.reportSafeIndex(37, 12) == -1); + assert(c.reportSafeIndex(37, 13) == -1); + assert(c.reportSafeIndex(37, 14) == -1); + assert(c.reportSafeIndex(37, 15) == -1); + assert(c.reportSafeIndex(37, 16) == -1); + assert(c.reportSafeIndex(37, 17) == -1); + assert(c.reportSafeIndex(37, 18) == -1); + assert(c.reportSafeIndex(37, 19) == -1); + assert(c.reportSafeIndex(37, 20) == -1); + assert(c.reportSafeIndex(37, 21) == -1); + assert(c.reportSafeIndex(37, 22) == -1); + assert(c.reportSafeIndex(37, 23) == -1); + assert(c.reportSafeIndex(37, 24) == -1); + assert(c.reportSafeIndex(37, 25) == -1); + assert(c.reportSafeIndex(37, 26) == -1); + assert(c.reportSafeIndex(37, 27) == -1); + assert(c.reportSafeIndex(37, 28) == -1); + assert(c.reportSafeIndex(37, 29) == -1); + assert(c.reportSafeIndex(37, 30) == -1); + assert(c.reportSafeIndex(37, 31) == -1); + assert(c.reportSafeIndex(37, 32) == -1); + assert(c.reportSafeIndex(37, 33) == -1); + assert(c.reportSafeIndex(37, 34) == -1); + assert(c.reportSafeIndex(37, 35) == -1); + assert(c.reportSafeIndex(37, 36) == -1); + assert(c.reportSafeIndex(37, 37) == -1); + assert(c.reportSafeIndex(37, 38) == -1); + assert(c.reportSafeIndex(37, 39) == -1); + assert(c.reportSafeIndex(37, 40) == -1); + assert(c.reportSafeIndex(37, 41) == -1); + assert(c.reportSafeIndex(38, -5) == -1); + assert(c.reportSafeIndex(38, -4) == -1); + assert(c.reportSafeIndex(38, -3) == -1); + assert(c.reportSafeIndex(38, -2) == -1); + assert(c.reportSafeIndex(38, -1) == -1); + assert(c.reportSafeIndex(38, 0) == -1); + assert(c.reportSafeIndex(38, 1) == -1); + assert(c.reportSafeIndex(38, 2) == -1); + assert(c.reportSafeIndex(38, 3) == -1); + assert(c.reportSafeIndex(38, 4) == -1); + assert(c.reportSafeIndex(38, 5) == -1); + assert(c.reportSafeIndex(38, 6) == -1); + assert(c.reportSafeIndex(38, 7) == -1); + assert(c.reportSafeIndex(38, 8) == -1); + assert(c.reportSafeIndex(38, 9) == -1); + assert(c.reportSafeIndex(38, 10) == -1); + assert(c.reportSafeIndex(38, 11) == -1); + assert(c.reportSafeIndex(38, 12) == -1); + assert(c.reportSafeIndex(38, 13) == -1); + assert(c.reportSafeIndex(38, 14) == -1); + assert(c.reportSafeIndex(38, 15) == -1); + assert(c.reportSafeIndex(38, 16) == -1); + assert(c.reportSafeIndex(38, 17) == -1); + assert(c.reportSafeIndex(38, 18) == -1); + assert(c.reportSafeIndex(38, 19) == -1); + assert(c.reportSafeIndex(38, 20) == -1); + assert(c.reportSafeIndex(38, 21) == -1); + assert(c.reportSafeIndex(38, 22) == -1); + assert(c.reportSafeIndex(38, 23) == -1); + assert(c.reportSafeIndex(38, 24) == -1); + assert(c.reportSafeIndex(38, 25) == -1); + assert(c.reportSafeIndex(38, 26) == -1); + assert(c.reportSafeIndex(38, 27) == -1); + assert(c.reportSafeIndex(38, 28) == -1); + assert(c.reportSafeIndex(38, 29) == -1); + assert(c.reportSafeIndex(38, 30) == -1); + assert(c.reportSafeIndex(38, 31) == -1); + assert(c.reportSafeIndex(38, 32) == -1); + assert(c.reportSafeIndex(38, 33) == -1); + assert(c.reportSafeIndex(38, 34) == -1); + assert(c.reportSafeIndex(38, 35) == -1); + assert(c.reportSafeIndex(38, 36) == -1); + assert(c.reportSafeIndex(38, 37) == -1); + assert(c.reportSafeIndex(38, 38) == -1); + assert(c.reportSafeIndex(38, 39) == -1); + assert(c.reportSafeIndex(38, 40) == -1); + assert(c.reportSafeIndex(38, 41) == -1); + assert(c.reportSafeIndex(39, -5) == -1); + assert(c.reportSafeIndex(39, -4) == -1); + assert(c.reportSafeIndex(39, -3) == -1); + assert(c.reportSafeIndex(39, -2) == -1); + assert(c.reportSafeIndex(39, -1) == -1); + assert(c.reportSafeIndex(39, 0) == -1); + assert(c.reportSafeIndex(39, 1) == -1); + assert(c.reportSafeIndex(39, 2) == -1); + assert(c.reportSafeIndex(39, 3) == -1); + assert(c.reportSafeIndex(39, 4) == -1); + assert(c.reportSafeIndex(39, 5) == -1); + assert(c.reportSafeIndex(39, 6) == -1); + assert(c.reportSafeIndex(39, 7) == -1); + assert(c.reportSafeIndex(39, 8) == -1); + assert(c.reportSafeIndex(39, 9) == -1); + assert(c.reportSafeIndex(39, 10) == -1); + assert(c.reportSafeIndex(39, 11) == -1); + assert(c.reportSafeIndex(39, 12) == -1); + assert(c.reportSafeIndex(39, 13) == -1); + assert(c.reportSafeIndex(39, 14) == -1); + assert(c.reportSafeIndex(39, 15) == -1); + assert(c.reportSafeIndex(39, 16) == -1); + assert(c.reportSafeIndex(39, 17) == -1); + assert(c.reportSafeIndex(39, 18) == -1); + assert(c.reportSafeIndex(39, 19) == -1); + assert(c.reportSafeIndex(39, 20) == -1); + assert(c.reportSafeIndex(39, 21) == -1); + assert(c.reportSafeIndex(39, 22) == -1); + assert(c.reportSafeIndex(39, 23) == -1); + assert(c.reportSafeIndex(39, 24) == -1); + assert(c.reportSafeIndex(39, 25) == -1); + assert(c.reportSafeIndex(39, 26) == -1); + assert(c.reportSafeIndex(39, 27) == -1); + assert(c.reportSafeIndex(39, 28) == -1); + assert(c.reportSafeIndex(39, 29) == -1); + assert(c.reportSafeIndex(39, 30) == -1); + assert(c.reportSafeIndex(39, 31) == -1); + assert(c.reportSafeIndex(39, 32) == -1); + assert(c.reportSafeIndex(39, 33) == -1); + assert(c.reportSafeIndex(39, 34) == -1); + assert(c.reportSafeIndex(39, 35) == -1); + assert(c.reportSafeIndex(39, 36) == -1); + assert(c.reportSafeIndex(39, 37) == -1); + assert(c.reportSafeIndex(39, 38) == -1); + assert(c.reportSafeIndex(39, 39) == -1); + assert(c.reportSafeIndex(39, 40) == -1); + assert(c.reportSafeIndex(39, 41) == -1); + assert(c.reportSafeIndex(40, -5) == -1); + assert(c.reportSafeIndex(40, -4) == -1); + assert(c.reportSafeIndex(40, -3) == -1); + assert(c.reportSafeIndex(40, -2) == -1); + assert(c.reportSafeIndex(40, -1) == -1); + assert(c.reportSafeIndex(40, 0) == -1); + assert(c.reportSafeIndex(40, 1) == -1); + assert(c.reportSafeIndex(40, 2) == -1); + assert(c.reportSafeIndex(40, 3) == -1); + assert(c.reportSafeIndex(40, 4) == -1); + assert(c.reportSafeIndex(40, 5) == -1); + assert(c.reportSafeIndex(40, 6) == -1); + assert(c.reportSafeIndex(40, 7) == -1); + assert(c.reportSafeIndex(40, 8) == -1); + assert(c.reportSafeIndex(40, 9) == -1); + assert(c.reportSafeIndex(40, 10) == -1); + assert(c.reportSafeIndex(40, 11) == -1); + assert(c.reportSafeIndex(40, 12) == -1); + assert(c.reportSafeIndex(40, 13) == -1); + assert(c.reportSafeIndex(40, 14) == -1); + assert(c.reportSafeIndex(40, 15) == -1); + assert(c.reportSafeIndex(40, 16) == -1); + assert(c.reportSafeIndex(40, 17) == -1); + assert(c.reportSafeIndex(40, 18) == -1); + assert(c.reportSafeIndex(40, 19) == -1); + assert(c.reportSafeIndex(40, 20) == -1); + assert(c.reportSafeIndex(40, 21) == -1); + assert(c.reportSafeIndex(40, 22) == -1); + assert(c.reportSafeIndex(40, 23) == -1); + assert(c.reportSafeIndex(40, 24) == -1); + assert(c.reportSafeIndex(40, 25) == -1); + assert(c.reportSafeIndex(40, 26) == -1); + assert(c.reportSafeIndex(40, 27) == -1); + assert(c.reportSafeIndex(40, 28) == -1); + assert(c.reportSafeIndex(40, 29) == -1); + assert(c.reportSafeIndex(40, 30) == -1); + assert(c.reportSafeIndex(40, 31) == -1); + assert(c.reportSafeIndex(40, 32) == -1); + assert(c.reportSafeIndex(40, 33) == -1); + assert(c.reportSafeIndex(40, 34) == -1); + assert(c.reportSafeIndex(40, 35) == -1); + assert(c.reportSafeIndex(40, 36) == -1); + assert(c.reportSafeIndex(40, 37) == -1); + assert(c.reportSafeIndex(40, 38) == -1); + assert(c.reportSafeIndex(40, 39) == -1); + assert(c.reportSafeIndex(40, 40) == -1); + assert(c.reportSafeIndex(40, 41) == -1); + assert(c.reportSafeIndex(0, -5) == -1); + assert(c.reportSafeIndex(1, 41) == -1); + assert(c.reportSafeIndex(18, 37) == 10); + assert(c.reportSafeIndex(13, 30) == 12); + assert(c.reportSafeIndex(14, 3) == 1); + assert(c.reportSafeIndex(14, 24) == 10); + assert(c.reportSafeIndex(20, 9) == 5); + return 0; +} diff --git a/cs235/lab03/DONT_DELETE.supp b/cs235/lab03/DONT_DELETE.supp new file mode 100644 index 0000000..c2e5972 --- /dev/null +++ b/cs235/lab03/DONT_DELETE.supp @@ -0,0 +1,661 @@ +{ + Dynamic Linker Bug + Memcheck:Cond + fun:_dl_relocate_object + fun:dl_main + fun:_dl_sysdep_start + fun:_dl_start + obj:/lib/ld-2.6.so +} +{ + + Memcheck:Cond + fun:strlen + fun:_ZNSsC1EPKcRKSaIcE + fun:_Z11generateKeyv + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_Z12CompareListsRK10LinkedListRSt4listISsSaISsEE + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_Z12CompareListsRK10LinkedListRSt4listISsSaISsEE + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_Z12CompareListsRK10LinkedListRSt4listISsSaISsEE + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_Z12CompareListsRK10LinkedListRSt4listISsSaISsEE + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_Z12CompareListsRK10LinkedListRSt4listISsSaISsEE + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZSteqIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_ZNK10LinkedList4FindERKSsP6LLNode + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZSteqIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_ZNK10LinkedList4FindERKSsP6LLNode + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:strlen + fun:_ZNSsC1EPKcRKSaIcE + fun:_Z11generateKeyv + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZSteqIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_ZNK10LinkedList4FindERKSsP6LLNode + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZSteqIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_ZSt6__findISt14_List_iteratorISsESsET_S2_S2_RKT0_St18input_iterator_tag + fun:_ZSt4findISt14_List_iteratorISsESsET_S2_S2_RKT0_ + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZSteqIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_ZNK10LinkedList4FindERKSsP6LLNode + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:strlen + fun:_ZNSsC1EPKcRKSaIcE + fun:_Z11generateKeyv + fun:_Z7TestBSTb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_Z7TestBSTb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZSteqIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZSteqIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_Z7TestBSTb + fun:main +} +{ + + Memcheck:Cond + fun:strlen + fun:_ZNSsC1EPKcRKSaIcE + fun:_Z11generateKeyv + fun:_Z7TestBSTb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ +} +{ + + Memcheck:Cond + fun:strlen + fun:_ZNSsC1EPKcRKSaIcE +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStltIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStltIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs +} +{ + + Memcheck:Cond + fun:strlen + fun:_ZNSsC1EPKcRKSaIcE + fun:_Z11generateKeyv + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_Z12CompareListsRK10LinkedListRSt4listISsSaISsEE + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_Z12CompareListsRK10LinkedListRSt4listISsSaISsEE + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_Z12CompareListsRK10LinkedListRSt4listISsSaISsEE + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_Z12CompareListsRK10LinkedListRSt4listISsSaISsEE + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZNK10LinkedList4FindERKSsP6LLNode + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZNK10LinkedList4FindERKSsP6LLNode + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:strlen + fun:_ZNSsC1EPKcRKSaIcE + fun:_Z11generateKeyv + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZSteqIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZNK10LinkedList4FindERKSsP6LLNode + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:strlen + fun:_ZNSsC1EPKcRKSaIcE + fun:_Z11generateKeyv + fun:_Z7TestBSTb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_Z7TestBSTb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_Z7TestBSTb + fun:main +} +{ + + Memcheck:Cond + fun:strlen + fun:_ZNSsC1EPKcRKSaIcE + fun:_Z11generateKeyv + fun:_Z7TestBSTb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ +} +{ + + Memcheck:Cond + fun:strlen + fun:_ZNSsC1EPKcRKSaIcE + fun:_Z11generateKeyv + fun:_Z7TestBSTb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStltIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs +} +{ + + Memcheck:Cond + fun:strlen + fun:_ZNSsC1EPKcRKSaIcE + fun:_Z11generateKeyv + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_Z12CompareListsRK10LinkedListRSt4listISsSaISsEE + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_Z12CompareListsRK10LinkedListRSt4listISsSaISsEE + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_Z12CompareListsRK10LinkedListRSt4listISsSaISsEE + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_Z12CompareListsRK10LinkedListRSt4listISsSaISsEE + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_Z12CompareListsRK10LinkedListRSt4listISsSaISsEE + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZSteqIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZSteqIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ +} +{ + + Memcheck:Cond + fun:strlen + fun:_ZNSsC1EPKcRKSaIcE + fun:_Z11generateKeyv + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZSteqIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZSteqIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_ZSt6__findISt14_List_iteratorISsESsET_S2_S2_RKT0_St18input_iterator_tag + fun:_ZSt4findISt14_List_iteratorISsESsET_S2_S2_RKT0_ + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZSteqIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ +} +{ + + Memcheck:Cond + fun:strlen + fun:_ZNSsC1EPKcRKSaIcE + fun:_Z11generateKeyv + fun:_Z7TestBSTb + fun:main +} +{ + + Memcheck:Cond + fun:strlen + fun:_ZNSsC1EPKcRKSaIcE + fun:_Z11generateKeyv + fun:_Z7TestBSTb + fun:main +} +{ + + Memcheck:Cond + fun:strlen + fun:_ZNSsC1EPKcRKSaIcE + fun:_Z11generateKeyv + fun:_Z7TestBSTb + fun:main +} +{ + + Memcheck:Cond + fun:bcmp + fun:_ZNSt11char_traitsIcE7compareEPKcS2_j + fun:_ZSteqIcEN9__gnu_cxx11__enable_ifIXsrSt9__is_charIT_E7__valueEbE6__typeERKSbIS3_St11char_traitsIS3_ESaIS3_EESC_ + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_ZN3tut12_GLOBAL__N_113ensure_equalsISsSsEEvPKcRKT0_RKT_ + fun:_ZN7BSTTest7initBSTER3BST + fun:_ZN3tut11test_objectI7BSTTestE4testILi2EEEvv + fun:_ZN3tut10test_groupI7BSTTestLi50EE13run_test_seh_EMNS_11test_objectIS1_EEFvvERNS2_11safe_holderIS4_EERSs + fun:_ZN3tut10test_groupI7BSTTestLi50EE9run_test_ERKSt17_Rb_tree_iteratorISt4pairIKiMNS_11test_objectIS1_EEFvvEEERNS2_11safe_holderIS7_EE + fun:_ZN3tut10test_groupI7BSTTestLi50EE8run_nextEv + fun:_ZNK3tut11test_runner23run_all_tests_in_group_ESt23_Rb_tree_const_iteratorISt4pairIKSsPNS_10group_baseEEE + fun:_ZNK3tut11test_runner9run_testsEv +} +{ + + Memcheck:Cond + fun:bcmp + fun:_ZNSt11char_traitsIcE7compareEPKcS2_j + fun:_ZSteqIcEN9__gnu_cxx11__enable_ifIXsrSt9__is_charIT_E7__valueEbE6__typeERKSbIS3_St11char_traitsIS3_ESaIS3_EESC_ + fun:_ZNK10LinkedList4FindERKSsP6LLNode + fun:_ZN3tut11test_objectI14LinkedListTestE4testILi8EEEvv + fun:_ZN3tut10test_groupI14LinkedListTestLi50EE13run_test_seh_EMNS_11test_objectIS1_EEFvvERNS2_11safe_holderIS4_EERSs + fun:_ZN3tut10test_groupI14LinkedListTestLi50EE9run_test_ERKSt17_Rb_tree_iteratorISt4pairIKiMNS_11test_objectIS1_EEFvvEEERNS2_11safe_holderIS7_EE + fun:_ZN3tut10test_groupI14LinkedListTestLi50EE8run_nextEv + fun:_ZNK3tut11test_runner23run_all_tests_in_group_ESt23_Rb_tree_const_iteratorISt4pairIKSsPNS_10group_baseEEE + fun:_ZNK3tut11test_runner9run_testsEv + fun:_Z3runRKSsi + fun:main +} +{ + + Memcheck:Cond + fun:bcmp + fun:_ZNSt11char_traitsIcE7compareEPKcS2_j + fun:_ZSteqIcEN9__gnu_cxx11__enable_ifIXsrSt9__is_charIT_E7__valueEbE6__typeERKSbIS3_St11char_traitsIS3_ESaIS3_EESC_ + fun:_ZSt6__findISt14_List_iteratorISsESsET_S2_S2_RKT0_St18input_iterator_tag + fun:_ZSt4findISt14_List_iteratorISsESsET_S2_S2_RKT0_ + fun:_ZN3tut11test_objectI14LinkedListTestE4testILi9EEEvv + fun:_ZN3tut10test_groupI14LinkedListTestLi50EE13run_test_seh_EMNS_11test_objectIS1_EEFvvERNS2_11safe_holderIS4_EERSs + fun:_ZN3tut10test_groupI14LinkedListTestLi50EE9run_test_ERKSt17_Rb_tree_iteratorISt4pairIKiMNS_11test_objectIS1_EEFvvEEERNS2_11safe_holderIS7_EE + fun:_ZN3tut10test_groupI14LinkedListTestLi50EE8run_nextEv + fun:_ZNK3tut11test_runner23run_all_tests_in_group_ESt23_Rb_tree_const_iteratorISt4pairIKSsPNS_10group_baseEEE + fun:_ZNK3tut11test_runner9run_testsEv + fun:_Z3runRKSsi +} +{ + + Memcheck:Cond + fun:bcmp + fun:_ZNSt11char_traitsIcE7compareEPKcS2_j + fun:_ZSteqIcEN9__gnu_cxx11__enable_ifIXsrSt9__is_charIT_E7__valueEbE6__typeERKSbIS3_St11char_traitsIS3_ESaIS3_EESC_ + fun:_ZN3tut11test_objectI14LinkedListTestE4testILi9EEEvv + fun:_ZN3tut10test_groupI14LinkedListTestLi50EE13run_test_seh_EMNS_11test_objectIS1_EEFvvERNS2_11safe_holderIS4_EERSs + fun:_ZN3tut10test_groupI14LinkedListTestLi50EE9run_test_ERKSt17_Rb_tree_iteratorISt4pairIKiMNS_11test_objectIS1_EEFvvEEERNS2_11safe_holderIS7_EE + fun:_ZN3tut10test_groupI14LinkedListTestLi50EE8run_nextEv + fun:_ZNK3tut11test_runner23run_all_tests_in_group_ESt23_Rb_tree_const_iteratorISt4pairIKSsPNS_10group_baseEEE + fun:_ZNK3tut11test_runner9run_testsEv + fun:_Z3runRKSsi + fun:main +} diff --git a/cs235/lab03/Run_Test_Driver.sh b/cs235/lab03/Run_Test_Driver.sh new file mode 100755 index 0000000..c292c87 --- /dev/null +++ b/cs235/lab03/Run_Test_Driver.sh @@ -0,0 +1,16 @@ +#!/bin/bash +EXE=dont_run_me + +g++ -o$EXE Student_Code/*.cpp ignoreme.a -g + +if (( $? )) ; +then + echo Compilation Failed; + read -p "Press enter to exit"; +else + chmod 755 $EXE; + + valgrind --tool=memcheck --leak-check=yes --max-stackframe=5000000 --show-reachable=yes --suppressions=DONT_DELETE.supp ./$EXE + rm ./$EXE + read -p "Press any key to exit..." +fi; diff --git a/cs235/lab03/Student_Code/Factory.cpp b/cs235/lab03/Student_Code/Factory.cpp new file mode 100644 index 0000000..3b56a60 --- /dev/null +++ b/cs235/lab03/Student_Code/Factory.cpp @@ -0,0 +1,21 @@ +#include "Factory.h" +#include "linkedlist.h" + +//You may add #include statements here + +/* + You will MODIFY THIS DOCUMENT. +*/ +//======================================================================================= +/* + getLinkedList() + + Creates and returns an object whose class extends LinkedListInterface. + This should be an object of a class you have created. + + Example: If you made a class called "LinkedList", you might say, "return new LinkedList();". +*/ +LinkedListInterface * Factory::getLinkedList() +{ + return new linkedlist();//Modify this line +} diff --git a/cs235/lab03/Student_Code/Factory.h b/cs235/lab03/Student_Code/Factory.h new file mode 100644 index 0000000..3519c6e --- /dev/null +++ b/cs235/lab03/Student_Code/Factory.h @@ -0,0 +1,23 @@ +#pragma once +#include "LinkedListInterface.h" + +using namespace std; + +/* + WARNING: It is expressly forbidden to modify any part of this document, including its name +*/ +//======================================================================================= +/* + getLinkedList() + + Creates and returns an object whose class extends LinkedListInterface. + This should be an object of a class you have created. + + Example: If you made a class called "LinkedList", you might say, "return new LinkedList();". +*/ +class Factory +{ + public: + static LinkedListInterface * getLinkedList(); +}; +//======================================================================================= diff --git a/cs235/lab03/Student_Code/LinkedListInterface.h b/cs235/lab03/Student_Code/LinkedListInterface.h new file mode 100644 index 0000000..465a227 --- /dev/null +++ b/cs235/lab03/Student_Code/LinkedListInterface.h @@ -0,0 +1,80 @@ +//YOU MAY NOT MODIFY THIS DOCUMENT +#pragma once +#include + +using namespace std; + +class LinkedListInterface +{ + +public: + + LinkedListInterface(void){}; + virtual ~LinkedListInterface(void){}; + + /* + insertHead + + A node with the given value should be inserted at the beginning of the list. + + Only non-negative values should be added to the list. Do not allow + duplicate values in the list. + */ + virtual void insertHead(int value) = 0; + + /* + insertTail + + A node with the given value should be inserted at the end of the list. + + Only non-negative values should be added to the list. Do not allow + duplicate values in the list. + */ + virtual void insertTail(int value) = 0; + + /* + insertAfter + + A node with the given value should be inserted immediately after the + node whose value is equal to insertionNode. + + A node should only be added if the node whose value is equal to + insertionNode is in the list. Only non-negative values should be + added to the list. Do not allow duplicate values in the list. + */ + virtual void insertAfter(int value, int insertionNode) = 0; + + /* + remove + + The node with the given value should be removed from the list. + + The list may or may not include a node with the given value. + */ + virtual void remove(int value) = 0; + + /* + clear + + Remove all nodes from the list. + */ + virtual void clear() = 0; + + /* + at + + Returns the value of the node at the given index. The list begins at + index 0. + + If the given index is out of range of the list, return -1; + */ + virtual int at(int index) = 0; + + /* + size + + Returns the number of nodes in the list. + */ + virtual int size() = 0; + +}; diff --git a/cs235/lab03/Student_Code/Makefile b/cs235/lab03/Student_Code/Makefile new file mode 100644 index 0000000..cd2c4de --- /dev/null +++ b/cs235/lab03/Student_Code/Makefile @@ -0,0 +1,37 @@ +CXXFLAGS= -Wall -g -std=c++0x +OBJECTS=Factory.o linkedlist.o node.o ../ignoreme.a +EXE=main +all: libpwnd.so $(EXE) + +$(EXE): $(OBJECTS) + $(CXX) $(CXXFLAGS) $(OBJECTS) -o $@ +main.o: main.cpp Factory.o linkedlist.o node.o libpwnd.so ../ignoreme.a + g++ -Wall -g -o main Factory.o linkedlist.o ../ignoreme.a +Factory.o: Factory.cpp Factory.h +linkedlist.o: linkedlist.cpp linkedlist.h +node.o: node.cpp node.h +test: linkedlist.o node.o test.cpp + $(CXX) $(CXXFLAGS) linkedlist.o node.o test.cpp -o $@ + +run: main + LD_PRELOAD=libpwnd.so LD_LIBRARY_PATH=. ./main + +clean: + @rm -vf *.o + @rm -vf $(EXE) + @rm -vf *.1 + @rm -vf *.0 + +drun: main + LD_PRELOAD=libpwnd.so LD_LIBRARY_PATH=. gdb ./main + +debug: test + LD_PRELOAD=libpwnd.so LD_LIBRARY_PATH=. gdb ./test + +valgrind: $(EXE) + LD_PRELOAD=libpwnd.so LD_LIBRARY_PATH=. valgrind --tool=memcheck --leak-check=yes ./$(EXE) + +libpwnd.so: pwnd.c + gcc -shared -o libpwnd.so.1.0 pwnd.c + ln -fs libpwnd.so.1.0 libpwnd.so.1 + ln -fs libpwnd.so.1 libpwnd.so diff --git a/cs235/lab03/Student_Code/linkedlist.cpp b/cs235/lab03/Student_Code/linkedlist.cpp new file mode 100644 index 0000000..2292657 --- /dev/null +++ b/cs235/lab03/Student_Code/linkedlist.cpp @@ -0,0 +1,158 @@ +#include "linkedlist.h" + +linkedlist::linkedlist(): head(NULL) {} +linkedlist::~linkedlist(){clear();} + +bool linkedlist::is_it_zero(int value) { + if(value < 0) + return false; + else + return true; +} + +bool linkedlist::has_a(int value) { + // cout << "has_a(" << value << ")" << endl; + node* node_ptr = head; + if(head == NULL) + return false; + while(node_ptr != NULL) { + if(node_ptr->value == value) + return true; + node_ptr = node_ptr->next; + } + return false; +} + +void linkedlist::insertHead(int value) { + // cout << "inserthead(" << value << ")" << endl; + // cout << *this << endl; + if(!is_it_zero(value)) + return; + if(has_a(value)) + return; + head = new node(value, head); + // cout << *this << endl << endl; +} + +void linkedlist::insertTail(int value) { + // cout << "insertTail(" << value << ")" << endl; + // cout << *this << endl; + if(!is_it_zero(value)) + return; + if(has_a(value)) + return; + if(head == NULL) { + insertHead(value); + return; + } + node* node_ptr = head; + while(node_ptr != NULL) { + if(node_ptr->next == NULL) { + node_ptr->next = new node(value, NULL); + break; + } + node_ptr = node_ptr->next; + } + // cout << *this << endl << endl; +} + +void linkedlist::insertAfter(int value, int insertionNode) { + // cout << "insertAfter(" << value << ", insertionNode" << insertionNode << ")" << endl; + // cout << *this << endl; + if(value < 0 || insertionNode < 0) { + //cout << "first check" << endl; + return; + } + if(has_a(value) || !has_a(insertionNode)) { + //cout << "failing" << endl; + return; + } + node* node_ptr = head; + while(node_ptr != NULL) { + if(node_ptr->value == insertionNode) { + node* temp = new node(value, NULL); + temp->next = node_ptr->next; + node_ptr->next = temp; + } + node_ptr = node_ptr->next; + } + // cout << *this << endl << endl; +} + +void linkedlist::remove(int value) { + // cout << "remove(" << value << ")" << endl; + // cout << *this << endl; + if(!is_it_zero(value)) + return; + if(!has_a(value)) + return; + node* node_ptr = head; + if(node_ptr->value == value) { + //cout << "first" << endl; + node* ptr = head; + head = head->next; + delete ptr; + return; + } + while(node_ptr != NULL) { + if(node_ptr->next == NULL) + return; + if(node_ptr->next->value == value) { + node* ptr = node_ptr->next; + node_ptr->next = node_ptr->next->next; + delete ptr; + } + node_ptr = node_ptr->next; + } + // cout << *this << endl << endl; +} + +void linkedlist::clear() { + // cout << "clear()" << endl; + node* node_ptr = head; + while(head) { + node_ptr = head; + head = head->next; + delete node_ptr; + } +} + +int linkedlist::at(int index) { + // cout << *this << endl; + // cout << "at(" << index << ")" << endl; + if(index < 0) { + //cout << "is it zero" << endl; + return -1; + } + if(size() <= index) { + // cout << "this is the index=" << index << " size(" << size() << ")"<next; + } + if(head == NULL) + return -1; + return node_ptr->value; +} + +int linkedlist::size() { + int size_of_list = 0; + node* node_ptr = head; + while(node_ptr != NULL) { + size_of_list++; + node_ptr = node_ptr->next; + } + // cout << "size(" << size_of_list << ")" << endl; + return size_of_list; +} + +ostream & operator<<(ostream & os, linkedlist & f) { + node* node_ptr = f.head; + while(node_ptr != NULL) { + os << node_ptr->value << ","; + node_ptr = node_ptr->next; + } + return os; +} diff --git a/cs235/lab03/Student_Code/linkedlist.h b/cs235/lab03/Student_Code/linkedlist.h new file mode 100644 index 0000000..7315eed --- /dev/null +++ b/cs235/lab03/Student_Code/linkedlist.h @@ -0,0 +1,25 @@ +#ifndef __LINKEDLIST_H__ +#define __LINKEDLIST_H__ +#include +#include "LinkedListInterface.h" +#include "node.h" + +using namespace std; + +class linkedlist : public LinkedListInterface { + public: + linkedlist(); + ~linkedlist(); + node* head; + bool has_a(int value); + bool is_it_zero(int value); + void insertHead(int value); + void insertTail(int value); + void insertAfter(int value, int insertionNode); + void remove(int value); + void clear(); + int at(int index); + int size(); + friend ostream & operator<<(ostream & os, linkedlist & f); +}; +#endif diff --git a/cs235/lab03/Student_Code/main.cpp b/cs235/lab03/Student_Code/main.cpp new file mode 100644 index 0000000..a43aa94 --- /dev/null +++ b/cs235/lab03/Student_Code/main.cpp @@ -0,0 +1,24 @@ +#include +#include +#include "linkedlist.h" +#include "node.h" + +using namespace std; + +int main() { + node* head = new node(0, head); + node* tom = new node(1, NULL); + node* dick = new node(2, NULL); + head->next = tom; + tom->next = dick; + node* node_ptr = head; + while(node_ptr != NULL) { + cout << node_ptr->value; + if(node_ptr != NULL) { + cout << "====>"; + } + node_ptr = node_ptr->next; + } + cout << endl; + cout << dick->next << endl; +} diff --git a/cs235/lab03/Student_Code/node.cpp b/cs235/lab03/Student_Code/node.cpp new file mode 100644 index 0000000..eab6683 --- /dev/null +++ b/cs235/lab03/Student_Code/node.cpp @@ -0,0 +1,3 @@ +#include "node.h" + +node::node(const int value, node* next) : value(value), next(next) {} diff --git a/cs235/lab03/Student_Code/node.h b/cs235/lab03/Student_Code/node.h new file mode 100644 index 0000000..901bc84 --- /dev/null +++ b/cs235/lab03/Student_Code/node.h @@ -0,0 +1,14 @@ +#ifndef __NODE_H__ +#define __NODE_H__ + +#include + +using namespace std; + +class node { + public: + node(const int, node*); + int value; + node* next; +}; +#endif diff --git a/cs235/lab03/Student_Code/pwnd.c b/cs235/lab03/Student_Code/pwnd.c new file mode 100644 index 0000000..16bdefc --- /dev/null +++ b/cs235/lab03/Student_Code/pwnd.c @@ -0,0 +1,5 @@ +#include + +int usleep(useconds_t usec) { + return 0; +} diff --git a/cs235/lab03/Student_Code/test.cpp b/cs235/lab03/Student_Code/test.cpp new file mode 100644 index 0000000..a7ffbfe --- /dev/null +++ b/cs235/lab03/Student_Code/test.cpp @@ -0,0 +1,37 @@ +#include +#include "linkedlist.h" + +using namespace std; + +int main() { + linkedlist l; + l.insertTail(69); + l.insertHead(28); + l.insertHead(1); + l.insertHead(2); + l.insertHead(4); + l.insertHead(30); + l.insertHead(15); + l.insertTail(68); + l.insertHead(9); + l.insertHead(0); + l.insertHead(7); + l.insertHead(42); + l.insertHead(3); + cout << l << endl; + l.insertAfter(13,30); + cout << l << endl; + l.remove(13); + cout << endl << l << endl << "removed 13" << endl;; + l.remove(4); + cout << endl << l << endl << "removed 4" << endl;; + l.remove(68); + cout << endl << l << endl << "removed 68" << endl;; + l.remove(3); + cout << endl << endl; + cout << endl << l << endl << "removed 3" << endl;; + l.remove(0); + cout << endl << l << endl << "removed 9" << endl;; + l.remove(69); + cout << endl << l << endl << "removed 69" << endl;; +} diff --git a/cs235/lab03/sm/Factory.cpp b/cs235/lab03/sm/Factory.cpp new file mode 100644 index 0000000..befa7f3 --- /dev/null +++ b/cs235/lab03/sm/Factory.cpp @@ -0,0 +1,21 @@ +#include "Factory.h" +#include "ll.h" + +//You may add #include statements here + +/* + You will MODIFY THIS DOCUMENT. +*/ +//======================================================================================= +/* + getLinkedList() + + Creates and returns an object whose class extends LinkedListInterface. + This should be an object of a class you have created. + + Example: If you made a class called "LinkedList", you might say, "return new LinkedList();". +*/ +LinkedListInterface * Factory::getLinkedList() +{ + return new ll();//Modify this line +} diff --git a/cs235/lab03/sm/Factory.h b/cs235/lab03/sm/Factory.h new file mode 100644 index 0000000..35f2030 --- /dev/null +++ b/cs235/lab03/sm/Factory.h @@ -0,0 +1,23 @@ +#pragma once +#include "LinkedListInterface.h" + +using namespace std; + +/* + WARNING: It is expressly forbidden to modify any part of this document, including its name +*/ +//======================================================================================= +/* + getLinkedList() + + Creates and returns an object whose class extends LinkedListInterface. + This should be an object of a class you have created. + + Example: If you made a class called "LinkedList", you might say, "return new LinkedList();". +*/ +class Factory +{ + public: + static LinkedListInterface * getLinkedList(); +}; +//======================================================================================= diff --git a/cs235/lab03/sm/LinkedListInterface.h b/cs235/lab03/sm/LinkedListInterface.h new file mode 100644 index 0000000..465a227 --- /dev/null +++ b/cs235/lab03/sm/LinkedListInterface.h @@ -0,0 +1,80 @@ +//YOU MAY NOT MODIFY THIS DOCUMENT +#pragma once +#include + +using namespace std; + +class LinkedListInterface +{ + +public: + + LinkedListInterface(void){}; + virtual ~LinkedListInterface(void){}; + + /* + insertHead + + A node with the given value should be inserted at the beginning of the list. + + Only non-negative values should be added to the list. Do not allow + duplicate values in the list. + */ + virtual void insertHead(int value) = 0; + + /* + insertTail + + A node with the given value should be inserted at the end of the list. + + Only non-negative values should be added to the list. Do not allow + duplicate values in the list. + */ + virtual void insertTail(int value) = 0; + + /* + insertAfter + + A node with the given value should be inserted immediately after the + node whose value is equal to insertionNode. + + A node should only be added if the node whose value is equal to + insertionNode is in the list. Only non-negative values should be + added to the list. Do not allow duplicate values in the list. + */ + virtual void insertAfter(int value, int insertionNode) = 0; + + /* + remove + + The node with the given value should be removed from the list. + + The list may or may not include a node with the given value. + */ + virtual void remove(int value) = 0; + + /* + clear + + Remove all nodes from the list. + */ + virtual void clear() = 0; + + /* + at + + Returns the value of the node at the given index. The list begins at + index 0. + + If the given index is out of range of the list, return -1; + */ + virtual int at(int index) = 0; + + /* + size + + Returns the number of nodes in the list. + */ + virtual int size() = 0; + +}; diff --git a/cs235/lab03/sm/Makefile b/cs235/lab03/sm/Makefile new file mode 100644 index 0000000..31e913c --- /dev/null +++ b/cs235/lab03/sm/Makefile @@ -0,0 +1,27 @@ +CXXFLAGS=-Wall -g + +all: test main +test: test.cc Factory.o ll.o +main: pwnd.o Factory.o ../ignoreme.a ll.o + $(CXX) $(CXXFLAGS) -o $@ $^ + +# these deps need to be set up to allow for appropriate recompilation +Factory.o: Factory.h Factory.cpp +ll.o: ll.h ll.cc +pwnd.o: pwnd.c + +clean: + @rm -vf *.o main test core libpwnd* vgcore.* + +run: main + ./main + +gdb: test + gdb ./test + +rtest: test + ./test + +valgrind: main + valgrind --tool=memcheck --leak-check=yes --max-stackframe=5000000 \ + --show-reachable=yes --suppressions=../To\ Students/DONT_DELETE.supp ./main diff --git a/cs235/lab03/sm/ll.cc b/cs235/lab03/sm/ll.cc new file mode 100644 index 0000000..61cdddc --- /dev/null +++ b/cs235/lab03/sm/ll.cc @@ -0,0 +1,158 @@ +#include "ll.h" + +ll::ll(): _size(0), head(NULL), tail(NULL) { +} + +ll::~ll() { + clear(); +} + +void ll::insertHead(int value) { + if(value < 0 or find(value)) { + return; + } + if(head == NULL) { + assert(tail == NULL); + assert(_size == 0); + head = new node(value); + tail = head; + } + else { + node * tmp = head; + head = new node(value); + head->next = tmp; + } + _size++; +} + +void ll::insertTail(int value) { + if(value < 0 or find(value)) { + return; + } + if(tail == NULL) { + assert(head == NULL); + assert(_size == 0); + tail = new node(value); + head = tail; + } + else { + node * tmp = tail; + tmp->next = new node(value); + tail = tmp->next; + } + _size++; +} + +void ll::insertAfter(int value, int insertionNode) { + if(value < 0 or find(value)) { + return; + } + node * inlist = find(insertionNode); + if(not inlist) { + return; + } + node * toinsert = new node(value); + if(inlist == tail) { + tail = toinsert; + } + toinsert->next = inlist->next; + inlist->next = toinsert; + _size++; +} + +void ll::remove(int value) { + if(not find(value)) { + // should deal with empty list, and not found + return; + } + else if(head == tail) { + delete head; + head = tail = NULL; + _size--; + } + else { + node * cur = head; + if(head->value == value) { + head = head->next; + delete cur; + _size--; + } + else { + while(cur) { + if(cur->next == NULL) { + // removing end ... + break; + } + if(cur->next->value == value) { + node * to_remove = cur->next; + if(to_remove == tail) { + tail = cur; + } + cur->next = to_remove->next; + delete to_remove; + _size--; + } + cur = cur->next; + } + } + } +} + +void ll::clear() { + node * tmp = head; + while(head) { + tmp = head; + head = head->next; + delete tmp; + _size--; + } + head = tail = NULL; + assert(_size == 0); +} + +int ll::at(int index) { + if (index >= _size or index < 0) { + return -1; + } + node * tmp = head; + for(int i = 0; i < index; i++) { + tmp = tmp->next; + } + if(tmp == NULL) { + return -1; + } + else { + return tmp->value; + } +} + +int ll::size() { + return _size; +} + +node * ll::find(int value) { + node * tmp = head; + while(tmp) { + if(tmp->value == value) { + return tmp; + } + tmp = tmp->next; + } + return NULL; +} + +ostream & operator<<(ostream & os, const node & n) { + os << n.value; + return os; +} + +ostream & operator<<(ostream & os, const ll & l) { + os << "ll {(" << l._size << ") "; + node * cur = l.head; + while (cur) { + os << *cur << ", "; + cur = cur->next; + } + os << "}"; + return os; +} diff --git a/cs235/lab03/sm/ll.h b/cs235/lab03/sm/ll.h new file mode 100644 index 0000000..60652bd --- /dev/null +++ b/cs235/lab03/sm/ll.h @@ -0,0 +1,42 @@ +#ifndef __LL_H__ +#define __LL_H__ + +#include +#include +#include "Factory.h" + +using namespace std; + +struct node { + int value; + node * next; + node(int value): value(value), next(NULL) {}; + + friend ostream & operator<<(ostream &, const node &); +}; + +class ll : public LinkedListInterface +{ + private: + int _size; + public: + ll(); + ~ll(); + + node * head; + node * tail; + + void insertHead(int value); + void insertTail(int value); + void insertAfter(int value, int insertionNode); + void remove(int value); + void clear(); + int at(int index); + int size(); + + node * find(int value); + + friend ostream & operator<<(ostream & os, const ll & i); +}; + +#endif diff --git a/cs235/lab03/sm/pwnd.c b/cs235/lab03/sm/pwnd.c new file mode 100644 index 0000000..16bdefc --- /dev/null +++ b/cs235/lab03/sm/pwnd.c @@ -0,0 +1,5 @@ +#include + +int usleep(useconds_t usec) { + return 0; +} diff --git a/cs235/lab03/sm/test.cc b/cs235/lab03/sm/test.cc new file mode 100644 index 0000000..f9a60de --- /dev/null +++ b/cs235/lab03/sm/test.cc @@ -0,0 +1,174 @@ +#include +#include +using namespace std; + +#include "Factory.h" +#include "ll.h" + +int main() { + LinkedListInterface * lli = Factory::getLinkedList(); + ll * myll = (ll*)lli; + myll->insertHead(8); + myll->insertHead(5); + myll->insertHead(3); + myll->insertHead(2); + myll->insertHead(1); + // should be duplicate + myll->insertHead(1); + myll->insertHead(0); + assert(myll->size() == 6); + assert(myll->head->value == 0); + assert(myll->tail->value == 8); + assert(myll->at(-8) == -1); + assert(myll->at(3238) == -1); + assert(myll->at(3) == 3); + delete myll; + + { + ll ll2 = ll(); + ll2.insertTail(5); + ll2.insertTail(6); + assert(ll2.size() == 2); + assert(ll2.head->value == 5); + assert(ll2.tail->value == 6); + } + + { + // test for negative insertion + ll ll4 = ll(); + ll4.insertHead(-1); + ll4.insertTail(-1); + assert(ll4.size() == 0); + } + + { + // test for duplicate insertion + ll ll3 = ll(); + ll3.insertTail(1); + ll3.insertTail(2); + ll3.insertTail(3); + ll3.insertTail(4); + ll3.insertTail(4); + ll3.insertTail(4); + ll3.insertTail(5); + assert(ll3.size() == 5); + } + + { + // test find + ll ll5 = ll(); + ll5.insertHead(0); + ll5.insertTail(1); + ll5.insertTail(2); + assert(ll5.find(2)); + assert(ll5.find(200) == NULL); + } + + { + // test insertAfter + ll ll6 = ll(); + ll6.insertHead(0); + ll6.insertTail(2); + ll6.insertTail(3); + assert(ll6.size() == 3); + ll6.insertAfter(1, 0); + assert(ll6.size() == 4); + ll6.insertAfter(1, 23); + assert(ll6.size() == 4); + } + + { + ll ll7 = ll(); + ll7.insertAfter(1, 0); + } + + { + // test deletion + ll ll8 = ll(); + ll8.insertTail(1); + ll8.insertTail(2); + ll8.insertTail(3); + ll8.insertTail(4); + ll8.insertTail(5); + ll8.remove(4); + assert(ll8.find(4) == NULL); + assert(ll8.size() == 4); + assert(ll8.at(0) == 1); + assert(ll8.at(1) == 2); + assert(ll8.at(2) == 3); + assert(ll8.at(3) == 5); + assert(ll8.at(4) == -1); + assert(ll8.at(5) == -1); + } + + { + ll ll9 = ll(); + ll9.remove(4); + assert(ll9.size() == 0); + assert(ll9.at(1) == -1); + } + + { + ll ll10 = ll(); + ll10.insertTail(4); + assert(ll10.size() == 1); + ll10.remove(4); + assert(ll10.size() == 0); + } + + { + ll ll11 = ll(); + ll11.insertHead(4); + assert(ll11.size() == 1); + assert(ll11.at(0) == 4); + ll11.remove(4); + assert(ll11.size() == 0); + } + + { + ll ll12 = ll(); + ll12.insertTail(1); + ll12.insertTail(9); + assert(ll12.size() == 2); + ll12.remove(1); + assert(ll12.size() == 1); + } + + { + ll ll13 = ll(); + ll13.insertTail(9); + ll13.insertTail(3); + assert(ll13.size() == 2); + ll13.insertTail(16); + assert(ll13.size() == 3); + } + + { + ll ll_14 = ll(); + ll_14.insertHead(9); + ll_14.insertHead(3); + assert(ll_14.size() == 2); + ll_14.insertTail(16); + assert(ll_14.size() == 3); + } + + { + // test enforced by assert in clear + ll l = ll(); + l.insertHead(16); + l.insertTail(32); + l.insertTail(38); + l.remove(38); + l.insertTail(9); + } + + { + // test enforced by asserts in ll + ll l = ll(); + l.insertHead(16); + l.insertAfter(14, 16); + l.insertTail(17); + } + + cout << "You passed" << endl; +} diff --git a/cs235/lab04/ExpressionManagerInterface.h b/cs235/lab04/ExpressionManagerInterface.h new file mode 100644 index 0000000..6140d01 --- /dev/null +++ b/cs235/lab04/ExpressionManagerInterface.h @@ -0,0 +1,65 @@ +#pragma once +#include +#include +#include + +using namespace std; + + +class ExpressionManagerInterface +{ + public: + ExpressionManagerInterface(){} + virtual ~ExpressionManagerInterface(){} + + /* + * Checks whether an expression is balanced on its parentheses + * + * - The given expression will have a space between every number or operator + * + * @return true if expression is balanced + * @return false otherwise + */ + +virtual bool isBalanced(string expression) = 0; + + /** + * Converts a postfix expression into an infix expression + * and returns the infix expression. + * + * - The given postfix expression will have a space between every number or operator. + * - The returned infix expression must have a space between every number or operator. + * - Redundant parentheses are acceptable i.e. ( ( 3 * 4 ) + 5 ). + * - Check lab requirements for what will be considered invalid. + * + * return the string "invalid" if postfixExpression is not a valid postfix expression. + * otherwise, return the correct infix expression as a string. + */ +virtual string postfixToInfix(string postfixExpression) = 0; + + /* + * Converts an infix expression into a postfix expression + * and returns the postfix expression + * + * - The given infix expression will have a space between every number or operator. + * - The returned postfix expression must have a space between every number or operator. + * - Check lab requirements for what will be considered invalid. + * + * return the string "invalid" if infixExpression is not a valid infix expression. + * otherwise, return the correct postfix expression as a string. + */ +virtual string infixToPostfix(string infixExpression) = 0; + + /* + * Evaluates a postfix expression returns the result as a string + * + * - The given postfix expression will have a space between every number or operator. + * - Check lab requirements for what will be considered invalid. + * + * return the string "invalid" if postfixExpression is not a valid postfix Expression + * otherwise, return the correct evaluation as a string + */ +virtual string postfixEvaluate(string postfixExpression) = 0; + + +}; diff --git a/cs235/lab04/Factory.cpp b/cs235/lab04/Factory.cpp new file mode 100644 index 0000000..508b2f8 --- /dev/null +++ b/cs235/lab04/Factory.cpp @@ -0,0 +1,22 @@ +#include "Factory.h" +#include "expman.h" +//You may add #include statments here +using namespace std; + +/* + Unlike all other documents provided, you may modify this document slightly (but do not change its name) +*/ +//======================================================================================= +/* + createManager() + + Creates and returns an object whose class extends ExpressionManagerInterface. + This should be an object of a class you have created. + + Example: If you made a class called "ExpressionManager", you might say, "return new ExpressionManager();". +*/ +ExpressionManagerInterface* Factory::createManager() +{ + return new expman();//Modify this line +} +//======================================================================================= diff --git a/cs235/lab04/Factory.h b/cs235/lab04/Factory.h new file mode 100644 index 0000000..21e6f9e --- /dev/null +++ b/cs235/lab04/Factory.h @@ -0,0 +1,22 @@ +#pragma once +#include "ExpressionManagerInterface.h" +using namespace std; + +/* + WARNING: It is expressly forbidden to modify any part of this document, including its name +*/ +//======================================================================================= +/* + createExpressionManager() + + Creates and returns an object whose class extends ExpressionManagerInterface. + This should be an object of a class you have created. + + Example: If you made a class called "ExpressionManager", you might say, "return new ExpressionManager();". +*/ +class Factory +{ + public: + static ExpressionManagerInterface * createManager(); +}; +//======================================================================================= diff --git a/cs235/lab04/Makefile b/cs235/lab04/Makefile new file mode 100644 index 0000000..84e1915 --- /dev/null +++ b/cs235/lab04/Makefile @@ -0,0 +1,35 @@ +CXXFLAGS= -Wall -g -std=c++0x +OBJECTS=Factory.o expman.o pwnd.o ignoreme.a +EXE=main +all: pwnd.o $(EXE) test + +$(EXE): $(OBJECTS) + $(CXX) $(CXXFLAGS) $(OBJECTS) -o $@ +main.o: main.cpp Factory.o expman.o pwnd.o ignoreme.a +Factory.o: Factory.cpp Factory.h +expman.o: expman.cpp expman.h +pwnd.o: pwnd.c +test: test.cc expman.o + +run: main + ./main + +rtest: test + ./test + +clean: + @rm -vf *.o + @rm -vf $(EXE) + @rm -vf *.1 + @rm -vf *.0 + @rm -vf test + @rm -rvf *.dSYM + +drun: main + gdb ./main + +debug: test + gdb ./test + +valgrind: $(EXE) + valgrind --tool=memcheck --leak-check=yes ./$(EXE) diff --git a/cs235/lab04/Run Test Driver.bat b/cs235/lab04/Run Test Driver.bat new file mode 100644 index 0000000..e49a2ea --- /dev/null +++ b/cs235/lab04/Run Test Driver.bat @@ -0,0 +1,32 @@ +TITLE TA Test Driver + +@echo off + + +rem setting up environment variables needed for VS compiler +call "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86_amd64 + +rem compiling the .obj +cl /EHsc /Fedont_run_me.exe Student_Code\*.cpp ignoreme.lib + +rem if compilation failed, goto error section +if ERRORLEVEL 1 goto error + +rem cleanup unnecessary generated files +del *.obj + +rem run the driver in another window +.\dont_run_me.exe +del dont_run_me.exe +pause + +exit + +:error + +rem remove any generated files +del *.obj + +echo ----------ERROR-------- +pause +exit diff --git a/cs235/lab04/bin/runner.py b/cs235/lab04/bin/runner.py new file mode 100644 index 0000000..e322166 --- /dev/null +++ b/cs235/lab04/bin/runner.py @@ -0,0 +1,7 @@ +import sys + +from shunting import shunt + + +for line in sys.stdin: + print shunt(line.split()) diff --git a/cs235/lab04/expman.cpp b/cs235/lab04/expman.cpp new file mode 100644 index 0000000..4588fef --- /dev/null +++ b/cs235/lab04/expman.cpp @@ -0,0 +1,274 @@ +#include "expman.h" +#include + +const string OPEN = "([{"; +const string CLOSE = ")]}"; + +bool is_open(char ch) { + return OPEN.find(ch) != string::npos; +} + +bool is_close(char ch) { + return CLOSE.find(ch) != string::npos; +} + +int precedence(string ch) { + int status; + if(ch == "(") { + status = -1; + } + if(ch == ")") { + status = -1; + } + if(ch == "+" or ch == "-") + status = 2; + if(ch == "*" or ch == "/") + status = 3; + return status; +} + +void process_operator(string ch, stack & operator_stack, string & postfix) { + cout << ch << " this is ch" << endl; + if(operator_stack.empty()) { + operator_stack.push(ch); + return; + } + if(ch == ")") { + bool validity = true; + while(validity) { + postfix += operator_stack.top(); + postfix += " "; + operator_stack.pop(); + if(operator_stack.top() == "(") { + operator_stack.pop(); + validity = false; + } + } + } + else { + if(precedence(ch) > precedence(operator_stack.top())) { + operator_stack.push(ch); + } + else { + while(!operator_stack.empty() and (precedence(ch) <= precedence(operator_stack.top()))) { + postfix += operator_stack.top(); + postfix += " "; + operator_stack.pop(); + } + operator_stack.push(ch); + } + } +} + +bool is_operator(string token) { + if(token.size() != 1) { + return false; + } + if(token == "+" or + token == "-" or + token == "/" or + token == "*" or + token == "^" or + token == "(" or + token == ")") { + return true; + } + return false; +} + +bool is_int(string token) { + if(token == "") { + return false; + } + for(unsigned int i = 0; i < token.size(); i++) { + if(!isdigit(token[i])) { + return false; + } + } + return true; +} + +bool is_valid_token(string token) { + if(!is_int(token) and !is_operator(token) and !is_paren(token)) { + return false; + } + return true; +} + +bool is_paren(string token) { + if(token == "(" or + token == "{" or + token == "[" or + token == ")" or + token == "}" or + token == "]") { + return true; + } + return false; +} + +bool is_valid_expression(string expression) { + stack temp = parse_expression(expression); + while(!temp.empty()) { + if(not is_valid_token(temp.top())) { + return false; + } + temp.pop(); + } + return true; +} + +bool expman::isBalanced(string expression) { + stack s; + bool balanced = true; + string::const_iterator iter = expression.begin(); + while(balanced && (iter != expression.end())) { + char next_ch = *iter; + if(is_open(next_ch)) { + s.push(next_ch); + } + else if(is_close(next_ch)) { + if(s.empty()) { + balanced = false; + } + else { + char top_ch = s.top(); + s.pop(); + balanced = OPEN.find(top_ch) == CLOSE.find(next_ch); + } + } + ++iter; + } + return balanced && s.empty(); +} + +string expman::postfixToInfix(string postfixExpression) { + cout << postfixExpression << endl; + if(!isBalanced(postfixExpression)) + return "invalid"; + if(!is_valid_expression(postfixExpression)) + return "invalid"; + return "h"; +} + +string expman::infixToPostfix(string infixExpression) { + cout << infixExpression << endl; + if(!isBalanced(infixExpression)) + return "invalid"; + if(!is_valid_expression(infixExpression)) + return "invalid"; + stack operator_stack; + stack expression = parse_expression(infixExpression); + expression = reverse_stack(expression); + string postfix; + while(!expression.empty()) { + if(is_int(expression.top())) { + postfix += expression.top(); + postfix += " "; + expression.pop(); + } + else if(is_operator(expression.top())) { + process_operator(expression.top(), operator_stack, postfix); + expression.pop(); + } + else if(is_paren(expression.top())) { + expression.pop(); + } + } + while(!operator_stack.empty()) { + postfix += operator_stack.top(); + postfix += " "; + operator_stack.pop(); + } + cout << postfix << " final postfix" << endl; + return postfix; +} + +string expman::postfixEvaluate(string postfixExpression) { + cout << postfixExpression.size() << " size of string" << endl; + if(!isBalanced(postfixExpression)) { + cout << "not balanced" << endl; + return "invalid"; + } + if(!is_valid_expression(postfixExpression)) { + cout << "invalide character" << endl; + return "invalid"; + } + stack expression = parse_expression(postfixExpression); + if(expression.size() == 1) { + cout << "this is here" << endl; + return postfixExpression; + } + stack numbers; + expression = reverse_stack(expression); + while(!expression.empty()) { + if(is_int(expression.top())) { + numbers.push(expression.top()); + expression.pop(); + } + else if(is_operator(expression.top())) { + if(numbers.empty()) { + return "invalid"; + } + string r = numbers.top(); + string l = numbers.top(); + numbers.pop(); + numbers.pop(); + int left = atoi(l.c_str()); + int right = atoi(r.c_str()); + int result; + if(expression.top() == "+") { + result = left + right; + } + else if(expression.top() == "-") { + result = left - right; + } + else if(expression.top() == "*") { + result = left * right; + } + else if(expression.top()== "/") { + if(left == 0) { + return "invalid"; + } + else { + result = right / left; + } + } + expression.pop(); + stringstream ss; + ss << result; + numbers.push(ss.str()); + } + } + return numbers.top(); +} + +stack reverse_stack(stack s) { + stack r; + while(!s.empty()) { + r.push(s.top()); + s.pop(); + } + return r; +} + +stack parse_expression(string expression) { + stack results; + string s; + for(unsigned int i = 0; i < expression.length(); i++) { + char c = expression[i]; + if(c != ' ') { + s += c; + } + else { + if(s != "") { + results.push(s); + s.clear(); + } + } + } + if(s != "") { + results.push(s); + } + return results; +} diff --git a/cs235/lab04/expman.h b/cs235/lab04/expman.h new file mode 100644 index 0000000..195da1b --- /dev/null +++ b/cs235/lab04/expman.h @@ -0,0 +1,28 @@ +#ifndef __EXPMAN_H__ +#define __EXPMAN_H__ +#include +#include +#include +#include +#include +#include +#include "ExpressionManagerInterface.h" + +using namespace std; + +class expman : public ExpressionManagerInterface { + public: + bool isBalanced(string expression); + string postfixToInfix(string postfixExpression); + string infixToPostfix(string infixExpression); + string postfixEvaluate(string postfixExpression); +}; + +int precedence(string ch); +bool is_paren(string token); +bool is_int(string token); +bool is_valid_expression(string token); +bool is_valid_token(string token); +stack reverse_stack(stack); +stack parse_expression(string); +#endif diff --git a/cs235/lab04/ignoreme.lib b/cs235/lab04/ignoreme.lib new file mode 100644 index 0000000..57ddd83 Binary files /dev/null and b/cs235/lab04/ignoreme.lib differ diff --git a/cs235/lab04/lab04.html b/cs235/lab04/lab04.html new file mode 100644 index 0000000..a19d219 --- /dev/null +++ b/cs235/lab04/lab04.html @@ -0,0 +1,165 @@ + + + + +CS 235 - Labs + + + + + + + + + + +
+
+ + + +

Lab 4

+
+ +
+ + +
+

Purpose

+

To become familiarized with the use of stacks as data structures.

+ +

Key Reading

+
    +
  • 5.1-5.4
  • +
+ +

Background

+

Develop an Expression Manager that performs several operations on infix and postfix expressions. Be able to convert from one form to the other, evaluate postfix expressions, and check for balanced parenthetical expressions.

+ + +

You may also refer to Edsger Dijkstra's "Shunting-yard algorithm" for additional help, which can be viewed here.

+

Requirements

+ +

You will need these files to complete the assignment. Details for method constraints are found in these documents and are still a part of the requirements. (If downloaded before 9/17, 2:00 pm re-download. There are errors that were fixed that will cause the driver to break on working code.)

+ + +

Extend the ExpressionManagerInterface.h.

+

+

Part 1 - Balanced Symbols Check (10 points)

+
    +
  • Determine and report whether an expression is balanced. { [ } ] is not balanced. [ ( ) ] is balanced and valid
  • +
+

Part 2 - Infix to Postfix Conversion (10 points)

+
    +
  • Alert the user if the given infix expression is not valid
  • +
  • Convert the infix expression into a postfix expression and display the postfix expression
  • +
+

Part 3 - Postfix to Infix Conversion (10 points)

+
    +
  • Alert the user if the given postfix expression is not valid
  • +
  • Convert the postfix expression into an infix expression and display the infix expression
  • +
+

Part 4 - Postfix Expression Evaluation (10 points)

+
    +
  • Alert the user if the given postfix expression is not valid
  • +
  • Evaluate the given postfix expression and display the result
  • +
  • Handle attempts to divide by 0
  • +
+ +

Requirement Notes

+ +

General

+
    +
  • Valid expressions consist of integers; brackets, braces, and parentheses; and +, -, *, /, and %. Reject any invalid expressions and inform the user.
  • +
  • Your calculations should perform integer divison and produce integer results
  • +
  • Valid expressions also need to satisfy standard infix or postfix requirements
  • +
  • {,},(,),[, and ] are the only symbols considered for the Balanced Symbols Check
  • +
  • Your program should allow the user to repeat the activities
  • +
  • You can assume there will be a space between every number or operator
  • +
  • You must put parenthesis '()' around every part of the expression during the postfix to infix conversion. i.e. "4 2 5 + *" = "( 4 *( 2 + 5 ) )"
  • +
  • You must use the stack class pre-defined in the C++ Standard Template Library (STL). i.e. #include <stack>
  • +
+ +

Test Cases

+
    +
  • The expressions in the test files will be written with one complete expression on each line. Available below are small test files you may use to test your program for pass off on each part. + This is not a comprehensive set of tests, but it should familiarize you with the format and give you ideas for additional test cases you can add. Your program must display the result of the selected + operation for each of the expressions in the file. + +
  • +
+
+
+ \ No newline at end of file diff --git a/cs235/lab04/lab04_files/labs.css b/cs235/lab04/lab04_files/labs.css new file mode 100644 index 0000000..bbbf697 --- /dev/null +++ b/cs235/lab04/lab04_files/labs.css @@ -0,0 +1,13 @@ +form { + position: absolute; + left: 1015px; + top: 85px; +} +pre { + border: 1px solid #666; + background-color: #ddd; + padding: 15px; +} +h3 { + text-decoration: underline; +} \ No newline at end of file diff --git a/cs235/lab04/lab04_files/main.css b/cs235/lab04/lab04_files/main.css new file mode 100644 index 0000000..03931e7 --- /dev/null +++ b/cs235/lab04/lab04_files/main.css @@ -0,0 +1,344 @@ +body { + background: #fff url(../imgs/background.jpg) repeat-y left top; + font-size: 14px; + /*text-align: justify;*/ + color: #000000; +} + +body, th, td, input, textarea, select, option { + font-family: "Trebuchet MS", Helvetica, Arial, sans-serif;/*, "Times New Roman", Arial, Times, serif;*/ +} + +h1, h2, h3 { + text-transform: uppercase; + font-family: Arial, Helvetica, sans-serif; + font-weight: normal; + color: #1C1C1C; +} + +h1 { + letter-spacing: -2px; + font-size: 3em; +} + +h2 { + letter-spacing: -1px; + font-size: 2em; +} + +h3 { + font-size: 1.4em; + font-weight: bold; +} + +h4 { + font-size: 1.2em; +} + +p, ul, ol { + line-height: 150%; +} + +blockquote { + padding-left: 1em; +} + +blockquote p, blockquote ul, blockquote ol { + line-height: normal; + font-style: italic; +} + +a { + color: #030a36; + font-weight: bold; +} + +hr { + display: none; +} + +/* Header */ +#header { + width: 100%; + height: 75px; + margin: 0 left; + margin-bottom: 20px; + text-transform: uppercase; + font-family: Arial, Helvetica, sans-serif; + background-color: #999; + background-image: -webkit-gradient(linear, left top, left bottom, from(#ddd), to(#999)); + background-image: -moz-linear-gradient(-90deg, #ddd, #999); +} + +#header h1, #header p { + margin: 0; + padding: 0; +} + +#header h1 { + float: left; + padding: 5px 0 0 320px; + color: #F79F1A; + font-size: 36px; + text-align: center; +} + +#header p { + float: left; + padding: 17px 0 0 5px; + font-size: 12px; + font-weight: bold; + color: #EEEEEE; +} + +#header a { + text-decoration: none; + color: #F79F1A; +} + +/* Page */ +#page { + width: 1100px; + margin: 0 left; +} + +/* Content */ +#content { + float: right; + width: 800px; + margin: 0; +} + +#content img { + float: left; + margin-right: 15px; +} + +.post { + padding: 5px 0 0 0; +} + +.title { + padding: 0 20px 0 20px; + font-size: 24px; +} + +.title a { + text-decoration: none; +} + +.pagetitle { +} + +.byline { + margin: -30px 20px 0 20px; + color: #646464; +} + +.meta { + border-bottom: 10px solid #EEEEEE; + text-align: right; + color: #646464; + padding: 10px 20px 20px 20px; + text-transform: uppercase; + font-family: Arial, Helvetica, sans-serif; + font-size: 10px; +} + +.meta .more { + background: #EEEEEE; + padding: 5px 15px; +} + +.meta .comments { + background: #EEEEEE; + padding: 5px 15px; +} + +.meta a { +} + +.alignleft { + float: left; +} + +.alignright { + float: right; +} + +.posts { + margin: 0; + padding: 0; + list-style: none; + line-height: normal; +} + +.posts li { +} + +.posts h3 { + margin: 0; + font-weight: bold; +} + +.posts p { + margin: 0; + line-height: normal; +} + +.posts a { +} + +.entry { + margin: 0 20px 0 20px; +} + +.last { + border: none; +} + +/* Sidebar */ +#sidebar { + float: left; + width: 220px; + color: #EEEEEE; +} + +#menu { + border-bottom:thin dotted #FFFFFF; + margin: 0 20px 20px 10px; +} + +#sidebar ul { + margin: 0 0 0 45px; + padding: 0px 0 20px 0; + list-style: none; +} + +#sidebar li { +} + +#sidebar li ul { + padding: 0px 0px 16px 0px; +} + +#sidebar li li { + border-bottom: 1px dotted #1C1C1C; + padding: 0 0 10px 0px; +} + +#sidebar h2 { + margin: 0; + height: 30px; + padding: 0px 0px 0px 20px; + text-transform: uppercase; + font-family: Arial, Helvetica, sans-serif; + font-size: 18px; + color: #FFFFFF; +} + +#sidebar a { + color: #FFFFFF; +} + +#sidebar a:hover { + text-decoration: none; +} + +.infoBox { + position: relative; + border-top: 1px dotted #999; + padding: 15px 0; + overflow: auto; + margin: 0; +} + +/* Search */ +#search input { + display: none; +} + +#search input#s { + display: block; + width: 230px; + padding: 2px 5px; + border: 1px solid #3DD1FF; + background: #EEEEEE +} + +#search br { + display: none; +} + +/* Calendar */ +#calendar { +} + +#calendar h2 { + margin-bottom: 15px; +} + +#calendar table { + width: 80%; + margin: 0 auto; + text-align: center; +} + +#calendar caption { + width: 100%; + text-align: center; +} + +#next { + text-align: right; +} + +#prev { + text-align: left; +} + +/* Footer */ +#footer { + clear: both; + width: 900px; + height: 50px; + margin: 0 left; + text-align: right; + font-size: smaller; + font-family: Arial, Helvetica, sans-serif; +} + +#footer p { + padding: 50px 60px 20px 0; + text-transform: uppercase; +} + +#footer p a { +} + +table { + text-align: center; + margin: 15px auto 0 auto; + border-radius: 5px; + -webkit-border-radius: 5px; + -moz-border-radius: 5px; +} +th { + color: #fff; + background-color: #030a36; + padding: 3px 10px; +} +th:first-child { + -moz-border-radius: 7px 0 0 0; + -webkit-border-radius: 7px 0 0 0; + border-radius: 7px 0 0 0; +} +th:last-child { + -moz-border-radius: 0 7px 0 0; + -webkit-border-radius: 0 7px 0 0; + border-radius: 0 7px 0 0; +} +td { + background-color:#d6d6d6; + padding: 3px 10px; +} \ No newline at end of file diff --git a/cs235/lab04/lab04_files/title.png b/cs235/lab04/lab04_files/title.png new file mode 100644 index 0000000..dad96b6 Binary files /dev/null and b/cs235/lab04/lab04_files/title.png differ diff --git a/cs235/lab04/main.cpp b/cs235/lab04/main.cpp new file mode 100644 index 0000000..5afab12 --- /dev/null +++ b/cs235/lab04/main.cpp @@ -0,0 +1,14 @@ +#include +#include +#include "expman.h" + +using namespace std; + +int main() { + expman e; + assert(e.isBalanced("{ { [ ( ) ] } ( ) }") == true); + assert(e.isBalanced("{ [ ) }") == false); + assert(e.isBalanced("{ ( [ ] } )") == false); + assert(e.isBalanced("{") == false); + return 0; +} diff --git a/cs235/lab04/pwnd.c b/cs235/lab04/pwnd.c new file mode 100644 index 0000000..16bdefc --- /dev/null +++ b/cs235/lab04/pwnd.c @@ -0,0 +1,5 @@ +#include + +int usleep(useconds_t usec) { + return 0; +} diff --git a/cs235/lab04/setup.py b/cs235/lab04/setup.py new file mode 100644 index 0000000..d65f10e --- /dev/null +++ b/cs235/lab04/setup.py @@ -0,0 +1,11 @@ +from setuptools import setup, find_packages + +setup( + name = 'cs235lab04', + version = '0.0dev', + packages = find_packages(), + author = "Stephen M. McQuay", + author_email = "stephen@mcquay.me", + url = "http://bitbucket.org/smcquay/cs235", + license = "WTFPL", + ) diff --git a/cs235/lab04/shunting/__init__.py b/cs235/lab04/shunting/__init__.py new file mode 100644 index 0000000..0f93a76 --- /dev/null +++ b/cs235/lab04/shunting/__init__.py @@ -0,0 +1,23 @@ +from collections import deque + + +op_prec = { + '*': 3, + '/': 3, + '%': 3, + '+': 2, + '-': 2, +} + + +def shunt(tokens): + tokens = deque(tokens) + output = deque() + operators = deque() + while tokens: + token = tokens.pop() + if token.isdigit(): + output.append(token) + elif token in op_prec: + operators.append(token) + return output, operators diff --git a/cs235/lab04/shunting/test_shunt.py b/cs235/lab04/shunting/test_shunt.py new file mode 100644 index 0000000..21678d6 --- /dev/null +++ b/cs235/lab04/shunting/test_shunt.py @@ -0,0 +1,10 @@ +import unittest + +from shunting import shunt + + +class ShuntTest(unittest.TestCase): + + def test_simple_add(self): + o, s = shunt(['4', '+', '3']) + self.assertEquals(o, ['4', '3', '+']) diff --git a/cs235/lab04/test.cc b/cs235/lab04/test.cc new file mode 100644 index 0000000..1b7b657 --- /dev/null +++ b/cs235/lab04/test.cc @@ -0,0 +1,117 @@ +#include +#include +#include +#include +#include +#include +#include "expman.h" + +ostream & operator<<(ostream & os, stack & s) { + while(not s.empty()) { + os << "'" << s.top() << "', "; + s.pop(); + } + return os; +} + +int main() { + stringstream oss; + + auto r1 = parse_expression(string("1 + 40 + 3")); + oss << r1; + assert(oss.str() == "'3', '+', '40', '+', '1', "); + oss.str(""); + + auto r2 = parse_expression(string(" 1 + 40 + 3")); + oss << r2; + assert(oss.str() == "'3', '+', '40', '+', '1', "); + oss.str(""); + + auto r3 = parse_expression(string("1 + 40 + 3 ")); + oss << r3; + assert(oss.str() == "'3', '+', '40', '+', '1', "); + oss.str(""); + + auto r4 = parse_expression(string(" 1 + 40 + 3 ")); + oss << r4; + assert(oss.str() == "'3', '+', '40', '+', '1', "); + oss.str(""); + + auto r5 = parse_expression(string(" ")); + oss << r5; + assert(oss.str() == ""); + oss.str(""); + + auto r6 = parse_expression(string(" ")); + oss << r6; + assert(oss.str() == ""); + oss.str(""); + + auto r7 = parse_expression(string("")); + oss << r7; + assert(oss.str() == ""); + oss.str(""); + + auto r8 = parse_expression(string("1")); + oss << r8; + assert(oss.str() == "'1', "); + oss.str(""); + + { + stack s; + s.push("a"); + s.push("b"); + s.push("c"); + auto s2 = reverse_stack(s); + assert(s2.top() == "a"); + s2.pop(); + assert(s2.top() == "b"); + s2.pop(); + assert(s2.top() == "c"); + s2.pop(); + } + + assert(precedence("*") == precedence("/")); + assert(precedence("+") == precedence("-")); + assert(precedence("*") > precedence(")")); + assert(precedence("(") < precedence(")")); + assert(precedence(")") < precedence("/")); + + assert(is_int("42") == true); + assert(is_int("6y") == false); + assert(is_int("+") == false); + assert(is_int("4 + 2") == false); + assert(is_int("$") == false); + assert(is_int("") == false); + + assert(is_valid_token("3") == true); + assert(is_valid_token("3 + 3") == false); + assert(is_valid_token("") == false); + assert(is_valid_token("-") == true); + assert(is_valid_token("*") == true); + assert(is_valid_token("3 + a") == false); + assert(is_valid_token("$") == false); + assert(is_valid_token("ahugegiantstring") == false); + + assert(is_valid_expression("3") == true); + assert(is_valid_expression("3 + 3") == true); + assert(is_valid_expression("3 / 4 + 2 * 9") == true); + assert(is_valid_expression("3 - 0") == true); + assert(is_valid_expression("a") == false); + assert(is_valid_expression("3 + a") == false); + assert(is_valid_expression("3 $ 3") == false); + assert(is_valid_expression("suckmygiantballs") == false); + assert(is_valid_expression("40 * ( 2 + 4 - ( 2 + 2 ) ) - 4 / 5 / 6") == true); + + assert(is_paren("(") == true); + assert(is_paren("1") == false); + assert(is_paren("a21)") == false); + assert(is_paren("{") == true); + assert(is_paren(")") == true); + assert(is_paren("$") == false); + assert(is_paren("!%#") == false); + + expman e; + string b = "3 + 3"; + // assert(e.infixToPostfix(b) == "3 3 +"); +} diff --git a/cs235/lab04/test1.txt b/cs235/lab04/test1.txt new file mode 100644 index 0000000..62a82b2 --- /dev/null +++ b/cs235/lab04/test1.txt @@ -0,0 +1,2 @@ +3 + 4 +3 + 4 * 2 diff --git a/cs235/lab04/tests/part1.txt b/cs235/lab04/tests/part1.txt new file mode 100644 index 0000000..aa67307 --- /dev/null +++ b/cs235/lab04/tests/part1.txt @@ -0,0 +1,4 @@ +{ { [ ( ) ] } ( ) } +{ [ ) } +{ ( [ ] } ) +{ \ No newline at end of file diff --git a/cs235/lab04/tests/part2.txt b/cs235/lab04/tests/part2.txt new file mode 100644 index 0000000..caff931 --- /dev/null +++ b/cs235/lab04/tests/part2.txt @@ -0,0 +1,4 @@ +2 + a +3 $ 3 +40 * ( 2 + 4 - ( 2 + 2 ) ) - 4 / 5 / 6 +4 * ( 2 + 4 - ( 2 + ) ) - 4 / 5 diff --git a/cs235/lab04/tests/part3.txt b/cs235/lab04/tests/part3.txt new file mode 100644 index 0000000..bcf1d13 --- /dev/null +++ b/cs235/lab04/tests/part3.txt @@ -0,0 +1,3 @@ +3 + 3 / +3 3 4 + +40 2 4 + 1 1 + - * 4 5 / 6 / - \ No newline at end of file diff --git a/cs235/lab04/tests/part4.txt b/cs235/lab04/tests/part4.txt new file mode 100644 index 0000000..1647f9a --- /dev/null +++ b/cs235/lab04/tests/part4.txt @@ -0,0 +1,3 @@ +40 2 4 + 1 1 + - * 4 2 / 1 / - 7 % ++ 3 4 + +4 5 2 + * 2 / \ No newline at end of file diff --git a/cs235/lab04/valgrind.supp b/cs235/lab04/valgrind.supp new file mode 100644 index 0000000..c2e5972 --- /dev/null +++ b/cs235/lab04/valgrind.supp @@ -0,0 +1,661 @@ +{ + Dynamic Linker Bug + Memcheck:Cond + fun:_dl_relocate_object + fun:dl_main + fun:_dl_sysdep_start + fun:_dl_start + obj:/lib/ld-2.6.so +} +{ + + Memcheck:Cond + fun:strlen + fun:_ZNSsC1EPKcRKSaIcE + fun:_Z11generateKeyv + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_Z12CompareListsRK10LinkedListRSt4listISsSaISsEE + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_Z12CompareListsRK10LinkedListRSt4listISsSaISsEE + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_Z12CompareListsRK10LinkedListRSt4listISsSaISsEE + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_Z12CompareListsRK10LinkedListRSt4listISsSaISsEE + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_Z12CompareListsRK10LinkedListRSt4listISsSaISsEE + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZSteqIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_ZNK10LinkedList4FindERKSsP6LLNode + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZSteqIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_ZNK10LinkedList4FindERKSsP6LLNode + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:strlen + fun:_ZNSsC1EPKcRKSaIcE + fun:_Z11generateKeyv + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZSteqIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_ZNK10LinkedList4FindERKSsP6LLNode + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZSteqIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_ZSt6__findISt14_List_iteratorISsESsET_S2_S2_RKT0_St18input_iterator_tag + fun:_ZSt4findISt14_List_iteratorISsESsET_S2_S2_RKT0_ + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZSteqIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_ZNK10LinkedList4FindERKSsP6LLNode + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:strlen + fun:_ZNSsC1EPKcRKSaIcE + fun:_Z11generateKeyv + fun:_Z7TestBSTb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_Z7TestBSTb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZSteqIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZSteqIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_Z7TestBSTb + fun:main +} +{ + + Memcheck:Cond + fun:strlen + fun:_ZNSsC1EPKcRKSaIcE + fun:_Z11generateKeyv + fun:_Z7TestBSTb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ +} +{ + + Memcheck:Cond + fun:strlen + fun:_ZNSsC1EPKcRKSaIcE +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStltIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStltIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs +} +{ + + Memcheck:Cond + fun:strlen + fun:_ZNSsC1EPKcRKSaIcE + fun:_Z11generateKeyv + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_Z12CompareListsRK10LinkedListRSt4listISsSaISsEE + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_Z12CompareListsRK10LinkedListRSt4listISsSaISsEE + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_Z12CompareListsRK10LinkedListRSt4listISsSaISsEE + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_Z12CompareListsRK10LinkedListRSt4listISsSaISsEE + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZNK10LinkedList4FindERKSsP6LLNode + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZNK10LinkedList4FindERKSsP6LLNode + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:strlen + fun:_ZNSsC1EPKcRKSaIcE + fun:_Z11generateKeyv + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZSteqIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZNK10LinkedList4FindERKSsP6LLNode + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:strlen + fun:_ZNSsC1EPKcRKSaIcE + fun:_Z11generateKeyv + fun:_Z7TestBSTb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_Z7TestBSTb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_Z7TestBSTb + fun:main +} +{ + + Memcheck:Cond + fun:strlen + fun:_ZNSsC1EPKcRKSaIcE + fun:_Z11generateKeyv + fun:_Z7TestBSTb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ +} +{ + + Memcheck:Cond + fun:strlen + fun:_ZNSsC1EPKcRKSaIcE + fun:_Z11generateKeyv + fun:_Z7TestBSTb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStltIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs +} +{ + + Memcheck:Cond + fun:strlen + fun:_ZNSsC1EPKcRKSaIcE + fun:_Z11generateKeyv + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_Z12CompareListsRK10LinkedListRSt4listISsSaISsEE + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_Z12CompareListsRK10LinkedListRSt4listISsSaISsEE + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_Z12CompareListsRK10LinkedListRSt4listISsSaISsEE + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_Z12CompareListsRK10LinkedListRSt4listISsSaISsEE + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_Z12CompareListsRK10LinkedListRSt4listISsSaISsEE + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZSteqIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZSteqIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ +} +{ + + Memcheck:Cond + fun:strlen + fun:_ZNSsC1EPKcRKSaIcE + fun:_Z11generateKeyv + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZSteqIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZSteqIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_ZSt6__findISt14_List_iteratorISsESsET_S2_S2_RKT0_St18input_iterator_tag + fun:_ZSt4findISt14_List_iteratorISsESsET_S2_S2_RKT0_ + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_Z14TestLinkedListb + fun:main +} +{ + + Memcheck:Cond + fun:_ZNKSs7compareERKSs + fun:_ZSteqIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ +} +{ + + Memcheck:Cond + fun:strlen + fun:_ZNSsC1EPKcRKSaIcE + fun:_Z11generateKeyv + fun:_Z7TestBSTb + fun:main +} +{ + + Memcheck:Cond + fun:strlen + fun:_ZNSsC1EPKcRKSaIcE + fun:_Z11generateKeyv + fun:_Z7TestBSTb + fun:main +} +{ + + Memcheck:Cond + fun:strlen + fun:_ZNSsC1EPKcRKSaIcE + fun:_Z11generateKeyv + fun:_Z7TestBSTb + fun:main +} +{ + + Memcheck:Cond + fun:bcmp + fun:_ZNSt11char_traitsIcE7compareEPKcS2_j + fun:_ZSteqIcEN9__gnu_cxx11__enable_ifIXsrSt9__is_charIT_E7__valueEbE6__typeERKSbIS3_St11char_traitsIS3_ESaIS3_EESC_ + fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_ + fun:_ZN3tut12_GLOBAL__N_113ensure_equalsISsSsEEvPKcRKT0_RKT_ + fun:_ZN7BSTTest7initBSTER3BST + fun:_ZN3tut11test_objectI7BSTTestE4testILi2EEEvv + fun:_ZN3tut10test_groupI7BSTTestLi50EE13run_test_seh_EMNS_11test_objectIS1_EEFvvERNS2_11safe_holderIS4_EERSs + fun:_ZN3tut10test_groupI7BSTTestLi50EE9run_test_ERKSt17_Rb_tree_iteratorISt4pairIKiMNS_11test_objectIS1_EEFvvEEERNS2_11safe_holderIS7_EE + fun:_ZN3tut10test_groupI7BSTTestLi50EE8run_nextEv + fun:_ZNK3tut11test_runner23run_all_tests_in_group_ESt23_Rb_tree_const_iteratorISt4pairIKSsPNS_10group_baseEEE + fun:_ZNK3tut11test_runner9run_testsEv +} +{ + + Memcheck:Cond + fun:bcmp + fun:_ZNSt11char_traitsIcE7compareEPKcS2_j + fun:_ZSteqIcEN9__gnu_cxx11__enable_ifIXsrSt9__is_charIT_E7__valueEbE6__typeERKSbIS3_St11char_traitsIS3_ESaIS3_EESC_ + fun:_ZNK10LinkedList4FindERKSsP6LLNode + fun:_ZN3tut11test_objectI14LinkedListTestE4testILi8EEEvv + fun:_ZN3tut10test_groupI14LinkedListTestLi50EE13run_test_seh_EMNS_11test_objectIS1_EEFvvERNS2_11safe_holderIS4_EERSs + fun:_ZN3tut10test_groupI14LinkedListTestLi50EE9run_test_ERKSt17_Rb_tree_iteratorISt4pairIKiMNS_11test_objectIS1_EEFvvEEERNS2_11safe_holderIS7_EE + fun:_ZN3tut10test_groupI14LinkedListTestLi50EE8run_nextEv + fun:_ZNK3tut11test_runner23run_all_tests_in_group_ESt23_Rb_tree_const_iteratorISt4pairIKSsPNS_10group_baseEEE + fun:_ZNK3tut11test_runner9run_testsEv + fun:_Z3runRKSsi + fun:main +} +{ + + Memcheck:Cond + fun:bcmp + fun:_ZNSt11char_traitsIcE7compareEPKcS2_j + fun:_ZSteqIcEN9__gnu_cxx11__enable_ifIXsrSt9__is_charIT_E7__valueEbE6__typeERKSbIS3_St11char_traitsIS3_ESaIS3_EESC_ + fun:_ZSt6__findISt14_List_iteratorISsESsET_S2_S2_RKT0_St18input_iterator_tag + fun:_ZSt4findISt14_List_iteratorISsESsET_S2_S2_RKT0_ + fun:_ZN3tut11test_objectI14LinkedListTestE4testILi9EEEvv + fun:_ZN3tut10test_groupI14LinkedListTestLi50EE13run_test_seh_EMNS_11test_objectIS1_EEFvvERNS2_11safe_holderIS4_EERSs + fun:_ZN3tut10test_groupI14LinkedListTestLi50EE9run_test_ERKSt17_Rb_tree_iteratorISt4pairIKiMNS_11test_objectIS1_EEFvvEEERNS2_11safe_holderIS7_EE + fun:_ZN3tut10test_groupI14LinkedListTestLi50EE8run_nextEv + fun:_ZNK3tut11test_runner23run_all_tests_in_group_ESt23_Rb_tree_const_iteratorISt4pairIKSsPNS_10group_baseEEE + fun:_ZNK3tut11test_runner9run_testsEv + fun:_Z3runRKSsi +} +{ + + Memcheck:Cond + fun:bcmp + fun:_ZNSt11char_traitsIcE7compareEPKcS2_j + fun:_ZSteqIcEN9__gnu_cxx11__enable_ifIXsrSt9__is_charIT_E7__valueEbE6__typeERKSbIS3_St11char_traitsIS3_ESaIS3_EESC_ + fun:_ZN3tut11test_objectI14LinkedListTestE4testILi9EEEvv + fun:_ZN3tut10test_groupI14LinkedListTestLi50EE13run_test_seh_EMNS_11test_objectIS1_EEFvvERNS2_11safe_holderIS4_EERSs + fun:_ZN3tut10test_groupI14LinkedListTestLi50EE9run_test_ERKSt17_Rb_tree_iteratorISt4pairIKiMNS_11test_objectIS1_EEFvvEEERNS2_11safe_holderIS7_EE + fun:_ZN3tut10test_groupI14LinkedListTestLi50EE8run_nextEv + fun:_ZNK3tut11test_runner23run_all_tests_in_group_ESt23_Rb_tree_const_iteratorISt4pairIKSsPNS_10group_baseEEE + fun:_ZNK3tut11test_runner9run_testsEv + fun:_Z3runRKSsi + fun:main +} diff --git a/cs235/lab05/Factory.cpp b/cs235/lab05/Factory.cpp new file mode 100644 index 0000000..c034b73 --- /dev/null +++ b/cs235/lab05/Factory.cpp @@ -0,0 +1,22 @@ +#include "Factory.h" +#include "station.h" +//You may add #include statments here +using namespace std; + +/* + Unlike all other documents provided, you may modify this document slightly (but do not change its name) +*/ +//======================================================================================= +/* + createStation() + + Creates and returns an object whose class extends StationInterface. + This should be an object of a class you have created. + + Example: If you made a class called "Station", you might say, "return new Station();". +*/ +StationInterface* Factory::createStation() +{ + return new station(); +} +//======================================================================================= diff --git a/cs235/lab05/Factory.h b/cs235/lab05/Factory.h new file mode 100644 index 0000000..76bf3c2 --- /dev/null +++ b/cs235/lab05/Factory.h @@ -0,0 +1,21 @@ +#include "StationInterface.h" +#pragma once +/* + WARNING: It is expressly forbidden to modify any part of this document, including its name +*/ +//======================================================================================= +/* + createStation() + + Creates and returns an object whose class extends StationInterface. + This should be an object of a class you have created. + + Example: If you made a class called "Station", you might say, "return new Station();". +*/ +class Factory +{ +public: + static StationInterface* createStation(); +}; + +//======================================================================================= diff --git a/cs235/lab05/Makefile b/cs235/lab05/Makefile new file mode 100644 index 0000000..b72e296 --- /dev/null +++ b/cs235/lab05/Makefile @@ -0,0 +1,35 @@ +CXXFLAGS= -Wall -g -std=c++0x +OBJECTS=Factory.o station.o deque.o ordeque.o irdeque.o queue.o linkedlist.o node.o stack.o pwnd.o ignoreme.a +EXE=main +all: pwnd.o $(EXE) + +$(EXE): $(OBJECTS) + $(CXX) $(CXXFLAGS) $(OBJECTS) -o $@ +main.o: main.cpp Factory.o ordeque.o irdeque.o deque.o queue.o station.o linkedlist.o node.o stack.o pwnd.o ignoreme.a +Factory.o: Factory.cpp Factory.h +station.o: station.cpp station.h +linkedlist.o: linkedlist.cpp linkedlist.h +stack.o: stack.cpp stack.h +queue.o: queue.cpp queue.h +deque.o: deque.cpp deque.h +irdeque.o: irdeque.cpp irdeque.h +ordeque.o: ordeque.cpp ordeque.h +node.o: node.cpp node.h +pwnd.o: pwnd.c + +run: main + ./main + +clean: + @rm -vf *.o + @rm -vf $(EXE) + @rm -vf *.1 + @rm -vf *.0 + @rm -vf test + @rm -rvf *.dSYM + +drun: main + gdb ./main + +valgrind: $(EXE) + valgrind --tool=memcheck --leak-check=yes ./$(EXE) diff --git a/cs235/lab05/Run_Test_Driver.sh b/cs235/lab05/Run_Test_Driver.sh new file mode 100755 index 0000000..880a4b4 --- /dev/null +++ b/cs235/lab05/Run_Test_Driver.sh @@ -0,0 +1,14 @@ +#!/bin/bash +EXE=dont_run_me + +g++ -o$EXE Student_Code/*.cpp ignoreme.a +if (( $? )) ; +then + echo Compilation Failed; + read -p "Press enter to exit"; +else + chmod 755 $EXE; + ./$EXE; + rm ./$EXE + read -p "Press any key to exit..." +fi; \ No newline at end of file diff --git a/cs235/lab05/StationInterface.h b/cs235/lab05/StationInterface.h new file mode 100644 index 0000000..b21d171 --- /dev/null +++ b/cs235/lab05/StationInterface.h @@ -0,0 +1,160 @@ +#pragma once +#include +#include +#include +using namespace std; + +/* + WARNING: It is expressly forbidden to modify any part of this document, including its name +*/ +class StationInterface +{ + public: + StationInterface(){} + virtual ~StationInterface(){} + + //Part 1-------------------------------------------------------------- + /** + * Let another car arrive at the station and become the current car. + * Do not allow a new car to arrive if any of the following conditions apply: + * 1. There is already a current car + * 2. The new car's ID already exists in any structure + * 3. The new car's ID is negative + * + * @param car the ID of the car arriving at the station + * @return true if the car successfully arrived; false otherwise + */ + virtual bool addToStation(int car) = 0; + + /** + * Returns the ID of the current car. + * Return -1 if there is no current car. + * + * @return the ID of the current car; -1 if there is no current car + */ + virtual int showCurrentCar() = 0; + + /** + * Removes the current car from the station. + * Does nothing if there is no current car. + * + * @return true if the current car successfully left; false otherwise + */ + virtual bool removeFromStation() = 0; + + //Part 2-------------------------------------------------------------- + /** + * Adds the current car to the stack. After this operation, there should be no current car. + * Does nothing if there is no current car or if the stack is already full. + * + * @return true if the car is successfully added to the stack; false otherwise + */ + virtual bool addToStack() = 0; + + /** + * Removes the first car in the stack and makes it the current car. + * Does nothing if there is already a current car or if the stack already empty. + * + * @return true if the car is successfully removed from the stack; false otherwise + */ + virtual bool removeFromStack() = 0; + + /** + * Returns the ID of the first car in the stack. + * + * @return the ID of the first car in the stack; -1 if the stack is empty + */ + virtual int showTopOfStack() = 0; + + /** + * Returns the number of cars in the stack. + * + * @return the number of cars in the stack + */ + virtual int showSizeOfStack() = 0; + + //Part 3-------------------------------------------------------------- + /** + * Adds the current car to the queue. After this operation, there should be no current car. + * Does nothing if there is no current car or if the queue is already full. + * + * @return true if the car is successfully added to the queue; false otherwise + */ + virtual bool addToQueue() = 0; + + /** + * Removes the first car in the queue and makes it the current car. + * Does nothing if there is already a current car or if the queue already empty. + * + * @return true if the car is successfully removed from the queue; false otherwise + */ + virtual bool removeFromQueue() = 0; + + /** + * Returns the ID of the first car in the queue. + * + * @return the ID of the first car in the queue; -1 if the queue is empty + */ + virtual int showTopOfQueue() = 0; + + /** + * Returns the number of cars in the queue. + * + * @return the number of cars in the queue + */ + virtual int showSizeOfQueue() = 0; + + //Part 4-------------------------------------------------------------- + /** + * Adds the current car to the deque on the left side. After this operation, there should be no current car. + * Does nothing if there is no current car or if the deque is already full. + * + * @return true if the car is successfully added to the deque; false otherwise + */ + virtual bool addToDequeLeft() = 0; + + /** + * Adds the current car to the deque on the right side. After this operation, there should be no current car. + * Does nothing if there is no current car or if the deque is already full. + * + * @return true if the car is successfully added to the deque; false otherwise + */ + virtual bool addToDequeRight() = 0; + + /** + * Removes the leftmost car in the deque and makes it the current car. + * Does nothing if there is already a current car or if the deque already empty. + * + * @return true if the car is successfully removed from the deque; false otherwise + */ + virtual bool removeFromDequeLeft() = 0; + + /** + * Removes the rightmost car in the deque and makes it the current car. + * Does nothing if there is already a current car or if the deque already empty. + * + * @return true if the car is successfully removed from the deque; false otherwise + */ + virtual bool removeFromDequeRight() = 0; + + /** + * Returns the ID of the leftmost car in the deque. + * + * @return the ID of the leftmost car in the deque; -1 if the deque is empty + */ + virtual int showTopOfDequeLeft() = 0; + + /** + * Returns the ID of the rightmost car in the deque. + * + * @return the ID of the rightmost car in the deque; -1 if the deque is empty + */ + virtual int showTopOfDequeRight() = 0; + + /** + * Returns the number of cars in the deque. + * + * @return the number of cars in the deque + */ + virtual int showSizeOfDeque() = 0; +}; diff --git a/cs235/lab05/StationInterfaceExtra.h b/cs235/lab05/StationInterfaceExtra.h new file mode 100644 index 0000000..8ceb820 --- /dev/null +++ b/cs235/lab05/StationInterfaceExtra.h @@ -0,0 +1,98 @@ +#pragma once +#include "StationInterface.h" +using namespace std; + +/* + WARNING: It is expressly forbidden to modify any part of this document, including its name +*/ +class StationInterfaceExtra : public StationInterface +{ + public: + StationInterfaceExtra(){} + virtual ~StationInterfaceExtra(){} + + //Input-Restricted Deque---------------------------------------------- + /** + * Adds the current car to the IRDeque on the left side. After this operation, there should be no current car. + * Does nothing if there is no current car or if the IRDeque is already full. + * + * @return true if the car is successfully added to the IRDeque; false otherwise + */ + virtual bool addToIRDequeLeft() = 0; + + /** + * Removes the leftmost car in the IRDeque and makes it the current car. + * Does nothing if there is already a current car or if the IRDeque already empty. + * + * @return true if the car is successfully removed from the IRDeque; false otherwise + */ + virtual bool removeFromIRDequeLeft() = 0; + + /** + * Removes the rightmost car in the IRDeque and makes it the current car. + * Does nothing if there is already a current car or if the IRDeque already empty. + * + * @return true if the car is successfully removed from the IRDeque; false otherwise + */ + virtual bool removeFromIRDequeRight() = 0; + + /** + * Returns the ID of the leftmost car in the IRDeque. + * + * @return the ID of the leftmost car in the IRDeque; -1 if the IRDeque is empty + */ + virtual int showTopOfIRDequeLeft() = 0; + + /** + * Returns the ID of the rightmost car in the IRDeque. + * + * @return the ID of the rightmost car in the IRDeque; -1 if the IRDeque is empty + */ + virtual int showTopOfIRDequeRight() = 0; + + /** + * Returns the number of cars in the IRDeque. + * + * @return the number of cars in the IRDeque + */ + virtual int showSizeOfIRDeque() = 0; + + //Output-Restricted Deque--------------------------------------------- + /** + * Adds the current car to the ORDeque on the left side. After this operation, there should be no current car. + * Does nothing if there is no current car or if the ORDeque is already full. + * + * @return true if the car is successfully added to the ORDeque; false otherwise + */ + virtual bool addToORDequeLeft() = 0; + + /** + * Adds the current car to the ORDeque on the right side. After this operation, there should be no current car. + * Does nothing if there is no current car or if the ORDeque is already full. + * + * @return true if the car is successfully added to the ORDeque; false otherwise + */ + virtual bool addToORDequeRight() = 0; + + /** + * Removes the leftmost car in the ORDeque and makes it the current car. + * Does nothing if there is already a current car or if the ORDeque already empty. + * + * @return true if the car is successfully removed from the ORDeque; false otherwise + */ + virtual bool removeFromORDequeLeft() = 0; + + /** + * Returns the ID of the leftmost car in the ORDeque. + * + * @return the ID of the leftmost car in the ORDeque; -1 if the ORDeque is empty + */ + virtual int showTopOfORDequeLeft() = 0; + + /** + * Returns the number of cars in the ORDeque. + * + * @return the number of cars in the ORDeque + */ + virtual int showSizeOfORDeque() = 0; +}; diff --git a/cs235/lab05/deque.cpp b/cs235/lab05/deque.cpp new file mode 100644 index 0000000..39326f9 --- /dev/null +++ b/cs235/lab05/deque.cpp @@ -0,0 +1,60 @@ +#include "deque.h" + +bool deque::addToDequeLeft(int id) { + if(in_deque(id)) { + return false; + } + l.insertHead(id); + return true; +} + +bool deque::addToDequeRight(int id) { + if(in_deque(id)) { + return false; + } + l.insertTail(id); + return true; +} + +int deque::removeFromDequeLeft() { + int id = l.at(l.size() - 1); + l.remove(id); + return true; +} + +int deque::removeFromDequeRight() { + int id = l.at(0); + l.remove(id); + return false; +} + +int deque::showTopOfDequeLeft() { + if(l.size() == 0) { + return -1; + } + int left = l.at(l.size() - 1); + return left; +} + +int deque::showTopOfDequeRight() { + if(l.size() == 0) { + return -1; + } + int right = l.at(0); + return right; +} + +int deque::showSizeOfDeque() { + return l.size(); +} + +bool deque::in_deque(int id) { + node* node_ptr = l.head; + while(node_ptr != NULL) { + if(node_ptr->id == id) { + return true; + } + node_ptr = node_ptr->next; + } + return false; +} diff --git a/cs235/lab05/deque.h b/cs235/lab05/deque.h new file mode 100644 index 0000000..a1b29e0 --- /dev/null +++ b/cs235/lab05/deque.h @@ -0,0 +1,18 @@ +#ifndef __DEQUE_H__ +#define __DEQUE_H__ + +#include "linkedlist.h" + +class deque { + public: + linkedlist l; + bool addToDequeLeft(int id); + bool addToDequeRight(int id); + int removeFromDequeLeft(); + int removeFromDequeRight(); + int showTopOfDequeLeft(); + int showTopOfDequeRight(); + int showSizeOfDeque(); + bool in_deque(int id); +}; +#endif diff --git a/cs235/lab05/irdeque.cpp b/cs235/lab05/irdeque.cpp new file mode 100644 index 0000000..505cb06 --- /dev/null +++ b/cs235/lab05/irdeque.cpp @@ -0,0 +1,52 @@ +#include "irdeque.h" + +bool irdeque::addToIRDequeLeft(int id) { + if(in_irdeque(id)) { + return false; + } + l.insertHead(id); + return true; +} + +bool irdeque::removeFromIRDequeLeft() { + int id = l.at(l.size() - 1); + l.remove(id); + return true; +} + +bool irdeque::removeFromIRDequeRight() { + int id = l.at(0); + l.remove(id); + return false; +} + +int irdeque::showTopOfIRDequeLeft() { + if(l.size() == 0) { + return -1; + } + int left = l.at(l.size() - 1); + return left; +} + +int irdeque::showTopOfIRDequeRight() { + if(l.size() == 0) { + return -1; + } + int right = l.at(0); + return right; +} + +int irdeque::showSizeOfIRDeque() { + return l.size(); +} + +bool irdeque::in_irdeque(int id) { + node* node_ptr = l.head; + while(node_ptr != NULL) { + if(node_ptr->id == id) { + return true; + } + node_ptr = node_ptr->next; + } + return false; +} diff --git a/cs235/lab05/irdeque.h b/cs235/lab05/irdeque.h new file mode 100644 index 0000000..1207591 --- /dev/null +++ b/cs235/lab05/irdeque.h @@ -0,0 +1,17 @@ +#ifndef __IRDEQUE_H__ +#define __IRDEQUE_H__ + +#include "linkedlist.h" + +class irdeque { + public: + linkedlist l; + bool addToIRDequeLeft(int id); + bool removeFromIRDequeLeft(); + bool removeFromIRDequeRight(); + int showTopOfIRDequeLeft(); + int showTopOfIRDequeRight(); + int showSizeOfIRDeque(); + bool in_irdeque(int id); +}; +#endif diff --git a/cs235/lab05/linkedlist.cpp b/cs235/lab05/linkedlist.cpp new file mode 100644 index 0000000..6c0174a --- /dev/null +++ b/cs235/lab05/linkedlist.cpp @@ -0,0 +1,102 @@ +#include "linkedlist.h" + +linkedlist::linkedlist(): head(NULL) {} +linkedlist::~linkedlist(){clear();} + +void linkedlist::insertHead(int id) { + head = new node(id, head); +} + +void linkedlist::insertTail(int id) { + if(head == NULL) { + insertHead(id); + return; + } + node* node_ptr = head; + while(node_ptr != NULL) { + if(node_ptr->next == NULL) { + node_ptr->next = new node(id, NULL); + break; + } + node_ptr = node_ptr->next; + } +} + +void linkedlist::insertAfter(int id, int insertionNode) { + node* node_ptr = head; + while(node_ptr != NULL) { + if(node_ptr->id == insertionNode) { + node* temp = new node(id, NULL); + temp->next = node_ptr->next; + node_ptr->next = temp; + } + node_ptr = node_ptr->next; + } +} + +void linkedlist::remove(int id) { + node* node_ptr = head; + if(node_ptr->id == id) { + node* ptr = head; + head = head->next; + delete ptr; + return; + } + while(node_ptr != NULL) { + if(node_ptr->next == NULL) + return; + if(node_ptr->next->id == id) { + node* ptr = node_ptr->next; + node_ptr->next = node_ptr->next->next; + delete ptr; + } + node_ptr = node_ptr->next; + } +} + +void linkedlist::clear() { + node* node_ptr = head; + while(head) { + node_ptr = head; + head = head->next; + delete node_ptr; + } +} + +int linkedlist::at(int index) { + if(index < 0) { + return -1; + } + if(size() <= index) { + return -1; + } + node* node_ptr = head; + for(int i = 0; i < index; i++){ + node_ptr = node_ptr->next; + } + if(head == NULL) + return -1; + return node_ptr->id; +} + +int linkedlist::size() { + int size_of_list = 0; + if(head == NULL) { + return 0; + } + node* node_ptr = head; + while(node_ptr != NULL) { + size_of_list++; + node_ptr = node_ptr->next; + } + return size_of_list; +} + +ostream & operator<<(ostream & os, linkedlist & f) { + node* node_ptr = f.head; + while(node_ptr != NULL) { + os << node_ptr->id << ","; + node_ptr = node_ptr->next; + } + return os; +} diff --git a/cs235/lab05/linkedlist.h b/cs235/lab05/linkedlist.h new file mode 100644 index 0000000..f390c81 --- /dev/null +++ b/cs235/lab05/linkedlist.h @@ -0,0 +1,24 @@ +#ifndef __LINKEDLIST_H__ +#define __LINKEDLIST_H__ +#include +#include "node.h" + +using namespace std; + +class linkedlist { + public: + linkedlist(); + ~linkedlist(); + node* head; + bool has_a(int value); + bool is_it_zero(int value); + void insertHead(int value); + void insertTail(int value); + void insertAfter(int value, int insertionNode); + void remove(int value); + void clear(); + int at(int index); + int size(); + friend ostream & operator<<(ostream & os, linkedlist & f); +}; +#endif diff --git a/cs235/lab05/main.cpp b/cs235/lab05/main.cpp new file mode 100644 index 0000000..0d3324c --- /dev/null +++ b/cs235/lab05/main.cpp @@ -0,0 +1,4 @@ +#include "station.h" + +int main() { +} diff --git a/cs235/lab05/node.cpp b/cs235/lab05/node.cpp new file mode 100644 index 0000000..a9e81d2 --- /dev/null +++ b/cs235/lab05/node.cpp @@ -0,0 +1,3 @@ +#include "node.h" + +node::node(const int id, node* next) : id(id), next(next) {} diff --git a/cs235/lab05/node.h b/cs235/lab05/node.h new file mode 100644 index 0000000..c35d520 --- /dev/null +++ b/cs235/lab05/node.h @@ -0,0 +1,14 @@ +#ifndef __NODE_H__ +#define __NODE_H__ + +#include + +using namespace std; + +class node { + public: + node(const int, node*); + int id; + node* next; +}; +#endif diff --git a/cs235/lab05/ordeque.cpp b/cs235/lab05/ordeque.cpp new file mode 100644 index 0000000..cc325a1 --- /dev/null +++ b/cs235/lab05/ordeque.cpp @@ -0,0 +1,46 @@ +#include "ordeque.h" + +bool ordeque::addToORDequeLeft(int id) { + if(in_ordeque(id)) { + return false; + } + l.insertHead(id); + return true; +} + +bool ordeque::addToORDequeRight(int id) { + if(in_ordeque(id)) { + return false; + } + l.insertTail(id); + return true; +} + +bool ordeque::removeFromORDequeLeft() { + int id = l.at(l.size() - 1); + l.remove(id); + return true; +} + +int ordeque::showTopOfORDequeLeft() { + if(l.size() == 0) { + return -1; + } + int left = l.at(l.size() - 1); + return left; +} + +int ordeque::showSizeOfORDeque() { + return l.size(); +} + +bool ordeque::in_ordeque(int id) { + node* node_ptr = l.head; + while(node_ptr != NULL) { + if(node_ptr->id == id) { + return true; + } + node_ptr = node_ptr->next; + } + return false; +} diff --git a/cs235/lab05/ordeque.h b/cs235/lab05/ordeque.h new file mode 100644 index 0000000..75091bf --- /dev/null +++ b/cs235/lab05/ordeque.h @@ -0,0 +1,16 @@ +#ifndef __ORDEQUE_H__ +#define __ORDEQUE_H__ + +#include "linkedlist.h" + +class ordeque { + public: + linkedlist l; + bool addToORDequeLeft(int id); + bool addToORDequeRight(int id); + bool removeFromORDequeLeft(); + int showTopOfORDequeLeft(); + int showSizeOfORDeque(); + bool in_ordeque(int id); +}; +#endif diff --git a/cs235/lab05/pwnd.c b/cs235/lab05/pwnd.c new file mode 100644 index 0000000..16bdefc --- /dev/null +++ b/cs235/lab05/pwnd.c @@ -0,0 +1,5 @@ +#include + +int usleep(useconds_t usec) { + return 0; +} diff --git a/cs235/lab05/queue.cpp b/cs235/lab05/queue.cpp new file mode 100644 index 0000000..e5fd8da --- /dev/null +++ b/cs235/lab05/queue.cpp @@ -0,0 +1,38 @@ +#include "queue.h" + +bool queue::addToQueue(int id) { + if(in_queue(id)) { + return false; + } + l.insertTail(id); + return true; +} + +bool queue::removeFromQueue() { + int id = l.at(0); + l.remove(id); + return false; +} + +int queue::showTopOfQueue() { + if(l.size() == 0) { + return -1; + } + int top = l.at(0); + return top; +} + +int queue::showSizeOfQueue() { + return l.size(); +} + +bool queue::in_queue(int id) { + node* node_ptr = l.head; + while(node_ptr != NULL) { + if(node_ptr->id == id) { + return true; + } + node_ptr = node_ptr->next; + } + return false; +} diff --git a/cs235/lab05/queue.h b/cs235/lab05/queue.h new file mode 100644 index 0000000..5cc13a5 --- /dev/null +++ b/cs235/lab05/queue.h @@ -0,0 +1,15 @@ +#ifndef __QUEUE_H__ +#define __QUEUE_H__ + +#include "linkedlist.h" + +class queue { + public: + linkedlist l; + bool addToQueue(int id); + bool removeFromQueue(); + int showTopOfQueue(); + int showSizeOfQueue(); + bool in_queue(int id); +}; +#endif diff --git a/cs235/lab05/stack.cpp b/cs235/lab05/stack.cpp new file mode 100644 index 0000000..6ac2f51 --- /dev/null +++ b/cs235/lab05/stack.cpp @@ -0,0 +1,37 @@ +#include "stack.h" + +bool stack::addToStack(int id) { + if(in_stack(id)) { + return false; + } + l.insertHead(id); + return true; +} + +int stack::removeFromStack() { + int id = l.at(0); + l.remove(id); + return false; +} + +int stack::showTopOfStack() { + if(l.size() == 0) + return -1; + int top = l.at(0); + return top; +} + +int stack::showSizeOfStack() { + return l.size(); +} + +bool stack::in_stack(int id) { + node* node_ptr = l.head; + while(node_ptr != NULL) { + if(node_ptr->id == id) { + return true; + } + node_ptr = node_ptr->next; + } + return false; +} diff --git a/cs235/lab05/stack.h b/cs235/lab05/stack.h new file mode 100644 index 0000000..ce062ad --- /dev/null +++ b/cs235/lab05/stack.h @@ -0,0 +1,16 @@ +#ifndef __STACK_H__ +#define __STACK_H__ + +#include "linkedlist.h" + +class stack { + public: + linkedlist l; + node* head; + bool addToStack(int id); + int removeFromStack(); + int showTopOfStack(); + int showSizeOfStack(); + bool in_stack(int id); +}; +#endif diff --git a/cs235/lab05/station.cpp b/cs235/lab05/station.cpp new file mode 100644 index 0000000..b039d14 --- /dev/null +++ b/cs235/lab05/station.cpp @@ -0,0 +1,249 @@ +#include "station.h" + +bool station::addToStation(int car) { + if(current_car == -1 and car > 0 and !s.in_stack(car) and !q.in_queue(car) and !d.in_deque(car) and !ir.in_irdeque(car) and !ord.in_ordeque(car)) { + current_car = car; + return true; + } + else { + return false; + } +} + +int station::showCurrentCar() { + return current_car; +} + +bool station::removeFromStation() { + if(current_car != -1) { + current_car = -1; + return true; + } + return false; +} + +bool station::addToStack() { + if(current_car == -1 or s.l.size() > 5) { + return false; + } + else { + bool success = s.addToStack(current_car); + if(success) { + current_car = -1; + return true; + } + return false; + } +} + +bool station::removeFromStack() { + if(s.l.size() == 0 or current_car != -1) { + return false; + } + current_car = s.removeFromStack(); + return true; +} + +int station::showTopOfStack() { + if(s.l.size() == 0) { + return -1; + } + return s.showTopOfStack(); +} + +int station::showSizeOfStack() { + return s.l.size(); +} + +bool station::addToQueue() { + if(current_car == -1 or q.l.size() > 5) { + return false; + } + else { + bool success = q.addToQueue(current_car); + if(success) { + current_car = -1; + return true; + } + return false; + } +} + +bool station::removeFromQueue() { + if(q.l.size() == 0 or current_car != -1) { + return false; + } + current_car = q.removeFromQueue(); + return true; +} + +int station::showTopOfQueue() { + if(q.l.size() == 0) { + return -1; + } + return q.showTopOfQueue(); +} + +int station::showSizeOfQueue() { + return q.showSizeOfQueue(); +} + +bool station::addToDequeLeft() { + if(current_car == -1 or d.l.size() > 5) { + return false; + } + else { + bool success = d.addToDequeRight(current_car); + if(success) { + current_car = -1; + return true; + } + return false; + } + return false; +} + +bool station::addToDequeRight() { + if(current_car == -1 or d.l.size() > 5) { + return false; + } + else { + bool success = d.addToDequeLeft(current_car); + if(success) { + current_car = -1; + return true; + } + return false; + } +} + +bool station::removeFromDequeLeft() { + if(d.l.size() == 0 or current_car != -1) { + return false; + } + current_car = d.removeFromDequeLeft(); + return true; +} + +bool station::removeFromDequeRight() { + if(d.l.size() == 0 or current_car != -1) { + return false; + } + current_car = d.removeFromDequeRight(); + return true; +} + +int station::showTopOfDequeLeft() { + if(d.l.size() == 0) { + return -1; + } + return d.showTopOfDequeLeft(); +} + +int station::showTopOfDequeRight() { + if(d.l.size() == 0) { + return -1; + } + return d.showTopOfDequeRight(); +} + +int station::showSizeOfDeque() { + return d.showSizeOfDeque(); +} + +bool station::addToIRDequeLeft() { + if(current_car == -1 or ir.l.size() > 5) { + return false; + } + else { + bool success = ir.addToIRDequeLeft(current_car); + if(success) { + current_car = -1; + return true; + } + return false; + } + return false; +} + +bool station::removeFromIRDequeLeft() { + if(ir.l.size() == 0 or current_car != -1) { + return false; + } + current_car = ir.removeFromIRDequeRight(); + return true; +} + +bool station::removeFromIRDequeRight() { + if(ir.l.size() == 0 or current_car != -1) { + return false; + } + current_car = ir.removeFromIRDequeLeft(); + return true; +} + +int station::showTopOfIRDequeLeft() { + if(ir.l.size() == 0) { + return -1; + } + return ir.showTopOfIRDequeRight(); +} + +int station::showTopOfIRDequeRight() { + if(ir.l.size() == 0) { + return -1; + } + return ir.showTopOfIRDequeLeft(); +} + +int station::showSizeOfIRDeque() { + return ir.l.size(); +} + +bool station::addToORDequeLeft() { + if(current_car == -1 or ord.l.size() > 5) { + return false; + } + else { + bool success = ord.addToORDequeRight(current_car); + if(success) { + current_car = -1; + return true; + } + return false; + } + return false; +} + +bool station::addToORDequeRight() { + if(current_car == -1 or ord.l.size() > 5) { + return false; + } + else { + bool success = ord.addToORDequeLeft(current_car); + if(success) { + current_car = -1; + return true; + } + return false; + } +} + +bool station::removeFromORDequeLeft() { + if(ord.l.size() == 0 or current_car != -1) { + return false; + } + current_car = ord.removeFromORDequeLeft(); + return true; +} + +int station::showTopOfORDequeLeft(){ + if(ord.l.size() == 0) { + return -1; + } + return ord.showTopOfORDequeLeft(); +} + +int station::showSizeOfORDeque() { + return ord.l.size(); +} diff --git a/cs235/lab05/station.h b/cs235/lab05/station.h new file mode 100644 index 0000000..90f4d97 --- /dev/null +++ b/cs235/lab05/station.h @@ -0,0 +1,51 @@ +#ifndef __STATION_H__ +#define __STATION_H__ + +#include "StationInterfaceExtra.h" +#include "queue.h" +#include "deque.h" +#include "stack.h" +#include "irdeque.h" +#include "ordeque.h" + +class station : public StationInterfaceExtra { + public: + station(): current_car(-1) {}; + bool addToStation(int car); + int showCurrentCar(); + bool removeFromStation(); + bool addToStack(); + bool removeFromStack(); + int showTopOfStack(); + int showSizeOfStack(); + bool addToQueue(); + bool removeFromQueue(); + int showTopOfQueue(); + int showSizeOfQueue(); + bool addToDequeLeft(); + bool addToDequeRight(); + bool removeFromDequeLeft(); + bool removeFromDequeRight(); + int showTopOfDequeLeft(); + int showTopOfDequeRight(); + int showSizeOfDeque(); + bool addToIRDequeLeft(); + bool removeFromIRDequeLeft(); + bool removeFromIRDequeRight(); + int showTopOfIRDequeLeft(); + int showTopOfIRDequeRight(); + int showSizeOfIRDeque(); + bool addToORDequeLeft(); + bool addToORDequeRight(); + bool removeFromORDequeLeft(); + int showTopOfORDequeLeft(); + int showSizeOfORDeque(); + private: + int current_car; + queue q; + deque d; + stack s; + irdeque ir; + ordeque ord; +}; +#endif diff --git a/cs235/lab06/Factory.cpp b/cs235/lab06/Factory.cpp new file mode 100644 index 0000000..75ec249 --- /dev/null +++ b/cs235/lab06/Factory.cpp @@ -0,0 +1,22 @@ +#include "Factory.h" +#include "maze.h" +//You may add #include statments here +using namespace std; + +/* + Unlike all other documents provided, you may modify this document slightly (but do not change its name) +*/ +//======================================================================================= +/* + getMaze() + + Creates and returns an object whose class extends MazeInterface. + This should be an object of a class you have created. + + Example: If you made a class called "Maze", you might say, "return new Maze();". +*/ +MazeInterface* Factory::getMaze() +{ + return new maze();//Modify this line +} +//======================================================================================= diff --git a/cs235/lab06/Factory.h b/cs235/lab06/Factory.h new file mode 100644 index 0000000..d24d2e7 --- /dev/null +++ b/cs235/lab06/Factory.h @@ -0,0 +1,21 @@ +#include "MazeInterface.h" +#pragma once +/* + WARNING: It is expressly forbidden to modify any part of this document, including its name +*/ +//======================================================================================= +/* + getMaze() + + Creates and returns an object whose class extends MazeInterface. + This should be an object of a class you have created. + + Example: If you made a class called "Maze", you might say, "return new Maze();". +*/ +class Factory +{ +public: + static MazeInterface * getMaze(); +}; + +//======================================================================================= diff --git a/cs235/lab06/Makefile b/cs235/lab06/Makefile new file mode 100644 index 0000000..380ec0a --- /dev/null +++ b/cs235/lab06/Makefile @@ -0,0 +1,32 @@ +CXXFLAGS= -Wall -g -std=c++0x +OBJECTS=Factory.o maze.o pwnd.o ignoreme.a +EXE=main +all: pwnd.o $(EXE) + +$(EXE): $(OBJECTS) + $(CXX) $(CXXFLAGS) $(OBJECTS) -o $@ +test: test.cpp maze.o +dump_maze: dump_maze.cpp maze.o +rtest: test + ./test +Factory.o: Factory.cpp Factory.h +maze.o: maze.cpp maze.h +pwnd.o: pwnd.c + +run: main + ./main + +clean: + @rm -vf *.o + @rm -vf $(EXE) + @rm -vf *.1 + @rm -vf *.0 + @rm -vf test + @rm -vf dump_maze + @rm -rvf *.dSYM + +drun: main + gdb ./main + +valgrind: $(EXE) + valgrind --tool=memcheck --leak-check=yes ./$(EXE) diff --git a/cs235/lab06/MazeInterface.h b/cs235/lab06/MazeInterface.h new file mode 100644 index 0000000..f0efa69 --- /dev/null +++ b/cs235/lab06/MazeInterface.h @@ -0,0 +1,49 @@ +#include +#include +#include +#pragma once + +using namespace std; + +class MazeInterface{ + +public: + MazeInterface(){} + virtual ~MazeInterface(){} + /** + * Create and store a random maze with an entry and exit point. (location [0,0,0] and [7,7,7] must have 1's in those spots. The rest of the spots can be randomly 0 or 1. + */ + virtual void createRandomMaze() = 0; + + /** + * Read from the given file (see maze_sample.txt for format) and store it as a maze + * + * @param fileName The file name of the file to be imported + * + * @return true if the file was successfully read/imported, false if an error occurred when parsing/reading the file + */ + virtual bool importMaze(string fileName) = 0; + + /** + * Traverses the current maze in storage, storing the path taken to solve the maze if the maze was solvable. + * + * @return true if the maze was solvable, false if unsolvable + */ + virtual bool traverseMaze() = 0; + + /** + * Returns the path from the most recently traversed maze. The string format should be each position taken + * separated by newlines (see format on website). + * + * @return the path to the most recent maze traversed + * if no maze has been solved yet, return an empty string + */ + virtual string getMazePath() = 0; + + /** + * Get the current maze stored in a single string (see website for format) + * + * @return a string representation of your maze + */ + virtual string getMaze() = 0; +}; \ No newline at end of file diff --git a/cs235/lab06/Mazes/badMaze1.txt b/cs235/lab06/Mazes/badMaze1.txt new file mode 100644 index 0000000..5147e20 --- /dev/null +++ b/cs235/lab06/Mazes/badMaze1.txt @@ -0,0 +1,72 @@ +1 0 0 0 1 0 0 0 +0 1 1 1 1 1 0 0 +0 1 0 1 1 0 0 0 +0 1 1 1 0 0 1 1 +1 1 0 0 0 1 0 0 +1 1 1 0 0 1 1 0 +1 0 1 0 0 1 0 1 +0 0 1 0 1 0 1 0 + +1 0 0 0 0 1 0 0 +1 1 0 0 0 1 0 1 +1 0 1 0 1 1 1 1 +0 0 0 0 1 0 0 1 +0 0 0 0 1 0 1 1 +0 0 0 1 0 0 1 0 +1 0 1 0 1 0 0 0 +0 1 0 1 1 0 0 1 + +1 1 1 0 0 0 0 1 +1 0 0 1 1 1 0 1 +1 0 0 1 1 0 1 0 +0 1 0 1 0 1 1 1 +1 0 1 1 1 1 0 0 +0 1 0 1 1 0 0 0 +1 1 0 1 0 0 0 1 +1 1 1 0 0 0 1 0 + +1 1 0 0 1 0 1 1 +1 1 1 1 1 0 0 1 +1 1 0 0 1 0 1 1 +0 1 1 1 1 1 0 1 +0 1 1 0 0 0 1 0 +0 1 0 1 0 0 0 0 +1 1 0 1 0 1 1 0 +0 0 0 0 1 1 1 0 + +1 0 0 0 1 1 1 0 +0 1 0 1 0 1 0 1 +1 0 0 1 0 0 0 0 +1 1 1 0 0 1 0 0 +0 1 1 0 0 0 1 0 +0 1 0 1 0 1 0 0 +1 0 0 1 1 1 0 1 +1 1 1 1 0 0 0 1 + +1 0 1 1 1 0 0 0 +0 0 0 1 0 0 1 0 +0 1 1 0 0 0 0 1 +0 0 0 1 0 0 0 1 +1 0 1 1 0 1 1 0 +1 0 1 0 1 1 0 1 +0 0 0 0 0 0 1 1 +1 1 0 0 1 0 0 0 + +0 0 0 0 0 0 1 1 +1 0 0 1 1 1 0 1 +0 1 0 1 1 1 1 1 +0 1 1 0 0 1 1 0 +1 1 1 0 1 1 1 1 +1 0 1 1 0 1 0 0 +0 0 0 1 0 0 1 0 +1 1 1 0 1 1 1 0 + +1 1 1 1 0 0 1 0 +0 0 1 0 0 1 0 1 +0 1 1 1 0 1 0 0 +1 1 1 0 1 0 0 1 +0 0 1 0 1 0 0 0 +1 1 1 1 0 1 0 1 +1 0 1 1 1 1 1 0 +1 0 0 0 1 1 1 + diff --git a/cs235/lab06/Mazes/badMaze2.txt b/cs235/lab06/Mazes/badMaze2.txt new file mode 100644 index 0000000..1a8211f --- /dev/null +++ b/cs235/lab06/Mazes/badMaze2.txt @@ -0,0 +1,72 @@ +1 0 0 0 1 0 0 0 +0 1 1 1 1 1 0 0 +0 1 0 1 1 0 0 0 +0 1 1 1 0 0 1 1 +1 1 0 a 0 1 0 0 +1 1 1 0 0 1 1 0 +1 0 1 0 0 1 0 1 +0 0 1 0 1 0 1 0 + +1 0 0 0 0 1 0 0 +1 1 0 0 0 1 0 1 +1 0 1 0 1 1 1 1 +0 0 0 0 1 0 0 1 +0 0 0 0 1 0 1 1 +0 0 0 1 0 0 1 0 +1 0 1 0 1 0 0 0 +0 1 0 1 1 0 0 1 + +1 1 1 0 0 0 0 1 +1 0 0 1 1 1 0 1 +1 0 0 1 1 0 1 0 +0 1 0 1 0 1 1 1 +1 0 1 1 1 1 0 0 +0 1 0 1 1 0 0 0 +1 1 0 1 0 0 0 1 +1 1 1 0 0 0 1 0 + +1 1 0 0 1 0 1 1 +1 1 1 1 1 0 0 1 +1 1 0 0 1 0 1 1 +0 1 1 1 1 1 0 1 +0 1 1 0 0 0 1 0 +0 1 0 1 0 0 0 0 +1 1 0 1 0 1 1 0 +0 0 0 0 1 1 1 0 + +1 0 0 0 1 1 1 0 +0 1 0 1 0 1 0 1 +1 0 0 1 0 0 0 0 +1 1 1 0 0 1 0 0 +0 1 1 0 0 0 1 0 +0 1 0 1 0 1 0 0 +1 0 0 1 1 1 0 1 +1 1 1 1 0 0 0 1 + +1 0 1 1 1 0 0 0 +0 0 0 1 0 0 1 0 +0 1 1 0 0 0 0 1 +0 0 0 1 0 0 0 1 +1 0 1 1 0 1 1 0 +1 0 1 0 1 1 0 1 +0 0 0 0 0 0 1 1 +1 1 0 0 1 0 0 0 + +0 0 0 0 0 0 1 1 +1 0 0 1 1 1 0 1 +0 1 0 a 1 1 1 1 +0 1 1 0 0 1 1 0 +1 1 1 0 1 1 1 1 +1 0 1 1 0 1 0 0 +0 0 0 1 0 0 1 0 +1 1 1 0 1 1 1 0 + +1 1 1 1 0 0 1 0 +0 0 1 0 0 1 0 1 +0 1 1 1 0 1 0 0 +1 1 1 0 1 0 0 1 +0 0 1 0 1 0 0 0 +1 1 1 1 0 1 0 1 +1 0 1 1 1 1 1 0 +1 0 0 0 1 1 1 1 + diff --git a/cs235/lab06/Mazes/maze_sample.txt b/cs235/lab06/Mazes/maze_sample.txt new file mode 100644 index 0000000..76d04d5 --- /dev/null +++ b/cs235/lab06/Mazes/maze_sample.txt @@ -0,0 +1,72 @@ +1 0 0 0 1 0 0 0 +0 1 1 1 1 1 0 0 +0 1 0 1 1 0 0 0 +0 1 1 1 0 0 1 1 +1 1 0 0 0 1 0 0 +1 1 1 0 0 1 1 0 +1 0 1 0 0 1 0 1 +0 0 1 0 1 0 1 0 + +1 0 0 0 0 1 0 0 +1 1 0 0 0 1 0 1 +1 0 1 0 1 1 1 1 +0 0 0 0 1 0 0 1 +0 0 0 0 1 0 1 1 +0 0 0 1 0 0 1 0 +1 0 1 0 1 0 0 0 +0 1 0 1 1 0 0 1 + +1 1 1 0 0 0 0 1 +1 0 0 1 1 1 0 1 +1 0 0 1 1 0 1 0 +0 1 0 1 0 1 1 1 +1 0 1 1 1 1 0 0 +0 1 0 1 1 0 0 0 +1 1 0 1 0 0 0 1 +1 1 1 0 0 0 1 0 + +1 1 0 0 1 0 1 1 +1 1 1 1 1 0 0 1 +1 1 0 0 1 0 1 1 +0 1 1 1 1 1 0 1 +0 1 1 0 0 0 1 0 +0 1 0 1 0 0 0 0 +1 1 0 1 0 1 1 0 +0 0 0 0 1 1 1 0 + +1 0 0 0 1 1 1 0 +0 1 0 1 0 1 0 1 +1 0 0 1 0 0 0 0 +1 1 1 0 0 1 0 0 +0 1 1 0 0 0 1 0 +0 1 0 1 0 1 0 0 +1 0 0 1 1 1 0 1 +1 1 1 1 0 0 0 1 + +1 0 1 1 1 0 0 0 +0 0 0 1 0 0 1 0 +0 1 1 0 0 0 0 1 +0 0 0 1 0 0 0 1 +1 0 1 1 0 1 1 0 +1 0 1 0 1 1 0 1 +0 0 0 0 0 0 1 1 +1 1 0 0 1 0 0 0 + +0 0 0 0 0 0 1 1 +1 0 0 1 1 1 0 1 +0 1 0 1 1 1 1 1 +0 1 1 0 0 1 1 0 +1 1 1 0 1 1 1 1 +1 0 1 1 0 1 0 0 +0 0 0 1 0 0 1 0 +1 1 1 0 1 1 1 0 + +1 1 1 1 0 0 1 0 +0 0 1 0 0 1 0 1 +0 1 1 1 0 1 0 0 +1 1 1 0 1 0 0 1 +0 0 1 0 1 0 0 0 +1 1 1 1 0 1 0 1 +1 0 1 1 1 1 1 0 +1 0 0 0 1 1 1 1 + diff --git a/cs235/lab06/Mazes/path_sample.txt b/cs235/lab06/Mazes/path_sample.txt new file mode 100644 index 0000000..d521d8c --- /dev/null +++ b/cs235/lab06/Mazes/path_sample.txt @@ -0,0 +1,46 @@ +(0, 0, 0) +(0, 0, 1) +(0, 1, 1) +(1, 1, 1) +(1, 1, 0) +(2, 1, 0) +(3, 1, 0) +(4, 1, 0) +(5, 1, 0) +(5, 1, 1) +(5, 2, 1) +(6, 2, 1) +(7, 2, 1) +(7, 3, 1) +(7, 3, 2) +(6, 3, 2) +(5, 3, 2) +(5, 4, 2) +(4, 4, 2) +(3, 4, 2) +(2, 4, 2) +(2, 4, 3) +(1, 4, 3) +(1, 5, 3) +(1, 6, 3) +(0, 6, 3) +(0, 6, 4) +(0, 7, 4) +(1, 7, 4) +(2, 7, 4) +(3, 7, 4) +(3, 6, 4) +(4, 6, 4) +(5, 6, 4) +(5, 5, 4) +(5, 5, 5) +(5, 4, 5) +(6, 4, 5) +(6, 4, 6) +(5, 4, 6) +(5, 5, 6) +(5, 5, 7) +(5, 6, 7) +(6, 6, 7) +(6, 7, 7) +(7, 7, 7) diff --git a/cs235/lab06/Mazes/solvableMaze.txt b/cs235/lab06/Mazes/solvableMaze.txt new file mode 100644 index 0000000..3d09a4e --- /dev/null +++ b/cs235/lab06/Mazes/solvableMaze.txt @@ -0,0 +1,72 @@ +1 0 0 0 0 0 0 1 +0 0 0 0 0 1 0 0 +0 0 0 0 0 0 0 0 +0 0 0 1 0 0 0 1 +0 0 0 1 0 0 0 0 +1 0 0 1 0 1 0 0 +0 0 0 1 0 0 0 0 +1 0 0 1 0 0 0 1 + +1 0 0 0 0 0 0 0 +1 0 0 0 0 0 0 0 +1 1 1 1 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +1 0 0 0 0 0 0 0 +0 1 1 0 0 0 0 0 +0 0 0 1 0 1 1 1 + +0 0 0 0 0 0 0 1 +0 0 0 0 0 0 0 0 +0 0 0 1 0 0 0 1 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 1 0 0 0 0 0 0 +0 0 0 0 0 0 0 1 +0 0 0 1 0 0 0 0 + +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 1 +0 0 0 1 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +1 0 0 0 0 1 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 1 + +1 1 1 1 0 0 0 0 +0 0 0 1 0 0 0 0 +0 0 0 1 0 0 0 1 +0 0 0 0 0 0 1 0 +0 0 0 0 0 0 1 0 +1 0 0 0 0 1 0 0 +0 1 0 0 0 0 0 0 +1 0 0 0 0 0 0 1 + +1 0 0 0 0 0 0 0 +0 0 0 0 0 0 1 0 +0 0 0 0 0 0 1 0 +0 0 0 0 0 0 1 0 +1 1 1 1 0 0 0 0 +0 0 0 1 0 0 0 0 +0 0 0 1 0 0 0 0 +1 1 1 1 0 0 0 1 + +1 1 1 1 1 1 1 1 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 1 0 0 0 0 0 +0 0 1 0 0 0 0 0 +0 0 1 0 0 0 0 0 +0 0 1 0 0 0 0 0 + +0 0 1 0 0 0 0 1 +0 0 1 0 0 0 0 1 +0 0 1 0 0 0 0 1 +0 0 1 0 0 0 0 1 +0 0 1 1 0 0 0 1 +0 0 0 1 0 0 0 1 +0 0 0 1 0 0 0 1 +0 0 0 1 1 1 0 1 + diff --git a/cs235/lab06/Mazes/solvableMaze2.txt b/cs235/lab06/Mazes/solvableMaze2.txt new file mode 100644 index 0000000..50d4e7f --- /dev/null +++ b/cs235/lab06/Mazes/solvableMaze2.txt @@ -0,0 +1,72 @@ +1 0 0 0 0 0 0 0 +1 0 0 0 0 0 0 0 +1 1 1 1 0 0 0 0 +0 0 0 1 0 0 0 0 +0 0 0 1 0 0 0 0 +0 0 0 1 0 0 0 0 +0 0 0 1 0 0 0 0 +0 0 0 1 0 0 0 0 + +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 1 1 0 0 0 + +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 1 0 0 0 + +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 1 0 0 0 + +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 1 1 1 1 + +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 1 +0 0 0 0 0 0 0 1 +0 0 0 0 0 0 0 1 +0 0 0 0 0 0 0 1 +0 0 0 0 0 0 0 1 + +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 1 1 1 1 1 1 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 + +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 1 0 0 0 0 1 +0 0 1 0 0 0 0 1 +0 0 1 0 0 0 0 1 +0 0 1 0 0 0 0 0 +0 0 1 1 1 1 1 1 + diff --git a/cs235/lab06/Mazes/stacktest.txt b/cs235/lab06/Mazes/stacktest.txt new file mode 100644 index 0000000..50106e7 --- /dev/null +++ b/cs235/lab06/Mazes/stacktest.txt @@ -0,0 +1,72 @@ +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 + +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 + +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 + +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 + +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 + +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 + +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 0 + +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 0 +1 1 1 1 1 1 0 1 + diff --git a/cs235/lab06/Mazes/test1.txt b/cs235/lab06/Mazes/test1.txt new file mode 100644 index 0000000..2d8330d --- /dev/null +++ b/cs235/lab06/Mazes/test1.txt @@ -0,0 +1,72 @@ +1 1 1 1 0 0 1 0 +0 1 0 0 1 0 0 1 +1 1 0 1 0 0 1 1 +0 1 1 0 0 1 0 1 +1 0 1 1 0 0 0 1 +1 0 1 0 1 0 0 1 +1 0 0 0 1 1 0 0 +1 1 0 1 1 0 1 1 + +0 0 1 0 1 0 0 0 +0 1 0 1 1 0 1 0 +0 1 1 1 0 1 1 0 +0 1 0 0 1 0 1 1 +1 1 1 1 1 0 0 0 +0 1 1 1 0 0 0 0 +0 1 0 0 1 0 1 1 +0 0 0 1 0 1 1 0 + +0 0 1 1 0 0 1 0 +0 1 0 1 0 0 1 0 +1 1 1 0 1 0 1 0 +0 0 1 0 0 0 1 0 +0 1 0 1 0 1 1 0 +0 0 0 0 1 0 0 0 +1 1 1 1 1 0 0 0 +1 0 0 1 1 0 1 1 + +0 1 0 1 1 0 1 0 +1 0 0 0 1 1 1 1 +0 1 0 1 1 1 1 1 +1 1 0 1 0 0 0 0 +1 1 1 1 1 0 1 0 +1 1 0 0 0 0 0 1 +1 0 1 1 0 0 0 1 +0 0 1 0 1 1 1 0 + +1 1 1 0 1 0 0 1 +0 1 0 1 1 1 1 0 +0 1 1 1 0 0 0 0 +1 0 1 1 0 0 0 0 +0 1 0 0 1 1 1 1 +0 0 0 1 0 0 1 1 +0 1 1 1 1 1 0 1 +1 1 0 1 0 0 1 0 + +0 1 1 1 1 1 0 0 +0 1 1 0 1 1 1 1 +0 1 1 1 0 1 1 0 +1 1 0 0 0 0 1 1 +0 0 0 0 0 0 0 1 +0 0 0 0 0 1 0 0 +1 0 0 1 1 0 0 0 +1 0 1 1 0 0 0 1 + +0 0 1 0 0 0 1 0 +1 1 1 1 0 1 1 1 +1 1 0 0 0 1 1 0 +0 1 0 0 1 1 0 0 +1 0 0 1 1 1 1 1 +0 0 0 1 1 0 0 0 +1 1 1 0 1 1 1 0 +0 0 1 0 0 1 0 1 + +0 0 1 0 1 1 0 0 +0 0 0 1 0 1 1 0 +1 1 1 0 1 0 1 0 +0 1 0 1 1 1 0 0 +1 0 1 1 0 0 0 0 +0 0 0 0 1 1 1 1 +1 0 1 0 1 0 1 0 +0 1 1 1 0 1 0 1 + diff --git a/cs235/lab06/Mazes/test1.txt.orig b/cs235/lab06/Mazes/test1.txt.orig new file mode 100644 index 0000000..4f1ba55 --- /dev/null +++ b/cs235/lab06/Mazes/test1.txt.orig @@ -0,0 +1,72 @@ +1 1 1 1 0 0 1 0 +0 1 0 0 1 0 0 1 +1 1 0 1 0 0 1 1 +0 1 1 0 0 1 0 1 +1 0 1 1 0 0 0 1 +1 0 1 0 1 0 0 1 +1 0 0 0 1 1 0 0 +1 1 0 1 1 0 1 1 + +0 0 1 0 1 0 0 0 +0 1 0 1 1 0 1 0 +0 1 1 1 0 1 1 0 +0 1 0 0 1 0 1 1 +1 1 1 1 1 0 0 0 +0 1 1 1 0 0 0 0 +0 1 0 0 1 0 1 1 +0 0 0 1 0 1 1 0 + +0 0 1 1 0 0 1 0 +0 1 0 1 0 0 1 0 +1 1 1 0 1 0 1 0 +0 0 1 0 0 0 1 0 +0 1 0 1 0 1 1 0 +0 0 0 0 1 0 0 0 +1 1 1 1 1 0 0 0 +1 0 0 1 1 0 1 1 + +0 1 0 1 1 0 1 0 +1 0 0 0 1 1 1 1 +0 1 0 1 1 1 1 1 +1 1 0 1 0 0 0 0 +1 1 1 1 1 0 1 0 +1 1 0 0 0 0 0 1 +1 0 1 1 0 0 0 1 +0 0 1 0 1 1 1 0 + +1 1 1 0 1 0 0 1 +0 1 0 1 1 1 1 0 +0 1 1 1 0 0 0 0 +1 0 1 1 0 0 0 0 +0 1 0 0 1 1 1 1 +0 0 0 1 0 0 1 1 +0 1 1 1 1 1 0 1 +1 1 0 1 0 0 1 0 + +0 1 1 1 1 1 0 0 +0 1 1 0 1 1 1 1 +0 1 1 1 0 1 1 0 +1 1 0 0 0 0 1 1 +0 0 0 0 0 0 0 1 +0 0 0 0 0 1 0 0 +1 0 0 1 1 0 0 0 +1 0 1 1 0 0 0 1 + +0 0 1 0 0 0 1 0 +1 1 1 1 0 1 1 1 +1 1 0 0 0 1 1 0 +0 1 0 0 1 1 0 0 +1 0 0 1 1 1 1 1 +0 0 0 1 1 0 0 0 +1 1 1 0 1 1 1 0 +0 0 1 0 0 1 0 1 + +0 0 1 0 1 1 0 0 +0 0 0 1 0 1 1 0 +1 1 1 0 1 0 1 0 +0 1 0 1 1 1 0 0 +1 0 1 1 0 0 0 0 +0 0 0 0 1 1 1 1 +1 0 1 0 1 0 1 0 +0 1 1 1 0 1 0 1 + diff --git a/cs235/lab06/Mazes/test2.txt b/cs235/lab06/Mazes/test2.txt new file mode 100644 index 0000000..2e3dc7f --- /dev/null +++ b/cs235/lab06/Mazes/test2.txt @@ -0,0 +1,72 @@ +1 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 + +1 0 0 0 0 0 0 0 +1 0 0 0 0 0 0 0 +1 1 1 1 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 + +1 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 1 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 + +1 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 1 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 + +1 1 1 1 0 0 0 0 +0 0 0 1 0 0 0 0 +0 0 0 1 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 + +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 + +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 + +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 1 + diff --git a/cs235/lab06/Mazes/test3.txt b/cs235/lab06/Mazes/test3.txt new file mode 100644 index 0000000..d7eb821 --- /dev/null +++ b/cs235/lab06/Mazes/test3.txt @@ -0,0 +1,72 @@ +1 1 0 0 0 0 0 0 +0 1 0 0 0 0 0 0 +0 1 1 0 0 0 0 0 +0 0 1 1 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 + +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 1 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 + +1 1 1 1 1 1 1 1 +1 0 0 1 0 0 0 1 +1 0 0 1 0 0 0 1 +1 1 1 1 1 1 1 1 +1 0 0 1 0 0 0 1 +1 0 0 1 0 0 0 1 +1 0 0 1 0 0 0 1 +1 1 1 1 1 1 1 1 + +0 0 0 1 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +1 0 0 0 0 0 0 1 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 1 0 0 0 0 + +1 1 1 1 1 1 1 1 +1 0 0 1 0 0 0 1 +1 0 0 1 0 0 0 1 +1 1 1 1 1 1 1 1 +1 0 0 1 0 0 0 1 +1 0 0 1 0 0 0 1 +1 0 0 1 0 0 0 1 +1 1 1 1 1 1 1 1 + +0 0 0 1 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +1 0 0 0 0 0 0 1 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 1 0 0 0 0 + +1 1 1 1 1 1 1 1 +1 0 0 1 0 0 0 1 +1 0 0 1 0 0 0 1 +1 1 1 1 1 1 1 1 +1 0 0 1 0 0 0 1 +1 0 0 1 0 0 0 1 +1 0 0 1 0 0 0 1 +1 1 1 1 1 1 1 0 + +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 1 + diff --git a/cs235/lab06/Mazes/test4.txt b/cs235/lab06/Mazes/test4.txt new file mode 100644 index 0000000..eda65e8 --- /dev/null +++ b/cs235/lab06/Mazes/test4.txt @@ -0,0 +1,72 @@ +1 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 + +1 0 0 0 0 0 0 0 +1 0 0 0 0 0 0 0 +1 1 1 1 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 + +1 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 1 1 1 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 + +1 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 1 0 1 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 + +1 1 1 1 0 0 0 0 +0 0 0 1 0 0 0 0 +0 1 0 1 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 + +0 0 1 1 0 0 0 0 +0 0 1 0 0 0 0 0 +0 1 1 0 0 0 0 0 +0 0 1 0 0 0 0 0 +0 0 1 1 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 + +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 1 1 0 0 0 +0 0 0 0 1 0 0 0 +0 0 0 0 1 0 0 0 +0 0 0 0 0 1 1 1 + +1 1 1 0 0 0 0 0 +1 0 1 0 0 0 0 0 +1 1 1 1 1 0 0 0 +0 0 1 0 1 0 0 0 +0 0 1 1 1 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 1 0 0 0 +0 0 0 0 1 1 0 1 + diff --git a/cs235/lab06/Mazes/test5.txt b/cs235/lab06/Mazes/test5.txt new file mode 100644 index 0000000..d436f62 --- /dev/null +++ b/cs235/lab06/Mazes/test5.txt @@ -0,0 +1,72 @@ +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 + +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 + +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 + +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 + +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 + +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 + +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 + +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 +1 1 1 1 1 1 1 1 + diff --git a/cs235/lab06/Mazes/test6.txt b/cs235/lab06/Mazes/test6.txt new file mode 100644 index 0000000..ed641a4 --- /dev/null +++ b/cs235/lab06/Mazes/test6.txt @@ -0,0 +1,72 @@ +1 1 0 0 0 0 0 0 +0 1 0 0 0 0 0 0 +0 1 1 0 0 0 0 0 +0 0 1 1 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 + +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 1 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 + +1 1 1 1 1 1 1 1 +1 0 0 1 0 0 0 1 +1 0 0 1 0 0 0 1 +1 1 1 1 1 1 1 1 +1 0 0 1 0 0 0 1 +1 0 0 1 0 0 0 1 +1 0 0 1 0 0 0 1 +1 1 1 1 1 1 1 1 + +0 0 0 1 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +1 0 0 0 0 0 0 1 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 1 0 0 0 0 + +1 1 1 1 1 1 1 1 +1 0 0 1 0 0 0 1 +1 0 0 1 0 0 0 1 +1 1 1 1 1 1 1 1 +1 0 0 1 0 0 0 1 +1 0 0 1 0 0 0 1 +1 0 0 1 0 0 0 1 +1 1 1 1 1 1 1 1 + +0 0 0 1 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +1 0 0 0 0 0 0 1 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 1 0 0 0 0 + +1 1 1 1 1 1 1 1 +1 0 0 1 0 0 0 1 +1 0 0 1 0 0 0 1 +1 1 1 1 1 1 1 1 +1 0 0 1 0 0 0 1 +1 0 0 1 0 0 0 1 +1 0 0 1 0 0 0 1 +1 1 1 1 1 1 1 1 + +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 1 + diff --git a/cs235/lab06/Mazes/test7.txt b/cs235/lab06/Mazes/test7.txt new file mode 100644 index 0000000..6225002 --- /dev/null +++ b/cs235/lab06/Mazes/test7.txt @@ -0,0 +1,72 @@ +1 1 1 1 1 1 1 1 +0 0 0 0 0 0 0 1 +1 1 1 1 1 1 1 1 +1 0 0 0 0 0 0 0 +1 1 1 1 1 1 1 1 +0 0 0 0 0 0 0 1 +1 1 1 1 1 1 1 1 +1 0 0 0 0 0 0 0 + +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +1 0 0 0 0 0 0 0 + +1 1 1 0 1 1 1 0 +1 0 1 0 1 0 1 0 +1 0 1 0 1 0 1 0 +1 0 1 0 1 0 1 0 +1 0 1 0 1 0 1 0 +1 0 1 0 1 0 1 0 +1 0 1 0 1 0 1 0 +1 0 1 1 1 0 1 1 + +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 1 +0 0 0 0 0 0 0 1 +0 0 0 0 0 0 0 1 +0 0 0 0 0 0 0 1 +0 0 0 0 0 0 0 1 +0 0 0 0 0 0 0 1 +0 0 0 0 0 0 0 1 + +1 1 1 0 0 0 0 0 +1 0 1 0 0 0 0 1 +1 0 1 1 0 0 0 0 +1 0 0 0 1 1 1 0 +1 0 0 0 0 0 1 0 +1 0 1 1 1 0 1 0 +1 0 1 0 1 1 1 0 +1 1 1 0 0 0 0 1 + +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 1 +0 0 0 1 0 0 0 1 +0 0 0 0 1 0 0 1 +0 0 0 0 0 0 0 1 +0 0 0 0 0 0 0 1 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 1 1 + +1 1 1 1 1 1 1 0 +1 0 0 0 0 0 1 0 +1 0 1 1 0 0 1 0 +1 0 1 0 1 0 1 0 +1 0 1 0 0 0 1 0 +1 0 1 1 1 1 1 0 +1 0 0 0 0 0 0 0 +1 1 1 1 1 1 1 0 + +0 0 0 0 0 0 0 0 +0 1 1 1 1 1 0 0 +0 1 0 0 0 1 0 0 +0 1 0 0 1 1 0 0 +0 1 0 0 1 0 0 0 +0 1 0 0 0 0 0 0 +0 1 1 1 1 1 1 1 +0 0 0 0 0 0 0 1 + diff --git a/cs235/lab06/Mazes/test8.txt b/cs235/lab06/Mazes/test8.txt new file mode 100644 index 0000000..8fccfc1 --- /dev/null +++ b/cs235/lab06/Mazes/test8.txt @@ -0,0 +1,72 @@ +1 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 + +1 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 + +1 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 + +1 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 + +1 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 + +1 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 + +1 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 +0 0 0 0 0 0 0 0 + +1 0 0 0 0 0 0 0 +1 0 0 0 0 0 0 0 +1 0 0 0 0 0 0 0 +1 0 0 0 0 0 0 0 +1 0 0 0 0 0 0 0 +1 0 0 0 0 0 0 0 +1 0 0 0 0 0 0 0 +1 1 1 1 1 1 1 1 + diff --git a/cs235/lab06/Mazes/unsolvableMaze.txt b/cs235/lab06/Mazes/unsolvableMaze.txt new file mode 100644 index 0000000..e55d792 --- /dev/null +++ b/cs235/lab06/Mazes/unsolvableMaze.txt @@ -0,0 +1,72 @@ +1 1 0 0 1 1 1 0 +0 0 0 0 1 1 0 1 +1 1 0 1 0 1 0 0 +1 0 1 0 0 0 1 0 +1 0 0 1 0 1 0 0 +1 0 1 1 1 1 0 0 +1 0 0 1 0 1 0 0 +1 0 0 0 1 0 1 0 + +0 0 0 0 1 0 1 0 +0 0 1 0 1 0 0 0 +0 0 1 1 1 1 0 0 +1 1 1 1 1 1 1 0 +0 0 1 1 0 0 0 0 +0 0 1 1 0 1 1 0 +1 0 1 0 1 0 1 1 +1 1 1 0 1 1 1 0 + +1 0 1 0 1 0 1 1 +0 1 0 0 0 0 0 1 +0 1 0 1 1 1 1 1 +1 0 0 0 1 0 1 0 +0 0 0 1 0 1 0 0 +1 0 1 0 0 0 0 0 +0 0 1 1 1 1 0 0 +1 0 1 0 1 0 0 0 + +0 1 1 0 0 1 0 1 +1 0 1 0 1 1 1 0 +0 0 1 0 1 0 0 0 +0 1 1 1 0 1 0 0 +0 0 1 0 0 1 1 0 +1 1 0 1 1 0 0 0 +0 1 0 1 0 0 0 1 +1 1 0 0 0 0 1 1 + +1 1 1 0 1 1 1 1 +0 1 1 1 1 1 1 1 +0 1 1 0 0 0 1 1 +0 0 1 1 1 0 1 0 +1 1 0 0 0 1 1 0 +0 0 0 1 1 1 0 0 +0 1 0 1 0 0 1 0 +1 1 1 1 1 0 0 0 + +1 0 0 0 0 0 0 0 +1 1 0 1 1 0 0 1 +1 0 1 0 0 0 1 1 +1 1 0 0 1 1 0 1 +0 0 1 0 1 1 0 1 +1 0 0 0 1 1 0 1 +1 0 1 0 1 0 0 0 +0 0 1 1 0 1 0 1 + +0 1 1 0 1 1 0 1 +1 1 1 0 0 0 0 0 +0 1 1 0 0 0 0 0 +1 1 1 1 1 1 1 0 +0 1 1 0 1 0 0 1 +1 1 1 1 0 0 1 0 +0 1 0 1 1 1 0 1 +1 1 0 0 0 1 1 1 + +0 1 0 0 1 1 1 1 +1 0 1 1 0 1 0 1 +1 0 0 1 0 1 0 1 +0 1 0 1 1 1 1 1 +1 0 1 1 1 1 1 1 +1 1 0 1 0 1 0 1 +1 0 1 1 1 1 1 1 +1 1 0 1 0 1 1 1 + diff --git a/cs235/lab06/dump_maze.cpp b/cs235/lab06/dump_maze.cpp new file mode 100644 index 0000000..bf19161 --- /dev/null +++ b/cs235/lab06/dump_maze.cpp @@ -0,0 +1,14 @@ +#include +#include "maze.h" +using namespace std; + +int main(int argc, char * argv[]) { + maze m; + if (argc == 2) { + bool b = m.importMaze(argv[1]); + cout << b << endl; + cout << m << endl; + return 0; + } + return 1; +} diff --git a/cs235/lab06/maze.cpp b/cs235/lab06/maze.cpp new file mode 100644 index 0000000..7b41788 --- /dev/null +++ b/cs235/lab06/maze.cpp @@ -0,0 +1,149 @@ +#include "maze.h" + +int WALL = 0; +int EMPTY = 1; +int PATH = 2; +int DEAD = 3; + +maze::maze() { + map.resize(8); + for(int i = 0; i < 8; i++) { + map[i].resize(8); + for(int j = 0; j < 8; j++) { + map[i][j].resize(8); + } + } +} + +void vector_clear(vector>> & map) { + for(unsigned int k = 0; k < 8; k++) { + for(unsigned int j = 0; j < 8; j++) { + for(unsigned int i = 0; i < 8; i++) { + map[i][j][k] = 0; + } + } + } +} + +void maze::createRandomMaze() { + vector_clear(map); + for(int i = 0; i < 8; i++) { + for(int j = 0; j < 8; j++) { + for(int k = 0; k < 8; k++) { + int randnum = rand() % 2; + map[i][j][k] = randnum; + } + } + } + map[0][0][0] = 1; + map[7][7][7] = 1; +} + +bool maze::importMaze(string fileName) { + vector_clear(map); + ifstream infile(fileName.c_str()); + char c; + for(unsigned int k = 0; k < 8; k++) { + for(unsigned int j = 0; j < 8; j++) { + for(unsigned int i = 0; i < 8; i++) { + c = infile.get(); + if (c == '0') { + map[i][j][k] = 0; + } + else if (c == '1') { + map[i][j][k] = 1; + } + else if (c == 'a') { + return false; + } + c = infile.get(); + } + c = infile.get(); + if(c == '\r') { + c = infile.get(); + } + if(c != '\n') { + return false; + } + } + c = infile.get(); + if(c == '\r') { + c = infile.get(); + } + if(c != '\n') { + return false; + } + } + if(map[0][0][0] == 0 || map[7][7][7] == 0) { + return false; + } + return true; +} + +bool maze::solve(int x,int y, int z) { + if(x < 0 || y < 0 || z < 0 || x >= map.size() || y >= map.size() || z >= map.size()) { + return false; + } + else if(map[x][y][z] != EMPTY) { + return false; + } + else if(x == map.size() - 1 && y == map.size() - 1 && z == map.size() - 1) { + path << "(" << x << ", " << y << ", " << z << ")"; + ordered_pairs.push_back(path.str()); + path.str(""); + map[x][y][z] = PATH; + return true; + } + else { + map[x][y][z] = PATH; + if(solve(x - 1, y, z) || + solve(x + 1, y, z) || + solve(x, y - 1, z) || + solve(x, y + 1, z) || + solve(x, y, z - 1) || + solve(x, y, z + 1)) { + path << "(" << x << ", " << y << ", " << z << ")\n"; + ordered_pairs.push_back(path.str()); + path.str(""); + return true; + } + else { + map[x][y][z] = DEAD; + return false; + } + } +} + +bool maze::traverseMaze() { + path.str(""); + ordered_pairs.clear(); + bool b = solve(0,0,0); + return b; +} + +string maze::getMazePath() { + for(int i = ordered_pairs.size() - 1; i >= 0; i--) { + path << ordered_pairs[i]; + } + return path.str(); +} + +string maze::getMaze() { + stringstream os; + os << *this; + return os.str(); +} + +ostream & operator<<(ostream & os, maze & m) { + for(int k = 0; k < 8; k++) { + for(int j = 0; j < 8; j++) { + for(int i = 0; i < 8; i++) { + os << m.map[i][j][k]; + os << " "; + } + os << "\n"; + } + os << "\n"; + } + return os; +} diff --git a/cs235/lab06/maze.h b/cs235/lab06/maze.h new file mode 100644 index 0000000..d09fe1c --- /dev/null +++ b/cs235/lab06/maze.h @@ -0,0 +1,28 @@ +#ifndef __MAZE_H__ +#define __MAZE_H__ + +#include "MazeInterface.h" +#include +#include +#include +#include +#include + +using namespace std; + +class maze : public MazeInterface { + public: + maze(); + stringstream path; + vector ordered_pairs; + vector>> map; + void createRandomMaze(); + bool solve(int x,int y,int z); + bool importMaze(string fileName); + bool traverseMaze(); + string getMazePath(); + string getMaze(); + friend ostream & operator<<(ostream & os, maze & m); +}; +ostream & operator<<(ostream & os, vector>> & f) ; +#endif diff --git a/cs235/lab06/pwnd.c b/cs235/lab06/pwnd.c new file mode 100644 index 0000000..16bdefc --- /dev/null +++ b/cs235/lab06/pwnd.c @@ -0,0 +1,5 @@ +#include + +int usleep(useconds_t usec) { + return 0; +} diff --git a/cs235/lab06/quick.txt b/cs235/lab06/quick.txt new file mode 100644 index 0000000..214b221 --- /dev/null +++ b/cs235/lab06/quick.txt @@ -0,0 +1,5 @@ +just to see if it gets it + +and spills it out corrects + +010110101001010010 diff --git a/cs235/lab06/test.cpp b/cs235/lab06/test.cpp new file mode 100644 index 0000000..bbfdc95 --- /dev/null +++ b/cs235/lab06/test.cpp @@ -0,0 +1,45 @@ +#include +#include +#include +#include +#include "maze.h" + +int main(int argc, char * argv[]) { + srand (time(NULL)); + maze m; + + m.createRandomMaze(); + bool b; + if (argc == 2) { + b = m.importMaze(argv[1]); + } + else { + b = m.importMaze("Mazes/test1.txt"); + } + if (b) { + cout << "success" << endl; + } + else { + cout << "failure" << endl; + } + m.importMaze("Mazes/maze_sample.txt"); + assert(m.traverseMaze()); + cout << m << endl; + m.importMaze("Mazes/solvableMaze.txt"); + assert(m.traverseMaze()); + cout << m << endl; + cout << m.getMazePath() << endl; + cout << "first" << endl; + m.importMaze("Mazes/solvableMaze2.txt"); + assert(m.traverseMaze()); + cout << m.getMazePath() << endl; + cout << "second" << endl; + m.importMaze("Mazes/badMaze1.txt"); + assert(!m.traverseMaze()); + m.importMaze("Mazes/badMaze2.txt"); + assert(!m.traverseMaze()); + m.importMaze("Mazes/badMaze2.txt"); + assert(!m.traverseMaze()); + m.importMaze("Mazes/maze_sample.txt"); + +} diff --git a/cs235/lab07/Factory.cpp b/cs235/lab07/Factory.cpp new file mode 100644 index 0000000..3faa83d --- /dev/null +++ b/cs235/lab07/Factory.cpp @@ -0,0 +1,21 @@ +#include "Factory.h" +//You may add #include statments here +using namespace std; + +/* + Unlike all other documents provided, you may modify this document slightly (but do not change its name) +*/ +//======================================================================================= +/* + createMimic() + + Creates and returns an object whose class extends MimicInterface. + This should be an object of a class you have created. + + Example: If you made a class called "Mimic", you might say, "return new Mimic();". +*/ +MimicInterface* Factory::createMimic() +{ + return NULL;//Modify this line +} +//======================================================================================= diff --git a/cs235/lab07/Factory.h b/cs235/lab07/Factory.h new file mode 100644 index 0000000..5130ebf --- /dev/null +++ b/cs235/lab07/Factory.h @@ -0,0 +1,21 @@ +#include "MimicInterface.h" +#pragma once +/* + WARNING: It is expressly forbidden to modify any part of this document, including its name +*/ +//======================================================================================= +/* + createMimic() + + Creates and returns an object whose class extends MimicInterface. + This should be an object of a class you have created. + + Example: If you made a class called "Mimic", you might say, "return new Mimic();". +*/ +class Factory +{ +public: + static MimicInterface* createMimic(); +}; + +//======================================================================================= diff --git a/cs235/lab07/Makefile b/cs235/lab07/Makefile new file mode 100644 index 0000000..0dfb753 --- /dev/null +++ b/cs235/lab07/Makefile @@ -0,0 +1,31 @@ +CXXFLAGS= -Wall -g -std=c++0x +OBJECTS=Factory.o mimic.o dmmap.o pwnd.o #ignoreme.a +EXE=main +all: pwnd.o $(EXE) + +$(EXE): $(OBJECTS) + $(CXX) $(CXXFLAGS) $(OBJECTS) -o $@ +test: test.cpp mimic.o dmmap.o +rtest: test + ./test +Factory.o: Factory.cpp Factory.h +mimic.o: mimic.cpp mimic.h +dmmap.o: dmmap.cpp dmmap.h +pwnd.o: pwnd.c + +run: main + ./main + +clean: + @rm -vf *.o + @rm -vf $(EXE) + @rm -vf *.1 + @rm -vf *.0 + @rm -vf test + @rm -rvf *.dSYM + +drun: main + gdb ./main + +valgrind: $(EXE) + valgrind --tool=memcheck --leak-check=yes ./$(EXE) diff --git a/cs235/lab07/MimicInterface.h b/cs235/lab07/MimicInterface.h new file mode 100644 index 0000000..1360776 --- /dev/null +++ b/cs235/lab07/MimicInterface.h @@ -0,0 +1,69 @@ +#pragma once +#include +#include +#include +using namespace std; + +/* + WARNING: It is expressly forbidden to modify any part of this document, including its name +*/ +class MimicInterface +{ + public: + MimicInterface(){} + virtual ~MimicInterface(){} + + //Part 1-------------------------------------------------------------- + /** + * createMap + * + * Creates a prefix-suffix map based on the input text. + * + * Go through the input text and examine each group of 3 words. Refer + * to the first two words as the "prefix" and the third word as the + * "suffix". Create a map that associates each prefix with each suffix. + * If you encounter a prefix that has been read already, add the new + * suffix to the list of suffixes already associated with that prefix; + * in this manner, each prefix can be associated with multiple + * suffixes and even multiple copies of the same suffix. Note that + * the input texts will only contain words separated by spaces. Also + * note that the last two word prefix in the text should be associated + * with the suffix "THE_END". + * + * @param input + * the sample text to be mimicked + */ + virtual void createMap(string input) = 0; + + /** + * getSuffixList + * + * Returns the list of suffixes associated with the given prefix. + * Returns an empty vector if the given prefix is not in the map or no + * map has been created yet. + * + * @param prefix + * the prefix to be found + * @return a list of suffixes associated with the given prefix if the + * prefix is found; an empty vector otherwise + */ + virtual vector getSuffixList(string prefix) = 0; + + //Part 2-------------------------------------------------------------- + /** + * generateText + * + * Generates random text using the map created by the createMap method. + * + * To generate the new text, start with the first prefix that was read + * and randomly select one of the suffixes associated with that prefix. + * The next prefix is the second word from the previous prefix and the + * selected suffix. Continue selecting random suffixes and building the + * next prefix until the suffix "THE_END" is selected. The token + * "THE_END" should not be returned as part of your generated text. + * + * @return random text generated using the map created with the sample + * text; an empty string if no map has been created yet + */ + virtual string generateText() = 0; +}; diff --git a/cs235/lab07/dmmap.cpp b/cs235/lab07/dmmap.cpp new file mode 100644 index 0000000..4c9f379 --- /dev/null +++ b/cs235/lab07/dmmap.cpp @@ -0,0 +1,6 @@ +#include "dmmap.h" + +dmmap::dmmap(string prefix, string suffix) { + key = prefix; + values.push_back(suffix); +} diff --git a/cs235/lab07/dmmap.h b/cs235/lab07/dmmap.h new file mode 100644 index 0000000..17504ec --- /dev/null +++ b/cs235/lab07/dmmap.h @@ -0,0 +1,15 @@ +#ifndef __DMMAP_H__ +#define __DMMAP_H__ + +#include +#include + +using namespace std; + +class dmmap { + public: + dmmap(string prefix, string suffix); + string key; + vector values; +}; +#endif diff --git a/cs235/lab07/fungulator/main.go b/cs235/lab07/fungulator/main.go new file mode 100644 index 0000000..583e260 --- /dev/null +++ b/cs235/lab07/fungulator/main.go @@ -0,0 +1,71 @@ +package main + +import ( + "bufio" + "fmt" + "math/rand" + "os" + "strings" + "time" +) + +type LengthError struct { + Count int +} + +func (e LengthError) Error() string { + return fmt.Sprintf("%d is not long enough (must be longer than 2)", e.Count) +} + +func parse(words []string) (map[string][]string, string, error) { + if len(words) < 3 { + return nil, "", LengthError{len(words)} + } + mapping := make(map[string][]string) + var a, b, k, v, first_prefix string + for i := 0; i < len(words)-2; i++ { + a = words[i] + b = words[i+1] + k = fmt.Sprintf("%v %v", a, b) + if i == 0 { + first_prefix = k + } + v = words[i+2] + mapping[k] = append(mapping[k], v) + } + k = fmt.Sprintf("%v %v", words[len(words)-2], words[len(words)-1]) + v = "THE_END" + mapping[k] = append(mapping[k], v) + return mapping, first_prefix, nil +} + +func generateText(mapping map[string][]string, first_prefix string) string { + cur_prefix := first_prefix + output := first_prefix + for { + suffix := mapping[cur_prefix][rand.Intn(len(mapping[cur_prefix]))] + if suffix == "THE_END" { + break + } + output += fmt.Sprintf(" %v", suffix) + _prefix := strings.SplitN(cur_prefix, " ", 2) + cur_prefix = fmt.Sprintf("%v %v", _prefix[1], suffix) + } + return output +} + +func main() { + rand.Seed(time.Now().UnixNano()) + instream := bufio.NewReader(os.Stdin) + for s, err := instream.ReadBytes('\n'); err == nil; s, err = instream.ReadBytes('\n') { + words := strings.Fields(string(s)) + mapping, first_prefix, err := parse(words) + var generated_text string + if err != nil { + generated_text = err.Error() + } else { + generated_text = generateText(mapping, first_prefix) + } + fmt.Printf("%v\n\t%v\n\t%v\n", words, mapping, generated_text) + } +} diff --git a/cs235/lab07/mimic.cpp b/cs235/lab07/mimic.cpp new file mode 100644 index 0000000..fbd433a --- /dev/null +++ b/cs235/lab07/mimic.cpp @@ -0,0 +1,104 @@ +#include "mimic.h" + +mimic::mimic() {} + +vector parser(string input) { + vector results; + string s; + for(unsigned int i = 0; i < input.length(); i++) { + char c = input[i]; + if(c != ' ') { + s += c; + } + else { + if(s != "") { + results.push_back(s); + s.clear(); + } + } + } + if(s != "") { + results.push_back(s); + } + return results; +} + +void mimic::add_to_dmmap(string prefix, string suffix) { + bool add_test = true; + for(unsigned int i = 0; i < dmmaps.size(); i++) { + if(dmmaps[i].key == prefix) { + add_test = false; + dmmaps[i].values.push_back(suffix); + } + } + if(add_test) { + dmmaps.push_back(dmmap(prefix, suffix)); + } +} + +void mimic::createMap(string input) { + cout << input << endl; + vector parsed_input = parser(input); + for(unsigned int i = 0; i < parsed_input.size() - 2; i++) { + string prefix; + string suffix; + prefix += parsed_input[i]; + prefix += " "; + prefix += parsed_input[i+1]; + suffix += parsed_input[i+2]; + add_to_dmmap(prefix, suffix); + } + string prefix = parsed_input[parsed_input.size() - 2]; + prefix += " "; + prefix += parsed_input[parsed_input.size() - 1]; + add_to_dmmap(prefix, "THE_END"); +} + +vector mimic::getSuffixList(string prefix) { + for(unsigned int i = 0; i < dmmaps.size(); i++) { + if(dmmaps[i].key == prefix) { + return dmmaps[i].values; + } + } + vector v; + return v; +} + +string mimic::generateText() { + string text; + srand (time(NULL)); + if(dmmaps.size() == 0) { + return ""; + } + text += dmmaps[0].key; + text += " "; + string prefix1 = dmmaps[0].key; + while(true) { + vector v = getSuffixList(prefix1); + string temp = v[rand() % v.size()]; + if(temp == "THE_END") { + text.erase(text.find_last_not_of(" ")+1); + return text; + } + else { + text += temp; + text += " "; + } + vector prefix_vec = parser(prefix1); + prefix1 = prefix_vec[1]; + prefix1 += " "; + prefix1 += temp; + } +} + +ostream & operator<<(ostream & os, mimic m) { + os << "["; + for(unsigned int i = 0; i < m.dmmaps.size(); i++) { + os << "'" << m.dmmaps[i].key << "' ,"; + for(unsigned int j = 0; j < m.dmmaps[i].values.size(); j++) { + os << m.dmmaps[i].values[j] << ", "; + } + } + os << "]"; + return os; +} diff --git a/cs235/lab07/mimic.h b/cs235/lab07/mimic.h new file mode 100644 index 0000000..f08243d --- /dev/null +++ b/cs235/lab07/mimic.h @@ -0,0 +1,24 @@ +#ifndef __MIMIC__H_ +#define __MIMIC__H_ + +#include "MimicInterface.h" +#include "dmmap.h" +#include +#include +#include +#include + +using namespace std; + +class mimic : public MimicInterface { + public: + mimic(); + vector dmmaps; + void createMap(string input); + vector getSuffixList(string prefix); + void add_to_dmmap(string prefix, string suffix); + string generateText(); + friend ostream & operator<<(ostream & os, mimic m); +}; +ostream & operator<<(ostream & os, mimic m); +#endif diff --git a/cs235/lab07/pwnd.c b/cs235/lab07/pwnd.c new file mode 100644 index 0000000..16bdefc --- /dev/null +++ b/cs235/lab07/pwnd.c @@ -0,0 +1,5 @@ +#include + +int usleep(useconds_t usec) { + return 0; +} diff --git a/cs235/lab07/sample.txt b/cs235/lab07/sample.txt new file mode 100644 index 0000000..623259f --- /dev/null +++ b/cs235/lab07/sample.txt @@ -0,0 +1,8 @@ +I want to swing I want to boat Boat wants me +a b c d e f g +a b c +1 2 +1 + +No no no, no! +1 2 3 4 5 diff --git a/cs235/lab07/test.cpp b/cs235/lab07/test.cpp new file mode 100644 index 0000000..70cdf55 --- /dev/null +++ b/cs235/lab07/test.cpp @@ -0,0 +1,13 @@ +#include "mimic.h" + +using namespace std; + +int main() { + mimic m; + m.createMap("I want to swing I want to boat Boat wants me"); + cout << m << endl; + cout << "'" << m.generateText() << "'" << endl; +// m.createMap("hello beautiful world"); +// cout << m << endl; +// cout << m.generateText() << endl; +} diff --git a/cs235/lab08/Factory.cpp b/cs235/lab08/Factory.cpp new file mode 100644 index 0000000..65c03be --- /dev/null +++ b/cs235/lab08/Factory.cpp @@ -0,0 +1,22 @@ +#include "Factory.h" +#include "qstest.h" +//You may add #include statments here +using namespace std; + +/* + * Unlike all other documents provided, you may modify this document slightly (but do not change its name) + */ +//======================================================================================= +/* + createQSTest() + + Creates and returns an object whose class extends QSTestInterface. + This should be an object of a class you have created. + + Example: If you made a class called "QSTest", you might say, "return new QSTest();". +*/ +QSTestInterface* Factory::createQSTest() +{ + return new qstest();//Modify this line +} +//======================================================================================= diff --git a/cs235/lab08/Factory.h b/cs235/lab08/Factory.h new file mode 100644 index 0000000..ce51c74 --- /dev/null +++ b/cs235/lab08/Factory.h @@ -0,0 +1,21 @@ +#include "QSTestInterface.h" +#pragma once +/* + * WARNING: It is expressly forbidden to modify any part of this document, including its name + */ +//======================================================================================= +/* + createQSTest() + + Creates and returns an object whose class extends QSTestInterface. + This should be an object of a class you have created. + + Example: If you made a class called "QSTest", you might say, "return new QSTest();". +*/ +class Factory +{ + public: + static QSTestInterface* createQSTest(); +}; + +//======================================================================================= diff --git a/cs235/lab08/Makefile b/cs235/lab08/Makefile new file mode 100644 index 0000000..42bca88 --- /dev/null +++ b/cs235/lab08/Makefile @@ -0,0 +1,31 @@ +CXXFLAGS= -Wall -g -std=c++0x +OBJECTS=Factory.o qstest.o sorter.o pwnd.o ignoreme.a +EXE=main +all: pwnd.o $(EXE) + +$(EXE): $(OBJECTS) + $(CXX) $(CXXFLAGS) $(OBJECTS) -o $@ +test: test.cpp qstest.o sorter.o +rtest: test + ./test +Factory.o: Factory.cpp Factory.h +qstest.o: qstest.cpp qstest.h +sorter.o: sorter.cpp sorter.h +pwnd.o: pwnd.c + +run: main + ./main + +clean: + @rm -vf *.o + @rm -vf $(EXE) + @rm -vf *.1 + @rm -vf *.0 + @rm -vf test + @rm -rvf *.dSYM + +drun: main + gdb ./main + +valgrind: $(EXE) + valgrind --tool=memcheck --leak-check=yes ./$(EXE) diff --git a/cs235/lab08/QSInterface.h b/cs235/lab08/QSInterface.h new file mode 100644 index 0000000..01790ff --- /dev/null +++ b/cs235/lab08/QSInterface.h @@ -0,0 +1,126 @@ +#pragma once +#include +#include +using namespace std; + +/* + * WARNING: It is expressly forbidden to modify any part of this document, including its name + */ + +class QSInterface +{ + public: + QSInterface(){} + virtual ~QSInterface(){} + + /* + * For all methods below, it is assumed that the given array's size is correctly provided by the following parameter. + */ + + /* + * sortAll() + * + * Sorts elements of the given array. After this method is called, every + * element in the array is less than or equal to the following element. + * + * Does nothing if the given array is empty. + * + * @param data + * an array of integers + * @param size + * the correct size of the given array + */ + virtual void sortAll(int data[], int size) = 0; + + /* + * sort() + * + * Sorts elements of the given array between the two given indices. After + * this method is called, every element between the indices is less than or + * equal to the following element. + * + * Does nothing if the given array is empty, if either of the given integers + * is out of bounds, or if the first integer is not less than the second + * integer. + * + * @param data + * an array of integers + * @param size + * the correct size of the given array + * @param left + * the left boundary for the subarray to sort + * @param right + * the right boundary for the subarray to sort + */ + virtual void sort(int data[], int size, int left, int right) = 0; + + /* + * medianOfThree() + * + * Performs median-of-three pivot selection from among the values in + * the given array between the two given indices. Median-of-three pivot + * selection will sort the first, middle, and last elements in a given + * array with respect to each other. After this method is called, + * data[first] <= data[middle] <= data[last], where middle = + * (first + last)/2. + * + * Does nothing if the given array is empty, if either of the given integers + * is out of bounds, or if the first integer is not less than the second + * integer. + * + * @param data + * an array of integers + * @param size + * the correct size of the given array + * @param left + * the left boundary for the subarray from which to find a pivot + * @param right + * the right boundary for the subarray from which to find a pivot + */ + virtual void medianOfThree(int data[], int size, int left, int right) = 0; + + /* + * Partitions a subarray around a pivot value selected according to + * median-of-three pivot selection. + * + * The values which are smaller than the pivot should be placed to the left + * of the pivot; the values which are larger than the pivot should be placed + * to the right of the pivot. + * + * Does nothing and returns -1 if the given array is null, if either of the + * given integers is out of bounds, or if the first integer is not less than + * the second integer. + * + * @param data + * an array of integers + * @param size + * the correct size of the given array + * @param left + * the left boundary for the subarray to partition + * @param right + * the right boundary for the subarray to partition + * @return the pivot's ending index after the partition completes; -1 if + * provided with bad input + */ + virtual int partition(int data[], int size, int left, int right) = 0; + + /* + * Swaps the values at the given indices within the given array. + * + * After this method is called, the values in data[i] and data[j] should be + * swapped. + * + * Does nothing if the given array is null or if either of the given + * integers is out of bounds. + * + * @param data + * an array of integers + * @param size + * the correct size of the given array + * @param i + * the first index + * @param j + * the second index + */ + virtual void swap(int data[], int size, int i, int j) = 0; +}; diff --git a/cs235/lab08/QSTestInterface.h b/cs235/lab08/QSTestInterface.h new file mode 100644 index 0000000..1c0c6e1 --- /dev/null +++ b/cs235/lab08/QSTestInterface.h @@ -0,0 +1,80 @@ +#pragma once +#include +#include +#include "QSInterface.h" +using namespace std; + +/* + * WARNING: It is expressly forbidden to modify any part of this document, including its name + */ + +class QSTestInterface +{ + public: + QSTestInterface(){} + virtual ~QSTestInterface(){} + + /* + * For all methods below, the correctness of an operation is defined in QSInterface.h. + */ + + /* + * testSortAll + * + * Returns true if the given QSInterface object correctly sorts given arrays. + * + * @param test + * an implementation of the QSInterface class + * @return + * true if [test] correctly sorts; false otherwise + */ + virtual bool testSortAll(QSInterface* test) = 0; + + /* + * testSort + * + * Returns true if the given QSInterface object correctly sorts given subarrays. + * + * @param test + * an implementation of the QSInterface class + * @return + * true if [test] correctly sorts; false otherwise + */ + virtual bool testSort(QSInterface* test) = 0; + + /* + * testMedianOfThree + * + * Returns true if the given QSInterface object correctly performs median-of-three pivot selection. + * + * @param test + * an implementation of the QSInterface class + * @return + * true if [test] correctly selects a pivot; false otherwise + */ + virtual bool testMedianOfThree(QSInterface* test) = 0; + + /* + * testPartition + * + * Returns true if the given QSInterface object correctly partitions. + * + * @param test + * an implementation of the QSInterface class + * @return + * true if [test] correctly partitions; false otherwise + */ + virtual bool testPartition(QSInterface* test) = 0; + + /* + * testSwap + * + * Returns true if the given QSInterface object correctly swaps elements. + * + * @param test + * an implementation of the QSInterface class + * @return + * true if [test] correctly swaps elements = 0; false otherwise + */ + virtual bool testSwap(QSInterface* test) = 0; +}; diff --git a/cs235/lab08/pwnd.c b/cs235/lab08/pwnd.c new file mode 100644 index 0000000..16bdefc --- /dev/null +++ b/cs235/lab08/pwnd.c @@ -0,0 +1,5 @@ +#include + +int usleep(useconds_t usec) { + return 0; +} diff --git a/cs235/lab08/qstest.cpp b/cs235/lab08/qstest.cpp new file mode 100644 index 0000000..0a00e24 --- /dev/null +++ b/cs235/lab08/qstest.cpp @@ -0,0 +1,301 @@ +#include "qstest.h" + +bool qstest::testSortAll(QSInterface* test) { + sorter s; + int temp[20] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9, 53, 24, 64, 75, 132, 74, 24, 64, 745, 21}; + int temp1[20] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9, 53, 24, 64, 75, 132, 74, 24, 64, 745, 21}; + test->sortAll(temp, 20); + s.sortAll(temp1, 20); + for(unsigned int i = 0; i < 20; i++) { + if(temp[i] != temp1[i]) + return false; + } + return true;; +} + +bool qstest::testSort(QSInterface* test) { + sorter s; + int temp[20] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9, 53, 24, 64, 75, 132, 74, 24, 64, 745, 21}; + int temp1[20] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9, 53, 24, 64, 75, 132, 74, 24, 64, 745, 21}; + test->sort(temp, 20, 3, 15); + s.sort(temp1, 20, 3, 15); + for(unsigned int i = 0; i < 20; i++) { + if(temp[i] != temp1[i]) + return false; + } + int temp2[10] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9}; + int temp3[10] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9}; + test->sort(temp2, 10, 0, 9); + s.sort(temp3, 10, 0, 9); + for(unsigned int i = 0; i < 10; i++) { + if(temp2[i] != temp3[i]) + return false; + } + int temp4[10] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9}; + int temp5[10] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9}; + test->sort(temp4, 10, -1, 4); + s.sort(temp5, 10, -1, 4); + for(unsigned int i = 0; i < 10; i++) { + if(temp4[i] != temp5[i]) + return false; + } + int temp6[10] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9}; + int temp7[10] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9}; + test->sort(temp6, 10, 3, 7); + s.sort(temp7, 10, 3, 7); + for(unsigned int i = 0; i < 10; i++) { + if(temp6[i] != temp7[i]) + return false; + } + int temp8[10] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9}; + int temp9[10] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9}; + test->sort(temp8, 10, 3, 10); + s.sort(temp9, 10, 3, 10); + for(unsigned int i = 0; i < 10; i++) { + if(temp8[i] != temp9[i]) + return false; + } + int tempa[10] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9}; + int tempb[10] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9}; + test->sort(tempa, 10, 4, 4); + s.sort(tempb, 10, 4, 4); + for(unsigned int i = 0; i < 10; i++) { + if(tempa[i] != tempb[i]) + return false; + } + int tempc[10] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9}; + int tempd[10] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9}; + test->sort(tempc, 10, 4, 2); + s.sort(tempd, 10, 4, 2); + for(unsigned int i = 0; i < 10; i++) { + if(tempc[i] != tempd[i]) + return false; + } + int tempe[10] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9}; + int tempf[10] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9}; + test->sort(tempe, 10, 4, -1); + s.sort(tempf, 10, 4, -1); + for(unsigned int i = 0; i < 10; i++) { + if(tempe[i] != tempf[i]) + return false; + } + return true; +} + +bool qstest::testMedianOfThree(QSInterface* test) { + sorter s; + int what[9] = {5, 5, 5, 5, 1, 5, 5, 5, 5}; + int what1[9] = {5, 5, 5, 5, 1, 5, 5, 5, 5}; + test->medianOfThree(what, 9, 0, 8); + s.medianOfThree(what1, 9, 0, 8); + for(unsigned int i = 0; i < 9; i++) { + if(what[i] != what1[i]) + return false; + } + int temp[10] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9}; + int temp1[10] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9}; + test->medianOfThree(temp, 10, 0, 9); + s.medianOfThree(temp1, 10, 0, 9); + for(unsigned int i = 0; i < 10; i++) { + if(temp[i] != temp1[i]) + return false; + } + int temp2[10] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9}; + int temp3[10] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9}; + test->medianOfThree(temp2, 10, 2, 9); + s.medianOfThree(temp3, 10, 2, 9); + for(unsigned int i = 0; i < 10; i++) { + if(temp2[i] != temp3[i]) + return false; + } + int temp4[10] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9}; + int temp5[10] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9}; + test->medianOfThree(temp4, 10, -2, 9); + s.medianOfThree(temp5, 10, -2, 9); + for(unsigned int i = 0; i < 10; i++) { + if(temp4[i] != temp5[i]) + return false; + } + int temp6[10] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9}; + int temp7[10] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9}; + test->medianOfThree(temp6, 10, 2, -9); + s.medianOfThree(temp7, 10, 2, -9); + for(unsigned int i = 0; i < 10; i++) { + if(temp6[i] != temp7[i]) + return false; + } + int temp8[10] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9}; + int temp9[10] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9}; + test->medianOfThree(temp8, 10, 7, 5); + s.medianOfThree(temp9, 10, 7, 5); + for(unsigned int i = 0; i < 10; i++) { + if(temp8[i] != temp9[i]) + return false; + } + int tempa[10] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9}; + int tempb[10] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9}; + test->medianOfThree(tempa, 10, 7, 10); + s.medianOfThree(tempb, 10, 7, 10); + for(unsigned int i = 0; i < 10; i++) { + if(tempa[i] != tempb[i]) + return false; + } + int tempc[1] = {4}; + int tempd[1] = {4}; + test->medianOfThree(tempc, 10, 0, 0); + s.medianOfThree(tempd, 10, 0, 0); + for(unsigned int i = 0; i < 1; i++) { + if(tempc[i] != tempd[i]) + return false; + } + int tempe[2] = {4, 8}; + int tempf[2] = {4, 8}; + test->medianOfThree(tempe, 10, 0, 1); + s.medianOfThree(tempf, 10, 0, 1); + for(unsigned int i = 1; i < 2; i++) { + if(tempe[i] != tempf[i]) + return false; + } + int tempg[3] = {4, 8, 3}; + int temph[3] = {4, 8, 3}; + test->medianOfThree(tempg, 10, 0, 2); + s.medianOfThree(temph, 10, 0, 2); + for(unsigned int i = 3; i < 1; i++) { + if(tempg[i] != temph[i]) + return false; + } + int tempi[4] = {4, 8, 3, 6}; + int tempj[4] = {4, 8, 3, 6}; + test->medianOfThree(tempi, 10, 0, 3); + s.medianOfThree(tempj, 10, 0, 3); + for(unsigned int i = 0; i < 4; i++) { + if(tempi[i] != tempj[i]) + return false; + } + int tempk[4] = {4, 8, 3, 6}; + int templ[4] = {4, 8, 3, 6}; + test->medianOfThree(tempk, 10, 0, 5); + s.medianOfThree(templ, 10, 0, 5); + for(unsigned int i = 0; i < 4; i++) { + if(tempk[i] != templ[i]) + return false; + } + return true;; +} + +bool qstest::testPartition(QSInterface* test) { + sorter s; +// int size[1] = {7}; +// int size1[1] = {7}; +// if(test->partition(size, 1, 0, 0) != s.partition(size1, 1, 0, 0)) { +// return false; +// } +// for(unsigned int i = 0; i < 1; i++) { +// if(size[i] != size1[i]) +// return false; +// } + int size2[2] = {7, 1}; + int size3[2] = {7, 1}; + if(test->partition(size2, 2, 0, 1) != s.partition(size3, 2, 0, 1)) { + return false; + } + for(unsigned int i = 0; i < 2; i++) { + if(size2[i] != size3[i]) + return false; + } + int temp[14] = {6, 1, 6, 2, 0, 0, 5, 8, 2, 8, 1, 9, 6, 4}; + int temp1[14] = {6, 1, 6, 2, 0, 0, 5, 8, 2, 8, 1, 9, 6, 4}; + if(test->partition(temp, 14, 0, 14) != s.partition(temp1, 14, 0, 14)) { + return false; + } + for(unsigned int i = 0; i < 14; i++) { + if(temp[i] != temp1[i]) + return false; + } + int temp2[20] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9, 53, 24, 64, 75, 132, 74, 24, 64, 745, 21}; + int temp3[20] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9, 53, 24, 64, 75, 132, 74, 24, 64, 745, 21}; + if(test->partition(temp2, 20, 0, 19) != s.partition(temp3, 20, 0, 19)) { + return false; + } + for(unsigned int i = 0; i < 20; i++) { + if(temp2[i] != temp3[i]) + return false; + } + int temp4[14] = {6, 1, 6, 2, 0, 0, 5, 8, 2, 8, 1, 9, 6, 4}; + int temp5[14] = {6, 1, 6, 2, 0, 0, 5, 8, 2, 8, 1, 9, 6, 4}; + if(test->partition(temp4, 14, -1, 13) != s.partition(temp5, 14, -1, 13)) { + return false; + } + for(unsigned int i = 0; i < 14; i++) { + if(temp4[i] != temp5[i]) + return false; + } + int temp6[14] = {6, 1, 6, 2, 0, 0, 5, 8, 2, 8, 1, 9, 6, 4}; + int temp7[14] = {6, 1, 6, 2, 0, 0, 5, 8, 2, 8, 1, 9, 6, 4}; + if(test->partition(temp6, 14, 6, 2) != s.partition(temp7, 14, 6, 2)) { + return false; + } + for(unsigned int i = 0; i < 14; i++) { + if(temp6[i] != temp7[i]) + return false; + } + int temp8[14] = {6, 1, 6, 2, 0, 0, 5, 8, 2, 8, 1, 9, 6, 4}; + int temp9[14] = {6, 1, 6, 2, 0, 0, 5, 8, 2, 8, 1, 9, 6, 4}; + if(test->partition(temp8, 14, 6, -2) != s.partition(temp9, 14, 6, -2)) { + return false; + } + for(unsigned int i = 0; i < 14; i++) { + if(temp8[i] != temp9[i]) + return false; + } + return true; +} + +bool qstest::testSwap(QSInterface* test) { + sorter s; + int temp[10] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9}; + int temp1[10] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9}; + test->swap(temp, 10, 0, 9); + s.swap(temp1, 10, 0, 9); + for(unsigned int i = 0; i < 10; i++) { + if(temp[i] != temp1[i]) + return false; + } + test->swap(temp, 10, -1, 9); + s.swap(temp1, 10, -1, 9); + for(unsigned int i = 0; i < 10; i++) { + if(temp[i] != temp1[i]) + return false; + } + test->swap(temp, 10, 3, 6); + s.swap(temp1, 10, 3, 6); + for(unsigned int i = 0; i < 10; i++) { + if(temp[i] != temp1[i]) + return false; + } + test->swap(temp, 10, 8, 10); + s.swap(temp1, 10, 8, 10); + for(unsigned int i = 0; i < 10; i++) { + if(temp[i] != temp1[i]) + return false; + } + test->swap(temp, 10, 8, 8); + s.swap(temp1, 10, 8, 8); + for(unsigned int i = 0; i < 10; i++) { + if(temp[i] != temp1[i]) + return false; + } + test->swap(temp, 10, 8, 2); + s.swap(temp1, 10, 8, 2); + for(unsigned int i = 0; i < 10; i++) { + if(temp[i] != temp1[i]) + return false; + } + test->swap(temp, 10, 8, -2); + s.swap(temp1, 10, 8, -2); + for(unsigned int i = 0; i < 10; i++) { + if(temp[i] != temp1[i]) + return false; + } + return true;; +} diff --git a/cs235/lab08/qstest.h b/cs235/lab08/qstest.h new file mode 100644 index 0000000..a8edd77 --- /dev/null +++ b/cs235/lab08/qstest.h @@ -0,0 +1,16 @@ +#ifndef __QSTEST_H__ +#define __QSTEST_H__ + +#include "QSTestInterface.h" +#include "sorter.h" + +class qstest : public QSTestInterface { + public: + sorter s; + bool testSortAll(QSInterface* test); + bool testSort(QSInterface* test); + bool testMedianOfThree(QSInterface* test); + bool testPartition(QSInterface* test); + bool testSwap(QSInterface* test); +}; +#endif diff --git a/cs235/lab08/sorter.cpp b/cs235/lab08/sorter.cpp new file mode 100644 index 0000000..c8bc64c --- /dev/null +++ b/cs235/lab08/sorter.cpp @@ -0,0 +1,109 @@ +#include "sorter.h" + +int partitioner(int* input, int p, int r) { + int pivot = input[r]; + while (p < r) { + while (input[p] < pivot) + p++; + while (input[r] > pivot) + r--; + if (input[p] == input[r]) + p++; + else if (p < r) { + int tmp = input[p]; + input[p] = input[r]; + input[r] = tmp; + } + } + return r; +} + +void quicksort(int* input, int p, int r) { + if ( p < r ) { + int j = partitioner(input, p, r); + quicksort(input, p, j-1); + quicksort(input, j+1, r); + } +} + +void sorter::sortAll(int data[], int size) { + if(size == 0) + return; + quicksort(data, 0, size); +} + +void sorter::sort(int data[], int size, int left, int right) { + if(size == 0 || left < 0 || left >= size || right < 0 || right >= size) + return; + quicksort(data, left, right); +} + +void sorter::medianOfThree(int data[], int size, int left, int right) { + if(size == 0 || left < 0 || left >= size || right < 0 || right >= size || right < left) + return; + int l, m, r; + l = data[left]; + r = data[right]; + m = data [(left + right)/2]; + if(l > m) { + swap(data, size, left, (left + right)/2 ); + l = data[left]; + r = data[right]; + m = data [(left + right)/2]; + } + if(l > r) { + swap(data, size, left, right); + l = data[left]; + r = data[right]; + m = data [(left + right)/2]; + } + if(m > r) { + swap(data, size, (left + right)/2 , right); + l = data[left]; + r = data[right]; + m = data [(left + right)/2]; + } +} + +int sorter::partition(int data[], int size, int left, int right) { + if(size == 0 || left < 0 || left >= size || right < 0 || right >= size || right < left) + return -1; + if(size == 1) { + return 0; + } + medianOfThree(data, size, 0, right); + swap(data, size, 0, (right + left)/2); + int l = 1; + int r = size - 1; + bool ltrue = false; + bool rtrue = false; + while(l <= r) { + if(data[0] < data[l]) { + ltrue = true; + } + else { + l++; + } + if(data[0] > data[r]) { + rtrue = true; + } + else { + r--; + } + if(ltrue && rtrue) { + rtrue = false; + ltrue = false; + swap(data, size, l, r); + } + } + swap(data, size, 0, r); + return r; +} + +void sorter::swap(int data[], int size, int i, int j) { + if(size == 0 || i < 0 || i >= size || j < 0 || j >= size) + return; + int temp = data[i]; + data[i] = data[j]; + data[j] = temp; +} diff --git a/cs235/lab08/sorter.h b/cs235/lab08/sorter.h new file mode 100644 index 0000000..9c56e0d --- /dev/null +++ b/cs235/lab08/sorter.h @@ -0,0 +1,17 @@ +#ifndef __SORTER_H__ +#define __SORTER_H__ + +#include "QSInterface.h" +#include + +using namespace std; + +class sorter : public QSInterface { + public: + void sortAll(int data[], int size); + void sort(int data[], int size, int left, int right); + void medianOfThree(int data[], int size, int left, int right); + int partition(int data[], int size, int left, int right); + void swap(int data[], int size, int i, int j); +}; +#endif diff --git a/cs235/lab08/test.cpp b/cs235/lab08/test.cpp new file mode 100644 index 0000000..47819cf --- /dev/null +++ b/cs235/lab08/test.cpp @@ -0,0 +1,31 @@ +#include "sorter.h" +#include "qstest.h" +#include + +using namespace std; + +int main() { + int size = 5; + int temp[] = {5, 3, 7, 2, 8, 2, 4, 1, 6, 5}; + sorter s; + s.sortAll(temp, size); + int temp1[] = {5, 3, 7, 2, 5, 2, 4, 1, 6, 8}; + cout << "before" << endl; + s.sort(temp1, size, 3, 7); + // int temp3[20] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 5, 53, 24, 64, 75, 132, 74, 24, 64, 745, 21}; + int temp3[1] = {5}; +// int what1[10] = {5, 5, 5, 5, 1, 5, 5, 5, 5}; +// if(test->partition(what, 10, 0, 8) != s.partition(what1, 10, 0, 8)) { +// return false; +// } + cout << temp3[1/2] << " this is the median" << endl; + for(int i = 0; i < 1; i++) { + cout << temp3[i] << " "; + } + cout << "BEFORE" << endl; + cout << s.partition(temp3, 1, 0, 0) << " this is the return value" << endl; + for(int i = 0; i < 1; i++) { + cout << temp3[i] << " "; + } + cout << "AFTER" << endl; +} diff --git a/cs235/lab08/whattotest.txt b/cs235/lab08/whattotest.txt new file mode 100644 index 0000000..5a2d501 --- /dev/null +++ b/cs235/lab08/whattotest.txt @@ -0,0 +1,8 @@ +test all sizes + 0, 1, 2, 3, 4, many +test boundary cases + +test [5,5,5,5,1,5,5,5,5] +test [5, 1, 2, 3, 4, 1, 2, 3, 4, 5] +[1, 2, 3, 4, 5] +[5, 4, 3, 2, 1] diff --git a/cs235/lab09/BSTInterface.h b/cs235/lab09/BSTInterface.h new file mode 100644 index 0000000..af51020 --- /dev/null +++ b/cs235/lab09/BSTInterface.h @@ -0,0 +1,38 @@ +//YOU MAY NOT MODIFY THIS DOCUMENT +#pragma once + +#include "NodeInterface.h" + +using namespace std; + +class BSTInterface { +public: + BSTInterface() {} + virtual ~BSTInterface() {} + + //Please note that the class that implements this interface must be made + //of objects which implement the NodeInterface + + /* + * Returns the root node for this tree + * + * @return the root node for this tree. + */ + virtual NodeInterface * getRootNode() = 0; + + /* + * Attempts to add the given int to the BST tree + * + * @return true if added + * @return false if unsuccessful (i.e. the int is already in tree) + */ + virtual bool add(int data) = 0; + + /* + * Attempts to remove the given int from the BST tree + * + * @return true if successfully removed + * @return false if remove is unsuccessful(i.e. the int is not in the tree) + */ + virtual bool remove(int data) = 0; +}; diff --git a/cs235/lab09/Factory.cpp b/cs235/lab09/Factory.cpp new file mode 100644 index 0000000..2827300 --- /dev/null +++ b/cs235/lab09/Factory.cpp @@ -0,0 +1,20 @@ +#include "Factory.h" +#include "bst.h" +//You may add #include statements here + +/* + You will MODIFY THIS DOCUMENT. +*/ +//======================================================================================= +/* + getBST() + + Creates and returns an object whose class extends BSTInterface. + This should be an object of a class you have created. + + Example: If you made a class called "BST", you might say, "return new BST();". +*/ +BSTInterface * Factory::getBST() +{ + return new bst();//Modify this line +} diff --git a/cs235/lab09/Factory.h b/cs235/lab09/Factory.h new file mode 100644 index 0000000..3f989fb --- /dev/null +++ b/cs235/lab09/Factory.h @@ -0,0 +1,23 @@ +#pragma once +#include "BSTInterface.h" + +using namespace std; + +/* + WARNING: It is expressly forbidden to modify any part of this document, including its name +*/ +//======================================================================================= +/* + getBST() + + Creates and returns an object whose class extends BSTInterface. + This should be an object of a class you have created. + + Example: If you made a class called "BST", you might say, "return new BST();". +*/ +class Factory +{ + public: + static BSTInterface * getBST(); +}; +//======================================================================================= diff --git a/cs235/lab09/Makefile b/cs235/lab09/Makefile new file mode 100644 index 0000000..d423bee --- /dev/null +++ b/cs235/lab09/Makefile @@ -0,0 +1,31 @@ +CXXFLAGS= -Wall -g -std=c++0x +OBJECTS=Factory.o bst.o node.o pwnd.o ignoreme.a +EXE=main +all: $(EXE) + +$(EXE): $(OBJECTS) + $(CXX) $(CXXFLAGS) $(OBJECTS) -o $@ +test: test.cpp bst.o node.o +rtest: test + ./test +Factory.o: Factory.cpp Factory.h +bst.o: bst.cpp bst.h +node.o: node.cpp node.h +pwnd.o: pwnd.c + +run: main + ./main + +clean: + @rm -vf *.o + @rm -vf $(EXE) + @rm -vf *.1 + @rm -vf *.0 + @rm -vf test + @rm -rvf *.dSYM + +drun: main + gdb ./main + +valgrind: $(EXE) + valgrind --tool=memcheck --leak-check=yes ./$(EXE) diff --git a/cs235/lab09/NodeInterface.h b/cs235/lab09/NodeInterface.h new file mode 100644 index 0000000..b9105af --- /dev/null +++ b/cs235/lab09/NodeInterface.h @@ -0,0 +1,31 @@ +//YOU MAY NOT MODIFY THIS DOCUMENT +#pragma once + +class NodeInterface { + +public: + NodeInterface() {} + virtual ~NodeInterface() {} + + /* + * Returns the data that is stored in this node + * + * @return the data that is stored in this node. + */ + virtual int getData() = 0; + + /* + * Returns the left child of this node or null if it doesn't have one. + * + * @return the left child of this node or null if it doesn't have one. + */ + virtual NodeInterface * getLeftChild() = 0; + + /* + * Returns the right child of this node or null if it doesn't have one. + * + * @return the right child of this node or null if it doesn't have one. + */ + virtual NodeInterface * getRightChild() = 0; + +}; diff --git a/cs235/lab09/bst.cpp b/cs235/lab09/bst.cpp new file mode 100644 index 0000000..01c8ab7 --- /dev/null +++ b/cs235/lab09/bst.cpp @@ -0,0 +1,169 @@ +#include "bst.h" + +bst::bst(): root(NULL){} + +bst::~bst() { + while(root) { + remove(root->data); + } +} + +NodeInterface* bst::getRootNode() { + return root; +} + +bool bst::add(int data) { + if(root == NULL) { + root = new node(data); + return true; + } + else { + node* current; + node* parent; + current = root; + while(current) { + parent = current; + if(data == current->data) { + return false; + } + if(data > current->data) { + current = current->right; + } + else { + current = current->left; + } + } + if(data < parent->data) { + parent->left = new node(data); + return true; + } + else { + parent->right = new node(data); + return true;; + } + } + return true; +} + +bool bst::remove(int data) { + bool found = false; + if(root == NULL) { + return false; + } + node* current; + node* parent; + current = root; + while(current) { + if(current->data == data) { + found = true; + break; + } + else { + parent = current; + if(data > current->data) { + current = current->right; + } + else if(data < current->data) { + current = current->left; + } + } + } + if(!found) { + return false; + } + if(root->data == data) { + if(root->right != NULL && root->left == NULL) { + node* temp; + temp = root; + root = root->right; + delete temp; + return true; + } + if(root->right == NULL && root->left != NULL) { + node* temp; + temp = root; + root = root->left; + delete temp; + return true; + } + if(root->right != NULL && root->left != NULL) { + node* temp; + temp = root; + root = root->left; + root->right = temp->right; + delete temp; + return true; + } + else { + delete root; + root = NULL; + return true; + } + } + if((current->left == NULL) && (current->right == NULL)) { + if(parent->left == current) { + parent->left = NULL; + } + else { + parent->right = NULL; + } + delete current; + return true; + } + if((current->left == NULL && current->right != NULL) || (current->left != NULL && current->right == NULL)) { + if(current->left == NULL && current->right != NULL) { + if(parent->left == current) { + parent->left = current->right; + delete current; + } + else { + parent->right = current->right; + delete current; + } + } + else { + if(parent->left == current) { + parent->left = current->left; + delete current; + } + else { + parent->right = current->left; + delete current; + } + } + return true; + } + if(current->left != NULL && current->right != NULL) { + node* greatest_right; + greatest_right = current->right; + if((greatest_right->left == NULL) && (greatest_right->right == NULL)) { + current = greatest_right; + delete greatest_right; + current->right = NULL; + } + else { + if(current->right->left != NULL) { + node* left; + node* temp_left; + temp_left = current->right; + left = current->right->left; + while(left->left != NULL) { + temp_left = left; + left = left->left; + } + current->data = left->data; + delete left; + temp_left->left = NULL; + } + else { + node* temp; + temp = current->right; + current->data = temp->data; + current->right = temp->right; + delete temp; + } + } + return true; + } + return true; +} diff --git a/cs235/lab09/bst.h b/cs235/lab09/bst.h new file mode 100644 index 0000000..b35e0dc --- /dev/null +++ b/cs235/lab09/bst.h @@ -0,0 +1,20 @@ +#ifndef __BST_H__ +#define __BST_H__ + +#include "BSTInterface.h" +#include "node.h" +#include + +using namespace std; + + +class bst : public BSTInterface { + public: + bst(); + ~bst(); + node* root; + NodeInterface * getRootNode(); + bool add(int data); + bool remove(int data); +}; +#endif diff --git a/cs235/lab09/node.cpp b/cs235/lab09/node.cpp new file mode 100644 index 0000000..267597f --- /dev/null +++ b/cs235/lab09/node.cpp @@ -0,0 +1,15 @@ +#include "node.h" + +node::node(int data): data(data), right(NULL), left(NULL) {} + +int node::getData() { + return data; +} + +NodeInterface* node::getLeftChild() { + return left; +} + +NodeInterface* node::getRightChild() { + return right; +} diff --git a/cs235/lab09/node.h b/cs235/lab09/node.h new file mode 100644 index 0000000..207829c --- /dev/null +++ b/cs235/lab09/node.h @@ -0,0 +1,17 @@ +#ifndef __NODE_H__ +#define __NODE_H__ + +#include "NodeInterface.h" +#include + +class node : public NodeInterface { + public: + int data; + node(int data); + node* right; + node* left; + int getData(); + NodeInterface* getLeftChild(); + NodeInterface* getRightChild(); +}; +#endif diff --git a/cs235/lab09/pwnd.c b/cs235/lab09/pwnd.c new file mode 100644 index 0000000..16bdefc --- /dev/null +++ b/cs235/lab09/pwnd.c @@ -0,0 +1,5 @@ +#include + +int usleep(useconds_t usec) { + return 0; +} diff --git a/cs235/lab09/test.cpp b/cs235/lab09/test.cpp new file mode 100644 index 0000000..d370558 --- /dev/null +++ b/cs235/lab09/test.cpp @@ -0,0 +1,20 @@ +#include "bst.h" +#include + +using namespace std; + +int main() { + bst b; + b.add(1); + cout << b.root->data << endl; + b.add(-1); + cout << b.root->left->data << endl; + //cout << b.root->right->data << endl; + b.add(4); + cout << b.root->right->data << endl; + b.add(5); + cout << b.root->right->right->data << endl; + b.remove(5); + cout << b.root->data << endl; + +} diff --git a/cs235/lab10/AVLInterface.h b/cs235/lab10/AVLInterface.h new file mode 100644 index 0000000..5f0f6dc --- /dev/null +++ b/cs235/lab10/AVLInterface.h @@ -0,0 +1,59 @@ +//YOU MAY NOT MODIFY THIS DOCUMENT +#pragma once + +#include "NodeInterface.h" + +using namespace std; + +class AVLInterface { +public: + AVLInterface() {} + virtual ~AVLInterface() {} + + //Please note that the class that implements this interface must be made + //of objects which implement the NodeInterface + + /* + * Returns the root node for this tree + * + * @return the root node for this tree. + */ + virtual NodeInterface * getRootNode() = 0; + + /* + * Attempts to add the given int to the AVL tree + * Rebalances the tree if data is successfully added + * + * @return true if added + * @return false if unsuccessful (i.e. the int is already in tree) + */ + virtual bool add(int data) = 0; + + /* + * Attempts to remove the given int from the AVL tree + * Rebalances the tree if data is successfully removed + * + * @return true if successfully removed + * @return false if remove is unsuccessful(i.e. the int is not in the tree) + */ + virtual bool remove(int data) = 0; + + /* + * ROTATIONS: + * LEFT + * + * IDENTIFY NODES TO ROTATE + * + * ROT.RIGHT=CUR.LEFT + * CUR.LEFT=ROT + * CUR=ROT + * + * + * + * + * RIGHT + * + * + * + */ +}; diff --git a/cs235/lab10/Factory.cpp b/cs235/lab10/Factory.cpp new file mode 100644 index 0000000..6e39fc0 --- /dev/null +++ b/cs235/lab10/Factory.cpp @@ -0,0 +1,20 @@ +#include "Factory.h" +#include "avl.h" +//You may add #include statements here + +/* + You will MODIFY THIS DOCUMENT. +*/ +//======================================================================================= +/* + getAVL() + + Creates and returns an object whose class extends AVLInterface. + This should be an object of a class you have created. + + Example: If you made a class called "AVL", you might say, "return new AVL();". +*/ +AVLInterface * Factory::getAVL() +{ + return new avl();//Modify this line +} diff --git a/cs235/lab10/Factory.h b/cs235/lab10/Factory.h new file mode 100644 index 0000000..3f43c41 --- /dev/null +++ b/cs235/lab10/Factory.h @@ -0,0 +1,23 @@ +#pragma once +#include "AVLInterface.h" + +using namespace std; + +/* + WARNING: It is expressly forbidden to modify any part of this document, including its name +*/ +//======================================================================================= +/* + getAVL() + + Creates and returns an object whose class extends AVLInterface. + This should be an object of a class you have created. + + Example: If you made a class called "AVL", you might say, "return new AVL();". +*/ +class Factory +{ + public: + static AVLInterface * getAVL(); +}; +//======================================================================================= diff --git a/cs235/lab10/Makefile b/cs235/lab10/Makefile new file mode 100644 index 0000000..8b45924 --- /dev/null +++ b/cs235/lab10/Makefile @@ -0,0 +1,32 @@ +CXXFLAGS= -Wall -g -std=c++0x +OBJECTS=Factory.o avl.o node.o main.o pwnd.o +EXE=main +all: $(EXE) + +$(EXE): $(OBJECTS) + $(CXX) $(CXXFLAGS) $(OBJECTS) -o $@ +test: test.cpp avl.o node.o +rtest: test + ./test +Factory.o: Factory.cpp Factory.h +avl.o: avl.cpp avl.h +node.o: node.cpp node.h +main.o: main.cpp +pwnd.o: pwnd.c + +run: main + ./main + +clean: + @rm -vf *.o + @rm -vf $(EXE) + @rm -vf *.1 + @rm -vf *.0 + @rm -vf test + @rm -rvf *.dSYM + +drun: main + gdb ./main + +valgrind: $(EXE) + valgrind --tool=memcheck --leak-check=yes ./$(EXE) diff --git a/cs235/lab10/NodeInterface.h b/cs235/lab10/NodeInterface.h new file mode 100644 index 0000000..2cd2911 --- /dev/null +++ b/cs235/lab10/NodeInterface.h @@ -0,0 +1,40 @@ +//YOU MAY NOT MODIFY THIS DOCUMENT +#pragma once +#include + +using namespace std; + +class NodeInterface { + +public: + NodeInterface() {} + virtual ~NodeInterface() {} + + /* + * Returns the data that is stored in this node + * + * @return the data that is stored in this node. + */ + virtual int getData() = 0; + + /* + * Returns the left child of this node or null if it doesn't have one. + * + * @return the left child of this node or null if it doesn't have one. + */ + virtual NodeInterface * getLeftChild() = 0; + + /* + * Returns the right child of this node or null if it doesn't have one. + * + * @return the right child of this node or null if it doesn't have one. + */ + virtual NodeInterface * getRightChild() = 0; + + /* + * Returns the height of this node. The height is the number of edges + * from this node to this nodes farthest child. + */ + virtual int getHeight() = 0; + +}; diff --git a/cs235/lab10/avl.cpp b/cs235/lab10/avl.cpp new file mode 100644 index 0000000..186bd29 --- /dev/null +++ b/cs235/lab10/avl.cpp @@ -0,0 +1,383 @@ +#include "avl.h" + +avl::avl(): root(NULL) {} + +avl::~avl() { + deltree(root); +} + +void avl::deltree (node* root) { + // if(root != NULL) { + // deltree(root->left); + // deltree(root->right); + // } + // delete root; +} + +NodeInterface* avl::getRootNode() { + return root; +} + +bool avl::add(int data) { + bool found = false; + node* current; + node* parent; + current = root; + while(current) { + if(current->data == data) { + found = true; + break; + } + else { + parent = current; + if(data > current->data) { + current = current->right; + } + else if(data < current->data) { + current = current->left; + } + } + } + if(found) { + return false; + } + root = build(root, data); + return true; +} + +node* avl::build(node* root, int data) { + node* temp1 = NULL; + node* temp2 = NULL; + if(root == NULL) { + root = new node(data); + h = true; + return root; + } + if(data < root->data) { + root->left = build(root->left, data); + if(h) { + switch(root->height) { + case 1: + temp1 = root->left; + if(temp1->height == 1) { + root->left = temp1->right; + temp1->right = root; + root->height = 0; + root = temp1; + } + else { + temp2 = temp1->right; + temp1->right = temp2->left; + temp2->left = temp1; + root->left = temp2->right; + temp2->right = root; + if(temp2->height == 1) { + root->height = -1; + } + else { + root->height = 0; + } + if(temp2->height == -1) { + temp1->height = 1; + } + else { + temp1->height = 0; + } + root = temp2; + } + root->height = 0; + h = false; + break; + case 0: + root->height = 1; + break; + case -1: + root->height = 0; + h = false; + } + } + } + if(data > root->data) { + root->right = build(root->right, data); + if(h) { + switch(root->height) { + case 1: + root->height = 0; + h = false; + break; + case 0: + root->height = -1; + break; + case -1: + temp1 = root->right; + if(temp1->height == -1) { + root->right = temp1->left; + temp1->left = root; + root->height = 0; + root = temp1; + } + else { + temp2 = temp1->left; + temp1->left = temp2->right; + temp2->right = temp1; + root->right = temp2->left; + temp2->left = root; + if(temp2->height == -1) { + root->height = 1; + } + else { + root->height = 0; + } + if(temp2->height == 1) { + temp1->height = -1; + } + else { + temp1->height = 0; + } + root = temp2; + } + root->height = 0; + h = false; + } + } + } + return root; +} + +bool avl::remove(int data) { + bool found = false; + if(root == NULL) { + return false; + } + node* current; + node* parent; + current = root; + while(current) { + if(current->data == data) { + found = true; + break; + } + else { + parent = current; + if(data > current->data) { + current = current->right; + } + else if(data < current->data) { + current = current->left; + } + } + } + if(!found) { + return false; + } + root = del_leaf(root, data); + return true; +} + +node* avl::del_leaf(node* root, int data) { + node* temp; + if(root == NULL) { + return root; + } + else { + if(data < root->data) { + root->left = del_leaf(root->left, data); + if(h) { + root = bal_right(root); + } + } + else { + if(data > root->data) { + root->right = del_leaf(root->right, data); + if(h) { + root = bal_left(root); + } + } + else { + temp = root; + if(temp->right == NULL) { + root = temp->left; + h = true; + delete temp; + } + else { + if(temp->left == NULL) { + root = temp->right; + h = true; + delete temp; + } + else { + temp->left = del(temp->left, temp); + if(h) { + root = bal_right(root); + } + } + } + } + } + } + return root; +} + +node* avl::del(node* succ, node* n) { + node* temp = succ; + if(succ->right != NULL) { + succ->right = del(succ->right, n); + if(h) { + succ = bal_left(succ); + } + } + else { + node* current; + node* parent; + current = root; + while(current) { + if(current->data == succ->data) { + break; + } + else { + parent = current; + if(succ->data > current->data) { + current = current->right; + } + else if(succ->data < current->data) { + current = current->left; + } + } + } + if(root->left->data == succ->data) { + root->data = succ->data; + n->right = root->right; + succ = succ->left; + delete temp; + h = true; + return succ; + } + else if(root->right->data == succ->data) { + root->data = succ->data; + n->right = root->right; + succ = succ->right; + delete temp; + h = false; + return succ; + } + temp = succ; + n->data = succ->data; + succ = succ->left; + delete temp; + h = true; + } + return succ; +} + +node* avl::bal_right(node* root) { + node* temp1 = NULL; + node* temp2 = NULL; + switch(root->height) { + case 1: + root->height = 0; + break; + case 0: + root->height = -1; + h = false; + break; + case -1: + temp1 = root->right; + if(temp1->height <= 0) { + root->right = temp1->left; + temp1->left = root; + if(temp1->height == 0) { + root->height = -1; + temp1->height = 1; + h = false; + } + else { + root->height = 0; + temp1->height = 0; + } + root = temp1; + } + else { + temp2 = temp1->left; + temp1->left = temp2->right; + temp2->right = temp1; + root->right = temp2->left; + temp2->left = root; + if(temp2->height == -1) { + root->height = 1; + } + else { + root->height = 0; + } + if(temp2->height == 1) { + temp1->height = -1; + } + else { + temp1->height = 0; + } + root = temp2; + temp2->height = 0; + } + } + return root; +} + +node* avl::bal_left(node* root) { + node* temp1 = NULL; + node* temp2 = NULL; + switch(root->height) { + case -1: + root->height = 0; + break; + case 0: + root->height = 1; + h = false; + break; + case 1: + temp1 = root->left; + if(temp1->height >= 0) { + root->left = temp1->right; + temp1->right = root; + if(temp1->height == 0) { + root->height = 1; + temp1->height = -1; + h = false; + } + else { + root->height = 0; + temp1->height = 0; + } + root = temp1; + } + else { + temp2 = temp1->right; + temp1->right = temp2->left; + temp2->left = temp1; + root->left = temp2->right; + temp2->right = root; + if(temp2->height == 1) { + root->height = -1; + } + else { + root->height = 0; + } + if(temp2->height == -1) { + temp1->height = 1; + } + else { + temp1->height = 0; + } + root = temp2; + temp2->height = 0; + } + } + return root; +} + +void avl::display(node* root) { + if(root != NULL) { + display(root->left); + cout << root->data << "\t"; + display(root->right); + } +} diff --git a/cs235/lab10/avl.h b/cs235/lab10/avl.h new file mode 100644 index 0000000..ca71fed --- /dev/null +++ b/cs235/lab10/avl.h @@ -0,0 +1,25 @@ +#ifndef __AVL_H__ +#define __AVL_H__ + +#include "AVLInterface.h" +#include "node.h" +#include + +class avl : public AVLInterface { + public: + avl(); + ~avl(); + node* root; + bool h; + NodeInterface * getRootNode(); + void deltree (node* root); + node* build(node* root, int data); + bool add(int data); + node* del_leaf(node* root, int data); + void display(node* root); + node* del(node* succ, node* n); + node* bal_left(node* root); + node* bal_right(node* root); + bool remove(int data); +}; +#endif diff --git a/cs235/lab10/main.cpp b/cs235/lab10/main.cpp new file mode 100644 index 0000000..0393267 --- /dev/null +++ b/cs235/lab10/main.cpp @@ -0,0 +1,24 @@ +#include +#include "avl.h" + +using namespace std; + +int main() { + avl a; + + for(int i = 50; i > 0; i--) { + a.add(i); + a.add(-i); + } + for(int i = 0; i < 5; i++) { + a.remove(i); + a.remove(-i); + } + a.remove(5); + + // for(int i = 0; i < 50; i++) { + // a.remove(i); + // a.remove(-i); + // } + +} diff --git a/cs235/lab10/node.cpp b/cs235/lab10/node.cpp new file mode 100644 index 0000000..38ca301 --- /dev/null +++ b/cs235/lab10/node.cpp @@ -0,0 +1,19 @@ +#include "node.h" + +node::node(int data): data(data), height(0), right(NULL), left(NULL) {} + +int node::getData() { + return data; +} + +NodeInterface* node::getLeftChild() { + return left; +} + +NodeInterface* node::getRightChild() { + return right; +} + +int node::getHeight() { + return height; +} diff --git a/cs235/lab10/node.h b/cs235/lab10/node.h new file mode 100644 index 0000000..be2b789 --- /dev/null +++ b/cs235/lab10/node.h @@ -0,0 +1,18 @@ +#ifndef __NODE_H__ +#define __NODE_H__ + +#include "NodeInterface.h" + +class node : public NodeInterface { + public: + int data; + int height; + node* right; + node* left; + node(int data); + int getData(); + NodeInterface * getLeftChild(); + NodeInterface * getRightChild(); + int getHeight(); +}; +#endif diff --git a/cs235/lab10/pwnd.c b/cs235/lab10/pwnd.c new file mode 100644 index 0000000..16bdefc --- /dev/null +++ b/cs235/lab10/pwnd.c @@ -0,0 +1,5 @@ +#include + +int usleep(useconds_t usec) { + return 0; +} diff --git a/cs235/notes/Makefile b/cs235/notes/Makefile new file mode 100644 index 0000000..f224377 --- /dev/null +++ b/cs235/notes/Makefile @@ -0,0 +1,24 @@ +CXXFLAGS= -Wall -g -std=c++0x +OBJECTS= game.o +EXE=main +all: $(EXE) + +$(EXE): $(OBJECTS) + $(CXX) $(CXXFLAGS) $(OBJECTS) -o $@ +game.o: game.cpp +run: main + ./main + +clean: + @rm -vf *.o + @rm -vf $(EXE) + @rm -vf *.1 + @rm -vf *.0 + @rm -vf test + @rm -rvf *.dSYM + +drun: main + gdb ./main + +valgrind: $(EXE) + valgrind --tool=memcheck --leak-check=yes ./$(EXE) diff --git a/cs235/notes/game.cpp b/cs235/notes/game.cpp new file mode 100644 index 0000000..0b5f98f --- /dev/null +++ b/cs235/notes/game.cpp @@ -0,0 +1,59 @@ +#include +#include +#include +#include + +using namespace std; + +vector parse_expression(string expression) { //parses expression with " " being the delimeter + vector results; + string s; + for(unsigned int i = 0; i < expression.length(); i++) { + char c = expression[i]; + if(c != ' ') { + s += c; + } + else { + if(s != "") { + results.push_back(s); + s.clear(); + } + } + } + if(s != "") { + results.push_back(s); + } + return results; +} + +bool has_a(vector list, string name) { + for(unsigned int i = 0; i < list.size(); i++) { + if(list[i] == name) + return true; + } + return false; +} + +int main() { + srand(time(NULL)); + cout << "Please enter names with a space inbetween each player: "; + string names; + getline(cin, names); + vector list = parse_expression(names); + vector result; + while(result.size() != list.size()) { + int rand_m; + while(rand_m != 50) { + rand_m = rand() % 10000; + } + int rand_n = rand() % list.size(); + string adding = list[rand_n]; + if(!has_a(result, adding)) { + result.push_back(adding); + } + } + cout << "This is the order: " << endl; + for(unsigned int i = 0; i < result.size(); i++) { + cout << i + 1 << ") " << result[i] << endl; + } +}