adding cs235
This commit is contained in:
parent
8e52ce1982
commit
cf99ec6565
26
cs235/exam1/Makefile
Normal file
26
cs235/exam1/Makefile
Normal file
@ -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
Normal file
68
cs235/exam1/PolynomialListInterface.h
Normal file
@ -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
Normal file
84
cs235/exam1/PolynomialManagerInterface.h
Normal file
@ -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
Normal file
433
cs235/exam1/main.cpp
Normal file
@ -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
Normal file
42
cs235/exam1/node.cpp
Normal file
@ -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
Normal file
19
cs235/exam1/node.h
Normal file
@ -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
Normal file
438
cs235/exam1/polylist.cpp
Normal file
@ -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
Normal file
31
cs235/exam1/polylist.h
Normal file
@ -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
Normal file
107
cs235/exam1/polyman.cpp
Normal file
@ -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
Normal file
22
cs235/exam1/polyman.h
Normal file
@ -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
Normal file
31
cs235/final/Makefile
Normal file
@ -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
Normal file
43
cs235/final/RedBlackNodeInterface.h
Normal file
@ -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
Normal file
20
cs235/final/RedBlackTreeFactory.cpp
Normal file
@ -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
Normal file
23
cs235/final/RedBlackTreeFactory.h
Normal file
@ -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
Normal file
78
cs235/final/RedBlackTreeInterface.h
Normal file
@ -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.
|
||||
*/
|
||||
virtual void addPhrase(string words) = 0;
|
||||
|
||||
/*
|
||||
* Attempts to remove the given word from the tree
|
||||
*
|
||||
* Removal should be consistent with the instructions found in the exam instructions.
|
||||
*/
|
||||
virtual void remove(string word) = 0;
|
||||
|
||||
/*
|
||||
* The tree should print in the following format:
|
||||
*
|
||||
* Root (value & color)
|
||||
* Left subtree root (value & color)
|
||||
* Left left subtree root (value & color)
|
||||
* Left right subtree root (value & color)
|
||||
* Right subtree root (value & color)
|
||||
* Right left subtree root (value & color)
|
||||
* Right right subtree root (value & color)
|
||||
*
|
||||
* For example:
|
||||
* The tree:
|
||||
*
|
||||
* jumps(b)
|
||||
* brown(r) quick(r)
|
||||
* The(b) fox(b) over(b) the(b)
|
||||
* lazy(r)
|
||||
*
|
||||
* Will print:
|
||||
* jumps(b)
|
||||
* brown(r)
|
||||
* The(b)
|
||||
* fox(b)
|
||||
* quick(r)
|
||||
* over(b)
|
||||
* lazy(r);
|
||||
* the(b)
|
||||
*
|
||||
*/
|
||||
virtual string printTree() = 0;
|
||||
};
|
21
cs235/final/TwoThreeTreeFactory.cpp
Normal file
21
cs235/final/TwoThreeTreeFactory.cpp
Normal file
@ -0,0 +1,21 @@
|
||||
#include "TwoThreeTreeFactory.h"
|
||||
//You may add #include statements here
|
||||
|
||||
/*
|
||||
You will MODIFY THIS DOCUMENT.
|
||||
*/
|
||||
//=======================================================================================
|
||||
/*
|
||||
getTwoThreeTree()
|
||||
|
||||
Creates and returns an object whose class extends TwoTreeTreeInterface.
|
||||
This should be an object of a class you have created.
|
||||
|
||||
Example: If you made a class called "TwoThreeTree", you might say, "return new TwoThreeTree();".
|
||||
|
||||
This method should return NULL or 0 if you do not intent to do the extra credit
|
||||
*/
|
||||
TwoThreeTreeInterface * TwoThreeTreeFactory::getTwoThreeTree()
|
||||
{
|
||||
return 0;//Modify this line
|
||||
}
|
25
cs235/final/TwoThreeTreeFactory.h
Normal file
25
cs235/final/TwoThreeTreeFactory.h
Normal file
@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
#include "TwoThreeTreeInterface.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
/*
|
||||
WARNING: It is expressly forbidden to modify any part of this document, including its name
|
||||
*/
|
||||
//=======================================================================================
|
||||
/*
|
||||
getTwoThreeTree()
|
||||
|
||||
Creates and returns an object whose class extends TwoThreeTreeInterface.
|
||||
This should be an object of a class you have created.
|
||||
|
||||
Example: If you made a class called "TwoThreeTree", you might say, "return new TwoThreeTree();".
|
||||
|
||||
This method should return NULL or 0 if you do not intent to do the extra credit
|
||||
*/
|
||||
class TwoThreeTreeFactory
|
||||
{
|
||||
public:
|
||||
static TwoThreeTreeInterface * getTwoThreeTree();
|
||||
};
|
||||
//=======================================================================================
|
58
cs235/final/TwoThreeTreeInterface.h
Normal file
58
cs235/final/TwoThreeTreeInterface.h
Normal file
@ -0,0 +1,58 @@
|
||||
//YOU MAY NOT MODIFY THIS DOCUMENT
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class TwoThreeTreeInterface {
|
||||
public:
|
||||
TwoThreeTreeInterface() {}
|
||||
virtual ~TwoThreeTreeInterface() {}
|
||||
|
||||
//This class may use a node of your own creation.
|
||||
|
||||
/*
|
||||
* Attempts to add the given word to the tree
|
||||
*
|
||||
* Addition should be consistent with the instructions found in the exam instructions.
|
||||
*/
|
||||
virtual void add(string word) = 0;
|
||||
|
||||
/*
|
||||
* Attempts to add the given string of words to the tree
|
||||
*
|
||||
* A string of words consists of alphabetical characters, no punctuation marks,
|
||||
* and white space as the delimiter separating words. The string should be parsed and
|
||||
* each word should be added to the tree.
|
||||
*/
|
||||
virtual void addPhrase(string words) = 0;
|
||||
|
||||
/*
|
||||
* Attempts to remove the given word from the tree
|
||||
*
|
||||
* Removal should be consistent with the instructions found in the exam instructions.
|
||||
*/
|
||||
virtual void remove(string word) = 0;
|
||||
|
||||
/*
|
||||
* The tree should print in the following format:
|
||||
*
|
||||
* Root (value)
|
||||
* Left subtree root (value)
|
||||
* Left left subtree root (value)
|
||||
* Left middle subtree root (value)
|
||||
* Left right subtree root (value)
|
||||
* Middle subtree root (value)
|
||||
* Middle left subtree root (value)
|
||||
* Middle middle subtree root (value)
|
||||
* Middle right subtree root (value)
|
||||
*
|
||||
* Right subtree root (value)
|
||||
* Right left subtree root (value)
|
||||
* Right middle subtree root (value)
|
||||
* Right right subtree root (value)
|
||||
*
|
||||
*/
|
||||
virtual string printTree() = 0;
|
||||
};
|
19
cs235/final/br_node.cpp
Normal file
19
cs235/final/br_node.cpp
Normal file
@ -0,0 +1,19 @@
|
||||
#include "br_node.h"
|
||||
|
||||
br_node::br_node(string data): data(data), color(0), parent(NULL), left(NULL), right(NULL) {}
|
||||
|
||||
string br_node::getWord() {
|
||||
return data;
|
||||
}
|
||||
|
||||
int br_node::getColor() {
|
||||
return color;
|
||||
}
|
||||
|
||||
RedBlackNodeInterface* br_node::getLeftChild() {
|
||||
return left;
|
||||
}
|
||||
|
||||
RedBlackNodeInterface* br_node::getRightChild() {
|
||||
return right;
|
||||
}
|
21
cs235/final/br_node.h
Normal file
21
cs235/final/br_node.h
Normal file
@ -0,0 +1,21 @@
|
||||
#ifndef __BR_NODE_H__
|
||||
#define __BR_NODE_H__
|
||||
|
||||
#include "RedBlackNodeInterface.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
class br_node : public RedBlackNodeInterface {
|
||||
public:
|
||||
br_node(string data);
|
||||
string data;
|
||||
int color; //red = 0, black = 1
|
||||
br_node* parent;
|
||||
br_node* left;
|
||||
br_node* right;
|
||||
string getWord();
|
||||
int getColor();
|
||||
RedBlackNodeInterface * getLeftChild();
|
||||
RedBlackNodeInterface * getRightChild();
|
||||
};
|
||||
#endif
|
500
cs235/final/br_tree.cpp
Normal file
500
cs235/final/br_tree.cpp
Normal file
@ -0,0 +1,500 @@
|
||||
#include "br_tree.h"
|
||||
|
||||
br_tree::br_tree():root(NULL), h(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;
|
||||
}
|
||||
|
||||
bool br_tree::in_tree(string data) {
|
||||
bool found = false;
|
||||
br_node* current;
|
||||
br_node* parent;
|
||||
current = root;
|
||||
while(current) {
|
||||
if(current->data == data) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
else {
|
||||
parent = current;
|
||||
if(data > current->data) {
|
||||
current = current->right;
|
||||
}
|
||||
else if(data < current->data) {
|
||||
current = current->left;
|
||||
}
|
||||
}
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
RedBlackNodeInterface* br_tree::getRootNode() {
|
||||
return root;
|
||||
}
|
||||
|
||||
void br_tree::add(string word) {
|
||||
if(in_tree(word)) {
|
||||
return;
|
||||
}
|
||||
if(root == NULL) {
|
||||
root = new br_node(word);
|
||||
root->color = 1;
|
||||
return;
|
||||
}
|
||||
br_node* current;
|
||||
br_node* cur_parent;
|
||||
current = root;
|
||||
while(current) {
|
||||
cur_parent = current;
|
||||
if(word > current->data) {
|
||||
current = current->right;
|
||||
}
|
||||
else {
|
||||
current = current->left;
|
||||
}
|
||||
}
|
||||
if(word < cur_parent->data) {
|
||||
cur_parent->left = new br_node(word);
|
||||
cur_parent->left->parent = cur_parent;
|
||||
br_node* child_added = cur_parent->left;
|
||||
if(cur_parent->color == 0 && child_added->color == 0) {
|
||||
balance(cur_parent->left);
|
||||
}
|
||||
return;
|
||||
}
|
||||
else {
|
||||
cur_parent->right = new br_node(word);
|
||||
cur_parent->right->parent = cur_parent;
|
||||
br_node* child_added = cur_parent->right;
|
||||
if(cur_parent->color == 0 && child_added->color == 0) {
|
||||
balance(cur_parent->right);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void br_tree::balance(br_node* cur) {
|
||||
if((cur->data == root->data) || (cur->parent->data == root->data)) {
|
||||
return;
|
||||
}
|
||||
if((cur->parent->parent->right != NULL) && (cur->parent->parent->left != NULL)) {
|
||||
if((cur->parent->parent->left->color == 0) && (cur->parent->parent->right->color == 0)) {
|
||||
cur->parent->parent->left->color = 1;
|
||||
cur->parent->parent->right->color = 1;
|
||||
cur->parent->parent->color = 0;
|
||||
balance(cur->parent->parent);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if((cur->parent->color == 0) && (cur->color == 0)) {
|
||||
if((cur->parent->left != NULL) && (cur->parent->parent->left != NULL)) {
|
||||
if((cur->parent->left->data == cur->data) && (cur->parent->parent->left->data == cur->parent->data)) {
|
||||
//case III left
|
||||
br_node* temp = cur->parent->parent;
|
||||
if(cur->parent->right == NULL) {
|
||||
temp->left = cur->parent->right;
|
||||
}
|
||||
else {
|
||||
temp->left = cur->parent->right;
|
||||
cur->parent->right->parent = temp;
|
||||
}
|
||||
cur->parent->right = temp;
|
||||
if(temp->parent != NULL) {
|
||||
if(temp->parent->right->data == temp->data) {
|
||||
temp->parent->right = cur->parent;
|
||||
}
|
||||
else {
|
||||
temp->parent->left = cur->parent;
|
||||
}
|
||||
}
|
||||
else {
|
||||
root = cur->parent;
|
||||
}
|
||||
cur->parent->parent = temp->parent;
|
||||
temp->parent = cur->parent;
|
||||
cur->parent->color = 1;
|
||||
temp->color = 0;
|
||||
balance(cur->parent);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if((cur->parent->left != NULL) && (cur->parent->parent->right != NULL)) {
|
||||
if((cur->parent->left->data == cur->data) && (cur->parent->parent->right->data == cur->parent->data)){
|
||||
//case II right
|
||||
br_node* temp1 = cur->parent->parent;
|
||||
br_node* temp2 = cur->parent;
|
||||
if(cur->right == NULL) {
|
||||
temp2->left = cur->right;
|
||||
}
|
||||
else {
|
||||
temp2->left = cur->right;
|
||||
cur->right->parent = temp2;
|
||||
}
|
||||
cur->right = temp2;
|
||||
temp2->parent = cur;
|
||||
cur->parent = temp1;
|
||||
temp1->right = cur;
|
||||
balance(temp2);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if((cur->parent->right != NULL) && (cur->parent->parent->left != NULL)) {
|
||||
if((cur->parent->right->data == cur->data) && (cur->parent->parent->left->data == cur->parent->data)){
|
||||
//case II left
|
||||
br_node* temp1 = cur->parent->parent;
|
||||
br_node* temp2 = cur->parent;
|
||||
if(cur->left == NULL) {
|
||||
temp2->right = cur->left;
|
||||
}
|
||||
else {
|
||||
temp2->right = cur->left;
|
||||
cur->left->parent = temp2;
|
||||
}
|
||||
cur->left = temp2;
|
||||
temp2->parent = cur;
|
||||
cur->parent = temp1;
|
||||
temp1->left = cur;
|
||||
balance(temp2);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if((cur->parent->right != NULL) && (cur->parent->parent->right != NULL)) {
|
||||
if((cur->parent->right->data == cur->data) && (cur->parent->parent->right->data == cur->parent->data)) {
|
||||
//case III right
|
||||
br_node* temp = cur->parent->parent;
|
||||
if(cur->parent->left == NULL) {
|
||||
temp->right = cur->parent->left;
|
||||
}
|
||||
else {
|
||||
temp->right = cur->parent->left;
|
||||
cur->parent->left->parent = temp;
|
||||
}
|
||||
cur->parent->left = temp;
|
||||
if(temp->parent != NULL) {
|
||||
if(temp->parent->right->data == temp->data) {
|
||||
temp->parent->right = cur->parent;
|
||||
}
|
||||
else {
|
||||
temp->parent->left = cur->parent;
|
||||
}
|
||||
}
|
||||
else {
|
||||
root = cur->parent;
|
||||
}
|
||||
cur->parent->parent = temp->parent;
|
||||
temp->parent = cur->parent;
|
||||
cur->parent->color = 1;
|
||||
temp->color = 0;
|
||||
balance(cur->parent);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(root->color == 0) {
|
||||
root->color = 1;
|
||||
return;
|
||||
}
|
||||
balance(cur->parent);
|
||||
}
|
||||
|
||||
void br_tree::addPhrase(string words) {
|
||||
vector<string> parsed_string = parse_expression(words);
|
||||
for(unsigned int i = 0; i < parsed_string.size(); i++) {
|
||||
add(parsed_string[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void br_tree::remove(string word) {
|
||||
if(!(in_tree(word))) {
|
||||
return;
|
||||
}
|
||||
if(root == NULL) {
|
||||
return;
|
||||
}
|
||||
br_node* current;
|
||||
br_node* parent;
|
||||
current = root;
|
||||
while(current) {
|
||||
if(current->data == word) {
|
||||
break;
|
||||
}
|
||||
else {
|
||||
parent = current;
|
||||
if(word > current->data) {
|
||||
current = current->right;
|
||||
}
|
||||
else if(word < current->data) {
|
||||
current = current->left;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(current->color == 1) {
|
||||
h = true;
|
||||
}
|
||||
else {
|
||||
h = false;
|
||||
}
|
||||
root = del_leaf(root, word);
|
||||
}
|
||||
|
||||
br_node* br_tree::del_leaf(br_node* root, string data) {
|
||||
br_node* temp;
|
||||
if(root == NULL) {
|
||||
return root;
|
||||
}
|
||||
else {
|
||||
if(data < root->data) {
|
||||
root->left = del_leaf(root->left, data);
|
||||
if(h) {
|
||||
cout << "2" << endl;
|
||||
bal_del(root->left);
|
||||
}
|
||||
}
|
||||
else {
|
||||
if(data > root->data) {
|
||||
root->right = del_leaf(root->right, data);
|
||||
if(h) {
|
||||
cout << "3" << endl;
|
||||
cout << root->data << endl;
|
||||
bal_del(root->right);
|
||||
}
|
||||
}
|
||||
else {
|
||||
temp = root;
|
||||
if(temp->right == NULL) {
|
||||
root = temp->left;
|
||||
delete temp;
|
||||
}
|
||||
else {
|
||||
if(temp->left == NULL) {
|
||||
root = temp->right;
|
||||
delete temp;
|
||||
}
|
||||
else {
|
||||
temp->left = del(temp->left, temp);
|
||||
if(h) {
|
||||
cout << "4" << endl;
|
||||
if(h) {
|
||||
bal_del(temp);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return root;
|
||||
}
|
||||
|
||||
void br_tree::bal_del(br_node* x) {
|
||||
//cout << "bal_del(" << x->data << ")" << endl;
|
||||
if(x == NULL) {
|
||||
return;
|
||||
}
|
||||
while(x != root && x->color == 1) {
|
||||
if(x == x->parent->left) {
|
||||
br_node* w = x->parent->right;
|
||||
if(w == NULL) {
|
||||
return;
|
||||
}
|
||||
if(w->right == NULL || w->left == NULL) {
|
||||
return;
|
||||
}
|
||||
if(w != NULL && w->right != NULL && w->left != NULL) {
|
||||
if(w->color == 0) {
|
||||
cout << "case 1 first" << endl;
|
||||
w->color = 1; //Case 1
|
||||
x->parent->color = 0; //Case 1
|
||||
left_rot(x->parent); //Case 1
|
||||
w = x->parent->right; //Case 1
|
||||
}
|
||||
else if(w->left->color == 1 && w->right->color == 1) {
|
||||
cout << "case 2 first" << endl;
|
||||
w->color = 1; //Case 2
|
||||
x = x->parent; //Case 2
|
||||
}
|
||||
else {
|
||||
if(w->right->color == 1) {
|
||||
cout << "case 3 first" << endl;
|
||||
w->left->color = 1; //Case 3
|
||||
w->color = 0; //Case 3
|
||||
right_rot(w); //Case 3
|
||||
w = x->parent->right; //Case 3
|
||||
}
|
||||
cout << "case 4 first" << endl;
|
||||
w->color = x->parent->color; //Case 4
|
||||
x->parent->color = 1; //Case 4
|
||||
w->right->color = 1; //Case 4
|
||||
left_rot(x->parent);
|
||||
x = root;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
br_node* w = x->parent->left;
|
||||
if(w == NULL) {
|
||||
return;
|
||||
}
|
||||
if(w->right == NULL || w->left == NULL) {
|
||||
return;
|
||||
}
|
||||
if(w->color == 0) {
|
||||
cout << "case 1 second" << endl;
|
||||
w->color = 1; //Case 1
|
||||
x->parent->color = 0; //Case 1
|
||||
right_rot(x->parent); //Case 1
|
||||
w = x->parent->left; //Case 1
|
||||
}
|
||||
else if(w->right->color == 1 && w->left->color == 1) {
|
||||
cout << "case 2 second" << endl;
|
||||
w->color = 0; //Case 2
|
||||
x = x->parent; //Case 2
|
||||
}
|
||||
else {
|
||||
if(w->left->color == 1) {
|
||||
cout << "case 3 second" << endl;
|
||||
w->right->color = 1; //Case 3
|
||||
w->color = 0; //Case 3
|
||||
left_rot(w); //Case 3
|
||||
w = x->parent->left; //Case 3
|
||||
}
|
||||
cout << "case 4 second" << endl;
|
||||
w->color = x->parent->color; //Case 4
|
||||
x->parent->color = 1; //Case 4
|
||||
w->left->color = 1; //Case 4
|
||||
right_rot(x->parent);
|
||||
x = root;
|
||||
}
|
||||
}
|
||||
}
|
||||
x->color = 1;
|
||||
}
|
||||
|
||||
br_node* br_tree::del(br_node* succ, br_node* n) {
|
||||
br_node* temp = succ;
|
||||
if(succ->right != NULL) {
|
||||
succ->right = del(succ->right, n);
|
||||
}
|
||||
else {
|
||||
br_node* current;
|
||||
br_node* parent;
|
||||
current = root;
|
||||
while(current) {
|
||||
if(current->data == succ->data) {
|
||||
break;
|
||||
}
|
||||
else {
|
||||
parent = current;
|
||||
if(succ->data > current->data) {
|
||||
current = current->right;
|
||||
}
|
||||
else if(succ->data < current->data) {
|
||||
current = current->left;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(root->left->data == succ->data) {
|
||||
root->data = succ->data;
|
||||
n->right = root->right;
|
||||
succ = succ->left;
|
||||
delete temp;
|
||||
return succ;
|
||||
}
|
||||
else if(root->right->data == succ->data) {
|
||||
root->data = succ->data;
|
||||
n->right = root->right;
|
||||
succ = succ->right;
|
||||
delete temp;
|
||||
return succ;
|
||||
}
|
||||
temp = succ;
|
||||
n->data = succ->data;
|
||||
succ = succ->left;
|
||||
delete temp;
|
||||
}
|
||||
return succ;
|
||||
}
|
||||
|
||||
void br_tree::right_rot(br_node* x) {
|
||||
br_node* y = x->left;
|
||||
x->left = y->right;
|
||||
if(y->right != NULL) {
|
||||
y->right->parent = x;
|
||||
}
|
||||
y->parent = x->parent;
|
||||
if(x->parent == NULL) {
|
||||
root = y;
|
||||
}
|
||||
else if(x == x->parent->right) {
|
||||
x->parent->right = y;
|
||||
}
|
||||
else {
|
||||
x->parent->left = y;
|
||||
}
|
||||
y->right = x;
|
||||
x->parent = y;
|
||||
}
|
||||
|
||||
void br_tree::left_rot(br_node* x) {
|
||||
br_node* y = x->right;
|
||||
x->right = y->left;
|
||||
if(y->left != NULL) {
|
||||
y->left->parent = x;
|
||||
}
|
||||
y->parent = x->parent;
|
||||
if(x->parent == NULL) {
|
||||
root = y;
|
||||
}
|
||||
else if(x == x->parent->left) {
|
||||
x->parent->left = y;
|
||||
}
|
||||
else {
|
||||
x->parent->right = y;
|
||||
}
|
||||
y->left = x;
|
||||
x->parent = y;
|
||||
}
|
||||
|
||||
void br_tree::printer(br_node* node, int a) {
|
||||
a++;
|
||||
if(node == NULL) {
|
||||
return;
|
||||
}
|
||||
printer(node->right, a);
|
||||
for(int i = 0; i <= a; i++) {
|
||||
cout << "\t";
|
||||
}
|
||||
printf("%s %d", node->data.c_str(), node->color);
|
||||
cout << endl << endl;
|
||||
printer(node->left, a);
|
||||
}
|
||||
|
||||
string br_tree::printTree() {
|
||||
string tree;
|
||||
if(root == NULL) {
|
||||
return "";
|
||||
}
|
||||
cout << root->data << endl;
|
||||
cout << endl;
|
||||
printer(root, 0);
|
||||
cout << endl;
|
||||
return tree;
|
||||
}
|
34
cs235/final/br_tree.h
Normal file
34
cs235/final/br_tree.h
Normal file
@ -0,0 +1,34 @@
|
||||
#ifndef __BR_TREE_H__
|
||||
#define __BR_TREE_H__
|
||||
|
||||
#include "br_node.h"
|
||||
#include "RedBlackTreeInterface.h"
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class br_tree : public RedBlackTreeInterface {
|
||||
public:
|
||||
br_tree();
|
||||
br_node* root;
|
||||
bool h;
|
||||
RedBlackNodeInterface * getRootNode();
|
||||
bool in_tree(string data);
|
||||
void add(string word);
|
||||
void addPhrase(string words);
|
||||
void remove(string word);
|
||||
void balance(br_node* cur);
|
||||
void transfer(br_node* temp1, br_node* temp2);
|
||||
void bal_del(br_node* x);
|
||||
br_node* del_leaf(br_node* root, string data);
|
||||
void right_rot(br_node* x);
|
||||
void left_rot(br_node* x);
|
||||
void printer(br_node* node, int a);
|
||||
br_node* del(br_node* succ, br_node* n);
|
||||
br_node* min(br_node* temp);
|
||||
string printTree();
|
||||
};
|
||||
#endif
|
235
cs235/final/main.cpp
Normal file
235
cs235/final/main.cpp
Normal file
@ -0,0 +1,235 @@
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
#include "RedBlackTreeFactory.h"
|
||||
#include "TwoThreeTreeFactory.h"
|
||||
#include "br_tree.h"
|
||||
|
||||
/* This main is provided to help you test your code. Be aware that the TAs will use a separate
|
||||
* test driver that will only test your red black tree code. You may modify this code if you wish,
|
||||
* but do not try to modify the main to get your own code to work.
|
||||
*/
|
||||
|
||||
|
||||
using namespace std;
|
||||
int getOption(int max, int min);
|
||||
string getString();
|
||||
void exitPause();
|
||||
|
||||
RedBlackTreeInterface * redBlackTree;
|
||||
TwoThreeTreeInterface * twoThreeTree;
|
||||
|
||||
int main()
|
||||
{
|
||||
srand(time(NULL));
|
||||
br_tree b;
|
||||
redBlackTree = RedBlackTreeFactory::getRedBlackTree();
|
||||
|
||||
if(redBlackTree == NULL) {
|
||||
cout << endl;
|
||||
cout << "Factory getRedBlackTree() returned NULL!\nExiting Test Driver (press enter to exit)" << endl;
|
||||
|
||||
exitPause();
|
||||
return 0;
|
||||
}
|
||||
|
||||
// twoThreeTree = TwoThreeTreeFactory::getTwoThreeTree();
|
||||
|
||||
cout << "Final Exam Beginning" << endl;
|
||||
while(1)
|
||||
{
|
||||
int choice = 0;
|
||||
if(twoThreeTree == NULL) {
|
||||
cout<< "Please select an option:"
|
||||
<< "\n1. Add a single word to the tree"
|
||||
<< "\n2. Add a list of words to the tree"
|
||||
<< "\n3. Remove a single word from the tree"
|
||||
<< "\n4. Print the tree"
|
||||
<< "\n5. Quit Program" << endl;
|
||||
choice = getOption(1,5);
|
||||
}
|
||||
else {
|
||||
cout<< "Please select an option:"
|
||||
<< "\n1. Add a single word to the red black tree"
|
||||
<< "\n2. Add a list of words to the red black tree"
|
||||
<< "\n3. Remove a single word from the red black tree"
|
||||
<< "\n4. Print the red black tree"
|
||||
<< "\n5. Add a single word to the two three tree"
|
||||
<< "\n6. Add a list of words to the two three tree"
|
||||
<< "\n7. Remove a single word from the two three tree"
|
||||
<< "\n8. Print the two three tree"
|
||||
<< "\n9. Quit Program" << endl;
|
||||
choice = getOption(1,9);
|
||||
}
|
||||
if(choice == 1)
|
||||
{
|
||||
//Insert the string that the user passes to the program into the tree
|
||||
for(int i = 0; i < 50000; i++) {
|
||||
stringstream s;
|
||||
int rand_n = rand() % 50000;
|
||||
s << rand_n;
|
||||
b.add(s.str());
|
||||
}
|
||||
cout << b.root << endl;
|
||||
//cout<< "Enter the word to add to the tree (alphabetical characters, no punctuation marks, no spaces)."<<endl;
|
||||
//string insertToTree;
|
||||
//insertToTree = getString();
|
||||
//redBlackTree->add(insertToTree);
|
||||
}
|
||||
else if(choice == 2)
|
||||
{
|
||||
//Insert the string that the user passes to the program into the tree
|
||||
cout<< "Enter the string to add to the tree."
|
||||
<< "\n(alphabetical characters, no punctuation marks, and with white space as the delimiter separating words)" << endl;
|
||||
string insertToTree;
|
||||
insertToTree = getString();
|
||||
redBlackTree->addPhrase(insertToTree);
|
||||
}
|
||||
else if(choice == 3)
|
||||
{
|
||||
cout << "here" << endl;
|
||||
while(b.root) {
|
||||
cout << "ah" << endl;
|
||||
stringstream s;
|
||||
int rand_n = rand() % 50000;
|
||||
s << rand_n;
|
||||
b.remove(s.str());
|
||||
}
|
||||
//Remove the string that the user passes to the program into the tree
|
||||
//cout << "Enter the word to remove from the tree (alphabetical characters, no punctuation marks, no spaces)."<<endl;
|
||||
//string removeFromTree;
|
||||
//removeFromTree = getString();
|
||||
//redBlackTree->remove(removeFromTree);
|
||||
}
|
||||
else if(choice == 4)
|
||||
{
|
||||
cout << b.printTree();
|
||||
//cout << "Your tree:" << endl;
|
||||
//cout << "\t" << redBlackTree->printTree() << endl;
|
||||
}
|
||||
else if(choice == 5 && twoThreeTree == NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
else if(choice == 5 && twoThreeTree != NULL)
|
||||
{
|
||||
//Insert the string that the user passes to the program into the tree
|
||||
cout<< "Enter the word to add to the tree (alphabetical characters, no punctuation marks, no spaces)."<<endl;
|
||||
string insertToTree;
|
||||
insertToTree = getString();
|
||||
twoThreeTree->add(insertToTree);
|
||||
}
|
||||
else if(choice == 6)
|
||||
{
|
||||
//Insert the string that the user passes to the program into the tree
|
||||
cout<< "Enter the string to add to the tree."
|
||||
<< "\n(alphabetical characters, no punctuation marks, and with white space as the delimiter separating words)" << endl;
|
||||
string insertToTree;
|
||||
insertToTree = getString();
|
||||
twoThreeTree->addPhrase(insertToTree);
|
||||
}
|
||||
else if(choice == 7)
|
||||
{
|
||||
//Remove the string that the user passes to the program into the tree
|
||||
cout << "Enter the word to remove from the tree (alphabetical characters, no punctuation marks, no spaces)."<<endl;
|
||||
string removeFromTree;
|
||||
removeFromTree = getString();
|
||||
twoThreeTree->remove(removeFromTree);
|
||||
}
|
||||
else if(choice == 8)
|
||||
{
|
||||
cout << "Your tree:" << endl;
|
||||
cout << "\t" << twoThreeTree->printTree() << endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
cout << "Thanks for Playing!" << endl;
|
||||
|
||||
exitPause();
|
||||
return 0;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------------
|
||||
int getOption(int min, int max)
|
||||
{
|
||||
int input = 0;
|
||||
bool done = false;
|
||||
while(!done)
|
||||
{
|
||||
input = 0;
|
||||
cin >> input;
|
||||
cin.ignore(1000,'\n');
|
||||
if(cin.fail())
|
||||
{
|
||||
cin.clear();
|
||||
cin.ignore(1000,'\n');
|
||||
cout << "Error: Invalid" << endl;
|
||||
}
|
||||
else if(input < min || input > max)
|
||||
{
|
||||
cout << "Error: Invalid number" << endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
done = true;
|
||||
}
|
||||
}
|
||||
return input;
|
||||
}
|
||||
|
||||
string getString()
|
||||
{
|
||||
string input = "";
|
||||
bool done = false;
|
||||
while(!done)
|
||||
{
|
||||
input = "";
|
||||
getline(cin, input);
|
||||
if(cin.fail())
|
||||
{
|
||||
cin.clear();
|
||||
cin.ignore(1000,'\n');
|
||||
cout << "Error: Invalid name" << endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
done = true;
|
||||
}
|
||||
}
|
||||
return input;
|
||||
}
|
||||
|
||||
string getWord()
|
||||
{
|
||||
string input = "";
|
||||
bool done = false;
|
||||
while(!done)
|
||||
{
|
||||
input = "";
|
||||
getline(cin, input);
|
||||
if(cin.fail())
|
||||
{
|
||||
cin.clear();
|
||||
cin.ignore(1000,'\n');
|
||||
cout << "Error: Invalid name" << endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
done = true;
|
||||
}
|
||||
}
|
||||
return input;
|
||||
}
|
||||
|
||||
void exitPause() {
|
||||
#ifdef ON_WINDOWS
|
||||
system("pause");
|
||||
#else
|
||||
|
||||
#endif
|
||||
}
|
7
cs235/final/test.cpp
Normal file
7
cs235/final/test.cpp
Normal file
@ -0,0 +1,7 @@
|
||||
#include "br_tree.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
br_tree b;
|
||||
}
|
BIN
cs235/lab02/Lab 2 README.pdf
Executable file
BIN
cs235/lab02/Lab 2 README.pdf
Executable file
Binary file not shown.
33
cs235/lab02/Run Test Driver.bat
Executable file
33
cs235/lab02/Run Test Driver.bat
Executable file
@ -0,0 +1,33 @@
|
||||
TITLE TA Test Driver
|
||||
|
||||
@echo off
|
||||
|
||||
del Student_code\circle.cpp~
|
||||
|
||||
rem setting up environment variables needed for VS compiler
|
||||
call "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86_amd64
|
||||
|
||||
rem compiling the .obj
|
||||
cl /EHsc /Fedont_run_me.exe Student_Code\*.cpp ignoreme.lib
|
||||
|
||||
rem if compilation failed, goto error section
|
||||
if ERRORLEVEL 1 goto error
|
||||
|
||||
rem cleanup unnecessary generated files
|
||||
del *.obj
|
||||
|
||||
rem run the driver in another window
|
||||
.\dont_run_me.exe
|
||||
del dont_run_me.exe
|
||||
pause
|
||||
|
||||
exit
|
||||
|
||||
:error
|
||||
|
||||
rem remove any generated files
|
||||
del *.obj
|
||||
|
||||
echo ----------ERROR--------
|
||||
pause
|
||||
exit
|
20
cs235/lab02/Student_Code/Factory.cpp
Normal file
20
cs235/lab02/Student_Code/Factory.cpp
Normal file
@ -0,0 +1,20 @@
|
||||
#include "Factory.h"
|
||||
#include "circle.h"
|
||||
//You may add #include statments here
|
||||
|
||||
/*
|
||||
You will MODIFY THIS DOCUMENT.
|
||||
*/
|
||||
//=======================================================================================
|
||||
/*
|
||||
getGame()
|
||||
|
||||
Creates and returns an object whose class extends JosephusInterface.
|
||||
This should be an object of a class you have created.
|
||||
|
||||
Example: If you made a class called "Circle", you might say, "return new Circle();".
|
||||
*/
|
||||
JosephusInterface * Factory::getGame()
|
||||
{
|
||||
return new circle();
|
||||
}
|
23
cs235/lab02/Student_Code/Factory.h
Normal file
23
cs235/lab02/Student_Code/Factory.h
Normal file
@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
#include "JosephusInterface.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
/*
|
||||
WARNING: It is expressly forbidden to modify any part of this document, including its name
|
||||
*/
|
||||
//=======================================================================================
|
||||
/*
|
||||
getGame()
|
||||
|
||||
Creates and returns an object whose class extends JosephusInterface.
|
||||
This should be an object of a class you have created.
|
||||
|
||||
Example: If you made a class called "Circle", you might say, "return new Circle();".
|
||||
*/
|
||||
class Factory
|
||||
{
|
||||
public:
|
||||
static JosephusInterface * getGame();
|
||||
};
|
||||
//=======================================================================================
|
54
cs235/lab02/Student_Code/JosephusInterface.h
Normal file
54
cs235/lab02/Student_Code/JosephusInterface.h
Normal file
@ -0,0 +1,54 @@
|
||||
//YOU MAY NOT MODIFY THIS DOCUMENT
|
||||
#pragma once
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class JosephusInterface
|
||||
{
|
||||
public:
|
||||
JosephusInterface(void){}
|
||||
virtual ~JosephusInterface(void){}
|
||||
/*
|
||||
getNames
|
||||
|
||||
Returns a list of names in the order in which the people will be standing for the "game".
|
||||
Although fewer people may be playing, you must return 20 names here. Do not provide
|
||||
duplicate names.
|
||||
|
||||
For the sake of the test driver, this method must return the list of 20 names in the
|
||||
same order every time it is called, and this list of 20 names in this order must be used
|
||||
to play the "game".
|
||||
|
||||
This method will be called repeatedly.
|
||||
*/
|
||||
virtual vector<string> getNames() = 0;
|
||||
/*
|
||||
playGame
|
||||
|
||||
Plays a "game" with the first n people from the list (above) counting forward every m. An
|
||||
explanation for how the "game" works can be found in the exam specs.
|
||||
|
||||
This method should return a list of names in the order in which the lot fell upon them (including
|
||||
the survivor, who should be last). If n is not between 10 and 20 or if m is non-positive,
|
||||
return an empty vector.
|
||||
|
||||
This method will be called repeatedly.
|
||||
*/
|
||||
virtual vector<string> playGame(int n, int m) = 0;
|
||||
/*
|
||||
reportSafeIndex
|
||||
|
||||
Returns the "safe index", the last index/location in the circle that will be chosen when
|
||||
the "game" is played. The point of this method is to permit someone to cheat the system
|
||||
by finding the safe location ahead of time.
|
||||
|
||||
If n is not between 10 and 20 or if m is non-positive, return -1.
|
||||
|
||||
This method may be called repeatedly.
|
||||
*/
|
||||
virtual int reportSafeIndex(int n, int m) = 0;
|
||||
};
|
||||
|
25
cs235/lab02/Student_Code/Makefile
Normal file
25
cs235/lab02/Student_Code/Makefile
Normal file
@ -0,0 +1,25 @@
|
||||
CXXFLAGS= -Wall -g -std=c++0x
|
||||
OBJECTS=main.o Factory.o circle.o
|
||||
EXE=main
|
||||
|
||||
all: $(EXE)
|
||||
|
||||
|
||||
$(EXE): main.o
|
||||
$(CXX) $(CXXFLAGS) $(OBJECTS) -o $@
|
||||
main.o: main.cpp Factory.o circle.o
|
||||
Factory.o: Factory.cpp Factory.h
|
||||
circle.o: circle.cpp circle.h
|
||||
|
||||
run: $(EXE)
|
||||
@./$(EXE)
|
||||
|
||||
clean:
|
||||
@rm -vf *.o
|
||||
@rm -vf $(EXE)
|
||||
|
||||
debug: $(EXE)
|
||||
gdb ./$(EXE)
|
||||
|
||||
valgrind: $(EXE)
|
||||
valgrind --tool=memcheck --leak-check=yes ./$(EXE)
|
88
cs235/lab02/Student_Code/circle.cpp
Normal file
88
cs235/lab02/Student_Code/circle.cpp
Normal file
@ -0,0 +1,88 @@
|
||||
#include <vector>
|
||||
#include "circle.h"
|
||||
|
||||
ostream & operator<<(ostream & os, vector<string> strings) {
|
||||
for(unsigned int i = 0; i < strings.size(); i++)
|
||||
os << strings[i] << ", ";
|
||||
os << endl;
|
||||
return os;
|
||||
}
|
||||
|
||||
vector<string> circle::getNames() {
|
||||
vector<string> names;
|
||||
names.push_back("Josephus");
|
||||
names.push_back("A");
|
||||
names.push_back("B");
|
||||
names.push_back("C");
|
||||
names.push_back("D");
|
||||
names.push_back("E");
|
||||
names.push_back("F");
|
||||
names.push_back("G");
|
||||
names.push_back("H");
|
||||
names.push_back("I");
|
||||
names.push_back("J");
|
||||
names.push_back("K");
|
||||
names.push_back("L");
|
||||
names.push_back("M");
|
||||
names.push_back("N");
|
||||
names.push_back("O");
|
||||
names.push_back("P");
|
||||
names.push_back("Q");
|
||||
names.push_back("R");
|
||||
names.push_back("S");
|
||||
return names;
|
||||
}
|
||||
vector<string> circle::playGame(int n, int m) {
|
||||
vector<string> names = getNames();
|
||||
vector<string> temp;
|
||||
if(n < 10 || n > 20){
|
||||
names.clear();
|
||||
return names;
|
||||
}
|
||||
if(m < 1){
|
||||
names.clear();
|
||||
return names;
|
||||
}
|
||||
for(int i = 0; i < 20 - n; i++) {
|
||||
names.pop_back();
|
||||
}
|
||||
unsigned int count = 0;
|
||||
for(int i = 0; i < n; i++) {
|
||||
int hits = 0;
|
||||
while(hits < m) {
|
||||
if(names[count] == ""){
|
||||
}
|
||||
else {
|
||||
hits++;
|
||||
if(hits == m) {
|
||||
temp.push_back(names[count]);
|
||||
names[count] = "";
|
||||
hits = 100;
|
||||
}
|
||||
}
|
||||
count++;
|
||||
if(count+1 > names.size()) {
|
||||
count = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
return temp;
|
||||
}
|
||||
|
||||
int circle::reportSafeIndex(int n, int m) {
|
||||
if(m < 1) {
|
||||
return -1;
|
||||
}
|
||||
else if (n < 10 || n > 20){
|
||||
return -1;
|
||||
}
|
||||
vector<string> names = getNames();
|
||||
vector<string> kill_list = playGame(n, m);
|
||||
string safe_person_name = kill_list[kill_list.size() - 1];
|
||||
for(unsigned int i = 0; i < names.size(); i++) {
|
||||
if(safe_person_name == names[i]) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
19
cs235/lab02/Student_Code/circle.h
Normal file
19
cs235/lab02/Student_Code/circle.h
Normal file
@ -0,0 +1,19 @@
|
||||
#ifndef __CIRCLE_H__
|
||||
#define __CIRCLE_H__
|
||||
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
#include "JosephusInterface.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
class circle : public JosephusInterface
|
||||
{
|
||||
public:
|
||||
circle(){}
|
||||
vector<string> getNames();
|
||||
vector<string> playGame(int n, int m);
|
||||
int reportSafeIndex(int n, int m);
|
||||
};
|
||||
|
||||
#endif
|
1974
cs235/lab02/Student_Code/main.cpp
Normal file
1974
cs235/lab02/Student_Code/main.cpp
Normal file
File diff suppressed because it is too large
Load Diff
661
cs235/lab03/DONT_DELETE.supp
Normal file
661
cs235/lab03/DONT_DELETE.supp
Normal file
@ -0,0 +1,661 @@
|
||||
{
|
||||
Dynamic Linker Bug
|
||||
Memcheck:Cond
|
||||
fun:_dl_relocate_object
|
||||
fun:dl_main
|
||||
fun:_dl_sysdep_start
|
||||
fun:_dl_start
|
||||
obj:/lib/ld-2.6.so
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:strlen
|
||||
fun:_ZNSsC1EPKcRKSaIcE
|
||||
fun:_Z11generateKeyv
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_Z12CompareListsRK10LinkedListRSt4listISsSaISsEE
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_Z12CompareListsRK10LinkedListRSt4listISsSaISsEE
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_Z12CompareListsRK10LinkedListRSt4listISsSaISsEE
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_Z12CompareListsRK10LinkedListRSt4listISsSaISsEE
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_Z12CompareListsRK10LinkedListRSt4listISsSaISsEE
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZSteqIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_ZNK10LinkedList4FindERKSsP6LLNode
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZSteqIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_ZNK10LinkedList4FindERKSsP6LLNode
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:strlen
|
||||
fun:_ZNSsC1EPKcRKSaIcE
|
||||
fun:_Z11generateKeyv
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZSteqIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_ZNK10LinkedList4FindERKSsP6LLNode
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZSteqIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_ZSt6__findISt14_List_iteratorISsESsET_S2_S2_RKT0_St18input_iterator_tag
|
||||
fun:_ZSt4findISt14_List_iteratorISsESsET_S2_S2_RKT0_
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZSteqIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_ZNK10LinkedList4FindERKSsP6LLNode
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:strlen
|
||||
fun:_ZNSsC1EPKcRKSaIcE
|
||||
fun:_Z11generateKeyv
|
||||
fun:_Z7TestBSTb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_Z7TestBSTb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZSteqIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZSteqIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_Z7TestBSTb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:strlen
|
||||
fun:_ZNSsC1EPKcRKSaIcE
|
||||
fun:_Z11generateKeyv
|
||||
fun:_Z7TestBSTb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:strlen
|
||||
fun:_ZNSsC1EPKcRKSaIcE
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStltIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStltIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:strlen
|
||||
fun:_ZNSsC1EPKcRKSaIcE
|
||||
fun:_Z11generateKeyv
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_Z12CompareListsRK10LinkedListRSt4listISsSaISsEE
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_Z12CompareListsRK10LinkedListRSt4listISsSaISsEE
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_Z12CompareListsRK10LinkedListRSt4listISsSaISsEE
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_Z12CompareListsRK10LinkedListRSt4listISsSaISsEE
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZNK10LinkedList4FindERKSsP6LLNode
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZNK10LinkedList4FindERKSsP6LLNode
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:strlen
|
||||
fun:_ZNSsC1EPKcRKSaIcE
|
||||
fun:_Z11generateKeyv
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZSteqIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZNK10LinkedList4FindERKSsP6LLNode
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:strlen
|
||||
fun:_ZNSsC1EPKcRKSaIcE
|
||||
fun:_Z11generateKeyv
|
||||
fun:_Z7TestBSTb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_Z7TestBSTb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_Z7TestBSTb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:strlen
|
||||
fun:_ZNSsC1EPKcRKSaIcE
|
||||
fun:_Z11generateKeyv
|
||||
fun:_Z7TestBSTb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:strlen
|
||||
fun:_ZNSsC1EPKcRKSaIcE
|
||||
fun:_Z11generateKeyv
|
||||
fun:_Z7TestBSTb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStltIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:strlen
|
||||
fun:_ZNSsC1EPKcRKSaIcE
|
||||
fun:_Z11generateKeyv
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_Z12CompareListsRK10LinkedListRSt4listISsSaISsEE
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_Z12CompareListsRK10LinkedListRSt4listISsSaISsEE
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_Z12CompareListsRK10LinkedListRSt4listISsSaISsEE
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_Z12CompareListsRK10LinkedListRSt4listISsSaISsEE
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_Z12CompareListsRK10LinkedListRSt4listISsSaISsEE
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZSteqIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZSteqIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:strlen
|
||||
fun:_ZNSsC1EPKcRKSaIcE
|
||||
fun:_Z11generateKeyv
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZSteqIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZSteqIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_ZSt6__findISt14_List_iteratorISsESsET_S2_S2_RKT0_St18input_iterator_tag
|
||||
fun:_ZSt4findISt14_List_iteratorISsESsET_S2_S2_RKT0_
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZSteqIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:strlen
|
||||
fun:_ZNSsC1EPKcRKSaIcE
|
||||
fun:_Z11generateKeyv
|
||||
fun:_Z7TestBSTb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:strlen
|
||||
fun:_ZNSsC1EPKcRKSaIcE
|
||||
fun:_Z11generateKeyv
|
||||
fun:_Z7TestBSTb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:strlen
|
||||
fun:_ZNSsC1EPKcRKSaIcE
|
||||
fun:_Z11generateKeyv
|
||||
fun:_Z7TestBSTb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:bcmp
|
||||
fun:_ZNSt11char_traitsIcE7compareEPKcS2_j
|
||||
fun:_ZSteqIcEN9__gnu_cxx11__enable_ifIXsrSt9__is_charIT_E7__valueEbE6__typeERKSbIS3_St11char_traitsIS3_ESaIS3_EESC_
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_ZN3tut12_GLOBAL__N_113ensure_equalsISsSsEEvPKcRKT0_RKT_
|
||||
fun:_ZN7BSTTest7initBSTER3BST
|
||||
fun:_ZN3tut11test_objectI7BSTTestE4testILi2EEEvv
|
||||
fun:_ZN3tut10test_groupI7BSTTestLi50EE13run_test_seh_EMNS_11test_objectIS1_EEFvvERNS2_11safe_holderIS4_EERSs
|
||||
fun:_ZN3tut10test_groupI7BSTTestLi50EE9run_test_ERKSt17_Rb_tree_iteratorISt4pairIKiMNS_11test_objectIS1_EEFvvEEERNS2_11safe_holderIS7_EE
|
||||
fun:_ZN3tut10test_groupI7BSTTestLi50EE8run_nextEv
|
||||
fun:_ZNK3tut11test_runner23run_all_tests_in_group_ESt23_Rb_tree_const_iteratorISt4pairIKSsPNS_10group_baseEEE
|
||||
fun:_ZNK3tut11test_runner9run_testsEv
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:bcmp
|
||||
fun:_ZNSt11char_traitsIcE7compareEPKcS2_j
|
||||
fun:_ZSteqIcEN9__gnu_cxx11__enable_ifIXsrSt9__is_charIT_E7__valueEbE6__typeERKSbIS3_St11char_traitsIS3_ESaIS3_EESC_
|
||||
fun:_ZNK10LinkedList4FindERKSsP6LLNode
|
||||
fun:_ZN3tut11test_objectI14LinkedListTestE4testILi8EEEvv
|
||||
fun:_ZN3tut10test_groupI14LinkedListTestLi50EE13run_test_seh_EMNS_11test_objectIS1_EEFvvERNS2_11safe_holderIS4_EERSs
|
||||
fun:_ZN3tut10test_groupI14LinkedListTestLi50EE9run_test_ERKSt17_Rb_tree_iteratorISt4pairIKiMNS_11test_objectIS1_EEFvvEEERNS2_11safe_holderIS7_EE
|
||||
fun:_ZN3tut10test_groupI14LinkedListTestLi50EE8run_nextEv
|
||||
fun:_ZNK3tut11test_runner23run_all_tests_in_group_ESt23_Rb_tree_const_iteratorISt4pairIKSsPNS_10group_baseEEE
|
||||
fun:_ZNK3tut11test_runner9run_testsEv
|
||||
fun:_Z3runRKSsi
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:bcmp
|
||||
fun:_ZNSt11char_traitsIcE7compareEPKcS2_j
|
||||
fun:_ZSteqIcEN9__gnu_cxx11__enable_ifIXsrSt9__is_charIT_E7__valueEbE6__typeERKSbIS3_St11char_traitsIS3_ESaIS3_EESC_
|
||||
fun:_ZSt6__findISt14_List_iteratorISsESsET_S2_S2_RKT0_St18input_iterator_tag
|
||||
fun:_ZSt4findISt14_List_iteratorISsESsET_S2_S2_RKT0_
|
||||
fun:_ZN3tut11test_objectI14LinkedListTestE4testILi9EEEvv
|
||||
fun:_ZN3tut10test_groupI14LinkedListTestLi50EE13run_test_seh_EMNS_11test_objectIS1_EEFvvERNS2_11safe_holderIS4_EERSs
|
||||
fun:_ZN3tut10test_groupI14LinkedListTestLi50EE9run_test_ERKSt17_Rb_tree_iteratorISt4pairIKiMNS_11test_objectIS1_EEFvvEEERNS2_11safe_holderIS7_EE
|
||||
fun:_ZN3tut10test_groupI14LinkedListTestLi50EE8run_nextEv
|
||||
fun:_ZNK3tut11test_runner23run_all_tests_in_group_ESt23_Rb_tree_const_iteratorISt4pairIKSsPNS_10group_baseEEE
|
||||
fun:_ZNK3tut11test_runner9run_testsEv
|
||||
fun:_Z3runRKSsi
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:bcmp
|
||||
fun:_ZNSt11char_traitsIcE7compareEPKcS2_j
|
||||
fun:_ZSteqIcEN9__gnu_cxx11__enable_ifIXsrSt9__is_charIT_E7__valueEbE6__typeERKSbIS3_St11char_traitsIS3_ESaIS3_EESC_
|
||||
fun:_ZN3tut11test_objectI14LinkedListTestE4testILi9EEEvv
|
||||
fun:_ZN3tut10test_groupI14LinkedListTestLi50EE13run_test_seh_EMNS_11test_objectIS1_EEFvvERNS2_11safe_holderIS4_EERSs
|
||||
fun:_ZN3tut10test_groupI14LinkedListTestLi50EE9run_test_ERKSt17_Rb_tree_iteratorISt4pairIKiMNS_11test_objectIS1_EEFvvEEERNS2_11safe_holderIS7_EE
|
||||
fun:_ZN3tut10test_groupI14LinkedListTestLi50EE8run_nextEv
|
||||
fun:_ZNK3tut11test_runner23run_all_tests_in_group_ESt23_Rb_tree_const_iteratorISt4pairIKSsPNS_10group_baseEEE
|
||||
fun:_ZNK3tut11test_runner9run_testsEv
|
||||
fun:_Z3runRKSsi
|
||||
fun:main
|
||||
}
|
16
cs235/lab03/Run_Test_Driver.sh
Executable file
16
cs235/lab03/Run_Test_Driver.sh
Executable file
@ -0,0 +1,16 @@
|
||||
#!/bin/bash
|
||||
EXE=dont_run_me
|
||||
|
||||
g++ -o$EXE Student_Code/*.cpp ignoreme.a -g
|
||||
|
||||
if (( $? )) ;
|
||||
then
|
||||
echo Compilation Failed;
|
||||
read -p "Press enter to exit";
|
||||
else
|
||||
chmod 755 $EXE;
|
||||
|
||||
valgrind --tool=memcheck --leak-check=yes --max-stackframe=5000000 --show-reachable=yes --suppressions=DONT_DELETE.supp ./$EXE
|
||||
rm ./$EXE
|
||||
read -p "Press any key to exit..."
|
||||
fi;
|
21
cs235/lab03/Student_Code/Factory.cpp
Normal file
21
cs235/lab03/Student_Code/Factory.cpp
Normal file
@ -0,0 +1,21 @@
|
||||
#include "Factory.h"
|
||||
#include "linkedlist.h"
|
||||
|
||||
//You may add #include statements here
|
||||
|
||||
/*
|
||||
You will MODIFY THIS DOCUMENT.
|
||||
*/
|
||||
//=======================================================================================
|
||||
/*
|
||||
getLinkedList()
|
||||
|
||||
Creates and returns an object whose class extends LinkedListInterface.
|
||||
This should be an object of a class you have created.
|
||||
|
||||
Example: If you made a class called "LinkedList", you might say, "return new LinkedList();".
|
||||
*/
|
||||
LinkedListInterface * Factory::getLinkedList()
|
||||
{
|
||||
return new linkedlist();//Modify this line
|
||||
}
|
23
cs235/lab03/Student_Code/Factory.h
Normal file
23
cs235/lab03/Student_Code/Factory.h
Normal file
@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
#include "LinkedListInterface.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
/*
|
||||
WARNING: It is expressly forbidden to modify any part of this document, including its name
|
||||
*/
|
||||
//=======================================================================================
|
||||
/*
|
||||
getLinkedList()
|
||||
|
||||
Creates and returns an object whose class extends LinkedListInterface.
|
||||
This should be an object of a class you have created.
|
||||
|
||||
Example: If you made a class called "LinkedList", you might say, "return new LinkedList();".
|
||||
*/
|
||||
class Factory
|
||||
{
|
||||
public:
|
||||
static LinkedListInterface * getLinkedList();
|
||||
};
|
||||
//=======================================================================================
|
80
cs235/lab03/Student_Code/LinkedListInterface.h
Normal file
80
cs235/lab03/Student_Code/LinkedListInterface.h
Normal file
@ -0,0 +1,80 @@
|
||||
//YOU MAY NOT MODIFY THIS DOCUMENT
|
||||
#pragma once
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class LinkedListInterface
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
LinkedListInterface(void){};
|
||||
virtual ~LinkedListInterface(void){};
|
||||
|
||||
/*
|
||||
insertHead
|
||||
|
||||
A node with the given value should be inserted at the beginning of the list.
|
||||
|
||||
Only non-negative values should be added to the list. Do not allow
|
||||
duplicate values in the list.
|
||||
*/
|
||||
virtual void insertHead(int value) = 0;
|
||||
|
||||
/*
|
||||
insertTail
|
||||
|
||||
A node with the given value should be inserted at the end of the list.
|
||||
|
||||
Only non-negative values should be added to the list. Do not allow
|
||||
duplicate values in the list.
|
||||
*/
|
||||
virtual void insertTail(int value) = 0;
|
||||
|
||||
/*
|
||||
insertAfter
|
||||
|
||||
A node with the given value should be inserted immediately after the
|
||||
node whose value is equal to insertionNode.
|
||||
|
||||
A node should only be added if the node whose value is equal to
|
||||
insertionNode is in the list. Only non-negative values should be
|
||||
added to the list. Do not allow duplicate values in the list.
|
||||
*/
|
||||
virtual void insertAfter(int value, int insertionNode) = 0;
|
||||
|
||||
/*
|
||||
remove
|
||||
|
||||
The node with the given value should be removed from the list.
|
||||
|
||||
The list may or may not include a node with the given value.
|
||||
*/
|
||||
virtual void remove(int value) = 0;
|
||||
|
||||
/*
|
||||
clear
|
||||
|
||||
Remove all nodes from the list.
|
||||
*/
|
||||
virtual void clear() = 0;
|
||||
|
||||
/*
|
||||
at
|
||||
|
||||
Returns the value of the node at the given index. The list begins at
|
||||
index 0.
|
||||
|
||||
If the given index is out of range of the list, return -1;
|
||||
*/
|
||||
virtual int at(int index) = 0;
|
||||
|
||||
/*
|
||||
size
|
||||
|
||||
Returns the number of nodes in the list.
|
||||
*/
|
||||
virtual int size() = 0;
|
||||
|
||||
};
|
37
cs235/lab03/Student_Code/Makefile
Normal file
37
cs235/lab03/Student_Code/Makefile
Normal file
@ -0,0 +1,37 @@
|
||||
CXXFLAGS= -Wall -g -std=c++0x
|
||||
OBJECTS=Factory.o linkedlist.o node.o ../ignoreme.a
|
||||
EXE=main
|
||||
all: libpwnd.so $(EXE)
|
||||
|
||||
$(EXE): $(OBJECTS)
|
||||
$(CXX) $(CXXFLAGS) $(OBJECTS) -o $@
|
||||
main.o: main.cpp Factory.o linkedlist.o node.o libpwnd.so ../ignoreme.a
|
||||
g++ -Wall -g -o main Factory.o linkedlist.o ../ignoreme.a
|
||||
Factory.o: Factory.cpp Factory.h
|
||||
linkedlist.o: linkedlist.cpp linkedlist.h
|
||||
node.o: node.cpp node.h
|
||||
test: linkedlist.o node.o test.cpp
|
||||
$(CXX) $(CXXFLAGS) linkedlist.o node.o test.cpp -o $@
|
||||
|
||||
run: main
|
||||
LD_PRELOAD=libpwnd.so LD_LIBRARY_PATH=. ./main
|
||||
|
||||
clean:
|
||||
@rm -vf *.o
|
||||
@rm -vf $(EXE)
|
||||
@rm -vf *.1
|
||||
@rm -vf *.0
|
||||
|
||||
drun: main
|
||||
LD_PRELOAD=libpwnd.so LD_LIBRARY_PATH=. gdb ./main
|
||||
|
||||
debug: test
|
||||
LD_PRELOAD=libpwnd.so LD_LIBRARY_PATH=. gdb ./test
|
||||
|
||||
valgrind: $(EXE)
|
||||
LD_PRELOAD=libpwnd.so LD_LIBRARY_PATH=. valgrind --tool=memcheck --leak-check=yes ./$(EXE)
|
||||
|
||||
libpwnd.so: pwnd.c
|
||||
gcc -shared -o libpwnd.so.1.0 pwnd.c
|
||||
ln -fs libpwnd.so.1.0 libpwnd.so.1
|
||||
ln -fs libpwnd.so.1 libpwnd.so
|
158
cs235/lab03/Student_Code/linkedlist.cpp
Normal file
158
cs235/lab03/Student_Code/linkedlist.cpp
Normal file
@ -0,0 +1,158 @@
|
||||
#include "linkedlist.h"
|
||||
|
||||
linkedlist::linkedlist(): head(NULL) {}
|
||||
linkedlist::~linkedlist(){clear();}
|
||||
|
||||
bool linkedlist::is_it_zero(int value) {
|
||||
if(value < 0)
|
||||
return false;
|
||||
else
|
||||
return true;
|
||||
}
|
||||
|
||||
bool linkedlist::has_a(int value) {
|
||||
// cout << "has_a(" << value << ")" << endl;
|
||||
node* node_ptr = head;
|
||||
if(head == NULL)
|
||||
return false;
|
||||
while(node_ptr != NULL) {
|
||||
if(node_ptr->value == value)
|
||||
return true;
|
||||
node_ptr = node_ptr->next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void linkedlist::insertHead(int value) {
|
||||
// cout << "inserthead(" << value << ")" << endl;
|
||||
// cout << *this << endl;
|
||||
if(!is_it_zero(value))
|
||||
return;
|
||||
if(has_a(value))
|
||||
return;
|
||||
head = new node(value, head);
|
||||
// cout << *this << endl << endl;
|
||||
}
|
||||
|
||||
void linkedlist::insertTail(int value) {
|
||||
// cout << "insertTail(" << value << ")" << endl;
|
||||
// cout << *this << endl;
|
||||
if(!is_it_zero(value))
|
||||
return;
|
||||
if(has_a(value))
|
||||
return;
|
||||
if(head == NULL) {
|
||||
insertHead(value);
|
||||
return;
|
||||
}
|
||||
node* node_ptr = head;
|
||||
while(node_ptr != NULL) {
|
||||
if(node_ptr->next == NULL) {
|
||||
node_ptr->next = new node(value, NULL);
|
||||
break;
|
||||
}
|
||||
node_ptr = node_ptr->next;
|
||||
}
|
||||
// cout << *this << endl << endl;
|
||||
}
|
||||
|
||||
void linkedlist::insertAfter(int value, int insertionNode) {
|
||||
// cout << "insertAfter(" << value << ", insertionNode" << insertionNode << ")" << endl;
|
||||
// cout << *this << endl;
|
||||
if(value < 0 || insertionNode < 0) {
|
||||
//cout << "first check" << endl;
|
||||
return;
|
||||
}
|
||||
if(has_a(value) || !has_a(insertionNode)) {
|
||||
//cout << "failing" << endl;
|
||||
return;
|
||||
}
|
||||
node* node_ptr = head;
|
||||
while(node_ptr != NULL) {
|
||||
if(node_ptr->value == insertionNode) {
|
||||
node* temp = new node(value, NULL);
|
||||
temp->next = node_ptr->next;
|
||||
node_ptr->next = temp;
|
||||
}
|
||||
node_ptr = node_ptr->next;
|
||||
}
|
||||
// cout << *this << endl << endl;
|
||||
}
|
||||
|
||||
void linkedlist::remove(int value) {
|
||||
// cout << "remove(" << value << ")" << endl;
|
||||
// cout << *this << endl;
|
||||
if(!is_it_zero(value))
|
||||
return;
|
||||
if(!has_a(value))
|
||||
return;
|
||||
node* node_ptr = head;
|
||||
if(node_ptr->value == value) {
|
||||
//cout << "first" << endl;
|
||||
node* ptr = head;
|
||||
head = head->next;
|
||||
delete ptr;
|
||||
return;
|
||||
}
|
||||
while(node_ptr != NULL) {
|
||||
if(node_ptr->next == NULL)
|
||||
return;
|
||||
if(node_ptr->next->value == value) {
|
||||
node* ptr = node_ptr->next;
|
||||
node_ptr->next = node_ptr->next->next;
|
||||
delete ptr;
|
||||
}
|
||||
node_ptr = node_ptr->next;
|
||||
}
|
||||
// cout << *this << endl << endl;
|
||||
}
|
||||
|
||||
void linkedlist::clear() {
|
||||
// cout << "clear()" << endl;
|
||||
node* node_ptr = head;
|
||||
while(head) {
|
||||
node_ptr = head;
|
||||
head = head->next;
|
||||
delete node_ptr;
|
||||
}
|
||||
}
|
||||
|
||||
int linkedlist::at(int index) {
|
||||
// cout << *this << endl;
|
||||
// cout << "at(" << index << ")" << endl;
|
||||
if(index < 0) {
|
||||
//cout << "is it zero" << endl;
|
||||
return -1;
|
||||
}
|
||||
if(size() <= index) {
|
||||
// cout << "this is the index=" << index << " size(" << size() << ")"<<endl;
|
||||
return -1;
|
||||
}
|
||||
node* node_ptr = head;
|
||||
for(int i = 0; i < index; i++){
|
||||
node_ptr = node_ptr->next;
|
||||
}
|
||||
if(head == NULL)
|
||||
return -1;
|
||||
return node_ptr->value;
|
||||
}
|
||||
|
||||
int linkedlist::size() {
|
||||
int size_of_list = 0;
|
||||
node* node_ptr = head;
|
||||
while(node_ptr != NULL) {
|
||||
size_of_list++;
|
||||
node_ptr = node_ptr->next;
|
||||
}
|
||||
// cout << "size(" << size_of_list << ")" << endl;
|
||||
return size_of_list;
|
||||
}
|
||||
|
||||
ostream & operator<<(ostream & os, linkedlist & f) {
|
||||
node* node_ptr = f.head;
|
||||
while(node_ptr != NULL) {
|
||||
os << node_ptr->value << ",";
|
||||
node_ptr = node_ptr->next;
|
||||
}
|
||||
return os;
|
||||
}
|
25
cs235/lab03/Student_Code/linkedlist.h
Normal file
25
cs235/lab03/Student_Code/linkedlist.h
Normal file
@ -0,0 +1,25 @@
|
||||
#ifndef __LINKEDLIST_H__
|
||||
#define __LINKEDLIST_H__
|
||||
#include <iostream>
|
||||
#include "LinkedListInterface.h"
|
||||
#include "node.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
class linkedlist : public LinkedListInterface {
|
||||
public:
|
||||
linkedlist();
|
||||
~linkedlist();
|
||||
node* head;
|
||||
bool has_a(int value);
|
||||
bool is_it_zero(int value);
|
||||
void insertHead(int value);
|
||||
void insertTail(int value);
|
||||
void insertAfter(int value, int insertionNode);
|
||||
void remove(int value);
|
||||
void clear();
|
||||
int at(int index);
|
||||
int size();
|
||||
friend ostream & operator<<(ostream & os, linkedlist & f);
|
||||
};
|
||||
#endif
|
24
cs235/lab03/Student_Code/main.cpp
Normal file
24
cs235/lab03/Student_Code/main.cpp
Normal file
@ -0,0 +1,24 @@
|
||||
#include <iostream>
|
||||
#include <cassert>
|
||||
#include "linkedlist.h"
|
||||
#include "node.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
node* head = new node(0, head);
|
||||
node* tom = new node(1, NULL);
|
||||
node* dick = new node(2, NULL);
|
||||
head->next = tom;
|
||||
tom->next = dick;
|
||||
node* node_ptr = head;
|
||||
while(node_ptr != NULL) {
|
||||
cout << node_ptr->value;
|
||||
if(node_ptr != NULL) {
|
||||
cout << "====>";
|
||||
}
|
||||
node_ptr = node_ptr->next;
|
||||
}
|
||||
cout << endl;
|
||||
cout << dick->next << endl;
|
||||
}
|
3
cs235/lab03/Student_Code/node.cpp
Normal file
3
cs235/lab03/Student_Code/node.cpp
Normal file
@ -0,0 +1,3 @@
|
||||
#include "node.h"
|
||||
|
||||
node::node(const int value, node* next) : value(value), next(next) {}
|
14
cs235/lab03/Student_Code/node.h
Normal file
14
cs235/lab03/Student_Code/node.h
Normal file
@ -0,0 +1,14 @@
|
||||
#ifndef __NODE_H__
|
||||
#define __NODE_H__
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class node {
|
||||
public:
|
||||
node(const int, node*);
|
||||
int value;
|
||||
node* next;
|
||||
};
|
||||
#endif
|
5
cs235/lab03/Student_Code/pwnd.c
Normal file
5
cs235/lab03/Student_Code/pwnd.c
Normal file
@ -0,0 +1,5 @@
|
||||
#include <unistd.h>
|
||||
|
||||
int usleep(useconds_t usec) {
|
||||
return 0;
|
||||
}
|
37
cs235/lab03/Student_Code/test.cpp
Normal file
37
cs235/lab03/Student_Code/test.cpp
Normal file
@ -0,0 +1,37 @@
|
||||
#include <iostream>
|
||||
#include "linkedlist.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
linkedlist l;
|
||||
l.insertTail(69);
|
||||
l.insertHead(28);
|
||||
l.insertHead(1);
|
||||
l.insertHead(2);
|
||||
l.insertHead(4);
|
||||
l.insertHead(30);
|
||||
l.insertHead(15);
|
||||
l.insertTail(68);
|
||||
l.insertHead(9);
|
||||
l.insertHead(0);
|
||||
l.insertHead(7);
|
||||
l.insertHead(42);
|
||||
l.insertHead(3);
|
||||
cout << l << endl;
|
||||
l.insertAfter(13,30);
|
||||
cout << l << endl;
|
||||
l.remove(13);
|
||||
cout << endl << l << endl << "removed 13" << endl;;
|
||||
l.remove(4);
|
||||
cout << endl << l << endl << "removed 4" << endl;;
|
||||
l.remove(68);
|
||||
cout << endl << l << endl << "removed 68" << endl;;
|
||||
l.remove(3);
|
||||
cout << endl << endl;
|
||||
cout << endl << l << endl << "removed 3" << endl;;
|
||||
l.remove(0);
|
||||
cout << endl << l << endl << "removed 9" << endl;;
|
||||
l.remove(69);
|
||||
cout << endl << l << endl << "removed 69" << endl;;
|
||||
}
|
21
cs235/lab03/sm/Factory.cpp
Normal file
21
cs235/lab03/sm/Factory.cpp
Normal file
@ -0,0 +1,21 @@
|
||||
#include "Factory.h"
|
||||
#include "ll.h"
|
||||
|
||||
//You may add #include statements here
|
||||
|
||||
/*
|
||||
You will MODIFY THIS DOCUMENT.
|
||||
*/
|
||||
//=======================================================================================
|
||||
/*
|
||||
getLinkedList()
|
||||
|
||||
Creates and returns an object whose class extends LinkedListInterface.
|
||||
This should be an object of a class you have created.
|
||||
|
||||
Example: If you made a class called "LinkedList", you might say, "return new LinkedList();".
|
||||
*/
|
||||
LinkedListInterface * Factory::getLinkedList()
|
||||
{
|
||||
return new ll();//Modify this line
|
||||
}
|
23
cs235/lab03/sm/Factory.h
Normal file
23
cs235/lab03/sm/Factory.h
Normal file
@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
#include "LinkedListInterface.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
/*
|
||||
WARNING: It is expressly forbidden to modify any part of this document, including its name
|
||||
*/
|
||||
//=======================================================================================
|
||||
/*
|
||||
getLinkedList()
|
||||
|
||||
Creates and returns an object whose class extends LinkedListInterface.
|
||||
This should be an object of a class you have created.
|
||||
|
||||
Example: If you made a class called "LinkedList", you might say, "return new LinkedList();".
|
||||
*/
|
||||
class Factory
|
||||
{
|
||||
public:
|
||||
static LinkedListInterface * getLinkedList();
|
||||
};
|
||||
//=======================================================================================
|
80
cs235/lab03/sm/LinkedListInterface.h
Normal file
80
cs235/lab03/sm/LinkedListInterface.h
Normal file
@ -0,0 +1,80 @@
|
||||
//YOU MAY NOT MODIFY THIS DOCUMENT
|
||||
#pragma once
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class LinkedListInterface
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
LinkedListInterface(void){};
|
||||
virtual ~LinkedListInterface(void){};
|
||||
|
||||
/*
|
||||
insertHead
|
||||
|
||||
A node with the given value should be inserted at the beginning of the list.
|
||||
|
||||
Only non-negative values should be added to the list. Do not allow
|
||||
duplicate values in the list.
|
||||
*/
|
||||
virtual void insertHead(int value) = 0;
|
||||
|
||||
/*
|
||||
insertTail
|
||||
|
||||
A node with the given value should be inserted at the end of the list.
|
||||
|
||||
Only non-negative values should be added to the list. Do not allow
|
||||
duplicate values in the list.
|
||||
*/
|
||||
virtual void insertTail(int value) = 0;
|
||||
|
||||
/*
|
||||
insertAfter
|
||||
|
||||
A node with the given value should be inserted immediately after the
|
||||
node whose value is equal to insertionNode.
|
||||
|
||||
A node should only be added if the node whose value is equal to
|
||||
insertionNode is in the list. Only non-negative values should be
|
||||
added to the list. Do not allow duplicate values in the list.
|
||||
*/
|
||||
virtual void insertAfter(int value, int insertionNode) = 0;
|
||||
|
||||
/*
|
||||
remove
|
||||
|
||||
The node with the given value should be removed from the list.
|
||||
|
||||
The list may or may not include a node with the given value.
|
||||
*/
|
||||
virtual void remove(int value) = 0;
|
||||
|
||||
/*
|
||||
clear
|
||||
|
||||
Remove all nodes from the list.
|
||||
*/
|
||||
virtual void clear() = 0;
|
||||
|
||||
/*
|
||||
at
|
||||
|
||||
Returns the value of the node at the given index. The list begins at
|
||||
index 0.
|
||||
|
||||
If the given index is out of range of the list, return -1;
|
||||
*/
|
||||
virtual int at(int index) = 0;
|
||||
|
||||
/*
|
||||
size
|
||||
|
||||
Returns the number of nodes in the list.
|
||||
*/
|
||||
virtual int size() = 0;
|
||||
|
||||
};
|
27
cs235/lab03/sm/Makefile
Normal file
27
cs235/lab03/sm/Makefile
Normal file
@ -0,0 +1,27 @@
|
||||
CXXFLAGS=-Wall -g
|
||||
|
||||
all: test main
|
||||
test: test.cc Factory.o ll.o
|
||||
main: pwnd.o Factory.o ../ignoreme.a ll.o
|
||||
$(CXX) $(CXXFLAGS) -o $@ $^
|
||||
|
||||
# these deps need to be set up to allow for appropriate recompilation
|
||||
Factory.o: Factory.h Factory.cpp
|
||||
ll.o: ll.h ll.cc
|
||||
pwnd.o: pwnd.c
|
||||
|
||||
clean:
|
||||
@rm -vf *.o main test core libpwnd* vgcore.*
|
||||
|
||||
run: main
|
||||
./main
|
||||
|
||||
gdb: test
|
||||
gdb ./test
|
||||
|
||||
rtest: test
|
||||
./test
|
||||
|
||||
valgrind: main
|
||||
valgrind --tool=memcheck --leak-check=yes --max-stackframe=5000000 \
|
||||
--show-reachable=yes --suppressions=../To\ Students/DONT_DELETE.supp ./main
|
158
cs235/lab03/sm/ll.cc
Normal file
158
cs235/lab03/sm/ll.cc
Normal file
@ -0,0 +1,158 @@
|
||||
#include "ll.h"
|
||||
|
||||
ll::ll(): _size(0), head(NULL), tail(NULL) {
|
||||
}
|
||||
|
||||
ll::~ll() {
|
||||
clear();
|
||||
}
|
||||
|
||||
void ll::insertHead(int value) {
|
||||
if(value < 0 or find(value)) {
|
||||
return;
|
||||
}
|
||||
if(head == NULL) {
|
||||
assert(tail == NULL);
|
||||
assert(_size == 0);
|
||||
head = new node(value);
|
||||
tail = head;
|
||||
}
|
||||
else {
|
||||
node * tmp = head;
|
||||
head = new node(value);
|
||||
head->next = tmp;
|
||||
}
|
||||
_size++;
|
||||
}
|
||||
|
||||
void ll::insertTail(int value) {
|
||||
if(value < 0 or find(value)) {
|
||||
return;
|
||||
}
|
||||
if(tail == NULL) {
|
||||
assert(head == NULL);
|
||||
assert(_size == 0);
|
||||
tail = new node(value);
|
||||
head = tail;
|
||||
}
|
||||
else {
|
||||
node * tmp = tail;
|
||||
tmp->next = new node(value);
|
||||
tail = tmp->next;
|
||||
}
|
||||
_size++;
|
||||
}
|
||||
|
||||
void ll::insertAfter(int value, int insertionNode) {
|
||||
if(value < 0 or find(value)) {
|
||||
return;
|
||||
}
|
||||
node * inlist = find(insertionNode);
|
||||
if(not inlist) {
|
||||
return;
|
||||
}
|
||||
node * toinsert = new node(value);
|
||||
if(inlist == tail) {
|
||||
tail = toinsert;
|
||||
}
|
||||
toinsert->next = inlist->next;
|
||||
inlist->next = toinsert;
|
||||
_size++;
|
||||
}
|
||||
|
||||
void ll::remove(int value) {
|
||||
if(not find(value)) {
|
||||
// should deal with empty list, and not found
|
||||
return;
|
||||
}
|
||||
else if(head == tail) {
|
||||
delete head;
|
||||
head = tail = NULL;
|
||||
_size--;
|
||||
}
|
||||
else {
|
||||
node * cur = head;
|
||||
if(head->value == value) {
|
||||
head = head->next;
|
||||
delete cur;
|
||||
_size--;
|
||||
}
|
||||
else {
|
||||
while(cur) {
|
||||
if(cur->next == NULL) {
|
||||
// removing end ...
|
||||
break;
|
||||
}
|
||||
if(cur->next->value == value) {
|
||||
node * to_remove = cur->next;
|
||||
if(to_remove == tail) {
|
||||
tail = cur;
|
||||
}
|
||||
cur->next = to_remove->next;
|
||||
delete to_remove;
|
||||
_size--;
|
||||
}
|
||||
cur = cur->next;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ll::clear() {
|
||||
node * tmp = head;
|
||||
while(head) {
|
||||
tmp = head;
|
||||
head = head->next;
|
||||
delete tmp;
|
||||
_size--;
|
||||
}
|
||||
head = tail = NULL;
|
||||
assert(_size == 0);
|
||||
}
|
||||
|
||||
int ll::at(int index) {
|
||||
if (index >= _size or index < 0) {
|
||||
return -1;
|
||||
}
|
||||
node * tmp = head;
|
||||
for(int i = 0; i < index; i++) {
|
||||
tmp = tmp->next;
|
||||
}
|
||||
if(tmp == NULL) {
|
||||
return -1;
|
||||
}
|
||||
else {
|
||||
return tmp->value;
|
||||
}
|
||||
}
|
||||
|
||||
int ll::size() {
|
||||
return _size;
|
||||
}
|
||||
|
||||
node * ll::find(int value) {
|
||||
node * tmp = head;
|
||||
while(tmp) {
|
||||
if(tmp->value == value) {
|
||||
return tmp;
|
||||
}
|
||||
tmp = tmp->next;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ostream & operator<<(ostream & os, const node & n) {
|
||||
os << n.value;
|
||||
return os;
|
||||
}
|
||||
|
||||
ostream & operator<<(ostream & os, const ll & l) {
|
||||
os << "ll {(" << l._size << ") ";
|
||||
node * cur = l.head;
|
||||
while (cur) {
|
||||
os << *cur << ", ";
|
||||
cur = cur->next;
|
||||
}
|
||||
os << "}";
|
||||
return os;
|
||||
}
|
42
cs235/lab03/sm/ll.h
Normal file
42
cs235/lab03/sm/ll.h
Normal file
@ -0,0 +1,42 @@
|
||||
#ifndef __LL_H__
|
||||
#define __LL_H__
|
||||
|
||||
#include <iostream>
|
||||
#include <cassert>
|
||||
#include "Factory.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct node {
|
||||
int value;
|
||||
node * next;
|
||||
node(int value): value(value), next(NULL) {};
|
||||
|
||||
friend ostream & operator<<(ostream &, const node &);
|
||||
};
|
||||
|
||||
class ll : public LinkedListInterface
|
||||
{
|
||||
private:
|
||||
int _size;
|
||||
public:
|
||||
ll();
|
||||
~ll();
|
||||
|
||||
node * head;
|
||||
node * tail;
|
||||
|
||||
void insertHead(int value);
|
||||
void insertTail(int value);
|
||||
void insertAfter(int value, int insertionNode);
|
||||
void remove(int value);
|
||||
void clear();
|
||||
int at(int index);
|
||||
int size();
|
||||
|
||||
node * find(int value);
|
||||
|
||||
friend ostream & operator<<(ostream & os, const ll & i);
|
||||
};
|
||||
|
||||
#endif
|
5
cs235/lab03/sm/pwnd.c
Normal file
5
cs235/lab03/sm/pwnd.c
Normal file
@ -0,0 +1,5 @@
|
||||
#include <unistd.h>
|
||||
|
||||
int usleep(useconds_t usec) {
|
||||
return 0;
|
||||
}
|
174
cs235/lab03/sm/test.cc
Normal file
174
cs235/lab03/sm/test.cc
Normal file
@ -0,0 +1,174 @@
|
||||
#include <iostream>
|
||||
#include <cassert>
|
||||
using namespace std;
|
||||
|
||||
#include "Factory.h"
|
||||
#include "ll.h"
|
||||
|
||||
int main() {
|
||||
LinkedListInterface * lli = Factory::getLinkedList();
|
||||
ll * myll = (ll*)lli;
|
||||
myll->insertHead(8);
|
||||
myll->insertHead(5);
|
||||
myll->insertHead(3);
|
||||
myll->insertHead(2);
|
||||
myll->insertHead(1);
|
||||
// should be duplicate
|
||||
myll->insertHead(1);
|
||||
myll->insertHead(0);
|
||||
assert(myll->size() == 6);
|
||||
assert(myll->head->value == 0);
|
||||
assert(myll->tail->value == 8);
|
||||
assert(myll->at(-8) == -1);
|
||||
assert(myll->at(3238) == -1);
|
||||
assert(myll->at(3) == 3);
|
||||
delete myll;
|
||||
|
||||
{
|
||||
ll ll2 = ll();
|
||||
ll2.insertTail(5);
|
||||
ll2.insertTail(6);
|
||||
assert(ll2.size() == 2);
|
||||
assert(ll2.head->value == 5);
|
||||
assert(ll2.tail->value == 6);
|
||||
}
|
||||
|
||||
{
|
||||
// test for negative insertion
|
||||
ll ll4 = ll();
|
||||
ll4.insertHead(-1);
|
||||
ll4.insertTail(-1);
|
||||
assert(ll4.size() == 0);
|
||||
}
|
||||
|
||||
{
|
||||
// test for duplicate insertion
|
||||
ll ll3 = ll();
|
||||
ll3.insertTail(1);
|
||||
ll3.insertTail(2);
|
||||
ll3.insertTail(3);
|
||||
ll3.insertTail(4);
|
||||
ll3.insertTail(4);
|
||||
ll3.insertTail(4);
|
||||
ll3.insertTail(5);
|
||||
assert(ll3.size() == 5);
|
||||
}
|
||||
|
||||
{
|
||||
// test find
|
||||
ll ll5 = ll();
|
||||
ll5.insertHead(0);
|
||||
ll5.insertTail(1);
|
||||
ll5.insertTail(2);
|
||||
assert(ll5.find(2));
|
||||
assert(ll5.find(200) == NULL);
|
||||
}
|
||||
|
||||
{
|
||||
// test insertAfter
|
||||
ll ll6 = ll();
|
||||
ll6.insertHead(0);
|
||||
ll6.insertTail(2);
|
||||
ll6.insertTail(3);
|
||||
assert(ll6.size() == 3);
|
||||
ll6.insertAfter(1, 0);
|
||||
assert(ll6.size() == 4);
|
||||
ll6.insertAfter(1, 23);
|
||||
assert(ll6.size() == 4);
|
||||
}
|
||||
|
||||
{
|
||||
ll ll7 = ll();
|
||||
ll7.insertAfter(1, 0);
|
||||
}
|
||||
|
||||
{
|
||||
// test deletion
|
||||
ll ll8 = ll();
|
||||
ll8.insertTail(1);
|
||||
ll8.insertTail(2);
|
||||
ll8.insertTail(3);
|
||||
ll8.insertTail(4);
|
||||
ll8.insertTail(5);
|
||||
ll8.remove(4);
|
||||
assert(ll8.find(4) == NULL);
|
||||
assert(ll8.size() == 4);
|
||||
assert(ll8.at(0) == 1);
|
||||
assert(ll8.at(1) == 2);
|
||||
assert(ll8.at(2) == 3);
|
||||
assert(ll8.at(3) == 5);
|
||||
assert(ll8.at(4) == -1);
|
||||
assert(ll8.at(5) == -1);
|
||||
}
|
||||
|
||||
{
|
||||
ll ll9 = ll();
|
||||
ll9.remove(4);
|
||||
assert(ll9.size() == 0);
|
||||
assert(ll9.at(1) == -1);
|
||||
}
|
||||
|
||||
{
|
||||
ll ll10 = ll();
|
||||
ll10.insertTail(4);
|
||||
assert(ll10.size() == 1);
|
||||
ll10.remove(4);
|
||||
assert(ll10.size() == 0);
|
||||
}
|
||||
|
||||
{
|
||||
ll ll11 = ll();
|
||||
ll11.insertHead(4);
|
||||
assert(ll11.size() == 1);
|
||||
assert(ll11.at(0) == 4);
|
||||
ll11.remove(4);
|
||||
assert(ll11.size() == 0);
|
||||
}
|
||||
|
||||
{
|
||||
ll ll12 = ll();
|
||||
ll12.insertTail(1);
|
||||
ll12.insertTail(9);
|
||||
assert(ll12.size() == 2);
|
||||
ll12.remove(1);
|
||||
assert(ll12.size() == 1);
|
||||
}
|
||||
|
||||
{
|
||||
ll ll13 = ll();
|
||||
ll13.insertTail(9);
|
||||
ll13.insertTail(3);
|
||||
assert(ll13.size() == 2);
|
||||
ll13.insertTail(16);
|
||||
assert(ll13.size() == 3);
|
||||
}
|
||||
|
||||
{
|
||||
ll ll_14 = ll();
|
||||
ll_14.insertHead(9);
|
||||
ll_14.insertHead(3);
|
||||
assert(ll_14.size() == 2);
|
||||
ll_14.insertTail(16);
|
||||
assert(ll_14.size() == 3);
|
||||
}
|
||||
|
||||
{
|
||||
// test enforced by assert in clear
|
||||
ll l = ll();
|
||||
l.insertHead(16);
|
||||
l.insertTail(32);
|
||||
l.insertTail(38);
|
||||
l.remove(38);
|
||||
l.insertTail(9);
|
||||
}
|
||||
|
||||
{
|
||||
// test enforced by asserts in ll
|
||||
ll l = ll();
|
||||
l.insertHead(16);
|
||||
l.insertAfter(14, 16);
|
||||
l.insertTail(17);
|
||||
}
|
||||
|
||||
cout << "You passed" << endl;
|
||||
}
|
65
cs235/lab04/ExpressionManagerInterface.h
Normal file
65
cs235/lab04/ExpressionManagerInterface.h
Normal file
@ -0,0 +1,65 @@
|
||||
#pragma once
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <stack>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
class ExpressionManagerInterface
|
||||
{
|
||||
public:
|
||||
ExpressionManagerInterface(){}
|
||||
virtual ~ExpressionManagerInterface(){}
|
||||
|
||||
/*
|
||||
* Checks whether an expression is balanced on its parentheses
|
||||
*
|
||||
* - The given expression will have a space between every number or operator
|
||||
*
|
||||
* @return true if expression is balanced
|
||||
* @return false otherwise
|
||||
*/
|
||||
|
||||
virtual bool isBalanced(string expression) = 0;
|
||||
|
||||
/**
|
||||
* Converts a postfix expression into an infix expression
|
||||
* and returns the infix expression.
|
||||
*
|
||||
* - The given postfix expression will have a space between every number or operator.
|
||||
* - The returned infix expression must have a space between every number or operator.
|
||||
* - Redundant parentheses are acceptable i.e. ( ( 3 * 4 ) + 5 ).
|
||||
* - Check lab requirements for what will be considered invalid.
|
||||
*
|
||||
* return the string "invalid" if postfixExpression is not a valid postfix expression.
|
||||
* otherwise, return the correct infix expression as a string.
|
||||
*/
|
||||
virtual string postfixToInfix(string postfixExpression) = 0;
|
||||
|
||||
/*
|
||||
* Converts an infix expression into a postfix expression
|
||||
* and returns the postfix expression
|
||||
*
|
||||
* - The given infix expression will have a space between every number or operator.
|
||||
* - The returned postfix expression must have a space between every number or operator.
|
||||
* - Check lab requirements for what will be considered invalid.
|
||||
*
|
||||
* return the string "invalid" if infixExpression is not a valid infix expression.
|
||||
* otherwise, return the correct postfix expression as a string.
|
||||
*/
|
||||
virtual string infixToPostfix(string infixExpression) = 0;
|
||||
|
||||
/*
|
||||
* Evaluates a postfix expression returns the result as a string
|
||||
*
|
||||
* - The given postfix expression will have a space between every number or operator.
|
||||
* - Check lab requirements for what will be considered invalid.
|
||||
*
|
||||
* return the string "invalid" if postfixExpression is not a valid postfix Expression
|
||||
* otherwise, return the correct evaluation as a string
|
||||
*/
|
||||
virtual string postfixEvaluate(string postfixExpression) = 0;
|
||||
|
||||
|
||||
};
|
22
cs235/lab04/Factory.cpp
Normal file
22
cs235/lab04/Factory.cpp
Normal file
@ -0,0 +1,22 @@
|
||||
#include "Factory.h"
|
||||
#include "expman.h"
|
||||
//You may add #include statments here
|
||||
using namespace std;
|
||||
|
||||
/*
|
||||
Unlike all other documents provided, you may modify this document slightly (but do not change its name)
|
||||
*/
|
||||
//=======================================================================================
|
||||
/*
|
||||
createManager()
|
||||
|
||||
Creates and returns an object whose class extends ExpressionManagerInterface.
|
||||
This should be an object of a class you have created.
|
||||
|
||||
Example: If you made a class called "ExpressionManager", you might say, "return new ExpressionManager();".
|
||||
*/
|
||||
ExpressionManagerInterface* Factory::createManager()
|
||||
{
|
||||
return new expman();//Modify this line
|
||||
}
|
||||
//=======================================================================================
|
22
cs235/lab04/Factory.h
Normal file
22
cs235/lab04/Factory.h
Normal file
@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
#include "ExpressionManagerInterface.h"
|
||||
using namespace std;
|
||||
|
||||
/*
|
||||
WARNING: It is expressly forbidden to modify any part of this document, including its name
|
||||
*/
|
||||
//=======================================================================================
|
||||
/*
|
||||
createExpressionManager()
|
||||
|
||||
Creates and returns an object whose class extends ExpressionManagerInterface.
|
||||
This should be an object of a class you have created.
|
||||
|
||||
Example: If you made a class called "ExpressionManager", you might say, "return new ExpressionManager();".
|
||||
*/
|
||||
class Factory
|
||||
{
|
||||
public:
|
||||
static ExpressionManagerInterface * createManager();
|
||||
};
|
||||
//=======================================================================================
|
35
cs235/lab04/Makefile
Normal file
35
cs235/lab04/Makefile
Normal file
@ -0,0 +1,35 @@
|
||||
CXXFLAGS= -Wall -g -std=c++0x
|
||||
OBJECTS=Factory.o expman.o pwnd.o ignoreme.a
|
||||
EXE=main
|
||||
all: pwnd.o $(EXE) test
|
||||
|
||||
$(EXE): $(OBJECTS)
|
||||
$(CXX) $(CXXFLAGS) $(OBJECTS) -o $@
|
||||
main.o: main.cpp Factory.o expman.o pwnd.o ignoreme.a
|
||||
Factory.o: Factory.cpp Factory.h
|
||||
expman.o: expman.cpp expman.h
|
||||
pwnd.o: pwnd.c
|
||||
test: test.cc expman.o
|
||||
|
||||
run: main
|
||||
./main
|
||||
|
||||
rtest: test
|
||||
./test
|
||||
|
||||
clean:
|
||||
@rm -vf *.o
|
||||
@rm -vf $(EXE)
|
||||
@rm -vf *.1
|
||||
@rm -vf *.0
|
||||
@rm -vf test
|
||||
@rm -rvf *.dSYM
|
||||
|
||||
drun: main
|
||||
gdb ./main
|
||||
|
||||
debug: test
|
||||
gdb ./test
|
||||
|
||||
valgrind: $(EXE)
|
||||
valgrind --tool=memcheck --leak-check=yes ./$(EXE)
|
32
cs235/lab04/Run Test Driver.bat
Normal file
32
cs235/lab04/Run Test Driver.bat
Normal file
@ -0,0 +1,32 @@
|
||||
TITLE TA Test Driver
|
||||
|
||||
@echo off
|
||||
|
||||
|
||||
rem setting up environment variables needed for VS compiler
|
||||
call "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86_amd64
|
||||
|
||||
rem compiling the .obj
|
||||
cl /EHsc /Fedont_run_me.exe Student_Code\*.cpp ignoreme.lib
|
||||
|
||||
rem if compilation failed, goto error section
|
||||
if ERRORLEVEL 1 goto error
|
||||
|
||||
rem cleanup unnecessary generated files
|
||||
del *.obj
|
||||
|
||||
rem run the driver in another window
|
||||
.\dont_run_me.exe
|
||||
del dont_run_me.exe
|
||||
pause
|
||||
|
||||
exit
|
||||
|
||||
:error
|
||||
|
||||
rem remove any generated files
|
||||
del *.obj
|
||||
|
||||
echo ----------ERROR--------
|
||||
pause
|
||||
exit
|
7
cs235/lab04/bin/runner.py
Normal file
7
cs235/lab04/bin/runner.py
Normal file
@ -0,0 +1,7 @@
|
||||
import sys
|
||||
|
||||
from shunting import shunt
|
||||
|
||||
|
||||
for line in sys.stdin:
|
||||
print shunt(line.split())
|
274
cs235/lab04/expman.cpp
Normal file
274
cs235/lab04/expman.cpp
Normal file
@ -0,0 +1,274 @@
|
||||
#include "expman.h"
|
||||
#include <cctype>
|
||||
|
||||
const string OPEN = "([{";
|
||||
const string CLOSE = ")]}";
|
||||
|
||||
bool is_open(char ch) {
|
||||
return OPEN.find(ch) != string::npos;
|
||||
}
|
||||
|
||||
bool is_close(char ch) {
|
||||
return CLOSE.find(ch) != string::npos;
|
||||
}
|
||||
|
||||
int precedence(string ch) {
|
||||
int status;
|
||||
if(ch == "(") {
|
||||
status = -1;
|
||||
}
|
||||
if(ch == ")") {
|
||||
status = -1;
|
||||
}
|
||||
if(ch == "+" or ch == "-")
|
||||
status = 2;
|
||||
if(ch == "*" or ch == "/")
|
||||
status = 3;
|
||||
return status;
|
||||
}
|
||||
|
||||
void process_operator(string ch, stack<string> & operator_stack, string & postfix) {
|
||||
cout << ch << " this is ch" << endl;
|
||||
if(operator_stack.empty()) {
|
||||
operator_stack.push(ch);
|
||||
return;
|
||||
}
|
||||
if(ch == ")") {
|
||||
bool validity = true;
|
||||
while(validity) {
|
||||
postfix += operator_stack.top();
|
||||
postfix += " ";
|
||||
operator_stack.pop();
|
||||
if(operator_stack.top() == "(") {
|
||||
operator_stack.pop();
|
||||
validity = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
if(precedence(ch) > precedence(operator_stack.top())) {
|
||||
operator_stack.push(ch);
|
||||
}
|
||||
else {
|
||||
while(!operator_stack.empty() and (precedence(ch) <= precedence(operator_stack.top()))) {
|
||||
postfix += operator_stack.top();
|
||||
postfix += " ";
|
||||
operator_stack.pop();
|
||||
}
|
||||
operator_stack.push(ch);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool is_operator(string token) {
|
||||
if(token.size() != 1) {
|
||||
return false;
|
||||
}
|
||||
if(token == "+" or
|
||||
token == "-" or
|
||||
token == "/" or
|
||||
token == "*" or
|
||||
token == "^" or
|
||||
token == "(" or
|
||||
token == ")") {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool is_int(string token) {
|
||||
if(token == "") {
|
||||
return false;
|
||||
}
|
||||
for(unsigned int i = 0; i < token.size(); i++) {
|
||||
if(!isdigit(token[i])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool is_valid_token(string token) {
|
||||
if(!is_int(token) and !is_operator(token) and !is_paren(token)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool is_paren(string token) {
|
||||
if(token == "(" or
|
||||
token == "{" or
|
||||
token == "[" or
|
||||
token == ")" or
|
||||
token == "}" or
|
||||
token == "]") {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool is_valid_expression(string expression) {
|
||||
stack<string> temp = parse_expression(expression);
|
||||
while(!temp.empty()) {
|
||||
if(not is_valid_token(temp.top())) {
|
||||
return false;
|
||||
}
|
||||
temp.pop();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool expman::isBalanced(string expression) {
|
||||
stack<char> s;
|
||||
bool balanced = true;
|
||||
string::const_iterator iter = expression.begin();
|
||||
while(balanced && (iter != expression.end())) {
|
||||
char next_ch = *iter;
|
||||
if(is_open(next_ch)) {
|
||||
s.push(next_ch);
|
||||
}
|
||||
else if(is_close(next_ch)) {
|
||||
if(s.empty()) {
|
||||
balanced = false;
|
||||
}
|
||||
else {
|
||||
char top_ch = s.top();
|
||||
s.pop();
|
||||
balanced = OPEN.find(top_ch) == CLOSE.find(next_ch);
|
||||
}
|
||||
}
|
||||
++iter;
|
||||
}
|
||||
return balanced && s.empty();
|
||||
}
|
||||
|
||||
string expman::postfixToInfix(string postfixExpression) {
|
||||
cout << postfixExpression << endl;
|
||||
if(!isBalanced(postfixExpression))
|
||||
return "invalid";
|
||||
if(!is_valid_expression(postfixExpression))
|
||||
return "invalid";
|
||||
return "h";
|
||||
}
|
||||
|
||||
string expman::infixToPostfix(string infixExpression) {
|
||||
cout << infixExpression << endl;
|
||||
if(!isBalanced(infixExpression))
|
||||
return "invalid";
|
||||
if(!is_valid_expression(infixExpression))
|
||||
return "invalid";
|
||||
stack<string> operator_stack;
|
||||
stack<string> expression = parse_expression(infixExpression);
|
||||
expression = reverse_stack(expression);
|
||||
string postfix;
|
||||
while(!expression.empty()) {
|
||||
if(is_int(expression.top())) {
|
||||
postfix += expression.top();
|
||||
postfix += " ";
|
||||
expression.pop();
|
||||
}
|
||||
else if(is_operator(expression.top())) {
|
||||
process_operator(expression.top(), operator_stack, postfix);
|
||||
expression.pop();
|
||||
}
|
||||
else if(is_paren(expression.top())) {
|
||||
expression.pop();
|
||||
}
|
||||
}
|
||||
while(!operator_stack.empty()) {
|
||||
postfix += operator_stack.top();
|
||||
postfix += " ";
|
||||
operator_stack.pop();
|
||||
}
|
||||
cout << postfix << " final postfix" << endl;
|
||||
return postfix;
|
||||
}
|
||||
|
||||
string expman::postfixEvaluate(string postfixExpression) {
|
||||
cout << postfixExpression.size() << " size of string" << endl;
|
||||
if(!isBalanced(postfixExpression)) {
|
||||
cout << "not balanced" << endl;
|
||||
return "invalid";
|
||||
}
|
||||
if(!is_valid_expression(postfixExpression)) {
|
||||
cout << "invalide character" << endl;
|
||||
return "invalid";
|
||||
}
|
||||
stack<string> expression = parse_expression(postfixExpression);
|
||||
if(expression.size() == 1) {
|
||||
cout << "this is here" << endl;
|
||||
return postfixExpression;
|
||||
}
|
||||
stack<string> numbers;
|
||||
expression = reverse_stack(expression);
|
||||
while(!expression.empty()) {
|
||||
if(is_int(expression.top())) {
|
||||
numbers.push(expression.top());
|
||||
expression.pop();
|
||||
}
|
||||
else if(is_operator(expression.top())) {
|
||||
if(numbers.empty()) {
|
||||
return "invalid";
|
||||
}
|
||||
string r = numbers.top();
|
||||
string l = numbers.top();
|
||||
numbers.pop();
|
||||
numbers.pop();
|
||||
int left = atoi(l.c_str());
|
||||
int right = atoi(r.c_str());
|
||||
int result;
|
||||
if(expression.top() == "+") {
|
||||
result = left + right;
|
||||
}
|
||||
else if(expression.top() == "-") {
|
||||
result = left - right;
|
||||
}
|
||||
else if(expression.top() == "*") {
|
||||
result = left * right;
|
||||
}
|
||||
else if(expression.top()== "/") {
|
||||
if(left == 0) {
|
||||
return "invalid";
|
||||
}
|
||||
else {
|
||||
result = right / left;
|
||||
}
|
||||
}
|
||||
expression.pop();
|
||||
stringstream ss;
|
||||
ss << result;
|
||||
numbers.push(ss.str());
|
||||
}
|
||||
}
|
||||
return numbers.top();
|
||||
}
|
||||
|
||||
stack<string> reverse_stack(stack<string> s) {
|
||||
stack<string> r;
|
||||
while(!s.empty()) {
|
||||
r.push(s.top());
|
||||
s.pop();
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
stack<string> parse_expression(string expression) {
|
||||
stack<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(s);
|
||||
s.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
if(s != "") {
|
||||
results.push(s);
|
||||
}
|
||||
return results;
|
||||
}
|
28
cs235/lab04/expman.h
Normal file
28
cs235/lab04/expman.h
Normal file
@ -0,0 +1,28 @@
|
||||
#ifndef __EXPMAN_H__
|
||||
#define __EXPMAN_H__
|
||||
#include <iostream>
|
||||
#include <stack>
|
||||
#include <vector>
|
||||
#include <cstdlib>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include "ExpressionManagerInterface.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
class expman : public ExpressionManagerInterface {
|
||||
public:
|
||||
bool isBalanced(string expression);
|
||||
string postfixToInfix(string postfixExpression);
|
||||
string infixToPostfix(string infixExpression);
|
||||
string postfixEvaluate(string postfixExpression);
|
||||
};
|
||||
|
||||
int precedence(string ch);
|
||||
bool is_paren(string token);
|
||||
bool is_int(string token);
|
||||
bool is_valid_expression(string token);
|
||||
bool is_valid_token(string token);
|
||||
stack<string> reverse_stack(stack<string>);
|
||||
stack<string> parse_expression(string);
|
||||
#endif
|
BIN
cs235/lab04/ignoreme.lib
Normal file
BIN
cs235/lab04/ignoreme.lib
Normal file
Binary file not shown.
165
cs235/lab04/lab04.html
Normal file
165
cs235/lab04/lab04.html
Normal file
@ -0,0 +1,165 @@
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||||
<!-- saved from url=(0063)http://students.cs.byu.edu/~cs235headta/homework/labs.php?lab=4 -->
|
||||
<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
|
||||
|
||||
<title>CS 235 - Labs</title>
|
||||
<link rel="stylesheet" type="text/css" href="./lab04_files/main.css">
|
||||
<link rel="stylesheet" type="text/css" href="./lab04_files/labs.css">
|
||||
</head>
|
||||
<body>
|
||||
|
||||
|
||||
<div id="header">
|
||||
|
||||
|
||||
<a href="http://students.cs.byu.edu/~cs235headta/index.php"><img src="./lab04_files/title.png" alt="Computer Science 235" class="centered" border="0" height="77"></a>
|
||||
</div>
|
||||
|
||||
<div id="sidebar">
|
||||
<div id="menu">
|
||||
<h2>Information</h2>
|
||||
<ul>
|
||||
<li><a href="http://students.cs.byu.edu/~cs235headta/index.php">Announcements</a></li>
|
||||
<li><a href="http://students.cs.byu.edu/~cs235headta/information/ta_schedule.php">TA Schedule</a></li>
|
||||
<li><a href="http://students.cs.byu.edu/~cs235headta/information/personnel.php">Personnel</a></li>
|
||||
<li><a href="http://students.cs.byu.edu/~cs235headta/information/view_grades.php">View Grades</a></li>
|
||||
<li><a href="http://students.cs.byu.edu/~cs235headta/information/syllabus.php">Syllabus</a></li>
|
||||
<li><a href="http://students.cs.byu.edu/~cs235headta/homework/labs.php?lab=0">Orientation</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<div id="menu">
|
||||
<h2>Homework</h2>
|
||||
<ul>
|
||||
<li><a href="http://students.cs.byu.edu/~cs235headta/homework/labs.php?lab=1">Labs</a></li>
|
||||
<li><a href="http://students.cs.byu.edu/~cs235headta/homework/course_schedule.php">Course Schedule</a></li>
|
||||
<li><a href="http://students.cs.byu.edu/~cs235headta/homework/cover_sheets.php">Cover Sheets</a></li>
|
||||
<li><a href="http://students.cs.byu.edu/~cs235headta/homework/submit_exam.php">Submit Exam</a></li>
|
||||
<li><a href="http://students.cs.byu.edu/~cs235headta/homework/help_passoff.php">Help / Passoff</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<div id="menu">
|
||||
<h2>Reference</h2>
|
||||
<ul>
|
||||
<li><a href="http://students.cs.byu.edu/~cs235headta/reference/lecture_slides.php">Lecture Slides</a></li>
|
||||
<li><a href="http://students.cs.byu.edu/~cs235headta/reference/book_examples.php">Book Examples</a></li>
|
||||
<li><a href="http://students.cs.byu.edu/~cs235headta/reference/vs_guide.php">Visual Studio Guide</a></li>
|
||||
<li><a href="http://students.cs.byu.edu/~cs235headta/reference/eclipse_guide.php">Eclipse/Linux Guide</a></li>
|
||||
<li><a href="http://students.cs.byu.edu/~cs235headta/reference/msdn.php">MSDN Academic Alliance</a></li>
|
||||
<li><a href="http://students.cs.byu.edu/~cs235headta/reference/exam_guide.php">Exam Guide</a></li>
|
||||
<li><a href="http://www.cplusplus.com/reference/">C++ Reference</a></li>
|
||||
<li><a href="http://students.cs.byu.edu/~cs235headta/reference/javacpp.php">Java vs. C++</a></li>
|
||||
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<div id="menu">
|
||||
<h2>Software</h2>
|
||||
<ul>
|
||||
<li><a href="http://www.microsoft.com/visualstudio/en-us/products/2010-editions/visual-cpp-express"> Visual Studio</a>
|
||||
</li></ul>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div id="page">
|
||||
<div id="content">
|
||||
|
||||
<script type="text/javascript">
|
||||
function redirectLab(lab) {
|
||||
window.location = "labs.php?lab=" + lab;
|
||||
}
|
||||
|
||||
</script>
|
||||
|
||||
<h2>Lab 4</h2>
|
||||
<form action="" method="get" name="changeLab">
|
||||
<select name="lab" onchange="redirectLab(document.changeLab.lab.value);">
|
||||
<option value="0" selected="">Select a Lab:</option>
|
||||
<option value="0">Orientation</option>
|
||||
<option value="1">Lab 1</option>
|
||||
<option value="2">Lab 2</option>
|
||||
<option value="3">Lab 3</option>
|
||||
<option value="4" selected="selected">Lab 4</option>
|
||||
<option value="5">Lab 5</option>
|
||||
<option value="6">Lab 6</option>
|
||||
<option value="7">Lab 7</option>
|
||||
<option value="8">Lab 8</option>
|
||||
<option value="9">Lab 9</option>
|
||||
<option value="10">Lab 10</option>
|
||||
</select>
|
||||
</form>
|
||||
|
||||
|
||||
<div class="infoBox">
|
||||
<h3>Purpose</h3>
|
||||
<p>To become familiarized with the use of stacks as data structures.</p>
|
||||
|
||||
<h3>Key Reading</h3>
|
||||
<ul>
|
||||
<li>5.1-5.4</li>
|
||||
</ul>
|
||||
|
||||
<h3>Background</h3>
|
||||
<p>Develop an Expression Manager that performs several operations on infix and postfix expressions. Be able to convert from one form to the other, evaluate postfix expressions, and check for balanced parenthetical expressions.</p>
|
||||
|
||||
|
||||
<p>You may also refer to Edsger Dijkstra's "Shunting-yard algorithm" for additional help, which can be viewed <a href="http://en.wikipedia.org/wiki/Shunting_yard_algorithm" target="_blank">here</a>.</p>
|
||||
<h3>Requirements</h3>
|
||||
|
||||
<p>You will need <a href="http://students.cs.byu.edu/~cs235headta/homework/student_files/Lab3ShuntingYard.zip">these files</a> to complete the assignment. Details for method constraints are found in these documents and are still a part of the requirements. (If downloaded before 9/17, 2:00 pm re-download. There are errors that were fixed that will cause the driver to break on working code.)</p>
|
||||
|
||||
|
||||
<p>Extend the <code>ExpressionManagerInterface.h</code>.</p>
|
||||
<p></p>
|
||||
<h4>Part 1 - Balanced Symbols Check (10 points)</h4>
|
||||
<ul>
|
||||
<li>Determine and report whether an expression is balanced. { [ } ] is not balanced. [ ( ) ] is balanced and valid</li>
|
||||
</ul>
|
||||
<h4>Part 2 - Infix to Postfix Conversion (10 points)</h4>
|
||||
<ul>
|
||||
<li>Alert the user if the given infix expression is not valid</li>
|
||||
<li>Convert the infix expression into a postfix expression and display the postfix expression</li>
|
||||
</ul>
|
||||
<h4>Part 3 - Postfix to Infix Conversion (10 points)</h4>
|
||||
<ul>
|
||||
<li>Alert the user if the given postfix expression is not valid</li>
|
||||
<li>Convert the postfix expression into an infix expression and display the infix expression</li>
|
||||
</ul>
|
||||
<h4>Part 4 - Postfix Expression Evaluation (10 points)</h4>
|
||||
<ul>
|
||||
<li>Alert the user if the given postfix expression is not valid</li>
|
||||
<li>Evaluate the given postfix expression and display the result</li>
|
||||
<li>Handle attempts to divide by 0</li>
|
||||
</ul>
|
||||
|
||||
<h3>Requirement Notes</h3>
|
||||
|
||||
<h4>General</h4>
|
||||
<ul>
|
||||
<li>Valid expressions consist of integers; brackets, braces, and parentheses; and +, -, *, /, and %. Reject any invalid expressions and inform the user.</li>
|
||||
<li>Your calculations should perform integer divison and produce integer results</li>
|
||||
<li>Valid expressions also need to satisfy standard infix or postfix requirements</li>
|
||||
<li>{,},(,),[, and ] are the only symbols considered for the Balanced Symbols Check</li>
|
||||
<li>Your program should allow the user to repeat the activities</li>
|
||||
<li>You can assume there will be a space between every number or operator</li>
|
||||
<li>You must put parenthesis '()' around every part of the expression during the postfix to infix conversion. i.e. "4 2 5 + *" = "( 4 *( 2 + 5 ) )"</li>
|
||||
<li>You must use the stack class pre-defined in the C++ Standard Template Library (STL). i.e. <code>#include <stack></code></li>
|
||||
</ul>
|
||||
|
||||
<h4>Test Cases</h4>
|
||||
<ul>
|
||||
<li>The expressions in the test files will be written with one complete expression on each line. Available below are small test files you may use to test your program for pass off on each part.
|
||||
This is not a comprehensive set of tests, but it should familiarize you with the format and give you ideas for additional test cases you can add. Your program must display the result of the selected
|
||||
operation for each of the expressions in the file.
|
||||
<ul>
|
||||
<li><a href="http://students.cs.byu.edu/~cs235headta/homework/student_files/part1.txt">Part1.txt</a></li>
|
||||
<li><a href="http://students.cs.byu.edu/~cs235headta/homework/student_files/part2.txt">Part2.txt</a></li>
|
||||
<li><a href="http://students.cs.byu.edu/~cs235headta/homework/student_files/part3.txt">Part3.txt</a></li>
|
||||
<li><a href="http://students.cs.byu.edu/~cs235headta/homework/student_files/part4.txt">Part4.txt</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
</div> </div>
|
||||
</div>
|
||||
</body><link rel="stylesheet" type="text/css" href="data:text/css,"></html>
|
13
cs235/lab04/lab04_files/labs.css
Normal file
13
cs235/lab04/lab04_files/labs.css
Normal file
@ -0,0 +1,13 @@
|
||||
form {
|
||||
position: absolute;
|
||||
left: 1015px;
|
||||
top: 85px;
|
||||
}
|
||||
pre {
|
||||
border: 1px solid #666;
|
||||
background-color: #ddd;
|
||||
padding: 15px;
|
||||
}
|
||||
h3 {
|
||||
text-decoration: underline;
|
||||
}
|
344
cs235/lab04/lab04_files/main.css
Normal file
344
cs235/lab04/lab04_files/main.css
Normal file
@ -0,0 +1,344 @@
|
||||
body {
|
||||
background: #fff url(../imgs/background.jpg) repeat-y left top;
|
||||
font-size: 14px;
|
||||
/*text-align: justify;*/
|
||||
color: #000000;
|
||||
}
|
||||
|
||||
body, th, td, input, textarea, select, option {
|
||||
font-family: "Trebuchet MS", Helvetica, Arial, sans-serif;/*, "Times New Roman", Arial, Times, serif;*/
|
||||
}
|
||||
|
||||
h1, h2, h3 {
|
||||
text-transform: uppercase;
|
||||
font-family: Arial, Helvetica, sans-serif;
|
||||
font-weight: normal;
|
||||
color: #1C1C1C;
|
||||
}
|
||||
|
||||
h1 {
|
||||
letter-spacing: -2px;
|
||||
font-size: 3em;
|
||||
}
|
||||
|
||||
h2 {
|
||||
letter-spacing: -1px;
|
||||
font-size: 2em;
|
||||
}
|
||||
|
||||
h3 {
|
||||
font-size: 1.4em;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
h4 {
|
||||
font-size: 1.2em;
|
||||
}
|
||||
|
||||
p, ul, ol {
|
||||
line-height: 150%;
|
||||
}
|
||||
|
||||
blockquote {
|
||||
padding-left: 1em;
|
||||
}
|
||||
|
||||
blockquote p, blockquote ul, blockquote ol {
|
||||
line-height: normal;
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
a {
|
||||
color: #030a36;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
hr {
|
||||
display: none;
|
||||
}
|
||||
|
||||
/* Header */
|
||||
#header {
|
||||
width: 100%;
|
||||
height: 75px;
|
||||
margin: 0 left;
|
||||
margin-bottom: 20px;
|
||||
text-transform: uppercase;
|
||||
font-family: Arial, Helvetica, sans-serif;
|
||||
background-color: #999;
|
||||
background-image: -webkit-gradient(linear, left top, left bottom, from(#ddd), to(#999));
|
||||
background-image: -moz-linear-gradient(-90deg, #ddd, #999);
|
||||
}
|
||||
|
||||
#header h1, #header p {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
#header h1 {
|
||||
float: left;
|
||||
padding: 5px 0 0 320px;
|
||||
color: #F79F1A;
|
||||
font-size: 36px;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
#header p {
|
||||
float: left;
|
||||
padding: 17px 0 0 5px;
|
||||
font-size: 12px;
|
||||
font-weight: bold;
|
||||
color: #EEEEEE;
|
||||
}
|
||||
|
||||
#header a {
|
||||
text-decoration: none;
|
||||
color: #F79F1A;
|
||||
}
|
||||
|
||||
/* Page */
|
||||
#page {
|
||||
width: 1100px;
|
||||
margin: 0 left;
|
||||
}
|
||||
|
||||
/* Content */
|
||||
#content {
|
||||
float: right;
|
||||
width: 800px;
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
#content img {
|
||||
float: left;
|
||||
margin-right: 15px;
|
||||
}
|
||||
|
||||
.post {
|
||||
padding: 5px 0 0 0;
|
||||
}
|
||||
|
||||
.title {
|
||||
padding: 0 20px 0 20px;
|
||||
font-size: 24px;
|
||||
}
|
||||
|
||||
.title a {
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
.pagetitle {
|
||||
}
|
||||
|
||||
.byline {
|
||||
margin: -30px 20px 0 20px;
|
||||
color: #646464;
|
||||
}
|
||||
|
||||
.meta {
|
||||
border-bottom: 10px solid #EEEEEE;
|
||||
text-align: right;
|
||||
color: #646464;
|
||||
padding: 10px 20px 20px 20px;
|
||||
text-transform: uppercase;
|
||||
font-family: Arial, Helvetica, sans-serif;
|
||||
font-size: 10px;
|
||||
}
|
||||
|
||||
.meta .more {
|
||||
background: #EEEEEE;
|
||||
padding: 5px 15px;
|
||||
}
|
||||
|
||||
.meta .comments {
|
||||
background: #EEEEEE;
|
||||
padding: 5px 15px;
|
||||
}
|
||||
|
||||
.meta a {
|
||||
}
|
||||
|
||||
.alignleft {
|
||||
float: left;
|
||||
}
|
||||
|
||||
.alignright {
|
||||
float: right;
|
||||
}
|
||||
|
||||
.posts {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
list-style: none;
|
||||
line-height: normal;
|
||||
}
|
||||
|
||||
.posts li {
|
||||
}
|
||||
|
||||
.posts h3 {
|
||||
margin: 0;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.posts p {
|
||||
margin: 0;
|
||||
line-height: normal;
|
||||
}
|
||||
|
||||
.posts a {
|
||||
}
|
||||
|
||||
.entry {
|
||||
margin: 0 20px 0 20px;
|
||||
}
|
||||
|
||||
.last {
|
||||
border: none;
|
||||
}
|
||||
|
||||
/* Sidebar */
|
||||
#sidebar {
|
||||
float: left;
|
||||
width: 220px;
|
||||
color: #EEEEEE;
|
||||
}
|
||||
|
||||
#menu {
|
||||
border-bottom:thin dotted #FFFFFF;
|
||||
margin: 0 20px 20px 10px;
|
||||
}
|
||||
|
||||
#sidebar ul {
|
||||
margin: 0 0 0 45px;
|
||||
padding: 0px 0 20px 0;
|
||||
list-style: none;
|
||||
}
|
||||
|
||||
#sidebar li {
|
||||
}
|
||||
|
||||
#sidebar li ul {
|
||||
padding: 0px 0px 16px 0px;
|
||||
}
|
||||
|
||||
#sidebar li li {
|
||||
border-bottom: 1px dotted #1C1C1C;
|
||||
padding: 0 0 10px 0px;
|
||||
}
|
||||
|
||||
#sidebar h2 {
|
||||
margin: 0;
|
||||
height: 30px;
|
||||
padding: 0px 0px 0px 20px;
|
||||
text-transform: uppercase;
|
||||
font-family: Arial, Helvetica, sans-serif;
|
||||
font-size: 18px;
|
||||
color: #FFFFFF;
|
||||
}
|
||||
|
||||
#sidebar a {
|
||||
color: #FFFFFF;
|
||||
}
|
||||
|
||||
#sidebar a:hover {
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
.infoBox {
|
||||
position: relative;
|
||||
border-top: 1px dotted #999;
|
||||
padding: 15px 0;
|
||||
overflow: auto;
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
/* Search */
|
||||
#search input {
|
||||
display: none;
|
||||
}
|
||||
|
||||
#search input#s {
|
||||
display: block;
|
||||
width: 230px;
|
||||
padding: 2px 5px;
|
||||
border: 1px solid #3DD1FF;
|
||||
background: #EEEEEE
|
||||
}
|
||||
|
||||
#search br {
|
||||
display: none;
|
||||
}
|
||||
|
||||
/* Calendar */
|
||||
#calendar {
|
||||
}
|
||||
|
||||
#calendar h2 {
|
||||
margin-bottom: 15px;
|
||||
}
|
||||
|
||||
#calendar table {
|
||||
width: 80%;
|
||||
margin: 0 auto;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
#calendar caption {
|
||||
width: 100%;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
#next {
|
||||
text-align: right;
|
||||
}
|
||||
|
||||
#prev {
|
||||
text-align: left;
|
||||
}
|
||||
|
||||
/* Footer */
|
||||
#footer {
|
||||
clear: both;
|
||||
width: 900px;
|
||||
height: 50px;
|
||||
margin: 0 left;
|
||||
text-align: right;
|
||||
font-size: smaller;
|
||||
font-family: Arial, Helvetica, sans-serif;
|
||||
}
|
||||
|
||||
#footer p {
|
||||
padding: 50px 60px 20px 0;
|
||||
text-transform: uppercase;
|
||||
}
|
||||
|
||||
#footer p a {
|
||||
}
|
||||
|
||||
table {
|
||||
text-align: center;
|
||||
margin: 15px auto 0 auto;
|
||||
border-radius: 5px;
|
||||
-webkit-border-radius: 5px;
|
||||
-moz-border-radius: 5px;
|
||||
}
|
||||
th {
|
||||
color: #fff;
|
||||
background-color: #030a36;
|
||||
padding: 3px 10px;
|
||||
}
|
||||
th:first-child {
|
||||
-moz-border-radius: 7px 0 0 0;
|
||||
-webkit-border-radius: 7px 0 0 0;
|
||||
border-radius: 7px 0 0 0;
|
||||
}
|
||||
th:last-child {
|
||||
-moz-border-radius: 0 7px 0 0;
|
||||
-webkit-border-radius: 0 7px 0 0;
|
||||
border-radius: 0 7px 0 0;
|
||||
}
|
||||
td {
|
||||
background-color:#d6d6d6;
|
||||
padding: 3px 10px;
|
||||
}
|
BIN
cs235/lab04/lab04_files/title.png
Normal file
BIN
cs235/lab04/lab04_files/title.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 61 KiB |
14
cs235/lab04/main.cpp
Normal file
14
cs235/lab04/main.cpp
Normal file
@ -0,0 +1,14 @@
|
||||
#include <iostream>
|
||||
#include <cassert>
|
||||
#include "expman.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
expman e;
|
||||
assert(e.isBalanced("{ { [ ( ) ] } ( ) }") == true);
|
||||
assert(e.isBalanced("{ [ ) }") == false);
|
||||
assert(e.isBalanced("{ ( [ ] } )") == false);
|
||||
assert(e.isBalanced("{") == false);
|
||||
return 0;
|
||||
}
|
5
cs235/lab04/pwnd.c
Normal file
5
cs235/lab04/pwnd.c
Normal file
@ -0,0 +1,5 @@
|
||||
#include <unistd.h>
|
||||
|
||||
int usleep(useconds_t usec) {
|
||||
return 0;
|
||||
}
|
11
cs235/lab04/setup.py
Normal file
11
cs235/lab04/setup.py
Normal file
@ -0,0 +1,11 @@
|
||||
from setuptools import setup, find_packages
|
||||
|
||||
setup(
|
||||
name = 'cs235lab04',
|
||||
version = '0.0dev',
|
||||
packages = find_packages(),
|
||||
author = "Stephen M. McQuay",
|
||||
author_email = "stephen@mcquay.me",
|
||||
url = "http://bitbucket.org/smcquay/cs235",
|
||||
license = "WTFPL",
|
||||
)
|
23
cs235/lab04/shunting/__init__.py
Normal file
23
cs235/lab04/shunting/__init__.py
Normal file
@ -0,0 +1,23 @@
|
||||
from collections import deque
|
||||
|
||||
|
||||
op_prec = {
|
||||
'*': 3,
|
||||
'/': 3,
|
||||
'%': 3,
|
||||
'+': 2,
|
||||
'-': 2,
|
||||
}
|
||||
|
||||
|
||||
def shunt(tokens):
|
||||
tokens = deque(tokens)
|
||||
output = deque()
|
||||
operators = deque()
|
||||
while tokens:
|
||||
token = tokens.pop()
|
||||
if token.isdigit():
|
||||
output.append(token)
|
||||
elif token in op_prec:
|
||||
operators.append(token)
|
||||
return output, operators
|
10
cs235/lab04/shunting/test_shunt.py
Normal file
10
cs235/lab04/shunting/test_shunt.py
Normal file
@ -0,0 +1,10 @@
|
||||
import unittest
|
||||
|
||||
from shunting import shunt
|
||||
|
||||
|
||||
class ShuntTest(unittest.TestCase):
|
||||
|
||||
def test_simple_add(self):
|
||||
o, s = shunt(['4', '+', '3'])
|
||||
self.assertEquals(o, ['4', '3', '+'])
|
117
cs235/lab04/test.cc
Normal file
117
cs235/lab04/test.cc
Normal file
@ -0,0 +1,117 @@
|
||||
#include <iostream>
|
||||
#include <stack>
|
||||
#include <vector>
|
||||
#include <cassert>
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include "expman.h"
|
||||
|
||||
ostream & operator<<(ostream & os, stack<string> & s) {
|
||||
while(not s.empty()) {
|
||||
os << "'" << s.top() << "', ";
|
||||
s.pop();
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
int main() {
|
||||
stringstream oss;
|
||||
|
||||
auto r1 = parse_expression(string("1 + 40 + 3"));
|
||||
oss << r1;
|
||||
assert(oss.str() == "'3', '+', '40', '+', '1', ");
|
||||
oss.str("");
|
||||
|
||||
auto r2 = parse_expression(string(" 1 + 40 + 3"));
|
||||
oss << r2;
|
||||
assert(oss.str() == "'3', '+', '40', '+', '1', ");
|
||||
oss.str("");
|
||||
|
||||
auto r3 = parse_expression(string("1 + 40 + 3 "));
|
||||
oss << r3;
|
||||
assert(oss.str() == "'3', '+', '40', '+', '1', ");
|
||||
oss.str("");
|
||||
|
||||
auto r4 = parse_expression(string(" 1 + 40 + 3 "));
|
||||
oss << r4;
|
||||
assert(oss.str() == "'3', '+', '40', '+', '1', ");
|
||||
oss.str("");
|
||||
|
||||
auto r5 = parse_expression(string(" "));
|
||||
oss << r5;
|
||||
assert(oss.str() == "");
|
||||
oss.str("");
|
||||
|
||||
auto r6 = parse_expression(string(" "));
|
||||
oss << r6;
|
||||
assert(oss.str() == "");
|
||||
oss.str("");
|
||||
|
||||
auto r7 = parse_expression(string(""));
|
||||
oss << r7;
|
||||
assert(oss.str() == "");
|
||||
oss.str("");
|
||||
|
||||
auto r8 = parse_expression(string("1"));
|
||||
oss << r8;
|
||||
assert(oss.str() == "'1', ");
|
||||
oss.str("");
|
||||
|
||||
{
|
||||
stack<string> s;
|
||||
s.push("a");
|
||||
s.push("b");
|
||||
s.push("c");
|
||||
auto s2 = reverse_stack(s);
|
||||
assert(s2.top() == "a");
|
||||
s2.pop();
|
||||
assert(s2.top() == "b");
|
||||
s2.pop();
|
||||
assert(s2.top() == "c");
|
||||
s2.pop();
|
||||
}
|
||||
|
||||
assert(precedence("*") == precedence("/"));
|
||||
assert(precedence("+") == precedence("-"));
|
||||
assert(precedence("*") > precedence(")"));
|
||||
assert(precedence("(") < precedence(")"));
|
||||
assert(precedence(")") < precedence("/"));
|
||||
|
||||
assert(is_int("42") == true);
|
||||
assert(is_int("6y") == false);
|
||||
assert(is_int("+") == false);
|
||||
assert(is_int("4 + 2") == false);
|
||||
assert(is_int("$") == false);
|
||||
assert(is_int("") == false);
|
||||
|
||||
assert(is_valid_token("3") == true);
|
||||
assert(is_valid_token("3 + 3") == false);
|
||||
assert(is_valid_token("") == false);
|
||||
assert(is_valid_token("-") == true);
|
||||
assert(is_valid_token("*") == true);
|
||||
assert(is_valid_token("3 + a") == false);
|
||||
assert(is_valid_token("$") == false);
|
||||
assert(is_valid_token("ahugegiantstring") == false);
|
||||
|
||||
assert(is_valid_expression("3") == true);
|
||||
assert(is_valid_expression("3 + 3") == true);
|
||||
assert(is_valid_expression("3 / 4 + 2 * 9") == true);
|
||||
assert(is_valid_expression("3 - 0") == true);
|
||||
assert(is_valid_expression("a") == false);
|
||||
assert(is_valid_expression("3 + a") == false);
|
||||
assert(is_valid_expression("3 $ 3") == false);
|
||||
assert(is_valid_expression("suckmygiantballs") == false);
|
||||
assert(is_valid_expression("40 * ( 2 + 4 - ( 2 + 2 ) ) - 4 / 5 / 6") == true);
|
||||
|
||||
assert(is_paren("(") == true);
|
||||
assert(is_paren("1") == false);
|
||||
assert(is_paren("a21)") == false);
|
||||
assert(is_paren("{") == true);
|
||||
assert(is_paren(")") == true);
|
||||
assert(is_paren("$") == false);
|
||||
assert(is_paren("!%#") == false);
|
||||
|
||||
expman e;
|
||||
string b = "3 + 3";
|
||||
// assert(e.infixToPostfix(b) == "3 3 +");
|
||||
}
|
2
cs235/lab04/test1.txt
Normal file
2
cs235/lab04/test1.txt
Normal file
@ -0,0 +1,2 @@
|
||||
3 + 4
|
||||
3 + 4 * 2
|
4
cs235/lab04/tests/part1.txt
Normal file
4
cs235/lab04/tests/part1.txt
Normal file
@ -0,0 +1,4 @@
|
||||
{ { [ ( ) ] } ( ) }
|
||||
{ [ ) }
|
||||
{ ( [ ] } )
|
||||
{
|
4
cs235/lab04/tests/part2.txt
Normal file
4
cs235/lab04/tests/part2.txt
Normal file
@ -0,0 +1,4 @@
|
||||
2 + a
|
||||
3 $ 3
|
||||
40 * ( 2 + 4 - ( 2 + 2 ) ) - 4 / 5 / 6
|
||||
4 * ( 2 + 4 - ( 2 + ) ) - 4 / 5
|
3
cs235/lab04/tests/part3.txt
Normal file
3
cs235/lab04/tests/part3.txt
Normal file
@ -0,0 +1,3 @@
|
||||
3 + 3 /
|
||||
3 3 4 +
|
||||
40 2 4 + 1 1 + - * 4 5 / 6 / -
|
3
cs235/lab04/tests/part4.txt
Normal file
3
cs235/lab04/tests/part4.txt
Normal file
@ -0,0 +1,3 @@
|
||||
40 2 4 + 1 1 + - * 4 2 / 1 / - 7 %
|
||||
+ 3 4 +
|
||||
4 5 2 + * 2 /
|
661
cs235/lab04/valgrind.supp
Normal file
661
cs235/lab04/valgrind.supp
Normal file
@ -0,0 +1,661 @@
|
||||
{
|
||||
Dynamic Linker Bug
|
||||
Memcheck:Cond
|
||||
fun:_dl_relocate_object
|
||||
fun:dl_main
|
||||
fun:_dl_sysdep_start
|
||||
fun:_dl_start
|
||||
obj:/lib/ld-2.6.so
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:strlen
|
||||
fun:_ZNSsC1EPKcRKSaIcE
|
||||
fun:_Z11generateKeyv
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_Z12CompareListsRK10LinkedListRSt4listISsSaISsEE
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_Z12CompareListsRK10LinkedListRSt4listISsSaISsEE
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_Z12CompareListsRK10LinkedListRSt4listISsSaISsEE
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_Z12CompareListsRK10LinkedListRSt4listISsSaISsEE
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_Z12CompareListsRK10LinkedListRSt4listISsSaISsEE
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZSteqIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_ZNK10LinkedList4FindERKSsP6LLNode
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZSteqIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_ZNK10LinkedList4FindERKSsP6LLNode
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:strlen
|
||||
fun:_ZNSsC1EPKcRKSaIcE
|
||||
fun:_Z11generateKeyv
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZSteqIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_ZNK10LinkedList4FindERKSsP6LLNode
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZSteqIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_ZSt6__findISt14_List_iteratorISsESsET_S2_S2_RKT0_St18input_iterator_tag
|
||||
fun:_ZSt4findISt14_List_iteratorISsESsET_S2_S2_RKT0_
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZSteqIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_ZNK10LinkedList4FindERKSsP6LLNode
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:strlen
|
||||
fun:_ZNSsC1EPKcRKSaIcE
|
||||
fun:_Z11generateKeyv
|
||||
fun:_Z7TestBSTb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_Z7TestBSTb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZSteqIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZSteqIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_Z7TestBSTb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:strlen
|
||||
fun:_ZNSsC1EPKcRKSaIcE
|
||||
fun:_Z11generateKeyv
|
||||
fun:_Z7TestBSTb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:strlen
|
||||
fun:_ZNSsC1EPKcRKSaIcE
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStltIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStltIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:strlen
|
||||
fun:_ZNSsC1EPKcRKSaIcE
|
||||
fun:_Z11generateKeyv
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_Z12CompareListsRK10LinkedListRSt4listISsSaISsEE
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_Z12CompareListsRK10LinkedListRSt4listISsSaISsEE
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_Z12CompareListsRK10LinkedListRSt4listISsSaISsEE
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_Z12CompareListsRK10LinkedListRSt4listISsSaISsEE
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZNK10LinkedList4FindERKSsP6LLNode
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZNK10LinkedList4FindERKSsP6LLNode
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:strlen
|
||||
fun:_ZNSsC1EPKcRKSaIcE
|
||||
fun:_Z11generateKeyv
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZSteqIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZNK10LinkedList4FindERKSsP6LLNode
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:strlen
|
||||
fun:_ZNSsC1EPKcRKSaIcE
|
||||
fun:_Z11generateKeyv
|
||||
fun:_Z7TestBSTb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_Z7TestBSTb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_Z7TestBSTb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:strlen
|
||||
fun:_ZNSsC1EPKcRKSaIcE
|
||||
fun:_Z11generateKeyv
|
||||
fun:_Z7TestBSTb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:strlen
|
||||
fun:_ZNSsC1EPKcRKSaIcE
|
||||
fun:_Z11generateKeyv
|
||||
fun:_Z7TestBSTb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStltIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:strlen
|
||||
fun:_ZNSsC1EPKcRKSaIcE
|
||||
fun:_Z11generateKeyv
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_Z12CompareListsRK10LinkedListRSt4listISsSaISsEE
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_Z12CompareListsRK10LinkedListRSt4listISsSaISsEE
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_Z12CompareListsRK10LinkedListRSt4listISsSaISsEE
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_Z12CompareListsRK10LinkedListRSt4listISsSaISsEE
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_Z12CompareListsRK10LinkedListRSt4listISsSaISsEE
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZSteqIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZSteqIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:strlen
|
||||
fun:_ZNSsC1EPKcRKSaIcE
|
||||
fun:_Z11generateKeyv
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZSteqIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZSteqIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_ZSt6__findISt14_List_iteratorISsESsET_S2_S2_RKT0_St18input_iterator_tag
|
||||
fun:_ZSt4findISt14_List_iteratorISsESsET_S2_S2_RKT0_
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_Z14TestLinkedListb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:_ZNKSs7compareERKSs
|
||||
fun:_ZSteqIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:strlen
|
||||
fun:_ZNSsC1EPKcRKSaIcE
|
||||
fun:_Z11generateKeyv
|
||||
fun:_Z7TestBSTb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:strlen
|
||||
fun:_ZNSsC1EPKcRKSaIcE
|
||||
fun:_Z11generateKeyv
|
||||
fun:_Z7TestBSTb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:strlen
|
||||
fun:_ZNSsC1EPKcRKSaIcE
|
||||
fun:_Z11generateKeyv
|
||||
fun:_Z7TestBSTb
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:bcmp
|
||||
fun:_ZNSt11char_traitsIcE7compareEPKcS2_j
|
||||
fun:_ZSteqIcEN9__gnu_cxx11__enable_ifIXsrSt9__is_charIT_E7__valueEbE6__typeERKSbIS3_St11char_traitsIS3_ESaIS3_EESC_
|
||||
fun:_ZStneIcSt11char_traitsIcESaIcEEbRKSbIT_T0_T1_ES8_
|
||||
fun:_ZN3tut12_GLOBAL__N_113ensure_equalsISsSsEEvPKcRKT0_RKT_
|
||||
fun:_ZN7BSTTest7initBSTER3BST
|
||||
fun:_ZN3tut11test_objectI7BSTTestE4testILi2EEEvv
|
||||
fun:_ZN3tut10test_groupI7BSTTestLi50EE13run_test_seh_EMNS_11test_objectIS1_EEFvvERNS2_11safe_holderIS4_EERSs
|
||||
fun:_ZN3tut10test_groupI7BSTTestLi50EE9run_test_ERKSt17_Rb_tree_iteratorISt4pairIKiMNS_11test_objectIS1_EEFvvEEERNS2_11safe_holderIS7_EE
|
||||
fun:_ZN3tut10test_groupI7BSTTestLi50EE8run_nextEv
|
||||
fun:_ZNK3tut11test_runner23run_all_tests_in_group_ESt23_Rb_tree_const_iteratorISt4pairIKSsPNS_10group_baseEEE
|
||||
fun:_ZNK3tut11test_runner9run_testsEv
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:bcmp
|
||||
fun:_ZNSt11char_traitsIcE7compareEPKcS2_j
|
||||
fun:_ZSteqIcEN9__gnu_cxx11__enable_ifIXsrSt9__is_charIT_E7__valueEbE6__typeERKSbIS3_St11char_traitsIS3_ESaIS3_EESC_
|
||||
fun:_ZNK10LinkedList4FindERKSsP6LLNode
|
||||
fun:_ZN3tut11test_objectI14LinkedListTestE4testILi8EEEvv
|
||||
fun:_ZN3tut10test_groupI14LinkedListTestLi50EE13run_test_seh_EMNS_11test_objectIS1_EEFvvERNS2_11safe_holderIS4_EERSs
|
||||
fun:_ZN3tut10test_groupI14LinkedListTestLi50EE9run_test_ERKSt17_Rb_tree_iteratorISt4pairIKiMNS_11test_objectIS1_EEFvvEEERNS2_11safe_holderIS7_EE
|
||||
fun:_ZN3tut10test_groupI14LinkedListTestLi50EE8run_nextEv
|
||||
fun:_ZNK3tut11test_runner23run_all_tests_in_group_ESt23_Rb_tree_const_iteratorISt4pairIKSsPNS_10group_baseEEE
|
||||
fun:_ZNK3tut11test_runner9run_testsEv
|
||||
fun:_Z3runRKSsi
|
||||
fun:main
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:bcmp
|
||||
fun:_ZNSt11char_traitsIcE7compareEPKcS2_j
|
||||
fun:_ZSteqIcEN9__gnu_cxx11__enable_ifIXsrSt9__is_charIT_E7__valueEbE6__typeERKSbIS3_St11char_traitsIS3_ESaIS3_EESC_
|
||||
fun:_ZSt6__findISt14_List_iteratorISsESsET_S2_S2_RKT0_St18input_iterator_tag
|
||||
fun:_ZSt4findISt14_List_iteratorISsESsET_S2_S2_RKT0_
|
||||
fun:_ZN3tut11test_objectI14LinkedListTestE4testILi9EEEvv
|
||||
fun:_ZN3tut10test_groupI14LinkedListTestLi50EE13run_test_seh_EMNS_11test_objectIS1_EEFvvERNS2_11safe_holderIS4_EERSs
|
||||
fun:_ZN3tut10test_groupI14LinkedListTestLi50EE9run_test_ERKSt17_Rb_tree_iteratorISt4pairIKiMNS_11test_objectIS1_EEFvvEEERNS2_11safe_holderIS7_EE
|
||||
fun:_ZN3tut10test_groupI14LinkedListTestLi50EE8run_nextEv
|
||||
fun:_ZNK3tut11test_runner23run_all_tests_in_group_ESt23_Rb_tree_const_iteratorISt4pairIKSsPNS_10group_baseEEE
|
||||
fun:_ZNK3tut11test_runner9run_testsEv
|
||||
fun:_Z3runRKSsi
|
||||
}
|
||||
{
|
||||
<insert a suppression name here>
|
||||
Memcheck:Cond
|
||||
fun:bcmp
|
||||
fun:_ZNSt11char_traitsIcE7compareEPKcS2_j
|
||||
fun:_ZSteqIcEN9__gnu_cxx11__enable_ifIXsrSt9__is_charIT_E7__valueEbE6__typeERKSbIS3_St11char_traitsIS3_ESaIS3_EESC_
|
||||
fun:_ZN3tut11test_objectI14LinkedListTestE4testILi9EEEvv
|
||||
fun:_ZN3tut10test_groupI14LinkedListTestLi50EE13run_test_seh_EMNS_11test_objectIS1_EEFvvERNS2_11safe_holderIS4_EERSs
|
||||
fun:_ZN3tut10test_groupI14LinkedListTestLi50EE9run_test_ERKSt17_Rb_tree_iteratorISt4pairIKiMNS_11test_objectIS1_EEFvvEEERNS2_11safe_holderIS7_EE
|
||||
fun:_ZN3tut10test_groupI14LinkedListTestLi50EE8run_nextEv
|
||||
fun:_ZNK3tut11test_runner23run_all_tests_in_group_ESt23_Rb_tree_const_iteratorISt4pairIKSsPNS_10group_baseEEE
|
||||
fun:_ZNK3tut11test_runner9run_testsEv
|
||||
fun:_Z3runRKSsi
|
||||
fun:main
|
||||
}
|
22
cs235/lab05/Factory.cpp
Normal file
22
cs235/lab05/Factory.cpp
Normal file
@ -0,0 +1,22 @@
|
||||
#include "Factory.h"
|
||||
#include "station.h"
|
||||
//You may add #include statments here
|
||||
using namespace std;
|
||||
|
||||
/*
|
||||
Unlike all other documents provided, you may modify this document slightly (but do not change its name)
|
||||
*/
|
||||
//=======================================================================================
|
||||
/*
|
||||
createStation()
|
||||
|
||||
Creates and returns an object whose class extends StationInterface.
|
||||
This should be an object of a class you have created.
|
||||
|
||||
Example: If you made a class called "Station", you might say, "return new Station();".
|
||||
*/
|
||||
StationInterface* Factory::createStation()
|
||||
{
|
||||
return new station();
|
||||
}
|
||||
//=======================================================================================
|
21
cs235/lab05/Factory.h
Normal file
21
cs235/lab05/Factory.h
Normal file
@ -0,0 +1,21 @@
|
||||
#include "StationInterface.h"
|
||||
#pragma once
|
||||
/*
|
||||
WARNING: It is expressly forbidden to modify any part of this document, including its name
|
||||
*/
|
||||
//=======================================================================================
|
||||
/*
|
||||
createStation()
|
||||
|
||||
Creates and returns an object whose class extends StationInterface.
|
||||
This should be an object of a class you have created.
|
||||
|
||||
Example: If you made a class called "Station", you might say, "return new Station();".
|
||||
*/
|
||||
class Factory
|
||||
{
|
||||
public:
|
||||
static StationInterface* createStation();
|
||||
};
|
||||
|
||||
//=======================================================================================
|
35
cs235/lab05/Makefile
Normal file
35
cs235/lab05/Makefile
Normal file
@ -0,0 +1,35 @@
|
||||
CXXFLAGS= -Wall -g -std=c++0x
|
||||
OBJECTS=Factory.o station.o deque.o ordeque.o irdeque.o queue.o linkedlist.o node.o stack.o pwnd.o ignoreme.a
|
||||
EXE=main
|
||||
all: pwnd.o $(EXE)
|
||||
|
||||
$(EXE): $(OBJECTS)
|
||||
$(CXX) $(CXXFLAGS) $(OBJECTS) -o $@
|
||||
main.o: main.cpp Factory.o ordeque.o irdeque.o deque.o queue.o station.o linkedlist.o node.o stack.o pwnd.o ignoreme.a
|
||||
Factory.o: Factory.cpp Factory.h
|
||||
station.o: station.cpp station.h
|
||||
linkedlist.o: linkedlist.cpp linkedlist.h
|
||||
stack.o: stack.cpp stack.h
|
||||
queue.o: queue.cpp queue.h
|
||||
deque.o: deque.cpp deque.h
|
||||
irdeque.o: irdeque.cpp irdeque.h
|
||||
ordeque.o: ordeque.cpp ordeque.h
|
||||
node.o: node.cpp node.h
|
||||
pwnd.o: pwnd.c
|
||||
|
||||
run: main
|
||||
./main
|
||||
|
||||
clean:
|
||||
@rm -vf *.o
|
||||
@rm -vf $(EXE)
|
||||
@rm -vf *.1
|
||||
@rm -vf *.0
|
||||
@rm -vf test
|
||||
@rm -rvf *.dSYM
|
||||
|
||||
drun: main
|
||||
gdb ./main
|
||||
|
||||
valgrind: $(EXE)
|
||||
valgrind --tool=memcheck --leak-check=yes ./$(EXE)
|
14
cs235/lab05/Run_Test_Driver.sh
Executable file
14
cs235/lab05/Run_Test_Driver.sh
Executable file
@ -0,0 +1,14 @@
|
||||
#!/bin/bash
|
||||
EXE=dont_run_me
|
||||
|
||||
g++ -o$EXE Student_Code/*.cpp ignoreme.a
|
||||
if (( $? )) ;
|
||||
then
|
||||
echo Compilation Failed;
|
||||
read -p "Press enter to exit";
|
||||
else
|
||||
chmod 755 $EXE;
|
||||
./$EXE;
|
||||
rm ./$EXE
|
||||
read -p "Press any key to exit..."
|
||||
fi;
|
160
cs235/lab05/StationInterface.h
Normal file
160
cs235/lab05/StationInterface.h
Normal file
@ -0,0 +1,160 @@
|
||||
#pragma once
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
|
||||
/*
|
||||
WARNING: It is expressly forbidden to modify any part of this document, including its name
|
||||
*/
|
||||
class StationInterface
|
||||
{
|
||||
public:
|
||||
StationInterface(){}
|
||||
virtual ~StationInterface(){}
|
||||
|
||||
//Part 1--------------------------------------------------------------
|
||||
/**
|
||||
* Let another car arrive at the station and become the current car.
|
||||
* Do not allow a new car to arrive if any of the following conditions apply:
|
||||
* 1. There is already a current car
|
||||
* 2. The new car's ID already exists in any structure
|
||||
* 3. The new car's ID is negative
|
||||
*
|
||||
* @param car the ID of the car arriving at the station
|
||||
* @return true if the car successfully arrived; false otherwise
|
||||
*/
|
||||
virtual bool addToStation(int car) = 0;
|
||||
|
||||
/**
|
||||
* Returns the ID of the current car.
|
||||
* Return -1 if there is no current car.
|
||||
*
|
||||
* @return the ID of the current car; -1 if there is no current car
|
||||
*/
|
||||
virtual int showCurrentCar() = 0;
|
||||
|
||||
/**
|
||||
* Removes the current car from the station.
|
||||
* Does nothing if there is no current car.
|
||||
*
|
||||
* @return true if the current car successfully left; false otherwise
|
||||
*/
|
||||
virtual bool removeFromStation() = 0;
|
||||
|
||||
//Part 2--------------------------------------------------------------
|
||||
/**
|
||||
* Adds the current car to the stack. After this operation, there should be no current car.
|
||||
* Does nothing if there is no current car or if the stack is already full.
|
||||
*
|
||||
* @return true if the car is successfully added to the stack; false otherwise
|
||||
*/
|
||||
virtual bool addToStack() = 0;
|
||||
|
||||
/**
|
||||
* Removes the first car in the stack and makes it the current car.
|
||||
* Does nothing if there is already a current car or if the stack already empty.
|
||||
*
|
||||
* @return true if the car is successfully removed from the stack; false otherwise
|
||||
*/
|
||||
virtual bool removeFromStack() = 0;
|
||||
|
||||
/**
|
||||
* Returns the ID of the first car in the stack.
|
||||
*
|
||||
* @return the ID of the first car in the stack; -1 if the stack is empty
|
||||
*/
|
||||
virtual int showTopOfStack() = 0;
|
||||
|
||||
/**
|
||||
* Returns the number of cars in the stack.
|
||||
*
|
||||
* @return the number of cars in the stack
|
||||
*/
|
||||
virtual int showSizeOfStack() = 0;
|
||||
|
||||
//Part 3--------------------------------------------------------------
|
||||
/**
|
||||
* Adds the current car to the queue. After this operation, there should be no current car.
|
||||
* Does nothing if there is no current car or if the queue is already full.
|
||||
*
|
||||
* @return true if the car is successfully added to the queue; false otherwise
|
||||
*/
|
||||
virtual bool addToQueue() = 0;
|
||||
|
||||
/**
|
||||
* Removes the first car in the queue and makes it the current car.
|
||||
* Does nothing if there is already a current car or if the queue already empty.
|
||||
*
|
||||
* @return true if the car is successfully removed from the queue; false otherwise
|
||||
*/
|
||||
virtual bool removeFromQueue() = 0;
|
||||
|
||||
/**
|
||||
* Returns the ID of the first car in the queue.
|
||||
*
|
||||
* @return the ID of the first car in the queue; -1 if the queue is empty
|
||||
*/
|
||||
virtual int showTopOfQueue() = 0;
|
||||
|
||||
/**
|
||||
* Returns the number of cars in the queue.
|
||||
*
|
||||
* @return the number of cars in the queue
|
||||
*/
|
||||
virtual int showSizeOfQueue() = 0;
|
||||
|
||||
//Part 4--------------------------------------------------------------
|
||||
/**
|
||||
* Adds the current car to the deque on the left side. After this operation, there should be no current car.
|
||||
* Does nothing if there is no current car or if the deque is already full.
|
||||
*
|
||||
* @return true if the car is successfully added to the deque; false otherwise
|
||||
*/
|
||||
virtual bool addToDequeLeft() = 0;
|
||||
|
||||
/**
|
||||
* Adds the current car to the deque on the right side. After this operation, there should be no current car.
|
||||
* Does nothing if there is no current car or if the deque is already full.
|
||||
*
|
||||
* @return true if the car is successfully added to the deque; false otherwise
|
||||
*/
|
||||
virtual bool addToDequeRight() = 0;
|
||||
|
||||
/**
|
||||
* Removes the leftmost car in the deque and makes it the current car.
|
||||
* Does nothing if there is already a current car or if the deque already empty.
|
||||
*
|
||||
* @return true if the car is successfully removed from the deque; false otherwise
|
||||
*/
|
||||
virtual bool removeFromDequeLeft() = 0;
|
||||
|
||||
/**
|
||||
* Removes the rightmost car in the deque and makes it the current car.
|
||||
* Does nothing if there is already a current car or if the deque already empty.
|
||||
*
|
||||
* @return true if the car is successfully removed from the deque; false otherwise
|
||||
*/
|
||||
virtual bool removeFromDequeRight() = 0;
|
||||
|
||||
/**
|
||||
* Returns the ID of the leftmost car in the deque.
|
||||
*
|
||||
* @return the ID of the leftmost car in the deque; -1 if the deque is empty
|
||||
*/
|
||||
virtual int showTopOfDequeLeft() = 0;
|
||||
|
||||
/**
|
||||
* Returns the ID of the rightmost car in the deque.
|
||||
*
|
||||
* @return the ID of the rightmost car in the deque; -1 if the deque is empty
|
||||
*/
|
||||
virtual int showTopOfDequeRight() = 0;
|
||||
|
||||
/**
|
||||
* Returns the number of cars in the deque.
|
||||
*
|
||||
* @return the number of cars in the deque
|
||||
*/
|
||||
virtual int showSizeOfDeque() = 0;
|
||||
};
|
98
cs235/lab05/StationInterfaceExtra.h
Normal file
98
cs235/lab05/StationInterfaceExtra.h
Normal file
@ -0,0 +1,98 @@
|
||||
#pragma once
|
||||
#include "StationInterface.h"
|
||||
using namespace std;
|
||||
|
||||
/*
|
||||
WARNING: It is expressly forbidden to modify any part of this document, including its name
|
||||
*/
|
||||
class StationInterfaceExtra : public StationInterface
|
||||
{
|
||||
public:
|
||||
StationInterfaceExtra(){}
|
||||
virtual ~StationInterfaceExtra(){}
|
||||
|
||||
//Input-Restricted Deque----------------------------------------------
|
||||
/**
|
||||
* Adds the current car to the IRDeque on the left side. After this operation, there should be no current car.
|
||||
* Does nothing if there is no current car or if the IRDeque is already full.
|
||||
*
|
||||
* @return true if the car is successfully added to the IRDeque; false otherwise
|
||||
*/
|
||||
virtual bool addToIRDequeLeft() = 0;
|
||||
|
||||
/**
|
||||
* Removes the leftmost car in the IRDeque and makes it the current car.
|
||||
* Does nothing if there is already a current car or if the IRDeque already empty.
|
||||
*
|
||||
* @return true if the car is successfully removed from the IRDeque; false otherwise
|
||||
*/
|
||||
virtual bool removeFromIRDequeLeft() = 0;
|
||||
|
||||
/**
|
||||
* Removes the rightmost car in the IRDeque and makes it the current car.
|
||||
* Does nothing if there is already a current car or if the IRDeque already empty.
|
||||
*
|
||||
* @return true if the car is successfully removed from the IRDeque; false otherwise
|
||||
*/
|
||||
virtual bool removeFromIRDequeRight() = 0;
|
||||
|
||||
/**
|
||||
* Returns the ID of the leftmost car in the IRDeque.
|
||||
*
|
||||
* @return the ID of the leftmost car in the IRDeque; -1 if the IRDeque is empty
|
||||
*/
|
||||
virtual int showTopOfIRDequeLeft() = 0;
|
||||
|
||||
/**
|
||||
* Returns the ID of the rightmost car in the IRDeque.
|
||||
*
|
||||
* @return the ID of the rightmost car in the IRDeque; -1 if the IRDeque is empty
|
||||
*/
|
||||
virtual int showTopOfIRDequeRight() = 0;
|
||||
|
||||
/**
|
||||
* Returns the number of cars in the IRDeque.
|
||||
*
|
||||
* @return the number of cars in the IRDeque
|
||||
*/
|
||||
virtual int showSizeOfIRDeque() = 0;
|
||||
|
||||
//Output-Restricted Deque---------------------------------------------
|
||||
/**
|
||||
* Adds the current car to the ORDeque on the left side. After this operation, there should be no current car.
|
||||
* Does nothing if there is no current car or if the ORDeque is already full.
|
||||
*
|
||||
* @return true if the car is successfully added to the ORDeque; false otherwise
|
||||
*/
|
||||
virtual bool addToORDequeLeft() = 0;
|
||||
|
||||
/**
|
||||
* Adds the current car to the ORDeque on the right side. After this operation, there should be no current car.
|
||||
* Does nothing if there is no current car or if the ORDeque is already full.
|
||||
*
|
||||
* @return true if the car is successfully added to the ORDeque; false otherwise
|
||||
*/
|
||||
virtual bool addToORDequeRight() = 0;
|
||||
|
||||
/**
|
||||
* Removes the leftmost car in the ORDeque and makes it the current car.
|
||||
* Does nothing if there is already a current car or if the ORDeque already empty.
|
||||
*
|
||||
* @return true if the car is successfully removed from the ORDeque; false otherwise
|
||||
*/
|
||||
virtual bool removeFromORDequeLeft() = 0;
|
||||
|
||||
/**
|
||||
* Returns the ID of the leftmost car in the ORDeque.
|
||||
*
|
||||
* @return the ID of the leftmost car in the ORDeque; -1 if the ORDeque is empty
|
||||
*/
|
||||
virtual int showTopOfORDequeLeft() = 0;
|
||||
|
||||
/**
|
||||
* Returns the number of cars in the ORDeque.
|
||||
*
|
||||
* @return the number of cars in the ORDeque
|
||||
*/
|
||||
virtual int showSizeOfORDeque() = 0;
|
||||
};
|
60
cs235/lab05/deque.cpp
Normal file
60
cs235/lab05/deque.cpp
Normal file
@ -0,0 +1,60 @@
|
||||
#include "deque.h"
|
||||
|
||||
bool deque::addToDequeLeft(int id) {
|
||||
if(in_deque(id)) {
|
||||
return false;
|
||||
}
|
||||
l.insertHead(id);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool deque::addToDequeRight(int id) {
|
||||
if(in_deque(id)) {
|
||||
return false;
|
||||
}
|
||||
l.insertTail(id);
|
||||
return true;
|
||||
}
|
||||
|
||||
int deque::removeFromDequeLeft() {
|
||||
int id = l.at(l.size() - 1);
|
||||
l.remove(id);
|
||||
return true;
|
||||
}
|
||||
|
||||
int deque::removeFromDequeRight() {
|
||||
int id = l.at(0);
|
||||
l.remove(id);
|
||||
return false;
|
||||
}
|
||||
|
||||
int deque::showTopOfDequeLeft() {
|
||||
if(l.size() == 0) {
|
||||
return -1;
|
||||
}
|
||||
int left = l.at(l.size() - 1);
|
||||
return left;
|
||||
}
|
||||
|
||||
int deque::showTopOfDequeRight() {
|
||||
if(l.size() == 0) {
|
||||
return -1;
|
||||
}
|
||||
int right = l.at(0);
|
||||
return right;
|
||||
}
|
||||
|
||||
int deque::showSizeOfDeque() {
|
||||
return l.size();
|
||||
}
|
||||
|
||||
bool deque::in_deque(int id) {
|
||||
node* node_ptr = l.head;
|
||||
while(node_ptr != NULL) {
|
||||
if(node_ptr->id == id) {
|
||||
return true;
|
||||
}
|
||||
node_ptr = node_ptr->next;
|
||||
}
|
||||
return false;
|
||||
}
|
18
cs235/lab05/deque.h
Normal file
18
cs235/lab05/deque.h
Normal file
@ -0,0 +1,18 @@
|
||||
#ifndef __DEQUE_H__
|
||||
#define __DEQUE_H__
|
||||
|
||||
#include "linkedlist.h"
|
||||
|
||||
class deque {
|
||||
public:
|
||||
linkedlist l;
|
||||
bool addToDequeLeft(int id);
|
||||
bool addToDequeRight(int id);
|
||||
int removeFromDequeLeft();
|
||||
int removeFromDequeRight();
|
||||
int showTopOfDequeLeft();
|
||||
int showTopOfDequeRight();
|
||||
int showSizeOfDeque();
|
||||
bool in_deque(int id);
|
||||
};
|
||||
#endif
|
52
cs235/lab05/irdeque.cpp
Normal file
52
cs235/lab05/irdeque.cpp
Normal file
@ -0,0 +1,52 @@
|
||||
#include "irdeque.h"
|
||||
|
||||
bool irdeque::addToIRDequeLeft(int id) {
|
||||
if(in_irdeque(id)) {
|
||||
return false;
|
||||
}
|
||||
l.insertHead(id);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool irdeque::removeFromIRDequeLeft() {
|
||||
int id = l.at(l.size() - 1);
|
||||
l.remove(id);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool irdeque::removeFromIRDequeRight() {
|
||||
int id = l.at(0);
|
||||
l.remove(id);
|
||||
return false;
|
||||
}
|
||||
|
||||
int irdeque::showTopOfIRDequeLeft() {
|
||||
if(l.size() == 0) {
|
||||
return -1;
|
||||
}
|
||||
int left = l.at(l.size() - 1);
|
||||
return left;
|
||||
}
|
||||
|
||||
int irdeque::showTopOfIRDequeRight() {
|
||||
if(l.size() == 0) {
|
||||
return -1;
|
||||
}
|
||||
int right = l.at(0);
|
||||
return right;
|
||||
}
|
||||
|
||||
int irdeque::showSizeOfIRDeque() {
|
||||
return l.size();
|
||||
}
|
||||
|
||||
bool irdeque::in_irdeque(int id) {
|
||||
node* node_ptr = l.head;
|
||||
while(node_ptr != NULL) {
|
||||
if(node_ptr->id == id) {
|
||||
return true;
|
||||
}
|
||||
node_ptr = node_ptr->next;
|
||||
}
|
||||
return false;
|
||||
}
|
17
cs235/lab05/irdeque.h
Normal file
17
cs235/lab05/irdeque.h
Normal file
@ -0,0 +1,17 @@
|
||||
#ifndef __IRDEQUE_H__
|
||||
#define __IRDEQUE_H__
|
||||
|
||||
#include "linkedlist.h"
|
||||
|
||||
class irdeque {
|
||||
public:
|
||||
linkedlist l;
|
||||
bool addToIRDequeLeft(int id);
|
||||
bool removeFromIRDequeLeft();
|
||||
bool removeFromIRDequeRight();
|
||||
int showTopOfIRDequeLeft();
|
||||
int showTopOfIRDequeRight();
|
||||
int showSizeOfIRDeque();
|
||||
bool in_irdeque(int id);
|
||||
};
|
||||
#endif
|
102
cs235/lab05/linkedlist.cpp
Normal file
102
cs235/lab05/linkedlist.cpp
Normal file
@ -0,0 +1,102 @@
|
||||
#include "linkedlist.h"
|
||||
|
||||
linkedlist::linkedlist(): head(NULL) {}
|
||||
linkedlist::~linkedlist(){clear();}
|
||||
|
||||
void linkedlist::insertHead(int id) {
|
||||
head = new node(id, head);
|
||||
}
|
||||
|
||||
void linkedlist::insertTail(int id) {
|
||||
if(head == NULL) {
|
||||
insertHead(id);
|
||||
return;
|
||||
}
|
||||
node* node_ptr = head;
|
||||
while(node_ptr != NULL) {
|
||||
if(node_ptr->next == NULL) {
|
||||
node_ptr->next = new node(id, NULL);
|
||||
break;
|
||||
}
|
||||
node_ptr = node_ptr->next;
|
||||
}
|
||||
}
|
||||
|
||||
void linkedlist::insertAfter(int id, int insertionNode) {
|
||||
node* node_ptr = head;
|
||||
while(node_ptr != NULL) {
|
||||
if(node_ptr->id == insertionNode) {
|
||||
node* temp = new node(id, NULL);
|
||||
temp->next = node_ptr->next;
|
||||
node_ptr->next = temp;
|
||||
}
|
||||
node_ptr = node_ptr->next;
|
||||
}
|
||||
}
|
||||
|
||||
void linkedlist::remove(int id) {
|
||||
node* node_ptr = head;
|
||||
if(node_ptr->id == id) {
|
||||
node* ptr = head;
|
||||
head = head->next;
|
||||
delete ptr;
|
||||
return;
|
||||
}
|
||||
while(node_ptr != NULL) {
|
||||
if(node_ptr->next == NULL)
|
||||
return;
|
||||
if(node_ptr->next->id == id) {
|
||||
node* ptr = node_ptr->next;
|
||||
node_ptr->next = node_ptr->next->next;
|
||||
delete ptr;
|
||||
}
|
||||
node_ptr = node_ptr->next;
|
||||
}
|
||||
}
|
||||
|
||||
void linkedlist::clear() {
|
||||
node* node_ptr = head;
|
||||
while(head) {
|
||||
node_ptr = head;
|
||||
head = head->next;
|
||||
delete node_ptr;
|
||||
}
|
||||
}
|
||||
|
||||
int linkedlist::at(int index) {
|
||||
if(index < 0) {
|
||||
return -1;
|
||||
}
|
||||
if(size() <= index) {
|
||||
return -1;
|
||||
}
|
||||
node* node_ptr = head;
|
||||
for(int i = 0; i < index; i++){
|
||||
node_ptr = node_ptr->next;
|
||||
}
|
||||
if(head == NULL)
|
||||
return -1;
|
||||
return node_ptr->id;
|
||||
}
|
||||
|
||||
int linkedlist::size() {
|
||||
int size_of_list = 0;
|
||||
if(head == NULL) {
|
||||
return 0;
|
||||
}
|
||||
node* node_ptr = head;
|
||||
while(node_ptr != NULL) {
|
||||
size_of_list++;
|
||||
node_ptr = node_ptr->next;
|
||||
}
|
||||
return size_of_list;
|
||||
}
|
||||
|
||||
ostream & operator<<(ostream & os, linkedlist & f) {
|
||||
node* node_ptr = f.head;
|
||||
while(node_ptr != NULL) {
|
||||
os << node_ptr->id << ",";
|
||||
node_ptr = node_ptr->next;
|
||||
}
|
||||
return os;
|
||||
}
|
24
cs235/lab05/linkedlist.h
Normal file
24
cs235/lab05/linkedlist.h
Normal file
@ -0,0 +1,24 @@
|
||||
#ifndef __LINKEDLIST_H__
|
||||
#define __LINKEDLIST_H__
|
||||
#include <iostream>
|
||||
#include "node.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
class linkedlist {
|
||||
public:
|
||||
linkedlist();
|
||||
~linkedlist();
|
||||
node* head;
|
||||
bool has_a(int value);
|
||||
bool is_it_zero(int value);
|
||||
void insertHead(int value);
|
||||
void insertTail(int value);
|
||||
void insertAfter(int value, int insertionNode);
|
||||
void remove(int value);
|
||||
void clear();
|
||||
int at(int index);
|
||||
int size();
|
||||
friend ostream & operator<<(ostream & os, linkedlist & f);
|
||||
};
|
||||
#endif
|
4
cs235/lab05/main.cpp
Normal file
4
cs235/lab05/main.cpp
Normal file
@ -0,0 +1,4 @@
|
||||
#include "station.h"
|
||||
|
||||
int main() {
|
||||
}
|
3
cs235/lab05/node.cpp
Normal file
3
cs235/lab05/node.cpp
Normal file
@ -0,0 +1,3 @@
|
||||
#include "node.h"
|
||||
|
||||
node::node(const int id, node* next) : id(id), next(next) {}
|
14
cs235/lab05/node.h
Normal file
14
cs235/lab05/node.h
Normal file
@ -0,0 +1,14 @@
|
||||
#ifndef __NODE_H__
|
||||
#define __NODE_H__
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class node {
|
||||
public:
|
||||
node(const int, node*);
|
||||
int id;
|
||||
node* next;
|
||||
};
|
||||
#endif
|
46
cs235/lab05/ordeque.cpp
Normal file
46
cs235/lab05/ordeque.cpp
Normal file
@ -0,0 +1,46 @@
|
||||
#include "ordeque.h"
|
||||
|
||||
bool ordeque::addToORDequeLeft(int id) {
|
||||
if(in_ordeque(id)) {
|
||||
return false;
|
||||
}
|
||||
l.insertHead(id);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ordeque::addToORDequeRight(int id) {
|
||||
if(in_ordeque(id)) {
|
||||
return false;
|
||||
}
|
||||
l.insertTail(id);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ordeque::removeFromORDequeLeft() {
|
||||
int id = l.at(l.size() - 1);
|
||||
l.remove(id);
|
||||
return true;
|
||||
}
|
||||
|
||||
int ordeque::showTopOfORDequeLeft() {
|
||||
if(l.size() == 0) {
|
||||
return -1;
|
||||
}
|
||||
int left = l.at(l.size() - 1);
|
||||
return left;
|
||||
}
|
||||
|
||||
int ordeque::showSizeOfORDeque() {
|
||||
return l.size();
|
||||
}
|
||||
|
||||
bool ordeque::in_ordeque(int id) {
|
||||
node* node_ptr = l.head;
|
||||
while(node_ptr != NULL) {
|
||||
if(node_ptr->id == id) {
|
||||
return true;
|
||||
}
|
||||
node_ptr = node_ptr->next;
|
||||
}
|
||||
return false;
|
||||
}
|
16
cs235/lab05/ordeque.h
Normal file
16
cs235/lab05/ordeque.h
Normal file
@ -0,0 +1,16 @@
|
||||
#ifndef __ORDEQUE_H__
|
||||
#define __ORDEQUE_H__
|
||||
|
||||
#include "linkedlist.h"
|
||||
|
||||
class ordeque {
|
||||
public:
|
||||
linkedlist l;
|
||||
bool addToORDequeLeft(int id);
|
||||
bool addToORDequeRight(int id);
|
||||
bool removeFromORDequeLeft();
|
||||
int showTopOfORDequeLeft();
|
||||
int showSizeOfORDeque();
|
||||
bool in_ordeque(int id);
|
||||
};
|
||||
#endif
|
5
cs235/lab05/pwnd.c
Normal file
5
cs235/lab05/pwnd.c
Normal file
@ -0,0 +1,5 @@
|
||||
#include <unistd.h>
|
||||
|
||||
int usleep(useconds_t usec) {
|
||||
return 0;
|
||||
}
|
38
cs235/lab05/queue.cpp
Normal file
38
cs235/lab05/queue.cpp
Normal file
@ -0,0 +1,38 @@
|
||||
#include "queue.h"
|
||||
|
||||
bool queue::addToQueue(int id) {
|
||||
if(in_queue(id)) {
|
||||
return false;
|
||||
}
|
||||
l.insertTail(id);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool queue::removeFromQueue() {
|
||||
int id = l.at(0);
|
||||
l.remove(id);
|
||||
return false;
|
||||
}
|
||||
|
||||
int queue::showTopOfQueue() {
|
||||
if(l.size() == 0) {
|
||||
return -1;
|
||||
}
|
||||
int top = l.at(0);
|
||||
return top;
|
||||
}
|
||||
|
||||
int queue::showSizeOfQueue() {
|
||||
return l.size();
|
||||
}
|
||||
|
||||
bool queue::in_queue(int id) {
|
||||
node* node_ptr = l.head;
|
||||
while(node_ptr != NULL) {
|
||||
if(node_ptr->id == id) {
|
||||
return true;
|
||||
}
|
||||
node_ptr = node_ptr->next;
|
||||
}
|
||||
return false;
|
||||
}
|
15
cs235/lab05/queue.h
Normal file
15
cs235/lab05/queue.h
Normal file
@ -0,0 +1,15 @@
|
||||
#ifndef __QUEUE_H__
|
||||
#define __QUEUE_H__
|
||||
|
||||
#include "linkedlist.h"
|
||||
|
||||
class queue {
|
||||
public:
|
||||
linkedlist l;
|
||||
bool addToQueue(int id);
|
||||
bool removeFromQueue();
|
||||
int showTopOfQueue();
|
||||
int showSizeOfQueue();
|
||||
bool in_queue(int id);
|
||||
};
|
||||
#endif
|
37
cs235/lab05/stack.cpp
Normal file
37
cs235/lab05/stack.cpp
Normal file
@ -0,0 +1,37 @@
|
||||
#include "stack.h"
|
||||
|
||||
bool stack::addToStack(int id) {
|
||||
if(in_stack(id)) {
|
||||
return false;
|
||||
}
|
||||
l.insertHead(id);
|
||||
return true;
|
||||
}
|
||||
|
||||
int stack::removeFromStack() {
|
||||
int id = l.at(0);
|
||||
l.remove(id);
|
||||
return false;
|
||||
}
|
||||
|
||||
int stack::showTopOfStack() {
|
||||
if(l.size() == 0)
|
||||
return -1;
|
||||
int top = l.at(0);
|
||||
return top;
|
||||
}
|
||||
|
||||
int stack::showSizeOfStack() {
|
||||
return l.size();
|
||||
}
|
||||
|
||||
bool stack::in_stack(int id) {
|
||||
node* node_ptr = l.head;
|
||||
while(node_ptr != NULL) {
|
||||
if(node_ptr->id == id) {
|
||||
return true;
|
||||
}
|
||||
node_ptr = node_ptr->next;
|
||||
}
|
||||
return false;
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user