Browse Source

adding cs235

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

+ 26
- 0
cs235/exam1/Makefile View File

@@ -0,0 +1,26 @@
1
+CXXFLAGS= -Wall -g -std=c++0x
2
+OBJECTS=main.o node.o polylist.o polyman.o
3
+EXE=main
4
+
5
+all: $(EXE)
6
+
7
+
8
+$(EXE): main.o
9
+	$(CXX) $(CXXFLAGS) $(OBJECTS) -o $@
10
+main.o: main.cpp node.o polylist.o polyman.o
11
+polylist.o: polylist.cpp polylist.h
12
+polyman.o: polyman.cpp polyman.h
13
+node.o: node.cpp node.h
14
+
15
+run: $(EXE)
16
+	@./$(EXE)
17
+
18
+clean:
19
+	@rm -vf *.o
20
+	@rm -vf $(EXE)
21
+
22
+drun: $(EXE)
23
+	gdb ./$(EXE)
24
+
25
+valgrind: $(EXE)
26
+	valgrind --tool=memcheck --leak-check=yes ./$(EXE)

+ 68
- 0
cs235/exam1/PolynomialListInterface.h View File

@@ -0,0 +1,68 @@
1
+//YOU MAY NOT MODIFY THIS DOCUMENT
2
+#pragma once
3
+#include <string>
4
+
5
+using namespace std;
6
+
7
+class PolynomialListInterface
8
+{
9
+
10
+public:
11
+	/*
12
+	 * You must create a Node class that will hold each polynomial segment and will be used to create the
13
+	 * links in your PolynomialList.
14
+	 * Your Node will likely have 3 private variables in addition to a Node * next you may have.
15
+	 * These 3 variables will be for the Coefficient, the Variable, and the Exponent respectively.
16
+	 * i.e. 2x^7
17
+	 * 		Coefficient = 2
18
+	 * 		Variable = x
19
+	 * 		Exponent = 7
20
+	 */
21
+	PolynomialListInterface(void){}
22
+	virtual ~PolynomialListInterface(void){}
23
+	
24
+	/*
25
+		insert
26
+
27
+		A node with the given term should be inserted at the appropriate spot in the list.
28
+		A term can be accepted only with Coefficient value > 0, and exponent value non-negative.
29
+		If the term is invalid, do not add it to the list.
30
+		i.e. If the given term segment is 3x^5 and your list contains 2x^8, x^4, 11x^2
31
+			the node should be added after 2x^8. Your updated list should look like 2x^8, 3x^5, x^4, 11x^2.
32
+
33
+	 */
34
+	virtual void insert(string term) = 0;
35
+
36
+	/*
37
+		clear
38
+
39
+		Remove all nodes from the list.
40
+	 */
41
+	virtual void clear() = 0;
42
+
43
+	/*
44
+		at
45
+
46
+		Returns the polynomial of the node at the given index. The list begins at
47
+		index 0.
48
+
49
+		If the given index is out of range of the list, return "invalid";
50
+	 */
51
+	virtual string at(int index) = 0;
52
+
53
+	/*
54
+		size
55
+
56
+		Returns the number of nodes in the list.
57
+	 */
58
+	virtual int size() = 0;
59
+
60
+	/*
61
+	 * This function should string together all of the nodes and print out the entire polynomial separated with '+' signs.
62
+	 * If an exponent of 0 exists however, you should print only the Coefficient.
63
+	 * i.e. list of Nodes = (2x^4)->(4x^2)->(3x^1)->(11x^0)
64
+	 * 		printList() = return "2 x ^ 4 + 4 x ^ 2 + 3 x ^ 1 + 11 x";
65
+	 */
66
+	virtual string printList() = 0;
67
+
68
+};

+ 84
- 0
cs235/exam1/PolynomialManagerInterface.h View File

@@ -0,0 +1,84 @@
1
+//YOU MAY NOT MODIFY THIS DOCUMENT
2
+#ifndef POLYNOMIALMANAGERINTERFACE_H_
3
+#define POLYNOMIALMANAGERINTERFACE_H_
4
+#include "PolynomialListInterface.h"
5
+#include <string>
6
+
7
+using namespace std;
8
+
9
+class PolynomialManagerInterface {
10
+public:
11
+	PolynomialManagerInterface(){}
12
+	virtual ~PolynomialManagerInterface(){}
13
+
14
+	/*
15
+	 * addLists()
16
+	 * This function goes through the process of adding your two list's together and returning the resulting list.
17
+	 * Like terms should be added together correctly, creating one Node for the final list.
18
+	 * If both of the lists are empty, return an empty list. This list must be sorted in correct polynomial order by degree.
19
+	 * i.e.
20
+	 * (2x^4)->(4x^2)->(3x^1)->(11x^0), not (2x^4)->(3x^1)->(11x^0)->(4x^2) or anything else.
21
+	 */
22
+	virtual PolynomialListInterface * addLists() = 0;
23
+
24
+	/*
25
+	 * subtractLists()
26
+	 * This function goes through the process of subtracting your list two from your list one and returning the resulting list.
27
+	 * Like terms should be subtracted correctly, creating one Node for the final list. If a resulting coefficient is 0, that node should be removed.
28
+	 * If both of the lists are empty, return an empty list. This list must be sorted in correct polynomial order by degree.
29
+	 * i.e.
30
+	 * (2x^4)->(4x^2)->(3x^1)->(11x^0), not (2x^4)->(3x^1)->(11x^0)->(4x^2) or anything else.
31
+	 */
32
+	virtual PolynomialListInterface * subtractLists() = 0;
33
+
34
+	/*
35
+	 * fillListOne(string term)
36
+	 * This adds a single term at a time.
37
+	 * This function will be called many times while your first list is being filled.
38
+	 * You should add the new term at the appropriate spot in the list
39
+	 * which may involve adding it to an existing polynomial list, the list should be ordered by degrees.
40
+	 * All segments will have at least a variable and an exponent, however the Coefficient may be an implied 1.
41
+	 * i.e. x^6 is valid, but 2x is not.
42
+	 * If the term is not valid, it should not be added to the list.
43
+	 *
44
+	 */
45
+	virtual void fillListOne(string term) = 0;
46
+
47
+	/*
48
+	 * fillListTwo(string term)
49
+	 * This adds a single term at a time.
50
+	 * This function will be called many times while your second list is being filled.
51
+	 * You should add the new term at the appropriate spot in the list
52
+	 * which may involve adding it to an existing polynomial list, the list should be ordered by degrees.
53
+	 * All segments will have at least a variable and an exponent, however the Coefficient may be an implied 1.
54
+	 * i.e. x^6 is valid, but 2x is not.
55
+	 * If the term is not valid, it should not be added to the list.
56
+	 */
57
+	virtual void fillListTwo(string term) = 0;
58
+
59
+	/*
60
+	 * clearListOne()
61
+	 * This function should empty your first list entirely.
62
+	 */
63
+	virtual void clearListOne() = 0;
64
+
65
+	/*
66
+	 * clearListTwo()
67
+	 * This function should empty your second list entirely.
68
+	 */
69
+	virtual void clearListTwo() = 0;
70
+
71
+	/*
72
+	 * getListOne()
73
+	 * Returns a pointer to the first list.
74
+	 */
75
+	virtual PolynomialListInterface * getListOne() = 0;
76
+
77
+	/*
78
+	 * getListTwo()
79
+	 * Returns a pointer to the second list.
80
+	 */
81
+	virtual PolynomialListInterface * getListTwo() = 0;
82
+};
83
+
84
+#endif /* POLYNOMIALMANAGERINTERFACE_H_ */

+ 433
- 0
cs235/exam1/main.cpp View File

