Browse Source

adding cs235

master
Derek McQuay 6 years ago
parent
commit
cf99ec6565
  1. 26
      cs235/exam1/Makefile
  2. 68
      cs235/exam1/PolynomialListInterface.h
  3. 84
      cs235/exam1/PolynomialManagerInterface.h
  4. 433
      cs235/exam1/main.cpp
  5. 42
      cs235/exam1/node.cpp
  6. 19
      cs235/exam1/node.h
  7. 438
      cs235/exam1/polylist.cpp
  8. 31
      cs235/exam1/polylist.h
  9. 107
      cs235/exam1/polyman.cpp
  10. 22
      cs235/exam1/polyman.h
  11. 31
      cs235/final/Makefile
  12. 43
      cs235/final/RedBlackNodeInterface.h
  13. 20
      cs235/final/RedBlackTreeFactory.cpp
  14. 23
      cs235/final/RedBlackTreeFactory.h
  15. 78
      cs235/final/RedBlackTreeInterface.h
  16. 21
      cs235/final/TwoThreeTreeFactory.cpp
  17. 25
      cs235/final/TwoThreeTreeFactory.h
  18. 58
      cs235/final/TwoThreeTreeInterface.h
  19. 19
      cs235/final/br_node.cpp
  20. 21
      cs235/final/br_node.h
  21. 500
      cs235/final/br_tree.cpp
  22. 34
      cs235/final/br_tree.h
  23. 235
      cs235/final/main.cpp
  24. 7
      cs235/final/test.cpp
  25. BIN
      cs235/lab02/Lab 2 README.pdf
  26. 33
      cs235/lab02/Run Test Driver.bat
  27. 20
      cs235/lab02/Student_Code/Factory.cpp
  28. 23
      cs235/lab02/Student_Code/Factory.h
  29. 54
      cs235/lab02/Student_Code/JosephusInterface.h
  30. 25
      cs235/lab02/Student_Code/Makefile
  31. 88
      cs235/lab02/Student_Code/circle.cpp
  32. 19
      cs235/lab02/Student_Code/circle.h
  33. 1974
      cs235/lab02/Student_Code/main.cpp
  34. 661
      cs235/lab03/DONT_DELETE.supp
  35. 16
      cs235/lab03/Run_Test_Driver.sh
  36. 21
      cs235/lab03/Student_Code/Factory.cpp
  37. 23
      cs235/lab03/Student_Code/Factory.h
  38. 80
      cs235/lab03/Student_Code/LinkedListInterface.h
  39. 37
      cs235/lab03/Student_Code/Makefile
  40. 158
      cs235/lab03/Student_Code/linkedlist.cpp
  41. 25
      cs235/lab03/Student_Code/linkedlist.h
  42. 24
      cs235/lab03/Student_Code/main.cpp
  43. 3
      cs235/lab03/Student_Code/node.cpp
  44. 14
      cs235/lab03/Student_Code/node.h
  45. 5
      cs235/lab03/Student_Code/pwnd.c
  46. 37
      cs235/lab03/Student_Code/test.cpp
  47. 21
      cs235/lab03/sm/Factory.cpp
  48. 23
      cs235/lab03/sm/Factory.h
  49. 80
      cs235/lab03/sm/LinkedListInterface.h
  50. 27
      cs235/lab03/sm/Makefile
  51. 158
      cs235/lab03/sm/ll.cc
  52. 42
      cs235/lab03/sm/ll.h
  53. 5
      cs235/lab03/sm/pwnd.c
  54. 174
      cs235/lab03/sm/test.cc
  55. 65
      cs235/lab04/ExpressionManagerInterface.h
  56. 22
      cs235/lab04/Factory.cpp
  57. 22
      cs235/lab04/Factory.h
  58. 35
      cs235/lab04/Makefile
  59. 32
      cs235/lab04/Run Test Driver.bat
  60. 7
      cs235/lab04/bin/runner.py
  61. 274
      cs235/lab04/expman.cpp
  62. 28
      cs235/lab04/expman.h
  63. BIN
      cs235/lab04/ignoreme.lib
  64. 165
      cs235/lab04/lab04.html
  65. 13
      cs235/lab04/lab04_files/labs.css
  66. 344
      cs235/lab04/lab04_files/main.css
  67. BIN
      cs235/lab04/lab04_files/title.png
  68. 14
      cs235/lab04/main.cpp
  69. 5
      cs235/lab04/pwnd.c
  70. 11
      cs235/lab04/setup.py
  71. 23
      cs235/lab04/shunting/__init__.py
  72. 10
      cs235/lab04/shunting/test_shunt.py
  73. 117
      cs235/lab04/test.cc
  74. 2
      cs235/lab04/test1.txt
  75. 4
      cs235/lab04/tests/part1.txt
  76. 4
      cs235/lab04/tests/part2.txt
  77. 3
      cs235/lab04/tests/part3.txt
  78. 3
      cs235/lab04/tests/part4.txt
  79. 661
      cs235/lab04/valgrind.supp
  80. 22
      cs235/lab05/Factory.cpp
  81. 21
      cs235/lab05/Factory.h
  82. 35
      cs235/lab05/Makefile
  83. 14
      cs235/lab05/Run_Test_Driver.sh
  84. 160
      cs235/lab05/StationInterface.h
  85. 98
      cs235/lab05/StationInterfaceExtra.h
  86. 60
      cs235/lab05/deque.cpp
  87. 18
      cs235/lab05/deque.h
  88. 52
      cs235/lab05/irdeque.cpp
  89. 17
      cs235/lab05/irdeque.h
  90. 102
      cs235/lab05/linkedlist.cpp
  91. 24
      cs235/lab05/linkedlist.h
  92. 4
      cs235/lab05/main.cpp
  93. 3
      cs235/lab05/node.cpp
  94. 14
      cs235/lab05/node.h
  95. 46
      cs235/lab05/ordeque.cpp
  96. 16
      cs235/lab05/ordeque.h
  97. 5
      cs235/lab05/pwnd.c
  98. 38
      cs235/lab05/queue.cpp
  99. 15
      cs235/lab05/queue.h
  100. 37
      cs235/lab05/stack.cpp