@@ -0,0 +1,433 @@
1
+#include <iostream>
2
+#include <cassert>
3
+#include <string>
4
+#include <sstream>
5
+#include "node.h"
6
+#include "polylist.h"
7
+#include "polyman.h"
8
+
9
+using namespace std;
10
+
11
+int main() {
12
+//node::node(const int exponent, const int coefficient, const char variable, node* next) :
13
+    node n(0,0,'x',NULL);
14
+    node n1(0,1,'x',NULL);
15
+    node n2(1,0,'x',NULL);
16
+    node n3(1,1,'x',NULL);
17
+    node n4(1,'x',NULL);
18
+    node n5(0,'x',NULL);
19
+    node n6(1,'x',NULL);
20
+
21
+    stringstream s;
22
+
23
+    s << n;
24
+    assert(s.str() == "");
25
+    s.str("");
26
+
27
+    s << n1;
28
+    assert(s.str() == "1");
29
+    s.str("");
30
+
31
+    s << n2;
32
+    assert(s.str() == "");
33
+    s.str("");
34
+
35
+    s << n3;
36
+    assert(s.str() == "x");
37
+    s.str("");
38
+
39
+    string a1 = "1 x ^ 1";
40
+    vector<string> v = parse_expression(a1);
41
+    assert(v[0] == "1");
42
+    assert(v[1] == "x");
43
+    assert(v[3] == "1");
44
+    assert(is_valid(v) == true);
45
+    
46
+    string a2 = "123 g ^ 152";
47
+    v = parse_expression(a2);
48
+    assert(is_valid(v) == true);
49
+
50
+    string a3 = "a b c";
51
+    v = parse_expression(a3);
52
+    assert(is_valid(v) == false);
53
+
54
+    string a4 = "";
55
+    v = parse_expression(a4);
56
+    assert(is_valid(v) == false);
57
+    
58
+    string a5 = "1 x";
59
+    v = parse_expression(a5);
60
+    assert(is_valid(v) == true);
61
+
62
+    string a6 = "x";
63
+    v = parse_expression(a6);
64
+    assert(is_valid(v) == true);
65
+
66
+    string a7 = "morethanonecharacter";
67
+    v = parse_expression(a7);
68
+    assert(is_valid(v) == false);
69
+    
70
+    string a8 = "$";
71
+    v = parse_expression(a8);
72
+    assert(is_valid(v) == false);
73
+
74
+    string a9 = "1 & ^ x";
75
+    v = parse_expression(a9);
76
+    assert(is_valid(v) == false);
77
+
78
+    string b1 = "123 g ^ %";
79
+    v = parse_expression(b1);
80
+    assert(is_valid(v) == false);
81
+
82
+    string b2 = "1r32 g ^ 152";
83
+    v = parse_expression(b2);
84
+    assert(is_valid(v) == false);
85
+    
86
+    string b3 = "123 ^ 152";
87
+    v = parse_expression(b3);
88
+    assert(is_valid(v) == false);
89
+
90
+    string b4 = "123 g ^";
91
+    v = parse_expression(b4);
92
+    assert(is_valid(v) == false);
93
+
94
+    string b5 = "5 x ^ -2";
95
+    v = parse_expression(b5);
96
+    assert(is_valid(v) == false);
97
+    
98
+    polylist p;
99
+    stringstream sr;
100
+
101
+    p.insert("x");
102
+    sr << p.printList();
103
+    assert(sr.str() == "1");
104
+
105
+    p.insert("2 x");
106
+    sr.str("");
107
+    sr << p.printList();
108
+    assert(sr.str() == "3");
109
+
110
+    p.insert("-5 x");
111
+    sr.str("");
112
+    sr << p.printList();
113
+    assert(sr.str() == "-2");
114
+
115
+    assert(p.size() == 1);
116
+
117
+    p.insert("x ^ 1");
118
+    sr.str("");
119
+    sr << p.printList();
120
+    assert(sr.str() == "x + -2");
121
+
122
+    assert(p.size() == 2);
123
+
124
+    p.insert("x ^ 4");
125
+    sr.str("");
126
+    sr << p.printList();
127
+    assert(sr.str() == "x ^ 4 + x + -2");
128
+
129
+
130
+    assert(p.size() == 3);
131
+
132
+    p.insert("x ^ 5");
133
+    sr.str("");
134
+    sr << p.printList();
135
+    assert(sr.str() == "x ^ 5 + x ^ 4 + x + -2");
136
+
137
+    p.insert("x ^ -1");
138
+    sr.str("");
139
+    sr << p.printList();
140
+    assert(sr.str() == "x ^ 5 + x ^ 4 + x + -2");
141
+
142
+    p.insert("x ^ 4");
143
+    sr.str("");
144
+    sr << p.printList();
145
+    assert(sr.str() == "x ^ 5 + 2 x ^ 4 + x + -2");
146
+
147
+    p.insert("x ^ 4");
148
+    sr.str("");
149
+    sr << p.printList();
150
+    assert(sr.str() == "x ^ 5 + 3 x ^ 4 + x + -2");
151
+
152
+    p.insert("x ^ 4");
153
+    sr.str("");
154
+    sr << p.printList();
155
+    assert(sr.str() == "x ^ 5 + 4 x ^ 4 + x + -2");
156
+
157
+    p.insert("x ^ 5");
158
+    sr.str("");
159
+    sr << p.printList();
160
+    assert(sr.str() == "2 x ^ 5 + 4 x ^ 4 + x + -2");
161
+
162
+
163
+    assert(p.size() == 4);
164
+
165
+    p.insert("x ^ 1");
166
+    sr.str("");
167
+    sr << p.printList();
168
+    assert(sr.str() == "2 x ^ 5 + 4 x ^ 4 + 2 x + -2");
169
+
170
+    p.insert("x");
171
+    sr.str("");
172
+    sr << p.printList();
173
+    assert(sr.str() == "2 x ^ 5 + 4 x ^ 4 + 2 x + -1");
174
+
175
+    p.insert("x ^ 7");
176
+    sr.str("");
177
+    sr << p.printList();
178
+    assert(sr.str() == "x ^ 7 + 2 x ^ 5 + 4 x ^ 4 + 2 x + -1");
179
+
180
+    p.insert("x ^ -7");
181
+    sr.str("");
182
+    sr << p.printList();
183
+    assert(sr.str() == "x ^ 7 + 2 x ^ 5 + 4 x ^ 4 + 2 x + -1");
184
+
185
+    p.insert("x ^ 7");
186
+    sr.str("");
187
+    sr << p.printList();
188
+    assert(sr.str() == "2 x ^ 7 + 2 x ^ 5 + 4 x ^ 4 + 2 x + -1");
189
+
190
+    assert(p.size() == 5);
191
+
192
+    p.insert("2 x ^ 3");
193
+    sr.str("");
194
+    sr << p.printList();
195
+    assert(sr.str() == "2 x ^ 7 + 2 x ^ 5 + 4 x ^ 4 + 2 x ^ 3 + 2 x + -1");
196
+
197
+
198
+    p.insert("2 x ^ 3");
199
+    sr.str("");
200
+    sr << p.printList();
201
+    assert(sr.str() == "2 x ^ 7 + 2 x ^ 5 + 4 x ^ 4 + 4 x ^ 3 + 2 x + -1");
202
+
203
+
204
+    p.insert("2 x ^ 2");
205
+    sr.str("");
206
+    sr << p.printList();
207
+    assert(sr.str() == "2 x ^ 7 + 2 x ^ 5 + 4 x ^ 4 + 4 x ^ 3 + 2 x ^ 2 + 2 x + -1");
208
+
209
+    p.insert("2 x ^ 7");
210
+    sr.str("");
211
+    sr << p.printList();
212
+    assert(sr.str() == "4 x ^ 7 + 2 x ^ 5 + 4 x ^ 4 + 4 x ^ 3 + 2 x ^ 2 + 2 x + -1");
213
+
214
+    p.insert("2 x ^ 0");
215
+    sr.str("");
216
+    sr << p.printList();
217
+    assert(sr.str() == "4 x ^ 7 + 2 x ^ 5 + 4 x ^ 4 + 4 x ^ 3 + 2 x ^ 2 + 2 x + 1");
218
+
219
+    p.insert("-4 x ^ 4");
220
+    sr.str("");
221
+    sr << p.printList();
222
+    assert(sr.str() == "4 x ^ 7 + 2 x ^ 5 + 4 x ^ 3 + 2 x ^ 2 + 2 x + 1");
223
+
224
+    p.insert("2 x ^ 3");
225
+    sr.str("");
226
+    sr << p.printList();
227
+    assert(sr.str() == "4 x ^ 7 + 2 x ^ 5 + 6 x ^ 3 + 2 x ^ 2 + 2 x + 1");
228
+
229
+    p.insert("8 x ^ 4");
230
+    sr.str("");
231
+    sr << p.printList();
232
+    assert(sr.str() == "4 x ^ 7 + 2 x ^ 5 + 8 x ^ 4 + 6 x ^ 3 + 2 x ^ 2 + 2 x + 1");
233
+
234
+    p.insert("2 x ^ -3");
235
+    sr.str("");
236
+    sr << p.printList();
237
+    assert(sr.str() == "4 x ^ 7 + 2 x ^ 5 + 8 x ^ 4 + 6 x ^ 3 + 2 x ^ 2 + 2 x + 1");
238
+
239
+    p.insert("-1 x ^ 3");
240
+    sr.str("");
241
+    sr << p.printList();
242
+    assert(sr.str() == "4 x ^ 7 + 2 x ^ 5 + 8 x ^ 4 + 5 x ^ 3 + 2 x ^ 2 + 2 x + 1");
243
+
244
+    p.insert("-2 x ^ 7");
245
+    sr.str("");
246
+    sr << p.printList();
247
+    assert(sr.str() == "2 x ^ 7 + 2 x ^ 5 + 8 x ^ 4 + 5 x ^ 3 + 2 x ^ 2 + 2 x + 1");
248
+
249
+    p.insert("2 x ^ 9");
250
+    sr.str("");
251
+    sr << p.printList();
252
+    assert(sr.str() == "2 x ^ 9 + 2 x ^ 7 + 2 x ^ 5 + 8 x ^ 4 + 5 x ^ 3 + 2 x ^ 2 + 2 x + 1");
253
+
254
+    p.insert("-2 x ^ 9");
255
+    sr.str("");
256
+    sr << p.printList();
257
+    assert(sr.str() == "2 x ^ 7 + 2 x ^ 5 + 8 x ^ 4 + 5 x ^ 3 + 2 x ^ 2 + 2 x + 1");
258
+
259
+    p.insert("10 x ^ 7");
260
+    sr.str("");
261
+    sr << p.printList();
262
+    assert(sr.str() == "12 x ^ 7 + 2 x ^ 5 + 8 x ^ 4 + 5 x ^ 3 + 2 x ^ 2 + 2 x + 1");
263
+
264
+    p.insert("x");
265
+    sr.str("");
266
+    sr << p.printList();
267
+    assert(sr.str() == "12 x ^ 7 + 2 x ^ 5 + 8 x ^ 4 + 5 x ^ 3 + 2 x ^ 2 + 2 x + 2");
268
+
269
+    p.insert("x ^ 7");
270
+    sr.str("");
271
+    sr << p.printList();
272
+    assert(sr.str() == "13 x ^ 7 + 2 x ^ 5 + 8 x ^ 4 + 5 x ^ 3 + 2 x ^ 2 + 2 x + 2");
273
+
274
+    p.insert("-10 x ^ 7");
275
+    sr.str("");
276
+    sr << p.printList();
277
+    assert(sr.str() == "3 x ^ 7 + 2 x ^ 5 + 8 x ^ 4 + 5 x ^ 3 + 2 x ^ 2 + 2 x + 2");
278
+
279
+    p.insert("10 x");
280
+    sr.str("");
281
+    sr << p.printList();
282
+    assert(sr.str() == "3 x ^ 7 + 2 x ^ 5 + 8 x ^ 4 + 5 x ^ 3 + 2 x ^ 2 + 2 x + 12");
283
+
284
+    p.insert("-10 x");
285
+    sr.str("");
286
+    sr << p.printList();
287
+    assert(sr.str() == "3 x ^ 7 + 2 x ^ 5 + 8 x ^ 4 + 5 x ^ 3 + 2 x ^ 2 + 2 x + 2");
288
+
289
+    p.insert("-2 x");
290
+    sr.str("");
291
+    sr << p.printList();
292
+    assert(sr.str() == "3 x ^ 7 + 2 x ^ 5 + 8 x ^ 4 + 5 x ^ 3 + 2 x ^ 2 + 2 x");
293
+
294
+    p.insert("2 x ^ 0");
295
+    sr.str("");
296
+    sr << p.printList();
297
+    assert(sr.str() == "3 x ^ 7 + 2 x ^ 5 + 8 x ^ 4 + 5 x ^ 3 + 2 x ^ 2 + 2 x + 2");
298
+
299
+    p.insert("x ^ 3");
300
+    sr.str("");
301
+    sr << p.printList();
302
+    assert(sr.str() == "3 x ^ 7 + 2 x ^ 5 + 8 x ^ 4 + 6 x ^ 3 + 2 x ^ 2 + 2 x + 2");
303
+
304
+    p.insert("");
305
+    sr.str("");
306
+    sr << p.printList();
307
+    assert(sr.str() == "3 x ^ 7 + 2 x ^ 5 + 8 x ^ 4 + 6 x ^ 3 + 2 x ^ 2 + 2 x + 2");
308
+
309
+    p.insert("-3 x ^ 7");
310
+    sr.str("");
311
+    sr << p.printList();
312
+    assert(sr.str() == "2 x ^ 5 + 8 x ^ 4 + 6 x ^ 3 + 2 x ^ 2 + 2 x + 2");
313
+    
314
+    p.insert("-2 x ^ 5");
315
+    sr.str("");
316
+    sr << p.printList();
317
+    assert(sr.str() == "8 x ^ 4 + 6 x ^ 3 + 2 x ^ 2 + 2 x + 2");
318
+
319
+    p.insert("-2 x ^ 2");
320
+    sr.str("");
321
+    sr << p.printList();
322
+    assert(sr.str() == "8 x ^ 4 + 6 x ^ 3 + 2 x + 2");
323
+
324
+    p.insert("-3 x ^ 7");
325
+    sr.str("");
326
+    sr << p.printList();
327
+    assert(sr.str() == "-3 x ^ 7 + 8 x ^ 4 + 6 x ^ 3 + 2 x + 2");
328
+
329
+    p.insert("-3 x ^ 7");
330
+    sr.str("");
331
+    sr << p.printList();
332
+    assert(sr.str() == "-6 x ^ 7 + 8 x ^ 4 + 6 x ^ 3 + 2 x + 2");
333
+
334
+    p.insert("9 x ^ 7");
335
+    sr.str("");
336
+    sr << p.printList();
337
+    assert(sr.str() == "3 x ^ 7 + 8 x ^ 4 + 6 x ^ 3 + 2 x + 2");
338
+
339
+    p.clear();
340
+    sr.str("");
341
+    sr << p.printList();
342
+    assert(sr.str() == "");
343
+
344
+    assert(p.size() == 0);
345
+
346
+    p.insert("9 x ^ 7");
347
+    sr.str("");
348
+    sr << p.printList();
349
+    assert(sr.str() == "9 x ^ 7");
350
+
351
+    assert(p.size() == 1); 
352
+
353
+    p.clear();
354
+    sr.str("");
355
+    sr << p.printList();
356
+    assert(sr.str() == "");
357
+
358
+    assert(p.size() == 0);
359
+
360
+    p.insert("x ^ 7");
361
+    sr.str("");
362
+    sr << p.printList();
363
+    assert(sr.str() == "x ^ 7");
364
+
365
+    p.insert("-x ^ 7");
366
+    sr.str("");
367
+    sr << p.printList();
368
+    assert(sr.str() == "");
369
+
370
+    p.clear();
371
+    sr.str("");
372
+    sr << p.printList();
373
+    assert(sr.str() == "");
374
+
375
+    assert(p.size() == 0);
376
+
377
+    p.insert("2 x");
378
+    sr.str("");
379
+    sr << p.printList();
380
+    assert(sr.str() == "2");
381
+
382
+    p.clear();
383
+    sr.str("");
384
+    sr << p.printList();
385
+    assert(sr.str() == "");
386
+
387
+    assert(p.size() == 0);
388
+
389
+    p.insert("x");
390
+    sr.str("");
391
+    sr << p.printList();
392
+    assert(sr.str() == "1");
393
+
394
+    p.remove(0);
395
+    sr.str("");
396
+    sr << p.printList();
397
+    assert(sr.str() == "");
398
+
399
+    p.insert("-x");
400
+    sr.str("");
401
+    sr << p.printList();
402
+    assert(sr.str() == "-1");
403
+
404
+    p.insert("2 x");
405
+    sr.str("");
406
+    sr << p.printList();
407
+    assert(sr.str() == "1");
408
+
409
+    node n7(3, -1, 'x',NULL);
410
+
411
+    polyman m;
412
+    m.addLists();
413
+    m.subtractLists();
414
+    m.fillListOne("x");
415
+    m.fillListOne("2 x");
416
+    m.fillListOne("3 x ^ 3");
417
+    m.fillListOne("4aqkjx");
418
+    m.fillListOne("-4 x ^ 323");
419
+    m.fillListOne("-2 x ^ 400");
420
+    m.fillListOne("x");
421
+    m.fillListOne("x");
422
+    m.fillListOne("x");
423
+    m.fillListOne("x");
424
+    m.fillListTwo("-6 x");
425
+    m.fillListTwo("-4 x ^ 323");
426
+    m.fillListTwo("-x ^ 3");
427
+    m.fillListTwo("7 x ^ 8");
428
+    m.fillListTwo("4 x ^ 3");
429
+    m.fillListTwo("-x");
430
+    m.fillListTwo("14 x");
431
+    m.addLists();
432
+    m.subtractLists();
433
+}

+ 42
- 0
cs235/exam1/node.cpp View File

@@ -0,0 +1,42 @@
1
+//Derek McQuay 647465151 CS 235 Fall 2012 midterm 1
2
+#include "node.h"
3
+
4
+node::node(const int exponent, const int coefficient, const char variable, node* next) :
5
+                    exponent(exponent), coefficient(coefficient), variable(variable), next(next) {}
6
+
7
+node::node(const int exponent, const char variable, node* next) : //construtor for when coefficient is assumed 1
8
+                    exponent(exponent), coefficient(1), variable(variable), next(next) {}
9
+
10
+ostream & operator<<(ostream & os, node n) { //used to correctly print out each node
11
+    if(n.coefficient == 0) {
12
+        os << ""; 
13
+    }
14
+    else if(n.coefficient == 1 && n.exponent == 0) {
15
+        os << n.coefficient;
16
+    }
17
+    else if(n.coefficient == -1 && n.exponent == 0) {
18
+        os <<  n.coefficient;
19
+    }
20
+    else if(n.exponent == 0) {
21
+        os << n.coefficient;
22
+    }
23
+    else if(n.coefficient == -1) {
24
+        os << "-" <<  n.variable << " ^ " << n.exponent;
25
+    }
26
+    else if(n.coefficient < 0 && n.exponent == 1) {
27
+        os << n.coefficient << n.variable;
28
+    }
29
+    else if(n.coefficient == 1 && n.exponent == 1) {
30
+        os << n.variable;
31
+    }
32
+    else if(n.exponent == 1) {
33
+       os << n.coefficient << " " << n.variable;
34
+    }
35
+    else if(n.coefficient == 1) {
36
+        os << n.variable << " ^ " << n.exponent;
37
+    }
38
+    else {
39
+        os << n.coefficient << " " <<  n.variable << " ^ " << n.exponent;
40
+    }
41
+    return os;
42
+}

+ 19
- 0
cs235/exam1/node.h View File

@@ -0,0 +1,19 @@
1
+//Derek McQuay 647465151 CS 235 Fall 2012 midterm 1
2
+#ifndef __NODE_H__
3
+#define __NODE_H__
4
+
5
+#include <iostream>
6
+
7
+using namespace std;
8
+
9
+class node {
10
+    public:
11
+        node(const int, const int, const char, node*);
12
+        node(const int, const char, node*);
13
+        int exponent;
14
+        int coefficient;
15
+        char variable;
16
+        node* next;
17
+        friend ostream & operator<<(ostream & os, node n);
18
+};
19
+#endif

+ 438
- 0
cs235/exam1/polylist.cpp View File