26
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)

68
cs235/exam1/PolynomialListInterface.h

@ -0,0 +1,68 @@
//YOU MAY NOT MODIFY THIS DOCUMENT
#pragma once
#include <string>
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;
};

84
cs235/exam1/PolynomialManagerInterface.h

@ -0,0 +1,84 @@
//YOU MAY NOT MODIFY THIS DOCUMENT
#ifndef POLYNOMIALMANAGERINTERFACE_H_
#define POLYNOMIALMANAGERINTERFACE_H_
#include "PolynomialListInterface.h"
#include <string>
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_ */

433
cs235/exam1/main.cpp

@ -0,0 +1,433 @@
#include <iostream>
#include <cassert>
#include <string>
#include <sstream>
#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<string> 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();
}

42
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;
}

19
cs235/exam1/node.h

@ -0,0 +1,19 @@
//Derek McQuay 647465151 CS 235 Fall 2012 midterm 1
#ifndef __NODE_H__
#define __NODE_H__
#include <iostream>
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

438
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<string> 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<string> 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<string> 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<string> 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<string> parse_expression(string expression) { //parses expression with " " being the delimeter
vector<string> 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;
}

31
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 <cctype>
#include <string>
#include <sstream>
#include <vector>
#include <iostream>
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<string> parse_expression(string expression); //these are included outside of the class because i used them in my test.cpp
bool is_valid(vector<string> expression); //and wanted them not to be included in the class
#endif

107
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;
}

22
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

31
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)

43
cs235/final/RedBlackNodeInterface.h

@ -0,0 +1,43 @@
//YOU MAY NOT MODIFY THIS DOCUMENT
#pragma once
#include <string>
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;
};

20
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
}

23
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();
};
//=======================================================================================

78
cs235/final/RedBlackTreeInterface.h

@ -0,0 +1,78 @@
//YOU MAY NOT MODIFY THIS DOCUMENT
#pragma once
#include <string>
#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.
*/