@@ -0,0 +1,438 @@
1
+//Derek McQuay 647465151 CS 235 Fall 2012 midterm 1
2
+#include "polylist.h"
3
+
4
+polylist::polylist(): head(NULL) {}
5
+polylist::~polylist() {clear();}
6
+
7
+void polylist::insert(string term) {
8
+    vector<string> v = parse_expression(term);
9
+    if(!is_valid(v)) {
10
+        return;
11
+    }
12
+    if(v.size() == 1) { //expression of the form x
13
+        if(v[0][0] == '-') {
14
+            insertTail(0, -1, v[0][1]);
15
+        }
16
+        else {
17
+            insertTail(0, 1, v[0][0]);
18
+        }
19
+    }
20
+    if(v.size() == 2) { // expresion of the form 2 x
21
+        int exp = atoi(v[0].c_str());
22
+        insertTail(0, exp, v[1][0]);
23
+    }
24
+    if(v.size() == 3) {    // expression of the form x ^ 4
25
+        int exp = atoi(v[2].c_str());
26
+        int after_exp = 0;
27
+        if(exp == 0) {
28
+            if(v[0][0] == '-') {
29
+                insertTail(exp, -1, v[0][0]);
30
+            }
31
+            else {
32
+                insertTail(exp, 1, v[0][0]);
33
+            }
34
+        }
35
+        node* node_ptr = head;
36
+        while(node_ptr != NULL) {
37
+            if(node_ptr->exponent <= exp) {
38
+                after_exp++;
39
+                break;
40
+            }
41
+            after_exp++;
42
+            node_ptr = node_ptr->next;
43
+        }
44
+        string before = at(after_exp - 1);
45
+        vector<string> previous_vector = parse_expression(before);
46
+        if(previous_vector.size() == 1) {
47
+            if(node_ptr == head) {
48
+                if(node_ptr == head && size() == 0) {
49
+                    if(v[0][0] == '-') {
50
+                        insertHead(exp, -1, v[0][0]);
51
+                    }
52
+                    else {
53
+                        insertHead(exp, 1, v[0][0]);
54
+                    }
55
+                }
56
+                else if(node_ptr->exponent == exp) {
57
+                    node_ptr->coefficient += 1;
58
+                    if(node_ptr->coefficient == 0) {
59
+                        remove(exp);
60
+                    }
61
+                }
62
+                else {
63
+                    if(v[0][0] == '-') {
64
+                        insertHead(exp, -1, v[0][1]);
65
+                    }
66
+                    else {
67
+                        insertHead(exp, 1, v[0][0]);
68
+                    }
69
+                }
70
+            }
71
+            else {
72
+                node_ptr->coefficient += 1;
73
+                if(node_ptr->coefficient == 0) {
74
+                    remove(exp);
75
+                }
76
+            }
77
+        }
78
+        if(previous_vector.size() == 2) {
79
+            if(v[0][0] == '-') {
80
+                insertHead(exp, -1, v[0][1]);
81
+            }
82
+            else {
83
+                insertHead(exp, 1, v[0][0]);
84
+            }
85
+        }
86
+        if(previous_vector.size() == 3) { 
87
+            int insertionNodeExponent = atoi(previous_vector[2].c_str());
88
+            if(insertionNodeExponent == exp) {
89
+                node* node_ptr2 = head;
90
+                while(node_ptr2 != NULL) {
91
+                    if(node_ptr2->exponent == exp) {
92
+                        if(v[0][0] == '-') {
93
+                            node_ptr2->coefficient -= 1;
94
+                        }
95
+                        else {
96
+                            node_ptr2->coefficient += 1;
97
+                        }
98
+                        if(node_ptr2->coefficient == 0) {
99
+                            remove(exp);
100
+                        }
101
+                    }
102
+                    node_ptr2 = node_ptr2->next;
103
+                }
104
+            }
105
+            else{
106
+                if(v[0][0] == '-') {
107
+                    insertAfter(exp, -1, v[0][1], insertionNodeExponent); 
108
+                }
109
+                else {
110
+                    insertAfter(exp, 1, v[0][0], insertionNodeExponent); 
111
+                }
112
+            }
113
+        }
114
+        if(previous_vector.size() == 4) {
115
+            int insertionNodeExponent = atoi(previous_vector[3].c_str());
116
+            if(insertionNodeExponent == exp) {
117
+                node* node_ptr2 = head;
118
+                while(node_ptr2 != NULL) {
119
+                    if(node_ptr2->exponent == exp) {
120
+                        node_ptr2->coefficient += 1;
121
+                        if(node_ptr2->coefficient == 0) {
122
+                            remove(exp);
123
+                        }
124
+                    }
125
+                    node_ptr2 = node_ptr2->next;
126
+                }
127
+            }
128
+            else {
129
+                if(v[0][0] == '-') {
130
+                    insertAfter(exp, -1, v[0][1], insertionNodeExponent);
131
+                }
132
+                else {
133
+                    insertAfter(exp, 1, v[0][0], insertionNodeExponent);
134
+                }
135
+            }
136
+        }
137
+    }
138
+    if(v.size() == 4) {               //expression of the form "2 x ^ 3"
139
+        int coef = atoi(v[0].c_str());
140
+        int exp = atoi(v[3].c_str());
141
+        int after_exp = 0;
142
+        node* node_ptr = head;
143
+        if(exp == 0) {
144
+            insertTail(exp, coef, v[1][0]);
145
+            return;
146
+        }
147
+        while(node_ptr != NULL) {
148
+            if(node_ptr->exponent <= exp) {
149
+                break;
150
+            }
151
+            after_exp++;
152
+            node_ptr = node_ptr->next;
153
+        }
154
+        string before = at(after_exp - 1);
155
+        vector<string> previous_vector = parse_expression(before);
156
+        if(previous_vector.size() == 1) {
157
+            if(node_ptr == head) {
158
+                if(node_ptr == head && size() == 0) {
159
+                    insertHead(exp, coef, v[1][0]);
160
+                }
161
+                else if(node_ptr->exponent == exp) {
162
+                    node_ptr->coefficient += coef;
163
+                    if(node_ptr->coefficient == 0) {
164
+                        remove(exp);
165
+                    }
166
+                }
167
+                else {
168
+                    insertHead(exp, coef, v[1][0]);
169
+                }
170
+            }
171
+            else {
172
+                node_ptr->coefficient += coef;
173
+                if(node_ptr->coefficient == 0) {
174
+                    remove(exp);
175
+                }
176
+            }
177
+        }
178
+        if(previous_vector.size() == 2) {
179
+            insertHead(exp, coef, v[1][0]);
180
+        }
181
+        if(previous_vector.size() == 3) {
182
+            int insertionNodeExponent = atoi(previous_vector[2].c_str());
183
+            if(node_ptr->exponent == exp) {
184
+                node* node_ptr2 = head;
185
+                while(node_ptr2 != NULL) {
186
+                    if(node_ptr2->exponent == exp) {
187
+                        node_ptr2->coefficient += coef;
188
+                        if(node_ptr2->coefficient == 0) {
189
+                            remove(exp);
190
+                        }
191
+                    }
192
+                    node_ptr2 = node_ptr2->next;
193
+                }
194
+            }
195
+            else{
196
+                insertAfter(exp, coef, v[1][0], insertionNodeExponent); 
197
+            }
198
+        }
199
+        if(previous_vector.size() == 4) {
200
+            int insertionNodeExponent = atoi(previous_vector[3].c_str());
201
+            if(node_ptr == NULL) {
202
+                insertTail(exp, coef, v[1][0]);
203
+            }
204
+            else {
205
+                if(node_ptr->exponent == exp) {
206
+                    node* node_ptr2 = head;
207
+                    while(node_ptr2 != NULL) {
208
+                        if(node_ptr2->exponent == exp) {
209
+                            node_ptr2->coefficient += coef;
210
+                            if(node_ptr2->coefficient == 0) {
211
+                                remove(exp);
212
+                            }
213
+                        }
214
+                        node_ptr2 = node_ptr2->next;
215
+                    }
216
+                }
217
+                else {
218
+                    insertAfter(exp, coef, v[1][0], insertionNodeExponent);
219
+                }
220
+            }
221
+        }
222
+    }
223
+}
224
+
225
+void polylist::clear() {
226
+    node* node_ptr = head;
227
+    while(head) {
228
+        node_ptr = head;
229
+        head = head->next;
230
+        delete node_ptr;
231
+    }
232
+}
233
+
234
+string polylist::at(int index) {
235
+    if(index < 0) {
236
+        return "invalid";
237
+    }
238
+    if(size() <= index) {
239
+        return "invalid";
240
+    }
241
+    node* node_ptr = head;
242
+    for(int i = 0; i < index; i++){
243
+        node_ptr = node_ptr->next;
244
+    }
245
+    if(head == NULL)
246
+        return "invalid";
247
+    stringstream s;
248
+    s << *node_ptr;
249
+    return s.str();
250
+}
251
+
252
+int polylist::size() {
253
+    int size_of_list = 0;
254
+    node* node_ptr = head;
255
+    while(node_ptr != NULL) {
256
+        size_of_list++;
257
+        node_ptr = node_ptr->next;
258
+    }
259
+    return size_of_list;
260
+}
261
+
262
+string polylist::printList() {
263
+    stringstream s;
264
+    if(head == NULL)
265
+        return "";
266
+    node* node_ptr = head;
267
+    while(node_ptr != NULL) {
268
+        if(node_ptr->next == NULL) {
269
+            s << *node_ptr;
270
+            return s.str();
271
+        }
272
+        else {
273
+            s << *node_ptr << " + ";
274
+            node_ptr = node_ptr->next;
275
+        }
276
+    }
277
+    return s.str();
278
+}
279
+
280
+void polylist::remove(int exponent) {
281
+    node* node_ptr = head;
282
+    if(node_ptr->exponent == exponent) {
283
+        node* ptr = head;
284
+        head = head->next;
285
+        delete ptr;
286
+        return;
287
+    }
288
+    while(node_ptr != NULL) {
289
+        if(node_ptr->next == NULL)
290
+            return;
291
+        if(node_ptr->next->exponent == exponent) {
292
+            node* ptr = node_ptr->next;
293
+            node_ptr->next = node_ptr->next->next;
294
+            delete ptr;
295
+        }
296
+        node_ptr = node_ptr->next;
297
+    }
298
+}
299
+
300
+void polylist::insertHead(int exponent, int coefficient, char variable) {
301
+    head = new node(exponent, coefficient, variable, head);
302
+}
303
+
304
+void polylist::insertTail(int exponent, int coefficient, char variable) {
305
+    if(head == NULL) {
306
+        insertHead(exponent, coefficient, variable);
307
+        return;
308
+    }
309
+    node* node_ptr = head;
310
+    while(node_ptr != NULL) {
311
+        if(node_ptr->next == NULL) {
312
+            if(node_ptr->exponent == exponent) {
313
+                node_ptr->coefficient += coefficient;
314
+                if(node_ptr->coefficient == 0) {
315
+                    remove(exponent);
316
+                }
317
+            }
318
+            else{
319
+                node_ptr->next = new node(exponent, coefficient, variable, NULL);
320
+                break;
321
+            }
322
+        }
323
+        node_ptr = node_ptr->next;
324
+    }
325
+}
326
+
327
+void polylist::insertAfter(int exponent, int coefficient, char variable, int insertionNodeExponent) {
328
+    node* node_ptr = head;
329
+    while(node_ptr != NULL) {
330
+        if(node_ptr->exponent == insertionNodeExponent) {
331
+            if(head == node_ptr) {
332
+                insertHead(exponent, coefficient, variable);
333
+            }
334
+            else{
335
+                node* temp = new node(exponent, coefficient, variable, NULL);
336
+                temp->next = node_ptr->next;
337
+                node_ptr->next = temp;
338
+            }
339
+        }
340
+        node_ptr = node_ptr->next;
341
+    }
342
+}
343
+
344
+bool is_valid(vector<string> expression) {
345
+    if(expression.empty()) {    //if expression is size 0
346
+        return false;
347
+    }
348
+    if(expression.size() == 1) {          //if expression is size one. only case if "x"
349
+        if(expression[0][0] == '-') {
350
+            if(expression[0] == "-x")
351
+                return true;
352
+            if(!isalpha(expression[0][1]) or expression[0].size() > 1) {
353
+                return false;
354
+            }
355
+        }
356
+        else if(!isalpha(expression[0][0]) or expression[0].size() > 1) {
357
+            return false;
358
+        }
359
+        return true;
360
+    }
361
+    if(expression.size() == 2) {        //if expression is size two, only case is of form "2 x"
362
+        for(unsigned int i = 0; i < expression[0].size(); i++) {
363
+            if(expression[0][0] != '-') {
364
+                if(!isdigit(expression[0][i])) {
365
+                    return false;
366
+                }
367
+            }
368
+        }
369
+        if(!isalpha(expression[1][0]) or expression[1].size() > 1) {
370
+            return false;
371
+        }
372
+        return true;
373
+    }
374
+    if(expression.size() == 3) {     //if expression is size three, only case is of form "x ^ 1"
375
+        if(expression[0][0] == '-') {
376
+            if(expression[0] == "-x")
377
+                return true;
378
+            if(!isalpha(expression[0][1]) or expression[0].size() > 1) {
379
+                return false;
380
+            }
381
+        }
382
+        else if(!isalpha(expression[0][0]) or expression[0].size() > 1) {
383
+            return false;
384
+        }
385
+        if(expression[1] != "^") {
386
+            return false;
387
+        }
388
+        for(unsigned int i = 0; i < expression[2].size(); i++) {
389
+            if(!isdigit(expression[2][i])) {
390
+                return false;
391
+            }
392
+        }
393
+        return true;
394
+    }
395
+    if(expression.size() == 4) {      //if expression is size four, only case is of form "1 x ^ 1"
396
+        for(unsigned int i = 0; i < expression[0].size(); i++) {
397
+            if(expression[0][0] != '-') {
398
+                if(!isdigit(expression[0][i])) {
399
+                    return false;
400
+                }
401
+            }
402
+        }
403
+        if(!isalpha(expression[1][0]) or expression[1].size() > 1) {
404
+            return false;
405
+        }
406
+        if(expression[2] != "^") {
407
+            return false;
408
+        }
409
+        for(unsigned int i = 0; i < expression[3].size(); i++) {
410
+            if(!isdigit(expression[3][i])) {
411
+                return false;
412
+            }
413
+        }
414
+        return true;
415
+    }
416
+    return false;
417
+}
418
+
419
+vector<string> parse_expression(string expression) { //parses expression with " " being the delimeter
420
+    vector<string> results;
421
+    string s;
422
+    for(unsigned int i = 0; i < expression.length(); i++) {
423
+        char c = expression[i];
424
+        if(c != ' ') {
425
+            s += c;
426
+        }
427
+        else {
428
+            if(s != "") {
429
+                results.push_back(s);
430
+                s.clear();
431
+            }
432
+        }
433
+    }
434
+    if(s != "") {
435
+        results.push_back(s);
436
+    }
437
+    return results;
438
+}

+ 31
- 0
cs235/exam1/polylist.h View File

@@ -0,0 +1,31 @@
1
+//Derek McQuay 647465151 CS 235 Fall 2012 midterm 1
2
+#ifndef __POLYLIST_H__
3
+#define __POLYLIST_H__
4
+#include "PolynomialListInterface.h"
5
+#include "node.h"
6
+#include <cctype>
7
+#include <string>
8
+#include <sstream>
9
+#include <vector>
10
+#include <iostream>
11
+
12
+using namespace std;
13
+
14
+class polylist : public PolynomialListInterface {
15
+    public:
16
+        polylist();
17
+        ~polylist();
18
+        node* head;
19
+        void insert(string term);
20
+        void clear();
21
+        string at(int index);
22
+        int size();
23
+        string printList();
24
+        void remove(int exponent);
25
+        void insertAfter(int exponent, int coefficient, char value, int insertionNodeExponent);
26
+        void insertTail(int exponent, int coefficient, char value);
27
+        void insertHead(int exponent, int coefficient, char value);
28
+};
29
+vector<string> parse_expression(string expression); //these are included outside of the class because i used them in my test.cpp
30
+bool is_valid(vector<string> expression);           //and wanted them not to be included in the class
31
+#endif

+ 107
- 0
cs235/exam1/polyman.cpp View File

@@ -0,0 +1,107 @@
1
+//Derek McQuay 647465151 CS 235 Fall 2012 midterm 1
2
+#include "polyman.h"
3
+
4
+PolynomialListInterface * polyman::addLists() {
5
+    node * node_ptr1 = l1.head;
6
+    node * node_ptr2 = l2.head;
7
+    polylist l3;
8
+    stringstream exp;
9
+    while(node_ptr1 != NULL) {
10
+        if(node_ptr1->coefficient != 0) {
11
+            exp << node_ptr1->coefficient;
12
+            exp << " ";
13
+        }
14
+        exp << node_ptr1->variable;
15
+        if(node_ptr1->exponent != 0) {
16
+            exp << " ^ ";
17
+            exp << node_ptr1->exponent;
18
+        }
19
+        l3.insert(exp.str());
20
+        exp.str("");
21
+        node_ptr1 = node_ptr1->next;
22
+    }
23
+    while(node_ptr2 != NULL) {
24
+        if(node_ptr2->coefficient != 0) {
25
+            exp << node_ptr2->coefficient;
26
+            exp << " ";
27
+        }
28
+        exp << node_ptr2->variable;
29
+        if(node_ptr2->exponent != 0) {
30
+            exp << " ^ ";
31
+            exp << node_ptr2->exponent;
32
+        }
33
+        l3.insert(exp.str());
34
+        exp.str("");
35
+        node_ptr2 = node_ptr2->next;
36
+    }
37
+    polylist * p = NULL;
38
+    p = &l3;
39
+    return p;
40
+}
41
+
42
+PolynomialListInterface * polyman::subtractLists() {
43
+    node * node_ptr1 = l1.head;
44
+    node * node_ptr2 = l2.head;
45
+    polylist l3;
46
+    stringstream exp;
47
+    while(node_ptr1 != NULL) {
48
+        if(node_ptr1->coefficient != 0) {
49
+            exp << node_ptr1->coefficient;
50
+            exp << " ";
51
+        }
52
+        exp << node_ptr1->variable;
53
+        if(node_ptr1->exponent != 0) {
54
+            exp << " ^ ";
55
+            exp << node_ptr1->exponent;
56
+        }
57
+        l3.insert(exp.str());
58
+        exp.str("");
59
+        node_ptr1 = node_ptr1->next;
60
+    }
61
+    while(node_ptr2 != NULL) {
62
+        if(node_ptr2->coefficient != 0) {
63
+            node_ptr2->coefficient = node_ptr2->coefficient * -1; // will cause it to be subtracting 
64
+            exp << node_ptr2->coefficient;
65
+            exp << " ";
66
+        }
67
+        exp << node_ptr2->variable;
68
+        if(node_ptr2->exponent != 0) {
69
+            exp << " ^ ";
70
+            exp << node_ptr2->exponent;
71
+        }
72
+        l3.insert(exp.str());
73
+        exp.str("");
74
+        node_ptr2 = node_ptr2->next;
75
+    }
76
+    polylist * p = NULL;
77
+    p = &l3;
78
+    return p;
79
+}
80
+
81
+void polyman::fillListOne(string term) {
82
+    l1.insert(term);
83
+}
84
+
85
+void polyman::fillListTwo(string term) {
86
+    l2.insert(term);
87
+}
88
+
89
+void polyman::clearListOne() {
90
+    l1.clear();
91
+}
92
+
93
+void polyman::clearListTwo() {
94
+    l2.clear();
95
+}
96
+
97
+PolynomialListInterface * polyman::getListOne() {
98
+    polylist * p = NULL;
99
+    p = &l1;
100
+    return p;
101
+}
102
+
103
+PolynomialListInterface * polyman::getListTwo() {
104
+    polylist * p = NULL;
105
+    p = &l2;
106
+    return p;
107
+}

+ 22
- 0
cs235/exam1/polyman.h View File

@@ -0,0 +1,22 @@
1
+//Derek McQuay 647465151 CS 235 Fall 2012 midterm 1
2
+#ifndef __POLYMAN_H__
3
+#define __POLYMAN_H__
4
+#include "PolynomialManagerInterface.h"
5
+#include "polylist.h"
6
+
7
+using namespace std;
8
+
9
+class polyman : public PolynomialManagerInterface {
10
+    public:
11
+        polylist l1;
12
+        polylist l2;
13
+        PolynomialListInterface * addLists();
14
+        PolynomialListInterface * subtractLists();
15
+        void fillListOne(string term);
16
+        void fillListTwo(string term);
17
+        void clearListOne();
18
+        void clearListTwo();
19
+        PolynomialListInterface * getListOne();
20
+        PolynomialListInterface * getListTwo();
21
+};
22
+#endif

+ 31
- 0
cs235/final/Makefile View File

@@ -0,0 +1,31 @@
1
+CXXFLAGS= -Wall -g -std=c++0x 
2
+OBJECTS=RedBlackTreeFactory.o br_tree.o br_node.o main.o
3
+EXE=main
4
+all: $(EXE)
5
+
6
+$(EXE): $(OBJECTS)
7
+	$(CXX) $(CXXFLAGS) $(OBJECTS) -o $@
8
+test: test.cpp RedBlackTreeFactory.o br_node.o 
9
+rtest: test
10
+	./test
11
+RedBlackTreeFactory.o: RedBlackTreeFactory.cpp RedBlackTreeFactory.h
12
+br_node.o: br_node.cpp br_node.h
13
+br_tree.o: br_tree.cpp br_tree.h
14
+main.o: main.cpp
15
+
16
+run: main
17
+	./main
18
+
19
+clean:
20
+	@rm -vf *.o
21
+	@rm -vf $(EXE)
22
+	@rm -vf *.1
23
+	@rm -vf *.0
24
+	@rm -vf test
25
+	@rm -rvf *.dSYM
26
+
27
+drun: main
28
+	gdb ./main
29
+
30
+valgrind: $(EXE)
31
+	valgrind --tool=memcheck --leak-check=yes ./$(EXE)

+ 43
- 0
cs235/final/RedBlackNodeInterface.h View File

@@ -0,0 +1,43 @@
1
+//YOU MAY NOT MODIFY THIS DOCUMENT
2
+#pragma once
3
+
4
+#include <string>
5
+
6
+using namespace std;
7
+
8
+class RedBlackNodeInterface {
9
+
10
+public:
11
+	RedBlackNodeInterface() {}
12
+	virtual ~RedBlackNodeInterface() {}
13
+
14
+	/*
15
+	 * Returns the word that is stored in this node
16
+	 *
17
+	 * @return the word that is stored in this node.
18
+	 */
19
+	virtual string getWord() = 0;
20
+
21
+	/*
22
+	 * Returns the color of the node. A red node should return 0
23
+	 * and a black node should return 1.
24
+	 *
25
+	 * @return the color of the node
26
+	 */
27
+	virtual int getColor() = 0;
28
+
29
+	/*
30
+	 * Returns the left child of this node or null if it doesn't have one.
31
+	 *
32
+	 * @return the left child of this node or null if it doesn't have one.
33
+	 */
34
+	virtual RedBlackNodeInterface * getLeftChild() = 0;
35
+
36
+	/*
37
+	 * Returns the right child of this node or null if it doesn't have one.
38
+	 *
39
+	 * @return the right child of this node or null if it doesn't have one.
40
+	 */
41
+	virtual RedBlackNodeInterface * getRightChild() = 0;
42
+
43
+};

+ 20
- 0
cs235/final/RedBlackTreeFactory.cpp View File

@@ -0,0 +1,20 @@
1
+#include "RedBlackTreeFactory.h"
2
+#include "br_tree.h"
3
+//You may add #include statements here
4
+
5
+/*
6
+	You will MODIFY THIS DOCUMENT.
7
+*/
8
+//=======================================================================================
9
+/*
10
+	getRedBlackTree()
11
+
12
+	Creates and returns an object whose class extends RedBlackTreeInterface.
13
+	This should be an object of a class you have created.
14
+
15
+	Example: If you made a class called "RedBlackTree", you might say, "return new RedBlackTree();".
16
+*/
17
+RedBlackTreeInterface * RedBlackTreeFactory::getRedBlackTree()
18
+{
19
+	return new br_tree();//Modify this line
20
+}

+ 23
- 0
cs235/final/RedBlackTreeFactory.h View File

@@ -0,0 +1,23 @@
1
+#pragma once
2
+#include "RedBlackTreeInterface.h"
3
+
4
+using namespace std;
5
+
6
+/*
7
+	WARNING: It is expressly forbidden to modify any part of this document, including its name
8
+*/
9
+//=======================================================================================
10
+/*
11
+	getRedBlackTree()
12
+
13
+	Creates and returns an object whose class extends RedBlackTreeInterface.
14
+	This should be an object of a class you have created.
15
+
16
+	Example: If you made a class called "RedBlackTree", you might say, "return new RedBlackTree();".
17
+*/
18
+class RedBlackTreeFactory
19
+{
20
+	public:
21
+		static RedBlackTreeInterface * getRedBlackTree();
22
+};
23
+//=======================================================================================

+ 78
- 0
cs235/final/RedBlackTreeInterface.h View File

@@ -0,0 +1,78 @@
1
+//YOU MAY NOT MODIFY THIS DOCUMENT
2
+#pragma once
3
+
4
+#include <string>
5
+#include "RedBlackNodeInterface.h"
6
+
7
+using namespace std;
8
+
9
+class RedBlackTreeInterface {
10
+public:
11
+	RedBlackTreeInterface() {}
12
+	virtual ~RedBlackTreeInterface() {}
13
+
14
+	//Please note that the class that implements this interface must be made
15
+	//of objects which implement the NodeInterface
16
+
17
+	/*
18
+	 * Returns the root node for this tree
19
+	 *
20
+	 * @return the root node for this tree.
21
+	 */
22
+	virtual RedBlackNodeInterface * getRootNode() = 0;
23
+
24
+	/*
25
+	 * Attempts to add the given word to the tree
26
+	 *
27
+	 * Addition should be consistent with the instructions found in the exam instructions.
28
+	 */
29
+	virtual void add(string word) = 0;
30
+
31
+	/*
32
+	 * Attempts to add the given string of words to the tree
33
+	 *
34
+	 * A string of words consists of alphabetical characters, no punctuation marks,
35
+	 * and white space as the delimiter separating words. The string should be parsed and
36
+	 * each word should be added to the tree.
37
+	 */
38
+	virtual void addPhrase(string words) = 0;
39
+
40
+	/*
41
+	 * Attempts to remove the given word from the tree
42
+	 *
43
+	 * Removal should be consistent with the instructions found in the exam instructions.
44
+	 */
45
+	virtual void remove(string word) = 0;
46
+
47
+	/*
48
+	 * The tree should print in the following format:
49
+	 *
50
+	 * 	Root (value & color)
51
+	 *		Left subtree root (value & color)
52
+	 *			Left left subtree root (value & color)
53
+	 *			Left right subtree root (value & color)
54
+	 *		Right subtree root (value & color)
55
+	 *			Right left subtree root (value & color)
56
+	 *			Right right subtree root (value & color)
57
+	 *
58
+	 *	For example:
59
+	 *		The tree:
60
+	 *
61
+	 * 					jumps(b)
62
+	 *		brown(r)				quick(r)
63
+	 *	The(b)    fox(b)    	over(b)   the(b)
64
+	 *                 		lazy(r)
65
+	 *
66
+	 *		Will print:
67
+	 *		jumps(b)
68
+	 * 			brown(r)
69
+	 * 				The(b)
70
+	 * 				fox(b)
71
+	 * 			quick(r)
72
+	 * 				over(b)
73
+	 * 					lazy(r);
74
+	 * 				the(b)
75
+	 *
76
+	 */
77
+	virtual string printTree() = 0;
78
+};

+ 21
- 0
cs235/final/TwoThreeTreeFactory.cpp View File

@@ -0,0 +1,21 @@
1
+#include "TwoThreeTreeFactory.h"
2
+//You may add #include statements here
3
+
4
+/*
5
+	You will MODIFY THIS DOCUMENT.
6
+*/
7
+//=======================================================================================
8
+/*
9
+	getTwoThreeTree()
10
+
11
+	Creates and returns an object whose class extends TwoTreeTreeInterface.
12
+	This should be an object of a class you have created.
13
+
14
+	Example: If you made a class called "TwoThreeTree", you might say, "return new TwoThreeTree();".
15
+
16
+	This method should return NULL or 0 if you do not intent to do the extra credit
17
+*/
18
+TwoThreeTreeInterface * TwoThreeTreeFactory::getTwoThreeTree()
19
+{
20
+	return 0;//Modify this line
21
+}

+ 25
- 0
cs235/final/TwoThreeTreeFactory.h View File

@@ -0,0 +1,25 @@
1
+#pragma once
2
+#include "TwoThreeTreeInterface.h"
3
+
4
+using namespace std;
5
+
6
+/*
7
+	WARNING: It is expressly forbidden to modify any part of this document, including its name
8
+*/
9
+//=======================================================================================
10
+/*
11
+	getTwoThreeTree()
12
+
13
+	Creates and returns an object whose class extends TwoThreeTreeInterface.
14
+	This should be an object of a class you have created.
15
+
16
+	Example: If you made a class called "TwoThreeTree", you might say, "return new TwoThreeTree();".
17
+
18
+	This method should return NULL or 0 if you do not intent to do the extra credit
19
+*/
20
+class TwoThreeTreeFactory
21
+{
22
+	public:
23
+		static TwoThreeTreeInterface * getTwoThreeTree();
24
+};
25
+//=======================================================================================

+ 58
- 0
cs235/final/TwoThreeTreeInterface.h View File

@@ -0,0 +1,58 @@
1
+//YOU MAY NOT MODIFY THIS DOCUMENT
2
+#pragma once
3
+
4
+#include <string>
5
+
6
+using namespace std;
7
+
8
+class TwoThreeTreeInterface {
9
+public:
10
+	TwoThreeTreeInterface() {}
11
+	virtual ~TwoThreeTreeInterface() {}
12
+
13
+	//This class may use a node of your own creation.
14
+
15
+	/*
16
+	 * Attempts to add the given word to the tree
17
+	 *
18
+	 * Addition should be consistent with the instructions found in the exam instructions.
19
+	 */
20
+	virtual void add(string word) = 0;
21
+
22
+	/*
23
+	 * Attempts to add the given string of words to the tree
24
+	 *
25
+	 * A string of words consists of alphabetical characters, no punctuation marks,
26
+	 * and white space as the delimiter separating words. The string should be parsed and
27
+	 * each word should be added to the tree.
28
+	 */
29
+	virtual void addPhrase(string words) = 0;
30
+
31
+	/*
32
+	 * Attempts to remove the given word from the tree
33
+	 *
34
+	 * Removal should be consistent with the instructions found in the exam instructions.
35
+	 */
36
+	virtual void remove(string word) = 0;
37
+
38
+	/*
39
+	 * The tree should print in the following format:
40
+	 *
41
+	 * 	Root (value)
42
+	 *		Left subtree root (value)
43
+	 *			Left left subtree root (value)
44
+	 *			Left middle subtree root (value)
45
+	 *			Left right subtree root (value)
46
+	 *		Middle subtree root (value)
47
+	 *			Middle left subtree root (value)
48
+	 *			Middle middle subtree root (value)
49
+	 *			Middle right subtree root (value)
50
+	 *
51
+	 *		Right subtree root (value)
52
+	 *			Right left subtree root (value)
53
+	 *			Right middle subtree root (value)
54
+	 *			Right right subtree root (value)
55
+	 *
56
+	 */
57
+	virtual string printTree() = 0;
58
+};

+ 19
- 0
cs235/final/br_node.cpp View File

@@ -0,0 +1,19 @@
1
+#include "br_node.h"
2
+
3
+br_node::br_node(string data): data(data), color(0), parent(NULL), left(NULL), right(NULL) {}
4
+
5
+string br_node::getWord() {
6
+    return data;
7
+}
8
+
9
+int br_node::getColor() {
10
+    return color;
11
+}
12
+
13
+RedBlackNodeInterface* br_node::getLeftChild() {
14
+    return left;
15
+}
16
+
17
+RedBlackNodeInterface* br_node::getRightChild() {
18
+    return right;
19
+}

+ 21
- 0
cs235/final/br_node.h View File

@@ -0,0 +1,21 @@
1
+#ifndef __BR_NODE_H__
2
+#define __BR_NODE_H__
3
+
4
+#include "RedBlackNodeInterface.h"
5
+
6
+using namespace std;
7
+
8
+class br_node : public RedBlackNodeInterface {
9
+    public:
10
+        br_node(string data);
11
+        string data;
12
+        int color; //red = 0, black = 1
13
+        br_node* parent;
14
+        br_node* left;
15
+        br_node* right;
16
+        string getWord();
17
+        int getColor();
18
+        RedBlackNodeInterface * getLeftChild();
19
+        RedBlackNodeInterface * getRightChild();
20
+};
21
+#endif

+ 500
- 0
cs235/final/br_tree.cpp View File

@@ -0,0 +1,500 @@
1
+#include "br_tree.h"
2
+
3
+br_tree::br_tree():root(NULL), h(false){}
4
+
5
+vector<string> parse_expression(string expression) { //parses expression with " " being the delimeter
6
+    vector<string> results;
7
+    string s;
8
+    for(unsigned int i = 0; i < expression.length(); i++) {
9
+        char c = expression[i];
10
+        if(c != ' ') {
11
+            s += c;
12
+        }
13
+        else {
14
+            if(s != "") {
15
+                results.push_back(s);
16
+                s.clear();
17
+            }
18
+        }
19
+    }
20
+    if(s != "") {
21
+        results.push_back(s);
22
+    }
23
+    return results;
24
+}
25
+
26
+bool br_tree::in_tree(string data) {
27
+    bool found = false;
28
+    br_node* current;
29
+    br_node* parent;
30
+    current = root;
31
+    while(current) {
32
+        if(current->data == data) {
33
+            found = true;
34
+            break;
35
+        }
36
+        else {
37
+            parent = current;
38
+            if(data > current->data) {
39
+                current = current->right;
40
+            }
41
+            else if(data < current->data) {
42
+                current = current->left;
43
+            }
44
+        }
45
+    }
46
+    return found;
47
+}
48
+
49
+RedBlackNodeInterface* br_tree::getRootNode() {
50
+    return root;
51
+}
52
+
53
+void br_tree::add(string word) {
54
+    if(in_tree(word)) {
55
+        return;
56
+    }
57
+    if(root == NULL) {
58
+        root = new br_node(word);
59
+        root->color = 1;
60
+        return;
61
+    }
62
+    br_node* current;
63
+    br_node* cur_parent;
64
+    current = root;
65
+    while(current) {
66
+        cur_parent = current;
67
+        if(word > current->data) {
68
+            current = current->right;
69
+        }
70
+        else {
71
+            current = current->left;
72
+        }
73
+    }
74
+    if(word < cur_parent->data) {
75
+        cur_parent->left = new br_node(word);
76
+        cur_parent->left->parent = cur_parent;
77
+        br_node* child_added = cur_parent->left;
78
+        if(cur_parent->color == 0 && child_added->color == 0) {
79
+            balance(cur_parent->left);
80
+        }
81
+        return;
82
+    }
83
+    else {
84
+        cur_parent->right = new br_node(word);
85
+        cur_parent->right->parent = cur_parent;
86
+        br_node* child_added = cur_parent->right;
87
+        if(cur_parent->color == 0 && child_added->color == 0) {
88
+            balance(cur_parent->right);
89
+        }
90
+        return;
91
+    }
92
+}
93
+
94
+void br_tree::balance(br_node* cur) {
95
+    if((cur->data == root->data) || (cur->parent->data == root->data)) {
96
+        return;
97
+    }
98
+    if((cur->parent->parent->right != NULL) && (cur->parent->parent->left != NULL)) {
99
+        if((cur->parent->parent->left->color == 0) && (cur->parent->parent->right->color == 0)) {
100
+            cur->parent->parent->left->color = 1;
101
+            cur->parent->parent->right->color = 1;
102
+            cur->parent->parent->color = 0;
103
+            balance(cur->parent->parent);
104
+            return;
105
+        }
106
+    }
107
+    if((cur->parent->color == 0) && (cur->color == 0)) { 
108
+        if((cur->parent->left != NULL) && (cur->parent->parent->left != NULL)) {
109
+            if((cur->parent->left->data == cur->data) && (cur->parent->parent->left->data == cur->parent->data)) {
110
+                //case III left
111
+                br_node* temp = cur->parent->parent;
112
+                if(cur->parent->right == NULL) {
113
+                    temp->left = cur->parent->right;
114
+                }
115
+                else {
116
+                    temp->left = cur->parent->right;
117
+                    cur->parent->right->parent = temp;
118
+                }
119
+                cur->parent->right = temp;
120
+                if(temp->parent != NULL) {
121
+                    if(temp->parent->right->data == temp->data) {
122
+                        temp->parent->right = cur->parent;
123
+                    }
124
+                    else {
125
+                        temp->parent->left = cur->parent;
126
+                    }
127
+                }
128
+                else {
129
+                    root = cur->parent;
130
+                }
131
+                cur->parent->parent = temp->parent;
132
+                temp->parent = cur->parent;
133
+                cur->parent->color = 1;
134
+                temp->color = 0;
135
+                balance(cur->parent);
136
+                return;
137
+            }
138
+        }
139
+        if((cur->parent->left != NULL) && (cur->parent->parent->right != NULL)) {
140
+            if((cur->parent->left->data == cur->data) && (cur->parent->parent->right->data == cur->parent->data)){
141
+                //case II right
142
+                br_node* temp1 = cur->parent->parent;
143
+                br_node* temp2 = cur->parent;
144
+                if(cur->right == NULL) {
145
+                    temp2->left = cur->right;
146
+                }
147
+                else {
148
+                    temp2->left = cur->right;
149
+                    cur->right->parent = temp2;
150
+                }
151
+                cur->right = temp2;
152
+                temp2->parent = cur;
153
+                cur->parent = temp1;
154
+                temp1->right = cur;
155
+                balance(temp2);
156
+                return;
157
+            }
158
+        }
159
+        if((cur->parent->right != NULL) && (cur->parent->parent->left != NULL)) {
160
+            if((cur->parent->right->data == cur->data) && (cur->parent->parent->left->data == cur->parent->data)){
161
+                //case II left
162
+                br_node* temp1 = cur->parent->parent;
163
+                br_node* temp2 = cur->parent;
164
+                if(cur->left == NULL) {
165
+                    temp2->right = cur->left;
166
+                }
167
+                else {
168
+                    temp2->right = cur->left;
169
+                    cur->left->parent = temp2;
170
+                }
171
+                cur->left = temp2;
172
+                temp2->parent = cur;
173
+                cur->parent = temp1;
174
+                temp1->left = cur;
175
+                balance(temp2);
176
+                return;
177
+            }
178
+        }
179
+        if((cur->parent->right != NULL) && (cur->parent->parent->right != NULL)) {
180
+            if((cur->parent->right->data == cur->data) && (cur->parent->parent->right->data == cur->parent->data)) {
181
+                //case III right
182
+                br_node* temp = cur->parent->parent;
183
+                if(cur->parent->left == NULL) {
184
+                    temp->right = cur->parent->left;
185
+                }
186
+                else {
187
+                    temp->right = cur->parent->left;
188
+                    cur->parent->left->parent = temp;
189
+                }
190
+                cur->parent->left = temp;
191
+                if(temp->parent != NULL) {
192
+                    if(temp->parent->right->data == temp->data) {
193
+                        temp->parent->right = cur->parent;
194
+                    }
195
+                    else {
196
+                        temp->parent->left = cur->parent;
197
+                    }
198
+                }
199
+                else {
200
+                    root = cur->parent;
201
+                }
202
+                cur->parent->parent = temp->parent;
203
+                temp->parent = cur->parent;
204
+                cur->parent->color = 1;
205
+                temp->color = 0;
206
+                balance(cur->parent);
207
+                return;
208
+            }
209
+        }
210
+    }
211
+    if(root->color == 0) {
212
+        root->color = 1;
213
+        return;
214
+    }
215
+    balance(cur->parent);
216
+}
217
+
218
+void br_tree::addPhrase(string words) {
219
+    vector<string> parsed_string = parse_expression(words);
220
+    for(unsigned int i = 0; i < parsed_string.size(); i++) {
221
+        add(parsed_string[i]);
222
+    }
223
+}
224
+
225
+void br_tree::remove(string word) {
226
+    if(!(in_tree(word))) {
227
+        return;
228
+    }
229
+    if(root == NULL) {
230
+        return;
231
+    }
232
+    br_node* current;
233
+    br_node* parent;
234
+    current = root;
235
+    while(current) {
236
+        if(current->data == word) {
237
+            break;
238
+        }
239
+        else {
240
+            parent = current;
241
+            if(word > current->data) {
242
+                current = current->right;
243
+            }
244
+            else if(word < current->data) {
245
+                current = current->left;
246
+            }
247
+        }
248
+    }
249
+    if(current->color == 1) {
250
+        h = true;
251
+    }
252
+    else {
253
+        h = false;
254
+    }
255
+    root = del_leaf(root, word);
256
+}
257
+
258
+br_node* br_tree::del_leaf(br_node* root, string data) {
259
+    br_node* temp;
260
+    if(root == NULL) {
261
+        return root;
262
+    }
263
+    else {
264
+        if(data < root->data) {
265
+            root->left = del_leaf(root->left, data);
266
+            if(h) {
267
+                cout << "2" << endl;
268
+                bal_del(root->left);
269
+            }
270
+        }
271
+        else {
272
+            if(data > root->data) {
273
+                root->right = del_leaf(root->right, data);
274
+                if(h) {
275
+                    cout << "3" << endl;
276
+                    cout << root->data << endl;
277
+                    bal_del(root->right);
278
+                }
279
+            }
280
+            else {
281
+                temp = root;
282
+                if(temp->right == NULL) {
283
+                    root = temp->left;
284
+                    delete temp;
285
+                }
286
+                else {
287
+                    if(temp->left == NULL) {
288
+                        root = temp->right;
289
+                        delete temp;
290
+                    }
291
+                    else {
292
+                        temp->left = del(temp->left, temp);
293
+                        if(h) {
294
+                            cout << "4" << endl;
295
+                            if(h) {
296
+                                bal_del(temp);
297
+                            }
298
+                        }
299
+                    }
300
+                }
301
+            }
302
+        }
303
+    }
304
+    return root;
305
+}
306
+
307
+void br_tree::bal_del(br_node* x) {
308
+    //cout << "bal_del(" << x->data << ")" << endl;
309
+    if(x == NULL) {
310
+        return;
311
+    }
312
+    while(x != root && x->color == 1) {
313
+        if(x == x->parent->left) {
314
+            br_node* w = x->parent->right;
315
+            if(w == NULL) {
316
+                return;
317
+            }
318
+            if(w->right == NULL || w->left == NULL) {
319
+                return;
320
+            }
321
+            if(w != NULL && w->right != NULL && w->left != NULL) {
322
+                if(w->color == 0) {
323
+                    cout << "case 1 first" << endl;
324
+                    w->color = 1;                //Case 1
325
+                    x->parent->color = 0;      //Case 1
326
+                    left_rot(x->parent);      //Case 1
327
+                    w = x->parent->right;        //Case 1
328
+                }
329
+                else if(w->left->color == 1 && w->right->color == 1) {
330
+                    cout << "case 2 first" << endl;
331
+                    w->color = 1;                //Case 2
332
+                    x = x->parent;               //Case 2
333
+                }
334
+                else {
335
+                    if(w->right->color == 1) {
336
+                        cout << "case 3 first" << endl;
337
+                        w->left->color = 1;      //Case 3
338
+                        w->color = 0;            //Case 3
339
+                        right_rot(w);         //Case 3
340
+                        w = x->parent->right;    //Case 3
341
+                    }
342
+                    cout << "case 4 first" << endl;
343
+                    w->color = x->parent->color; //Case 4
344
+                    x->parent->color = 1;        //Case 4
345
+                    w->right->color = 1;         //Case 4
346
+                    left_rot(x->parent);
347
+                    x = root;
348
+                }
349
+            }
350
+        }
351
+        else {
352
+            br_node* w = x->parent->left;
353
+            if(w == NULL) {
354
+                return;
355
+            }
356
+            if(w->right == NULL || w->left == NULL) {
357
+                return;
358
+            }
359
+            if(w->color == 0) {
360
+                cout << "case 1 second" << endl;
361
+                w->color = 1;                 //Case 1
362
+                x->parent->color = 0;         //Case 1
363
+                right_rot(x->parent);      //Case 1
364
+                w = x->parent->left;          //Case 1
365
+            }
366
+            else if(w->right->color == 1 && w->left->color == 1) {
367
+                cout << "case 2 second" << endl;
368
+                w->color = 0;                 //Case 2
369
+                x = x->parent;                //Case 2
370
+            }
371
+            else {
372
+                if(w->left->color == 1) {
373
+                    cout << "case 3 second" << endl;
374
+                    w->right->color = 1;      //Case 3
375
+                    w->color = 0;             //Case 3
376
+                    left_rot(w);           //Case 3
377
+                    w = x->parent->left;      //Case 3
378
+                }
379
+                cout << "case 4 second" << endl;
380
+                w->color = x->parent->color;  //Case 4
381
+                x->parent->color = 1;         //Case 4
382
+                w->left->color = 1;           //Case 4
383
+                right_rot(x->parent);
384
+                x = root;
385
+            }
386
+        }
387
+    }
388
+    x->color = 1;
389
+}
390
+
391
+br_node* br_tree::del(br_node* succ, br_node* n) {
392
+    br_node* temp = succ;
393
+    if(succ->right != NULL) {
394
+        succ->right = del(succ->right, n);
395
+    }
396
+    else {
397
+        br_node* current;
398
+        br_node* parent;
399
+        current = root;
400
+        while(current) {
401
+            if(current->data == succ->data) {
402
+                break;
403
+            }
404
+            else {
405
+                parent = current;
406
+                if(succ->data > current->data) {
407
+                    current = current->right;
408
+                }
409
+                else if(succ->data < current->data) {
410
+                    current = current->left;
411
+                }
412
+            }
413
+        }
414
+        if(root->left->data == succ->data) {
415
+            root->data = succ->data;
416
+            n->right = root->right;
417
+            succ = succ->left;
418
+            delete temp;
419
+            return succ;
420
+        } 
421
+        else if(root->right->data == succ->data) {
422
+            root->data = succ->data;
423
+            n->right = root->right;
424
+            succ = succ->right;
425
+            delete temp;
426
+            return succ;
427
+        }
428
+        temp = succ;
429
+        n->data = succ->data;
430
+        succ = succ->left;
431
+        delete temp;
432
+    }
433
+    return succ;
434
+}
435
+
436
+void br_tree::right_rot(br_node* x) {
437
+    br_node* y = x->left;
438
+    x->left = y->right;
439
+    if(y->right != NULL) {
440
+        y->right->parent = x;
441
+    }
442
+    y->parent = x->parent;
443
+    if(x->parent == NULL) {
444
+        root = y;
445
+    }
446
+    else if(x == x->parent->right) {
447
+        x->parent->right = y;
448
+    }
449
+    else {
450
+        x->parent->left = y;
451
+    }
452
+    y->right = x;
453
+    x->parent = y;
454
+}
455
+
456
+void br_tree::left_rot(br_node* x) {
457
+    br_node* y = x->right;
458
+    x->right = y->left;
459
+    if(y->left != NULL) {
460
+        y->left->parent = x;
461
+    } 
462
+    y->parent = x->parent;
463
+    if(x->parent == NULL) {
464
+        root = y;
465
+    }
466
+    else if(x == x->parent->left) {
467
+        x->parent->left = y;
468
+    }
469
+    else {
470
+        x->parent->right = y;
471
+    }
472
+    y->left = x;
473
+    x->parent = y;
474
+}
475
+
476
+void br_tree::printer(br_node* node, int a) {
477
+    a++;
478
+    if(node == NULL) {
479
+        return;
480
+    }
481
+    printer(node->right, a); 
482
+    for(int i = 0; i <= a; i++) {
483
+        cout << "\t";
484
+    }
485
+    printf("%s %d", node->data.c_str(), node->color);
486
+    cout << endl << endl;
487
+    printer(node->left, a);
488
+}
489
+
490
+string br_tree::printTree() {
491
+    string tree;
492
+    if(root == NULL) {
493
+        return "";
494
+    }
495
+    cout << root->data << endl;
496
+    cout << endl;
497
+    printer(root, 0);
498
+    cout << endl;
499
+    return tree;
500
+}

+ 34
- 0
cs235/final/br_tree.h View File

@@ -0,0 +1,34 @@
1
+#ifndef __BR_TREE_H__
2
+#define __BR_TREE_H__
3
+
4
+#include "br_node.h"
5
+#include "RedBlackTreeInterface.h"
6
+#include <cstdio>
7
+#include <iostream>
8
+#include <vector>
9
+#include <string>
10
+
11
+using namespace std;
12
+
13
+class br_tree : public RedBlackTreeInterface {
14
+    public:
15
+        br_tree();
16
+        br_node* root;
17
+        bool h;
18
+        RedBlackNodeInterface * getRootNode();
19
+        bool in_tree(string data);
20
+        void add(string word);
21
+        void addPhrase(string words);
22
+        void remove(string word);
23
+        void balance(br_node* cur);
24
+        void transfer(br_node* temp1, br_node* temp2);
25
+        void bal_del(br_node* x);
26
+        br_node* del_leaf(br_node* root, string data);
27
+        void right_rot(br_node* x);
28
+        void left_rot(br_node* x);
29
+        void printer(br_node* node, int a);
30
+        br_node* del(br_node* succ, br_node* n);
31
+        br_node* min(br_node* temp);
32
+        string printTree();
33
+};
34
+#endif

+ 235
- 0
cs235/final/main.cpp View File

@@ -0,0 +1,235 @@
1
+#include <iostream>
2
+#include <sstream>
3
+#include <string>
4
+#include <time.h>
5
+#include <stdlib.h>
6
+#include "RedBlackTreeFactory.h"
7
+#include "TwoThreeTreeFactory.h"
8
+#include "br_tree.h"
9
+
10
+/* This main is provided to help you test your code. Be aware that the TAs will use a separate
11
+ * test driver that will only test your red black tree code. You may modify this code if you wish,
12
+ * but do not try to modify the main to get your own code to work.
13
+ */
14
+
15
+
16
+using namespace std;
17
+int getOption(int max, int min);
18
+string getString();
19
+void exitPause();
20
+
21
+RedBlackTreeInterface * redBlackTree;
22
+TwoThreeTreeInterface * twoThreeTree;
23
+
24
+int main()
25
+{
26
+    srand(time(NULL));
27
+    br_tree b;
28
+	redBlackTree = RedBlackTreeFactory::getRedBlackTree();
29
+
30
+	if(redBlackTree == NULL) {
31
+		cout << endl;
32
+		cout << "Factory getRedBlackTree() returned NULL!\nExiting Test Driver (press enter to exit)" << endl;
33
+
34
+		exitPause();
35
+		return 0;
36
+	}
37
+
38
+//	twoThreeTree = TwoThreeTreeFactory::getTwoThreeTree();
39
+
40
+	cout << "Final Exam Beginning" << endl;
41
+	while(1)
42
+	{
43
+		int choice = 0;
44
+		if(twoThreeTree == NULL) {
45
+			cout<< "Please select an option:"
46
+				<< "\n1. Add a single word to the tree"
47
+				<< "\n2. Add a list of words to the tree"
48
+				<< "\n3. Remove a single word from the tree"
49
+				<< "\n4. Print the tree"
50
+				<< "\n5. Quit Program" << endl;
51
+			choice = getOption(1,5);
52
+		}
53
+		else {
54
+			cout<< "Please select an option:"
55
+				<< "\n1. Add a single word to the red black tree"
56
+				<< "\n2. Add a list of words to the red black tree"
57
+				<< "\n3. Remove a single word from the red black tree"
58
+				<< "\n4. Print the red black tree"
59
+				<< "\n5. Add a single word to the two three tree"
60
+				<< "\n6. Add a list of words to the two three tree"
61
+				<< "\n7. Remove a single word from the two three tree"
62
+				<< "\n8. Print the two three tree"
63
+				<< "\n9. Quit Program" << endl;
64
+			choice = getOption(1,9);
65
+		}
66
+		if(choice == 1)
67
+		{
68
+			//Insert the string that the user passes to the program into the tree
69
+            for(int i = 0; i < 50000; i++) {
70
+                stringstream s;
71
+                int rand_n = rand() % 50000;
72
+                s << rand_n;
73
+                b.add(s.str());
74
+            }
75
+            cout << b.root << endl;
76
+			//cout<< "Enter the word to add to the tree (alphabetical characters, no punctuation marks, no spaces)."<<endl;
77
+			//string insertToTree;
78
+			//insertToTree = getString();
79
+			//redBlackTree->add(insertToTree);
80
+		}
81
+		else if(choice == 2)
82
+		{
83
+			//Insert the string that the user passes to the program into the tree
84
+			cout<< "Enter the string to add to the tree."
85
+				<< "\n(alphabetical characters, no punctuation marks, and with white space as the delimiter separating words)" << endl;
86
+			string insertToTree;
87
+			insertToTree = getString();
88
+			redBlackTree->addPhrase(insertToTree);
89
+		}
90
+		else if(choice == 3)
91
+		{
92
+            cout << "here" << endl;
93
+            while(b.root) {
94
+                cout << "ah" << endl;
95
+                stringstream s;
96
+                int rand_n = rand() % 50000;
97
+                s << rand_n;
98
+                b.remove(s.str());
99
+            }
100
+			//Remove the string that the user passes to the program into the tree
101
+			//cout << "Enter the word to remove from the tree (alphabetical characters, no punctuation marks, no spaces)."<<endl;
102
+			//string removeFromTree;
103
+			//removeFromTree = getString();
104
+			//redBlackTree->remove(removeFromTree);
105
+		}
106
+		else if(choice == 4)
107
+		{
108
+            cout << b.printTree();
109
+			//cout << "Your tree:" << endl;
110
+			//cout << "\t" << redBlackTree->printTree() << endl;
111
+		}
112
+		else if(choice == 5 && twoThreeTree == NULL)
113
+		{
114
+			break;
115
+		}
116
+		else if(choice == 5 && twoThreeTree != NULL)
117
+		{
118
+			//Insert the string that the user passes to the program into the tree
119
+			cout<< "Enter the word to add to the tree (alphabetical characters, no punctuation marks, no spaces)."<<endl;
120
+			string insertToTree;
121
+			insertToTree = getString();
122
+			twoThreeTree->add(insertToTree);
123
+		}
124
+		else if(choice == 6)
125
+		{
126
+			//Insert the string that the user passes to the program into the tree
127
+			cout<< "Enter the string to add to the tree."
128
+				<< "\n(alphabetical characters, no punctuation marks, and with white space as the delimiter separating words)" << endl;
129
+			string insertToTree;
130
+			insertToTree = getString();
131
+			twoThreeTree->addPhrase(insertToTree);
132
+		}
133
+		else if(choice == 7)
134
+		{
135
+			//Remove the string that the user passes to the program into the tree
136
+			cout << "Enter the word to remove from the tree (alphabetical characters, no punctuation marks, no spaces)."<<endl;
137
+			string removeFromTree;
138
+			removeFromTree = getString();
139
+			twoThreeTree->remove(removeFromTree);
140
+		}
141
+		else if(choice == 8)
142
+		{
143
+			cout << "Your tree:" << endl;
144
+			cout << "\t" << twoThreeTree->printTree() << endl;
145
+		}
146
+		else
147
+		{
148
+			break;
149
+		}
150
+	}
151
+	cout << "Thanks for Playing!" << endl;
152
+
153
+	exitPause();
154
+	return 0;
155
+}
156
+
157
+//-------------------------------------------------------------------------------------
158
+int getOption(int min, int max)
159
+{
160
+	int input = 0;
161
+	bool done = false;
162
+	while(!done)
163
+	{
164
+		input = 0;
165
+		cin >> input;
166
+		cin.ignore(1000,'\n');
167
+		if(cin.fail())
168
+		{
169
+			cin.clear();
170
+			cin.ignore(1000,'\n');
171
+			cout << "Error: Invalid" << endl;
172
+		}
173
+		else if(input < min  ||  input > max)
174
+		{
175
+			cout << "Error: Invalid number" << endl;
176
+		}
177
+		else
178
+		{
179
+			done = true;
180
+		}
181
+	}
182
+	return input;
183
+}
184
+
185
+string getString()
186
+{
187
+	string input = "";
188
+	bool done = false;
189
+	while(!done)
190
+	{
191
+		input = "";
192
+		getline(cin, input);
193
+		if(cin.fail())
194
+		{
195
+			cin.clear();
196
+			cin.ignore(1000,'\n');
197
+			cout << "Error: Invalid name" << endl;
198
+		}
199
+		else
200
+		{
201
+			done = true;
202
+		}
203
+	}
204
+	return input;
205
+}
206
+
207
+string getWord()
208
+{
209
+	string input = "";
210
+	bool done = false;
211
+	while(!done)
212
+	{
213
+		input = "";
214
+		getline(cin, input);
215
+		if(cin.fail())
216
+		{
217
+			cin.clear();
218
+			cin.ignore(1000,'\n');
219
+			cout << "Error: Invalid name" << endl;
220
+		}
221
+		else
222
+		{
223
+			done = true;
224
+		}
225
+	}
226
+	return input;
227
+}
228
+
229
+void exitPause() {
230
+	#ifdef ON_WINDOWS
231
+		system("pause");
232
+	#else
233
+
234
+	#endif
235
+}

+ 7
- 0
cs235/final/test.cpp View File

@@ -0,0 +1,7 @@
1
+#include "br_tree.h"
2
+
3
+using namespace std;
4
+
5
+int main() {
6
+    br_tree b;
7
+}

BIN
cs235/lab02/Lab 2 README.pdf View File


+ 33
- 0
cs235/lab02/Run Test Driver.bat View File

@@ -0,0 +1,33 @@
1
+TITLE TA Test Driver
2
+
3
+@echo off
4
+
5
+del Student_code\circle.cpp~
6
+
7
+rem setting up environment variables needed for VS compiler
8
+call "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86_amd64
9
+
10
+rem compiling the .obj
11
+cl /EHsc /Fedont_run_me.exe Student_Code\*.cpp ignoreme.lib
12
+
13
+rem if compilation failed, goto error section
14
+if ERRORLEVEL 1 goto error 
15
+
16
+rem cleanup unnecessary generated files
17
+del *.obj
18
+
19
+rem run the driver in another window
20
+.\dont_run_me.exe
21
+del dont_run_me.exe
22
+pause
23
+
24
+exit
25
+
26
+:error
27
+
28
+rem remove any generated files
29
+del *.obj
30
+
31
+echo ----------ERROR--------
32
+pause
33
+exit

+ 20
- 0
cs235/lab02/Student_Code/Factory.cpp View File

@@ -0,0 +1,20 @@
1
+#include "Factory.h"
2
+#include "circle.h"
3
+//You may add #include statments here
4
+
5
+/*
6
+	You will MODIFY THIS DOCUMENT.
7
+*/
8
+//=======================================================================================
9
+/*
10
+	getGame()
11
+
12
+	Creates and returns an object whose class extends JosephusInterface.
13
+	This should be an object of a class you have created.
14
+
15
+	Example: If you made a class called "Circle", you might say, "return new Circle();".
16
+*/
17
+JosephusInterface * Factory::getGame()
18
+{
19
+	return new circle();
20
+}

+ 23
- 0
cs235/lab02/Student_Code/Factory.h View File

@@ -0,0 +1,23 @@
1
+#pragma once
2
+#include "JosephusInterface.h"
3
+
4
+using namespace std;
5
+
6
+/*
7
+	WARNING: It is expressly forbidden to modify any part of this document, including its name
8
+*/
9
+//=======================================================================================
10
+/*
11
+	getGame()
12
+
13
+	Creates and returns an object whose class extends JosephusInterface.
14
+	This should be an object of a class you have created.
15
+
16
+	Example: If you made a class called "Circle", you might say, "return new Circle();".
17
+*/
18
+class Factory
19
+{
20
+	public:
21
+		static 	JosephusInterface * getGame();
22
+};
23
+//=======================================================================================

+ 54
- 0
cs235/lab02/Student_Code/JosephusInterface.h View File

@@ -0,0 +1,54 @@
1
+//YOU MAY NOT MODIFY THIS DOCUMENT
2
+#pragma once
3
+#include <iostream>
4
+#include <string>
5
+#include <vector>
6
+
7
+using namespace std;
8
+
9
+class JosephusInterface
10
+{
11
+public:
12
+	JosephusInterface(void){}
13
+	virtual ~JosephusInterface(void){}
14
+	/*
15
+		getNames
16
+
17
+		Returns a list of names in the order in which the people will be standing for the "game".
18
+		Although fewer people may be playing, you must return 20 names here. Do not provide
19
+		duplicate names.
20
+
21
+		For the sake of the test driver, this method must return the list of 20 names in the