├── 21-Container-of-Pointers ├── Makefile ├── bigthing_t.cpp ├── bigthing_t.h ├── list_t.cpp ├── list_t.h └── main.cpp ├── 21-Polymorphic-Container ├── basic │ ├── Makefile │ ├── bigthing_t.cpp │ ├── bigthing_t.h │ ├── list_t.cpp │ ├── list_t.h │ ├── main.cpp │ └── object_t.h └── three-types │ ├── Makefile │ ├── dyn_t.cpp │ ├── dyn_t.h │ ├── int_t.cpp │ ├── int_t.h │ ├── list_t.cpp │ ├── list_t.h │ ├── main.cpp │ ├── object_t.h │ ├── string_t.cpp │ └── string_t.h ├── L0-Test-Lecture ├── helloWorld.cpp └── obfuscatedCode.cpp ├── L10-Stream ├── cin.cpp ├── cout.cpp ├── failedState.cpp ├── getline.cpp ├── ifs.cpp ├── ifsBetter.cpp ├── input.txt └── string.cpp ├── L11-Testing ├── assert.cpp ├── clog.cpp └── factorial.cpp ├── L12-Exception ├── 1-basic │ └── exception.cpp ├── 2-exception-propagation │ └── exception.cpp ├── 3-no-catch │ └── exception.cpp ├── 4-no-matching-catch │ └── exception.cpp ├── 5-matching-catch │ └── exception.cpp ├── 6-after-catch │ └── exception.cpp ├── exception.cpp └── quiz.cpp ├── L13-ADT ├── IntSet-Efficiency │ ├── IntSet.cpp │ ├── IntSet.h │ ├── Makefile │ ├── main.cpp │ └── test.cpp ├── IntSet.cpp ├── IntSet.h ├── Makefile ├── anInt │ ├── anInt.cpp │ ├── anInt.h │ └── main.cpp ├── constructor ├── main.cpp └── test.cpp ├── L15-Subtype ├── 1-IntSet-private │ ├── IntSet.cpp │ ├── IntSet.h │ ├── Makefile │ └── main.cpp ├── 2-IntSet-protected │ ├── IntSet.cpp │ ├── IntSet.h │ ├── Makefile │ └── main.cpp ├── 3-PosIntSet │ ├── IntSet.cpp │ ├── IntSet.h │ ├── Makefile │ └── main.cpp ├── 4-virtual-PosIntSet │ ├── IntSet.cpp │ ├── IntSet.h │ ├── Makefile │ └── main.cpp └── MaxIntSet │ ├── IntSet.cpp │ ├── IntSet.h │ ├── Makefile │ ├── MaxIntSet.cpp │ ├── MaxIntSet.h │ └── main.cpp ├── L16-Interface └── IntSet-Interface │ ├── include │ └── IntSet.h │ ├── lib │ └── .gitignore │ ├── provider │ ├── IntSetImpl.cpp │ └── Makefile │ └── user │ ├── Makefile │ └── main.cpp ├── L17-Dynamic-Memory ├── Dynamic-Allocation │ ├── Makefile │ ├── delete.cpp │ ├── dynamicAllocation.cpp │ ├── leak.cpp │ └── leak2.cpp └── IntSet │ ├── IntSet.cpp │ ├── IntSet.h │ ├── Makefile │ └── main.cpp ├── L18-Deep-Copy ├── 1-Dangling-Pointer │ ├── IntSet.cpp │ ├── IntSet.h │ ├── Makefile │ └── main.cpp └── 2-Copy-Constructor-and-Assignment-Operator │ ├── IntSet.cpp │ ├── IntSet.h │ ├── Makefile │ └── main.cpp ├── L19-Dynamic-Resizing ├── 1-Inc │ ├── IntSet.cpp │ ├── IntSet.h │ ├── Makefile │ └── main.cpp └── 2-Double │ ├── IntSet.cpp │ ├── IntSet.h │ ├── Makefile │ └── main.cpp ├── L2-Linux ├── fruit.txt ├── my_add │ ├── add.cpp │ ├── add.h │ ├── add.o │ ├── input.txt │ ├── ls.txt │ ├── m1.cpp │ ├── makefile │ ├── my_add │ ├── my_add.dSYM │ │ └── Contents │ │ │ ├── Info.plist │ │ │ └── Resources │ │ │ └── DWARF │ │ │ └── my_add │ ├── my_add.txt │ ├── output.txt │ └── run_add.cpp ├── my_favorite.txt ├── test1.txt ├── test2.txt ├── test3.txt ├── test4.txt └── test5.txt ├── L20-Linked-List ├── IntList.cpp ├── IntList.h ├── Makefile └── main.cpp ├── L21-Template ├── 1-Template │ ├── withTemplate.cpp │ └── withoutTemplate.cpp ├── 2-List │ ├── List.h │ ├── Makefile │ └── main.cpp └── 3-Derived-Class │ └── test.cpp ├── L22-Operator-Overloading ├── 1-Complex │ └── complex.cpp └── 2-IntSet │ ├── IntSet.cpp │ ├── IntSet.h │ └── main.cpp ├── L23-Stack ├── IntList.cpp ├── IntList.h ├── Stack.cpp └── a.out ├── L25-STL-Sequential-Container └── vector.cpp ├── L3-Compilation ├── add │ ├── Makefile │ ├── add.cpp │ ├── add.h │ └── run_add.cpp ├── error.cpp ├── exercise │ ├── add.cpp │ ├── add.h │ ├── incdec.cpp │ ├── incdec.h │ ├── main.cpp │ ├── sub.cpp │ └── sub.h ├── header_guard │ ├── drawing.h │ ├── line.h │ ├── main.cpp │ └── point.h ├── no_header_guard │ ├── drawing.h │ ├── line.h │ ├── main.cpp │ └── point.h └── solution │ ├── add.cpp │ ├── add.h │ ├── incdec.cpp │ ├── incdec.h │ ├── main.cpp │ ├── makefile │ ├── sub.cpp │ └── sub.h ├── L4-Review ├── array.cpp ├── pointerArray.cpp ├── reference.cpp ├── rvalue.cpp ├── struct.cpp └── structArgument.cpp ├── L5-const ├── const.cpp └── constCall.cpp ├── L7-Function ├── factorial.cpp ├── fp.cpp ├── int.cpp └── recursiveCall.cpp ├── L8_enum ├── quiz.cpp └── suit.cpp ├── README.md └── docker └── Dockerfile /21-Container-of-Pointers/Makefile: -------------------------------------------------------------------------------- 1 | # variable definition 2 | 3 | CC = g++ 4 | 5 | DEFS = 6 | LIBS = 7 | INCLUDES = -I. 8 | HEADERS = list_t.h bigthing_t.h 9 | MAINSRCS = main.cpp 10 | OTHSRCS = list_t.cpp bigthing_t.cpp 11 | SRCS = $(MAINSRCS) $(OTHSRCS) 12 | OBJS = $(SRCS:.cpp=.o) 13 | TARGETS = $(MAINSRCS:.cpp=) 14 | 15 | CFLAGS = -g -Wall $(INCLUDES) $(DEFS) 16 | 17 | %.o: %.cpp 18 | $(CC) $(CFLAGS) -o $@ -c $< 19 | 20 | all: $(TARGETS) 21 | 22 | $(TARGETS): $(OBJS) 23 | $(CC) $(CFLAGS) -o $(TARGETS) $(OBJS) $(LIBS) 24 | 25 | depend: 26 | makedepend -Y $(INCLUDES) $(SRCS) 27 | 28 | memcheck: $(TARGETS) 29 | valgrind --leak-check=full ./$(TARGETS) 30 | 31 | clean: 32 | rm -f $(OBJS) $(TARGETS) 33 | 34 | .PHONY: all depend memcheck clean 35 | -------------------------------------------------------------------------------- /21-Container-of-Pointers/bigthing_t.cpp: -------------------------------------------------------------------------------- 1 | #include "bigthing_t.h" 2 | #include 3 | using namespace std; 4 | 5 | void BigThing::print() const 6 | { 7 | cout << value << " " << flush; 8 | } 9 | -------------------------------------------------------------------------------- /21-Container-of-Pointers/bigthing_t.h: -------------------------------------------------------------------------------- 1 | #ifndef BIGTHING_T_H 2 | #define BIGTHING_T_H 3 | 4 | class BigThing 5 | { 6 | private: 7 | int value; 8 | 9 | public: 10 | BigThing(int v = 0): value(v) {} 11 | 12 | int get_value() { return value; } 13 | void print() const; 14 | }; 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /21-Container-of-Pointers/list_t.cpp: -------------------------------------------------------------------------------- 1 | #include "list_t.h" 2 | #include 3 | using namespace std; 4 | 5 | List::List(): first(0) 6 | { 7 | } 8 | 9 | bool List::isEmpty() const 10 | { 11 | return (!first); 12 | } 13 | 14 | void List::insert(BigThing *v) 15 | { 16 | node *np = new node; 17 | np->value = v; 18 | np->next = first; 19 | first = np; 20 | } 21 | 22 | BigThing *List::remove() 23 | { 24 | if(isEmpty()) 25 | { 26 | listIsEmpty e; 27 | throw e; 28 | } 29 | 30 | node *victim = first; 31 | BigThing *result = victim->value; 32 | first = victim->next; 33 | delete victim; 34 | return result; 35 | } 36 | 37 | void List::print() const 38 | { 39 | node *cur = first; 40 | while(cur) 41 | { 42 | cur->value->print(); 43 | cur = cur->next; 44 | } 45 | cout << endl; 46 | } 47 | 48 | void List::copyList(node *np) 49 | { 50 | if(!np) return; 51 | copyList(np->next); 52 | BigThing *op = new BigThing(*np->value); 53 | insert(op); 54 | } 55 | 56 | void List::removeAll() 57 | { 58 | while(!isEmpty()) 59 | { 60 | BigThing *op = remove(); 61 | delete op; 62 | } 63 | } 64 | 65 | List::~List() 66 | { 67 | removeAll(); 68 | } 69 | 70 | List::List(const List &l): first(0) 71 | { 72 | copyList(l.first); 73 | } 74 | 75 | List& List::operator=(const List &l) 76 | { 77 | if(this != &l) 78 | { 79 | removeAll(); 80 | copyList(l.first); 81 | } 82 | return *this; 83 | } 84 | -------------------------------------------------------------------------------- /21-Container-of-Pointers/list_t.h: -------------------------------------------------------------------------------- 1 | #ifndef LIST_T_H 2 | #define LIST_T_H 3 | #include "bigthing_t.h" 4 | 5 | class listIsEmpty 6 | { 7 | }; 8 | 9 | struct node 10 | { 11 | BigThing *value; 12 | node *next; 13 | }; 14 | 15 | class List 16 | { 17 | node *first; 18 | void removeAll(); 19 | void copyList(node *listfirst); 20 | 21 | public: 22 | bool isEmpty() const; 23 | void insert(BigThing *v); 24 | BigThing *remove(); 25 | void print() const; 26 | 27 | List(); 28 | List(const List &l); 29 | List &operator=(const List &l); 30 | ~List(); 31 | }; 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /21-Container-of-Pointers/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "bigthing_t.h" 4 | #include "list_t.h" 5 | using namespace std; 6 | 7 | int main(int argc, char *argv[]) 8 | { 9 | List l; 10 | cout << "l insert 5 at the front. l is" << endl; 11 | BigThing *a = new BigThing(5); // Must dynamically allocate the object 12 | l.insert(a); 13 | l.print(); 14 | // l is (5) 15 | 16 | List ll; 17 | cout << "ll insert 4 at the front. ll is" << endl; 18 | BigThing *b = new BigThing(4); 19 | ll.insert(b); 20 | // ll is (4) 21 | ll.print(); 22 | 23 | cout << "Assign ll as a copy of l. ll is" << endl; 24 | ll = l; // call assignment operator 25 | // ll is (5) 26 | ll.print(); 27 | 28 | cout << "ll insert 3 at the front. ll is" << endl; 29 | BigThing *c = new BigThing(3); 30 | ll.insert(c); 31 | ll.print(); 32 | 33 | cout << "Remove the front of ll. ll is" << endl; 34 | BigThing *d = ll.remove(); 35 | delete d; // Must delete d! 36 | ll.print(); 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /21-Polymorphic-Container/basic/Makefile: -------------------------------------------------------------------------------- 1 | # variable definition 2 | 3 | CC = g++ 4 | 5 | DEFS = 6 | LIBS = 7 | INCLUDES = -I. 8 | HEADERS = list_t.h object_t.h bigthing_t.h 9 | MAINSRCS = main.cpp 10 | OTHSRCS = list_t.cpp bigthing_t.cpp 11 | SRCS = $(MAINSRCS) $(OTHSRCS) 12 | OBJS = $(SRCS:.cpp=.o) 13 | TARGETS = $(MAINSRCS:.cpp=) 14 | 15 | CFLAGS = -g -Wall $(INCLUDES) $(DEFS) 16 | 17 | %.o: %.cpp 18 | $(CC) $(CFLAGS) -o $@ -c $< 19 | 20 | all: $(TARGETS) 21 | 22 | $(TARGETS): $(OBJS) 23 | $(CC) $(CFLAGS) -o $(TARGETS) $(OBJS) $(LIBS) 24 | 25 | depend: 26 | makedepend -Y $(INCLUDES) $(SRCS) 27 | 28 | memcheck: $(TARGETS) 29 | valgrind --leak-check=full ./$(TARGETS) 30 | 31 | tags: $(SRCS) $(HEADERS) 32 | ctags $^ 33 | 34 | clean: 35 | rm -f $(OBJS) $(TARGETS) 36 | 37 | .PHONY: all depend memcheck tags clean 38 | -------------------------------------------------------------------------------- /21-Polymorphic-Container/basic/bigthing_t.cpp: -------------------------------------------------------------------------------- 1 | #include "bigthing_t.h" 2 | #include 3 | using namespace std; 4 | 5 | void BigThing::print() const 6 | { 7 | cout << value << " " << flush; 8 | } 9 | 10 | Object *BigThing::clone() 11 | { 12 | BigThing *bp = new BigThing(*this); 13 | return bp; 14 | } 15 | -------------------------------------------------------------------------------- /21-Polymorphic-Container/basic/bigthing_t.h: -------------------------------------------------------------------------------- 1 | #ifndef BIGTHING_T_H 2 | #define BIGTHING_T_H 3 | #include "object_t.h" 4 | 5 | class BigThing: public Object 6 | { 7 | private: 8 | int value; 9 | 10 | public: 11 | BigThing(int v = 0): value(v) {} 12 | BigThing(const BigThing &b): value(b.value) {} 13 | ~BigThing() {} 14 | 15 | Object *clone(); 16 | int get_value() { return value; } 17 | void print() const; 18 | }; 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /21-Polymorphic-Container/basic/list_t.cpp: -------------------------------------------------------------------------------- 1 | #include "list_t.h" 2 | #include 3 | using namespace std; 4 | 5 | List::List(): first(0) 6 | { 7 | } 8 | 9 | bool List::isEmpty() const 10 | { 11 | return (!first); 12 | } 13 | 14 | void List::print() const 15 | { 16 | node *cur = first; 17 | while(cur) 18 | { 19 | cur->value->print(); 20 | cur = cur->next; 21 | } 22 | cout << endl; 23 | } 24 | 25 | void List::insert(Object *v) 26 | { 27 | node *np = new node; 28 | np->value = v; 29 | np->next = first; 30 | first = np; 31 | } 32 | 33 | Object *List::remove() 34 | { 35 | if(isEmpty()) 36 | { 37 | listIsEmpty e; 38 | throw e; 39 | } 40 | 41 | node *victim = first; 42 | Object *result = victim->value; 43 | first = victim->next; 44 | delete victim; 45 | return result; 46 | } 47 | 48 | void List::copyList(node *np) 49 | { 50 | if(!np) return; 51 | copyList(np->next); 52 | Object *op = np->value->clone(); 53 | insert(op); 54 | } 55 | 56 | void List::removeAll() 57 | { 58 | while(!isEmpty()) 59 | { 60 | Object *op = remove(); 61 | delete op; 62 | } 63 | } 64 | 65 | List::~List() 66 | { 67 | removeAll(); 68 | } 69 | 70 | List::List(const List &l): first(0) 71 | { 72 | copyList(l.first); 73 | } 74 | 75 | List& List::operator=(const List &l) 76 | { 77 | if(this != &l) 78 | { 79 | removeAll(); 80 | copyList(l.first); 81 | } 82 | return *this; 83 | } 84 | -------------------------------------------------------------------------------- /21-Polymorphic-Container/basic/list_t.h: -------------------------------------------------------------------------------- 1 | #ifndef LIST_T_H 2 | #define LIST_T_H 3 | #include "object_t.h" 4 | 5 | class listIsEmpty 6 | { 7 | }; 8 | 9 | class List 10 | { 11 | public: 12 | bool isEmpty() const; 13 | void print() const; 14 | void insert(Object *v); 15 | Object *remove(); 16 | 17 | List(); 18 | List(const List &l); 19 | List &operator=(const List &l); 20 | ~List(); 21 | 22 | private: 23 | struct node 24 | { 25 | Object *value; 26 | node *next; 27 | }; 28 | 29 | node *first; 30 | 31 | void removeAll(); 32 | void copyList(node *listfirst); 33 | }; 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /21-Polymorphic-Container/basic/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "bigthing_t.h" 4 | #include "list_t.h" 5 | using namespace std; 6 | 7 | int main(int argc, char *argv[]) 8 | { 9 | List l; 10 | cout << "l insert 5 at the front. l is" << endl; 11 | BigThing *a = new BigThing(5); // Must dynamically allocate the object 12 | l.insert(a); 13 | // l is (5) 14 | l.print(); 15 | 16 | List ll; 17 | cout << "ll insert 4 at the front. ll is" << endl; 18 | BigThing *b = new BigThing(4); 19 | ll.insert(b); 20 | // ll is (4) 21 | ll.print(); 22 | 23 | cout << "Assign ll as a copy of l. ll is" << endl; 24 | ll = l; // call assignment operator 25 | // ll is (5) 26 | ll.print(); 27 | 28 | cout << "ll insert 3 at the front. ll is" << endl; 29 | BigThing *c = new BigThing(3); 30 | ll.insert(c); 31 | // ll is (3 5) 32 | ll.print(); 33 | 34 | cout << "Remove the front of ll. ll is" << endl; 35 | BigThing *d = dynamic_cast(ll.remove()); 36 | assert(d); 37 | delete d; // Must delete d! 38 | ll.print(); 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /21-Polymorphic-Container/basic/object_t.h: -------------------------------------------------------------------------------- 1 | #ifndef OBJECT_T_H 2 | #define OBJECT_T_H 3 | 4 | class Object 5 | { 6 | public: 7 | virtual Object *clone() = 0; 8 | virtual ~Object() {} 9 | virtual void print() const = 0; 10 | }; 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /21-Polymorphic-Container/three-types/Makefile: -------------------------------------------------------------------------------- 1 | # variable definition 2 | 3 | CC = g++ 4 | 5 | DEFS = 6 | LIBS = 7 | INCLUDES = -I. 8 | HEADERS = list_t.h object_t.h int_t.h string_t.h dyn_t.h 9 | MAINSRCS = main.cpp 10 | OTHSRCS = list_t.cpp int_t.cpp string_t.cpp dyn_t.cpp 11 | SRCS = $(MAINSRCS) $(OTHSRCS) 12 | OBJS = $(SRCS:.cpp=.o) 13 | TARGETS = $(MAINSRCS:.cpp=) 14 | 15 | CFLAGS = -g -Wall $(INCLUDES) $(DEFS) 16 | 17 | %.o: %.cpp 18 | $(CC) $(CFLAGS) -o $@ -c $< 19 | 20 | all: $(TARGETS) 21 | 22 | $(TARGETS): $(OBJS) 23 | $(CC) $(CFLAGS) -o $(TARGETS) $(OBJS) $(LIBS) 24 | 25 | depend: 26 | makedepend -Y $(INCLUDES) $(SRCS) 27 | 28 | memcheck: $(TARGETS) 29 | valgrind --leak-check=full ./$(TARGETS) 30 | 31 | tags: $(SRCS) $(HEADERS) 32 | ctags $^ 33 | 34 | clean: 35 | rm -f $(OBJS) $(TARGETS) 36 | 37 | .PHONY: all depend memcheck tags clean 38 | -------------------------------------------------------------------------------- /21-Polymorphic-Container/three-types/dyn_t.cpp: -------------------------------------------------------------------------------- 1 | #include "dyn_t.h" 2 | #include 3 | using namespace std; 4 | 5 | void dyn_t::print() const 6 | { 7 | cout << *pv << " " << flush; 8 | } 9 | 10 | Object *dyn_t::clone() 11 | { 12 | dyn_t *bp = new dyn_t(*this); 13 | return bp; 14 | } 15 | -------------------------------------------------------------------------------- /21-Polymorphic-Container/three-types/dyn_t.h: -------------------------------------------------------------------------------- 1 | #ifndef DYN_T_H 2 | #define DYN_T_H 3 | #include "object_t.h" 4 | 5 | class dyn_t: public Object 6 | { 7 | private: 8 | int *pv; 9 | 10 | public: 11 | dyn_t(int v = 0): pv(new int(v)) {} 12 | dyn_t(const dyn_t &b): pv(new int(*b.pv)) {} 13 | ~dyn_t() { delete pv; } 14 | 15 | Object *clone(); 16 | int get_value() { return *pv; } 17 | void print() const; 18 | }; 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /21-Polymorphic-Container/three-types/int_t.cpp: -------------------------------------------------------------------------------- 1 | #include "int_t.h" 2 | #include 3 | using namespace std; 4 | 5 | void int_t::print() const 6 | { 7 | cout << value << " " << flush; 8 | } 9 | 10 | Object *int_t::clone() 11 | { 12 | int_t *bp = new int_t(*this); 13 | return bp; 14 | } 15 | -------------------------------------------------------------------------------- /21-Polymorphic-Container/three-types/int_t.h: -------------------------------------------------------------------------------- 1 | #ifndef INT_T_H 2 | #define INT_T_H 3 | #include "object_t.h" 4 | 5 | class int_t: public Object 6 | { 7 | private: 8 | int value; 9 | 10 | public: 11 | int_t(int v = 0): value(v) {} 12 | int_t(const int_t &b): value(b.value) {} 13 | ~int_t() {} 14 | 15 | Object *clone(); 16 | int get_value() { return value; } 17 | void print() const; 18 | }; 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /21-Polymorphic-Container/three-types/list_t.cpp: -------------------------------------------------------------------------------- 1 | #include "list_t.h" 2 | #include 3 | using namespace std; 4 | 5 | List::List(): first(0) 6 | { 7 | } 8 | 9 | bool List::isEmpty() const 10 | { 11 | return (!first); 12 | } 13 | 14 | void List::print() const 15 | { 16 | node *cur = first; 17 | while(cur) 18 | { 19 | cur->value->print(); 20 | cur = cur->next; 21 | } 22 | cout << endl; 23 | } 24 | 25 | void List::insert(Object *v) 26 | { 27 | node *np = new node; 28 | np->value = v; 29 | np->next = first; 30 | first = np; 31 | } 32 | 33 | Object *List::remove() 34 | { 35 | if(isEmpty()) 36 | { 37 | listIsEmpty e; 38 | throw e; 39 | } 40 | 41 | node *victim = first; 42 | Object *result = victim->value; 43 | first = victim->next; 44 | delete victim; 45 | return result; 46 | } 47 | 48 | void List::copyList(node *np) 49 | { 50 | if(!np) return; 51 | copyList(np->next); 52 | Object *op = np->value->clone(); 53 | insert(op); 54 | } 55 | 56 | void List::removeAll() 57 | { 58 | while(!isEmpty()) 59 | { 60 | Object *op = remove(); 61 | delete op; 62 | } 63 | } 64 | 65 | List::~List() 66 | { 67 | removeAll(); 68 | } 69 | 70 | List::List(const List &l): first(0) 71 | { 72 | copyList(l.first); 73 | } 74 | 75 | List& List::operator=(const List &l) 76 | { 77 | if(this != &l) 78 | { 79 | removeAll(); 80 | copyList(l.first); 81 | } 82 | return *this; 83 | } 84 | -------------------------------------------------------------------------------- /21-Polymorphic-Container/three-types/list_t.h: -------------------------------------------------------------------------------- 1 | #ifndef LIST_T_H 2 | #define LIST_T_H 3 | #include "object_t.h" 4 | 5 | class listIsEmpty 6 | { 7 | }; 8 | 9 | class List 10 | { 11 | public: 12 | bool isEmpty() const; 13 | void print() const; 14 | void insert(Object *v); 15 | Object *remove(); 16 | 17 | List(); 18 | List(const List &l); 19 | List &operator=(const List &l); 20 | ~List(); 21 | 22 | private: 23 | struct node 24 | { 25 | Object *value; 26 | node *next; 27 | }; 28 | 29 | node *first; 30 | 31 | void removeAll(); 32 | void copyList(node *listfirst); 33 | }; 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /21-Polymorphic-Container/three-types/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "int_t.h" 4 | #include "string_t.h" 5 | #include "dyn_t.h" 6 | #include "list_t.h" 7 | using namespace std; 8 | 9 | int main(int argc, char *argv[]) 10 | { 11 | cout << "l insert an int object 5 at the front" << endl; 12 | List l; 13 | int_t *a = new int_t(5); // Must dynamically allocate the object 14 | l.insert(a); 15 | 16 | cout << "l insert a string object 'four' at the front" << endl; 17 | string_t *b = new string_t("four"); 18 | l.insert(b); 19 | // l is (four 5) 20 | 21 | cout << "l insert a dynamic int object 4 at the front" << endl; 22 | dyn_t *c = new dyn_t(4); 23 | l.insert(c); 24 | 25 | l.print(); 26 | 27 | cout << "Assign ll as a copy of l" << endl; 28 | List ll; 29 | ll = l; 30 | ll.print(); 31 | 32 | cout << "Remove the front of ll" << endl; 33 | dyn_t *d = dynamic_cast(ll.remove()); 34 | assert(d); 35 | delete d; // Must delete d! 36 | 37 | ll.print(); 38 | /* 39 | List ll; 40 | BigThing *b = new BigThing(4); 41 | ll.insert(b); 42 | // ll is (4) 43 | ll = l; // call assignment operator 44 | // ll is (5) 45 | BigThing *c = new BigThing(3); 46 | ll.insert(c); 47 | // ll is (3 5) 48 | 49 | ll.print(); 50 | 51 | BigThing *d = dynamic_cast(ll.remove()); 52 | assert(d); 53 | delete d; // Must delete d! 54 | */ 55 | 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /21-Polymorphic-Container/three-types/object_t.h: -------------------------------------------------------------------------------- 1 | #ifndef OBJECT_T_H 2 | #define OBJECT_T_H 3 | 4 | class Object 5 | { 6 | public: 7 | virtual Object *clone() = 0; 8 | virtual ~Object() {} 9 | 10 | virtual void print() const = 0; 11 | }; 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /21-Polymorphic-Container/three-types/string_t.cpp: -------------------------------------------------------------------------------- 1 | #include "string_t.h" 2 | #include 3 | using namespace std; 4 | 5 | void string_t::print() const 6 | { 7 | cout << value << " " << flush; 8 | } 9 | 10 | Object *string_t::clone() 11 | { 12 | string_t *bp = new string_t(*this); 13 | return bp; 14 | } 15 | -------------------------------------------------------------------------------- /21-Polymorphic-Container/three-types/string_t.h: -------------------------------------------------------------------------------- 1 | #ifndef STRING_T_H 2 | #define STRING_T_H 3 | #include "object_t.h" 4 | #include 5 | using namespace std; 6 | 7 | class string_t: public Object 8 | { 9 | private: 10 | string value; 11 | 12 | public: 13 | string_t(string v = 0): value(v) {} 14 | string_t(const string_t &b): value(b.value) {} 15 | ~string_t() {} 16 | 17 | Object *clone(); 18 | string get_value() { return value; } 19 | void print() const; 20 | }; 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /L0-Test-Lecture/helloWorld.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | char g=16777288, 5 | r=16777317, 6 | e=16777324, 7 | t=16777327, 8 | sp=16777248, 9 | w=16777303, 10 | o=16777327, 11 | R=16777330, 12 | l=16777324, 13 | d=16777316, 14 | ex=16777249; 15 | std::cout 16 | < 2 | int main() 3 | { 4 | int a=5,b=6,c=10,Q,d=16,e=47; 5 | for (b=e=0xA;Q="~YoYo--Hwz-Lyf=Tough(&ptr)(Woo)(add+all)\ 6 | YDt!ULo RCBGCDk RBMDk RBMDk RCBBBCBEk RBADADBDl+\ 7 | QCEAGDk RBDCGBl SBBFEBm TBJBn VJp XFr-\ 8 | [At HCBDBBABBCBBCBCBBC` GBABABBABAAABBABABAAABBADBa+\ 9 | GBABADDACBABABCBCBBC` [At LBABBCBCBCBBBBh LEABABADADCBj$\ 10 | LBABABABABCBEBj [At KDBBABEBBCBBCBe KBABABBBAAABBBABABCBe KDBBCBABCBABADABe" [b+++30]; ) 11 | for(; Q--> 0x40 ; ) 12 | putchar ( ++e=='Z' ? e =0xA:041^b&1); 13 | return 0x0; 14 | } 15 | -------------------------------------------------------------------------------- /L10-Stream/cin.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int foo; 6 | double bar; 7 | string baz; 8 | cin >> foo >> bar >> baz; 9 | 10 | cout << foo << " " << bar << " " << baz << endl; 11 | 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /L10-Stream/cout.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | int main(){ 9 | cout << "Welcome!" << endl; 10 | cout << "Let's sleep..."; 11 | this_thread::sleep_for(chrono::milliseconds(5000)); 12 | cout << " 5 seconds later with flush!" << flush; 13 | this_thread::sleep_for(chrono::milliseconds(5000)); 14 | cout << " 5 seconds later with EOL!\n"; 15 | this_thread::sleep_for(chrono::milliseconds(5000)); 16 | cout << "Enter a number: "; 17 | int n; 18 | cin >> n; // Try with 100 and 1000 19 | for(int i=0; i 2 | using namespace std; 3 | 4 | int main(){ 5 | int foo; 6 | double bar; 7 | string baz; 8 | cout << "Enter an integer: "; 9 | cin >> foo; 10 | while (!cin){ 11 | cout << "Enter an integer: "; 12 | cin.clear(); 13 | cin.ignore (100, '\n'); 14 | cin >> foo; 15 | } 16 | 17 | cout << "Enter a double: "; 18 | cin >> bar; 19 | while (!cin){ 20 | cout << "Enter a double: "; 21 | cin.clear(); 22 | cin.ignore (100, '\n'); 23 | cin >> bar; 24 | } 25 | 26 | cout << foo << " " << bar << endl; 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /L10-Stream/getline.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int foo; 6 | double bar; 7 | string baz; 8 | cin >> foo >> bar; 9 | // char ch; 10 | // cin.get(ch); 11 | getline(cin, baz); 12 | 13 | cout << foo << " " << bar << " " << baz << endl; 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /L10-Stream/ifs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main(int argc, char *argv[]) { 6 | ifstream iFile; 7 | string filename = argv[1]; // Better test argc before 8 | iFile.open(filename); 9 | if (iFile){ 10 | string line; 11 | while(iFile) { 12 | getline(iFile, line); 13 | cout << line << endl; 14 | } 15 | iFile.close(); 16 | } else{ 17 | cerr << "File could not be opened" << endl; 18 | } 19 | } 20 | 21 | -------------------------------------------------------------------------------- /L10-Stream/ifsBetter.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main(int argc, char *argv[]) { 6 | ifstream iFile; 7 | string filename = argv[1]; // Better test argc before 8 | iFile.open(filename); 9 | if (iFile){ 10 | string line; 11 | while(getline(iFile, line)) { 12 | cout << line << endl; 13 | } 14 | iFile.close(); 15 | } else{ 16 | cerr << "File could not be opened" << endl; 17 | } 18 | } 19 | 20 | -------------------------------------------------------------------------------- /L10-Stream/input.txt: -------------------------------------------------------------------------------- 1 | toto 2 | tata 3 | titi 4 | tutu 5 | -------------------------------------------------------------------------------- /L10-Stream/string.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main(){ 7 | ostringstream oStream; 8 | int n = 42; 9 | double pi = 3.14159; 10 | 11 | oStream << "n= " << n << " "; 12 | oStream << "pi= "; 13 | oStream << setprecision(5); 14 | oStream << pi << endl; 15 | 16 | string s = oStream.str(); 17 | cout << s; 18 | 19 | istringstream iStream; 20 | iStream.str(s); 21 | 22 | string sr; 23 | int nr; 24 | double pir; 25 | 26 | iStream >> sr >> nr; 27 | cout << sr << " " << nr << endl; 28 | iStream >> sr >> pir; 29 | cout << sr << " " << pir << endl; 30 | } 31 | -------------------------------------------------------------------------------- /L11-Testing/assert.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | //#define NDEBUG 3 | #include 4 | using namespace std; 5 | 6 | int main(){ 7 | cout << "Starts" << endl; 8 | assert(true); 9 | cout << "Continues" << endl; 10 | assert(false); 11 | cout << "Ends" << endl; 12 | } 13 | -------------------------------------------------------------------------------- /L11-Testing/clog.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | cout << "Hello!" << endl; 6 | clog << "Log message" << endl; 7 | cerr << "Error message" << endl; 8 | } 9 | -------------------------------------------------------------------------------- /L11-Testing/factorial.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int factorial(int n){ 5 | // REQUIRES: n>=0 6 | // EFFECTS: returns n! 7 | assert(n>=0); 8 | if (n==0 || n==1){ 9 | return 1; 10 | } 11 | return n*factorial(n-1); 12 | } 13 | 14 | int main(){ 15 | cout << "0!= " << factorial(0) << endl; 16 | cout << "1!= " << factorial(1) << endl; 17 | cout << "10!= " << factorial(10) << endl; 18 | 19 | cout << "16!= " << factorial(16) << endl; 20 | cout << "17!= " << factorial(17) << endl; 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /L12-Exception/1-basic/exception.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int factorial(int n){ 6 | int result = 1; 7 | if(n < 0) throw n; 8 | for(result = 1; n>0; n--) 9 | result *= n; 10 | 11 | return result; 12 | } 13 | 14 | void run_factorial(int n){ 15 | try{ 16 | cout << n << "! = " << factorial(n) << endl; 17 | } catch(int v){ 18 | cerr << "Error: negative input: " << v << endl; 19 | } 20 | } 21 | 22 | int main(int argc, char *argv[]){ 23 | int n; 24 | cout << "Please input a non-negative integer: " << flush; 25 | cin >> n; 26 | run_factorial(n); 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /L12-Exception/2-exception-propagation/exception.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int factorial(int n){ 6 | int result = 1; 7 | if(n < 0) throw n; 8 | for(result = 1; n>0; n--) 9 | result *= n; 10 | 11 | return result; 12 | } 13 | 14 | void run_factorial(int n){ 15 | cout << n << "! = " << factorial(n) << endl; 16 | } 17 | 18 | int main(int argc, char *argv[]){ 19 | int n; 20 | cout << "Please input a non-negative integer: " << flush; 21 | cin >> n; 22 | try{ 23 | run_factorial(n); 24 | } catch(int v){ 25 | cerr << "Exception handled in main" << endl; 26 | cerr << "Error: negative input: " << v << endl; 27 | } 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /L12-Exception/3-no-catch/exception.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int factorial(int n){ 6 | int result = 1; 7 | if(n < 0) throw n; 8 | for(result = 1; n>0; n--) 9 | result *= n; 10 | 11 | return result; 12 | } 13 | 14 | void run_factorial(int n){ 15 | cout << n << "! = " << factorial(n) << endl; 16 | } 17 | 18 | int main(int argc, char *argv[]){ 19 | int n; 20 | cout << "Please input a non-negative integer: " << flush; 21 | cin >> n; 22 | run_factorial(n); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /L12-Exception/4-no-matching-catch/exception.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int factorial(int n){ 6 | int result = 1; 7 | if(n < 0) throw n; 8 | for(result = 1; n>0; n--) 9 | result *= n; 10 | 11 | return result; 12 | } 13 | 14 | void run_factorial(int n){ 15 | try{ 16 | cout << n << "! = " << factorial(n) << endl; 17 | } catch(double v){ 18 | cerr << "In function run_factorial" << endl; 19 | cerr << "Error: negative input: " << v << endl; 20 | } 21 | } 22 | 23 | int main(int argc, char *argv[]){ 24 | int n; 25 | cout << "Please input a non-negative integer: " << flush; 26 | cin >> n; 27 | run_factorial(n); 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /L12-Exception/5-matching-catch/exception.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int factorial(int n){ 6 | int result = 1; 7 | if(n < 0) 8 | throw n; 9 | for(result = 1; n>0; n--) 10 | result *= n; 11 | 12 | return result; 13 | } 14 | 15 | void run_factorial(int n){ 16 | try{ 17 | cout << n << "! = " << factorial(n) << endl; 18 | } catch(double v){ 19 | cerr << "In function run_factorial" << endl; 20 | cerr << "Error: negative input: " << v << endl; 21 | } 22 | } 23 | 24 | int main(int argc, char *argv[]){ 25 | int n; 26 | cout << "Please input a non-negative integer: " << flush; 27 | cin >> n; 28 | try{ 29 | run_factorial(n); 30 | } 31 | catch(int v){ 32 | cerr << "In function main" << endl; 33 | cerr << "Error: negative input: " << v << endl; 34 | } 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /L12-Exception/6-after-catch/exception.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int factorial(int n){ 6 | int result = 1; 7 | if(n < 0) 8 | throw n; 9 | for(result = 1; n>0; n--) 10 | result *= n; 11 | 12 | return result; 13 | } 14 | 15 | void run_factorial(int n){ 16 | try{ 17 | cout << n << "! = " << factorial(n) << endl; 18 | } catch(int v){ 19 | cerr << "Error: negative input: " << v << endl; 20 | } 21 | cout << "End of run_factorial" << endl; 22 | } 23 | 24 | int main(int argc, char *argv[]){ 25 | int n; 26 | cout << "Please input a non-negative integer: " << flush; 27 | cin >> n; 28 | run_factorial(n); 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /L12-Exception/exception.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main(int argc, char *argv[]){ 6 | if (argc<2){ 7 | throw "Not enough arguments!"; 8 | } 9 | try{ 10 | ifstream ifs; 11 | ifs.open(argv[1]); 12 | if (!ifs){ 13 | throw string("File cannot be opened!"); 14 | } 15 | try{ 16 | int n; 17 | ifs >> n; 18 | if (!ifs){ 19 | throw 0; 20 | } 21 | } catch(long l){ 22 | } 23 | } catch(string s){ 24 | clog << s << endl; 25 | } catch(int n){ 26 | clog << n << endl; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /L12-Exception/quiz.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int x; 6 | try{ 7 | throw x; 8 | } catch(long n){ 9 | cout << "long"; 10 | } catch(int n){ 11 | cout << "int"; 12 | } 13 | cout << endl; 14 | 15 | long y; 16 | try{ 17 | throw y; 18 | } catch(long n){ 19 | cout << "long"; 20 | } catch(int n){ 21 | cout << "int"; 22 | } 23 | cout << endl; 24 | } 25 | -------------------------------------------------------------------------------- /L13-ADT/IntSet-Efficiency/IntSet.cpp: -------------------------------------------------------------------------------- 1 | #include "IntSet.h" 2 | #include 3 | 4 | using namespace std; 5 | 6 | IntSet::IntSet(): numElts(0){ 7 | } 8 | 9 | int IntSet::indexOf(int v){ 10 | int left = 0; 11 | int right = numElts-1; 12 | 13 | while (right >= left){ 14 | int size = right - left + 1; 15 | int middle = left + size/2; 16 | 17 | if (elts[middle] == v) 18 | return middle; 19 | else if (elts[middle] < v) 20 | left = middle+1; 21 | else 22 | right = middle-1; 23 | } 24 | return MAXELTS; 25 | } 26 | 27 | int IntSet::size(){ 28 | return numElts; 29 | } 30 | 31 | bool IntSet::query(int v){ 32 | return (indexOf(v) != MAXELTS); 33 | } 34 | 35 | void IntSet::insert(int v){ 36 | if (indexOf(v) == MAXELTS){ 37 | // duplicate not found 38 | if (numElts == MAXELTS) throw MAXELTS; // no room 39 | 40 | int cand = numElts-1; // largest (last) element 41 | while ((cand >= 0) && elts[cand] > v){ 42 | elts[cand+1] = elts[cand]; 43 | cand--; 44 | } 45 | // Now, cand points to the left of the "gap". 46 | elts[cand+1] = v; 47 | numElts++; // repair invariant 48 | } 49 | } 50 | 51 | void IntSet::remove(int v){ 52 | int victim = indexOf(v); 53 | if (victim != MAXELTS){ 54 | // victim points at hole 55 | numElts--; // one less element 56 | while (victim < numElts){ 57 | // there are elts to our right 58 | elts[victim] = elts[victim+1]; 59 | victim++; 60 | } 61 | } 62 | } 63 | 64 | void IntSet::print(){ 65 | for (int i = 0; i < numElts; i++){ 66 | cout << elts[i] << " " << flush; 67 | } 68 | cout << endl; 69 | } 70 | -------------------------------------------------------------------------------- /L13-ADT/IntSet-Efficiency/IntSet.h: -------------------------------------------------------------------------------- 1 | #ifndef INTSET_H 2 | #define INTSET_H 3 | 4 | const int MAXELTS = 1000000; 5 | 6 | class IntSet 7 | { 8 | // OVERVIEW: a mutable set of integers, |set| <= MAXELTS 9 | int elts[MAXELTS]; 10 | int numElts; 11 | int indexOf(int v); 12 | // EFFECTS: returns the index of v if it exists in the 13 | // array, MAXELTS otherwise. 14 | public: 15 | IntSet(); 16 | // EFFECTS: default constructor. Creates an empty IntSet. 17 | void insert(int v); 18 | // MODIFIES: this 19 | // EFFECTS: this = this + {v} if room, 20 | // throws int MAXELTS otherwise. 21 | void remove(int v); 22 | // MODIFIES: this 23 | // EFFECTS: this = this - {v} if v is in this 24 | bool query(int v); 25 | // EFFECTS: returns true if v is in this, false otherwise. 26 | int size(); 27 | // EFFECTS: returns |this|. 28 | void print(); 29 | // MODIFIES: cout 30 | // EFFECTS: print out the integers contained in the set in 31 | // sequence. 32 | }; 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /L13-ADT/IntSet-Efficiency/Makefile: -------------------------------------------------------------------------------- 1 | # variable definition 2 | 3 | CC = g++ 4 | 5 | DEFS = 6 | LIBS = 7 | INCLUDES = #-I. 8 | HEADERS = 9 | MAINSRCS = main.cpp 10 | OTHSRCS = IntSet.cpp 11 | SRCS = $(MAINSRCS) $(OTHSRCS) 12 | OBJS = $(SRCS:.cpp=.o) 13 | TARGETS = $(MAINSRCS:.cpp=) 14 | 15 | CFLAGS = -g -Wall $(INCLUDES) $(DEFS) 16 | 17 | %.o: %.cpp 18 | $(CC) $(CFLAGS) -o $@ -c $< 19 | 20 | all: $(TARGETS) 21 | 22 | $(TARGETS): $(OBJS) 23 | $(CC) $(CFLAGS) -o $(TARGETS) $(OBJS) $(LIBS) 24 | 25 | depend: 26 | makedepend -Y $(INCLUDES) $(SRCS) 27 | 28 | memcheck: $(TARGETS) 29 | valgrind --leak-check=full ./$(TARGETS) 30 | 31 | clean: 32 | rm -f $(OBJS) $(TARGETS) 33 | 34 | .PHONY: all depend memcheck clean 35 | -------------------------------------------------------------------------------- /L13-ADT/IntSet-Efficiency/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "IntSet.h" 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | IntSet foo; 8 | cout << "insert 7" << endl; 9 | foo.insert(7); 10 | foo.print(); 11 | 12 | cout << "insert 3" << endl; 13 | foo.insert(3); 14 | foo.print(); 15 | 16 | cout << "insert 4" << endl; 17 | foo.insert(4); 18 | foo.print(); 19 | 20 | cout << "insert 5" << endl; 21 | foo.insert(5); 22 | foo.print(); 23 | 24 | cout << "insert 7" << endl; 25 | foo.insert(7); 26 | foo.print(); 27 | 28 | cout << "insert 8" << endl; 29 | foo.insert(8); 30 | foo.print(); 31 | 32 | cout << "remove 5" << endl; 33 | foo.remove(5); 34 | foo.print(); 35 | 36 | cout << "remove 8" << endl; 37 | foo.remove(8); 38 | foo.print(); 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /L13-ADT/IntSet-Efficiency/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "IntSet.h" 6 | 7 | using namespace std; 8 | 9 | int main(int argc, char *argv[]) { 10 | IntSet foo; 11 | int n = atoi(argv[1]); 12 | srand(atoi(argv[2])); 13 | 14 | clock_t begin = clock(); 15 | for(int i=0; i 3 | 4 | using namespace std; 5 | 6 | IntSet::IntSet(): numElts(0){ 7 | } 8 | 9 | int IntSet::indexOf(int v) const{ 10 | for (int i = 0; i < numElts; i++){ 11 | if (elts[i] == v) return i; 12 | } 13 | return MAXELTS; 14 | } 15 | 16 | int IntSet::size() const{ 17 | return numElts; 18 | } 19 | 20 | bool IntSet::query(int v) const{ 21 | return (indexOf(v) != MAXELTS); 22 | } 23 | 24 | void IntSet::insert(int v){ 25 | if (indexOf(v) == MAXELTS){ 26 | if (numElts == MAXELTS) throw MAXELTS; 27 | elts[numElts++] = v; 28 | } 29 | } 30 | 31 | void IntSet::remove(int v){ 32 | int victim = indexOf(v); 33 | if (victim != MAXELTS){ 34 | elts[victim] = elts[numElts-1]; 35 | numElts--; 36 | } 37 | } 38 | 39 | void IntSet::print() const{ 40 | for (int i = 0; i < numElts; i++){ 41 | cout << elts[i] << " " << flush; 42 | } 43 | cout << endl; 44 | } 45 | -------------------------------------------------------------------------------- /L13-ADT/IntSet.h: -------------------------------------------------------------------------------- 1 | #ifndef INTSET_H 2 | #define INTSET_H 3 | 4 | const int MAXELTS = 1000000; 5 | 6 | class IntSet{ 7 | //protected: // Needed to make MaxIntSet work 8 | // OVERVIEW: a mutable set of integers, |set| <= MAXELTS 9 | int elts[MAXELTS]; 10 | int numElts; 11 | int indexOf(int v) const; 12 | // EFFECTS: returns the index of v if it exists in the 13 | // array, MAXELTS otherwise. 14 | public: 15 | IntSet(); 16 | // EFFECTS: default constructor. Creates an empty IntSet. 17 | void insert(int v); 18 | // MODIFIES: this 19 | // EFFECTS: this = this + {v} if room, 20 | // throws int MAXELTS otherwise. 21 | void remove(int v); 22 | // MODIFIES: this 23 | // EFFECTS: this = this - {v} if v is in this 24 | bool query(int v) const; 25 | // EFFECTS: returns true if v is in this, false otherwise. 26 | int size() const; 27 | // EFFECTS: returns |this|. 28 | void print() const; 29 | // MODIFIES: cout 30 | // EFFECTS: print out the integers contained in the set in 31 | // sequence. 32 | }; 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /L13-ADT/Makefile: -------------------------------------------------------------------------------- 1 | # variable definition 2 | 3 | CC = g++ 4 | 5 | DEFS = 6 | LIBS = 7 | INCLUDES = #-I. 8 | HEADERS = #int_t.h int_impl.h 9 | MAINSRCS = main.cpp 10 | OTHSRCS = IntSet.cpp 11 | SRCS = $(MAINSRCS) $(OTHSRCS) 12 | OBJS = $(SRCS:.cpp=.o) 13 | TARGETS = $(MAINSRCS:.cpp=) 14 | 15 | CFLAGS = -g -Wall $(INCLUDES) $(DEFS) 16 | 17 | %.o: %.cpp 18 | $(CC) $(CFLAGS) -o $@ -c $< 19 | 20 | all: $(TARGETS) 21 | 22 | $(TARGETS): $(OBJS) 23 | $(CC) $(CFLAGS) -o $(TARGETS) $(OBJS) $(LIBS) 24 | 25 | depend: 26 | makedepend -Y $(INCLUDES) $(SRCS) 27 | 28 | memcheck: $(TARGETS) 29 | valgrind --leak-check=full ./$(TARGETS) 30 | 31 | clean: 32 | rm -f $(OBJS) $(TARGETS) 33 | 34 | .PHONY: all depend memcheck clean 35 | -------------------------------------------------------------------------------- /L13-ADT/anInt/anInt.cpp: -------------------------------------------------------------------------------- 1 | #include "anInt.h" 2 | 3 | int anInt::getValue(){ 4 | return v; 5 | } 6 | 7 | void anInt::setValue(int v){ 8 | this->v = v; 9 | } 10 | -------------------------------------------------------------------------------- /L13-ADT/anInt/anInt.h: -------------------------------------------------------------------------------- 1 | class anInt{ 2 | int v; 3 | 4 | public: 5 | int getValue(); 6 | void setValue(int v); 7 | }; 8 | -------------------------------------------------------------------------------- /L13-ADT/anInt/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "anInt.h" 3 | using namespace std; 4 | 5 | void addOne(anInt i){ 6 | i.setValue(i.getValue()+1); 7 | } 8 | 9 | int main(){ 10 | anInt o; 11 | o.setValue(0); 12 | //cout << o.v << endl; 13 | addOne(o); 14 | cout << o.getValue() << endl; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /L13-ADT/constructor/constructor.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class C{ 5 | 6 | public: 7 | C(){ 8 | cout << "Constructor of C" << endl; 9 | }; 10 | }; 11 | 12 | class D{ 13 | C c; 14 | 15 | public: 16 | D(C x):c(x){ 17 | }; 18 | }; 19 | 20 | int main(){ 21 | C c; 22 | cout << "after c" << endl; 23 | D d(c); 24 | 25 | return 0; 26 | } 27 | 28 | -------------------------------------------------------------------------------- /L13-ADT/constructor/constructor2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class C{ 5 | 6 | public: 7 | C(){ 8 | cout << "Constructor of C" << endl; 9 | }; 10 | }; 11 | 12 | class D{ 13 | C c; 14 | 15 | public: 16 | D(C x){ 17 | c = x; 18 | }; 19 | }; 20 | 21 | int main(){ 22 | C c; 23 | cout << "after c" << endl; 24 | D d(c); 25 | 26 | return 0; 27 | } 28 | 29 | -------------------------------------------------------------------------------- /L13-ADT/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "IntSet.h" 3 | 4 | using namespace std; 5 | 6 | int main(){ 7 | IntSet foo; 8 | cout << "insert 7" << endl; 9 | foo.insert(7); 10 | foo.print(); 11 | 12 | cout << "insert 3" << endl; 13 | foo.insert(3); 14 | foo.print(); 15 | 16 | cout << "insert 4" << endl; 17 | foo.insert(4); 18 | foo.print(); 19 | 20 | cout << "insert 5" << endl; 21 | foo.insert(5); 22 | foo.print(); 23 | 24 | cout << "insert 7" << endl; 25 | foo.insert(7); 26 | foo.print(); 27 | 28 | cout << "insert 8" << endl; 29 | foo.insert(8); 30 | foo.print(); 31 | 32 | cout << "remove 5" << endl; 33 | foo.remove(5); 34 | foo.print(); 35 | 36 | cout << "remove 8" << endl; 37 | foo.remove(8); 38 | foo.print(); 39 | 40 | cout << "remove 6" << endl; 41 | foo.remove(6); 42 | foo.print(); 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /L13-ADT/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "IntSet.h" 6 | 7 | using namespace std; 8 | 9 | int main(int argc, char *argv[]) { 10 | IntSet foo; 11 | int n = atoi(argv[1]); 12 | srand(atoi(argv[2])); 13 | 14 | clock_t begin = clock(); 15 | for(int i=0; i 3 | #include 4 | 5 | using namespace std; 6 | 7 | IntSet::IntSet(): numElts(0){ 8 | } 9 | 10 | int IntSet::indexOf(int v){ 11 | for (int i = 0; i < numElts; i++){ 12 | if (elts[i] == v) 13 | return i; 14 | } 15 | return MAXELTS; 16 | } 17 | 18 | int IntSet::size(){ 19 | return numElts; 20 | } 21 | 22 | bool IntSet::query(int v){ 23 | return (indexOf(v) != MAXELTS); 24 | } 25 | 26 | void IntSet::insert(int v){ 27 | if (indexOf(v) == MAXELTS){ 28 | if (numElts == MAXELTS) 29 | throw MAXELTS; 30 | elts[numElts++] = v; 31 | } 32 | } 33 | 34 | void IntSet::remove(int v){ 35 | int victim = indexOf(v); 36 | if (victim != MAXELTS){ 37 | elts[victim] = elts[numElts-1]; 38 | numElts--; 39 | } else{ 40 | throw v; 41 | } 42 | } 43 | 44 | void IntSet::print(){ 45 | for (int i = 0; i < numElts; i++){ 46 | cout << elts[i] << " " << flush; 47 | } 48 | cout << endl; 49 | } 50 | 51 | int MaxIntSet::max(){ 52 | int so_far = elts[0]; 53 | for(int i = 1; i < numElts; i++){ 54 | if (elts[i] > so_far) 55 | so_far = elts[i]; 56 | } 57 | return so_far; 58 | } 59 | 60 | /* 61 | int MaxIntSet::max(){ 62 | int i; 63 | for (i=INT_MAX; i>=INT_MIN; i--){ 64 | if(query(i)) return i; 65 | } 66 | } 67 | */ 68 | -------------------------------------------------------------------------------- /L15-Subtype/1-IntSet-private/IntSet.h: -------------------------------------------------------------------------------- 1 | #ifndef INTSET_H 2 | #define INTSET_H 3 | 4 | const int MAXELTS = 100; 5 | 6 | class IntSet{ 7 | // OVERVIEW: a mutable set of integers, |set| <= MAXELTS 8 | int elts[MAXELTS]; 9 | int numElts; 10 | int indexOf(int v); 11 | // EFFECTS: returns the index of v if it exists in the 12 | // array, MAXELTS otherwise. 13 | public: 14 | IntSet(); 15 | // EFFECTS: default constructor. Creates an empty IntSet. 16 | void insert(int v); 17 | // MODIFIES: this 18 | // EFFECTS: this = this + {v} if room, 19 | // throws int MAXELTS otherwise. 20 | void remove(int v); 21 | // MODIFIES: this 22 | // EFFECTS: this = this - {v} if v is in this 23 | // throws int v otherwise. 24 | bool query(int v); 25 | // EFFECTS: returns true if v is in this, false otherwise. 26 | int size(); 27 | // EFFECTS: returns |this|. 28 | void print(); 29 | // MODIFIES: cout 30 | // EFFECTS: print out the integers contained in the set in 31 | // sequence. 32 | }; 33 | 34 | class MaxIntSet: public IntSet{ 35 | // OVERVIEW: a set of non-negative integers, 36 | // where |set| <= 100 37 | public: 38 | int max(); 39 | // REQUIRES: set is non-empty 40 | // EFFECTS: returns largest element in set. 41 | }; 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /L15-Subtype/1-IntSet-private/Makefile: -------------------------------------------------------------------------------- 1 | # variable definition 2 | 3 | CC = g++ 4 | 5 | DEFS = 6 | LIBS = 7 | INCLUDES = #-I. 8 | HEADERS = #int_t.h int_impl.h 9 | MAINSRCS = main.cpp 10 | OTHSRCS = IntSet.cpp 11 | SRCS = $(MAINSRCS) $(OTHSRCS) 12 | OBJS = $(SRCS:.cpp=.o) 13 | TARGETS = $(MAINSRCS:.cpp=) 14 | 15 | CFLAGS = -g -Wall $(INCLUDES) $(DEFS) 16 | 17 | %.o: %.cpp 18 | $(CC) $(CFLAGS) -o $@ -c $< 19 | 20 | all: $(TARGETS) 21 | 22 | $(TARGETS): $(OBJS) 23 | $(CC) $(CFLAGS) -o $(TARGETS) $(OBJS) $(LIBS) 24 | 25 | depend: 26 | makedepend -Y $(INCLUDES) $(SRCS) 27 | 28 | memcheck: $(TARGETS) 29 | valgrind --leak-check=full ./$(TARGETS) 30 | 31 | clean: 32 | rm -f $(OBJS) $(TARGETS) 33 | 34 | .PHONY: all depend memcheck clean 35 | -------------------------------------------------------------------------------- /L15-Subtype/1-IntSet-private/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "IntSet.h" 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | MaxIntSet foo; 8 | cout << "insert 7" << endl; 9 | foo.insert(7); 10 | foo.print(); 11 | 12 | cout << "insert 3" << endl; 13 | foo.insert(3); 14 | foo.print(); 15 | 16 | cout << "insert 4" << endl; 17 | foo.insert(4); 18 | foo.print(); 19 | 20 | cout << "insert 2147473203" << endl; 21 | foo.insert(2147473203); 22 | foo.print(); 23 | 24 | cout << "The max value is " << foo.max() << endl; 25 | 26 | /* 27 | cout << "insert 7" << endl; 28 | foo.insert(7); 29 | foo.print(); 30 | 31 | cout << "insert 8" << endl; 32 | foo.insert(8); 33 | foo.print(); 34 | 35 | cout << "remove 5" << endl; 36 | foo.remove(5); 37 | foo.print(); 38 | 39 | cout << "remove 8" << endl; 40 | foo.remove(8); 41 | foo.print(); 42 | */ 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /L15-Subtype/2-IntSet-protected/IntSet.cpp: -------------------------------------------------------------------------------- 1 | #include "IntSet.h" 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | IntSet::IntSet(): numElts(0){ 8 | } 9 | 10 | int IntSet::indexOf(int v){ 11 | for (int i = 0; i < numElts; i++){ 12 | if (elts[i] == v) 13 | return i; 14 | } 15 | return MAXELTS; 16 | } 17 | 18 | int IntSet::size(){ 19 | return numElts; 20 | } 21 | 22 | bool IntSet::query(int v){ 23 | return (indexOf(v) != MAXELTS); 24 | } 25 | 26 | void IntSet::insert(int v){ 27 | if (indexOf(v) == MAXELTS){ 28 | if (numElts == MAXELTS) 29 | throw MAXELTS; 30 | elts[numElts++] = v; 31 | } 32 | } 33 | 34 | void IntSet::remove(int v){ 35 | int victim = indexOf(v); 36 | if (victim != MAXELTS){ 37 | elts[victim] = elts[numElts-1]; 38 | numElts--; 39 | } else{ 40 | throw v; 41 | } 42 | } 43 | 44 | void IntSet::print(){ 45 | for (int i = 0; i < numElts; i++){ 46 | cout << elts[i] << " " << flush; 47 | } 48 | cout << endl; 49 | } 50 | 51 | int MaxIntSet::max(){ 52 | int so_far = elts[0]; 53 | for(int i = 1; i < numElts; i++){ 54 | if (elts[i] > so_far) 55 | so_far = elts[i]; 56 | } 57 | return so_far; 58 | } 59 | -------------------------------------------------------------------------------- /L15-Subtype/2-IntSet-protected/IntSet.h: -------------------------------------------------------------------------------- 1 | #ifndef INTSET_H 2 | #define INTSET_H 3 | 4 | const int MAXELTS = 100; 5 | 6 | class IntSet{ 7 | // OVERVIEW: a mutable set of integers, |set| <= MAXELTS 8 | protected: 9 | int elts[MAXELTS]; 10 | int numElts; 11 | int indexOf(int v); 12 | // EFFECTS: returns the index of v if it exists in the 13 | // array, MAXELTS otherwise. 14 | public: 15 | IntSet(); 16 | // EFFECTS: default constructor. Creates an empty IntSet. 17 | void insert(int v); 18 | // MODIFIES: this 19 | // EFFECTS: this = this + {v} if room, 20 | // throws int MAXELTS otherwise. 21 | void remove(int v); 22 | // MODIFIES: this 23 | // EFFECTS: this = this - {v} if v is in this 24 | // throws int v otherwise. 25 | bool query(int v); 26 | // EFFECTS: returns true if v is in this, false otherwise. 27 | int size(); 28 | // EFFECTS: returns |this|. 29 | void print(); 30 | // MODIFIES: cout 31 | // EFFECTS: print out the integers contained in the set in 32 | // sequence. 33 | }; 34 | 35 | class MaxIntSet: public IntSet{ 36 | // OVERVIEW: a set of non-negative integers, 37 | // where |set| <= 100 38 | public: 39 | int max(); 40 | // REQUIRES: set is non-empty 41 | // EFFECTS: returns largest element in set. 42 | }; 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /L15-Subtype/2-IntSet-protected/Makefile: -------------------------------------------------------------------------------- 1 | # variable definition 2 | 3 | CC = g++ 4 | 5 | DEFS = 6 | LIBS = 7 | INCLUDES = #-I. 8 | HEADERS = #int_t.h int_impl.h 9 | MAINSRCS = main.cpp 10 | OTHSRCS = IntSet.cpp 11 | SRCS = $(MAINSRCS) $(OTHSRCS) 12 | OBJS = $(SRCS:.cpp=.o) 13 | TARGETS = $(MAINSRCS:.cpp=) 14 | 15 | CFLAGS = -g -Wall $(INCLUDES) $(DEFS) 16 | 17 | %.o: %.cpp 18 | $(CC) $(CFLAGS) -o $@ -c $< 19 | 20 | all: $(TARGETS) 21 | 22 | $(TARGETS): $(OBJS) 23 | $(CC) $(CFLAGS) -o $(TARGETS) $(OBJS) $(LIBS) 24 | 25 | depend: 26 | makedepend -Y $(INCLUDES) $(SRCS) 27 | 28 | memcheck: $(TARGETS) 29 | valgrind --leak-check=full ./$(TARGETS) 30 | 31 | clean: 32 | rm -f $(OBJS) $(TARGETS) 33 | 34 | .PHONY: all depend memcheck clean 35 | -------------------------------------------------------------------------------- /L15-Subtype/2-IntSet-protected/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "IntSet.h" 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | MaxIntSet foo; 8 | cout << "insert 7" << endl; 9 | foo.insert(7); 10 | foo.print(); 11 | 12 | cout << "insert 3" << endl; 13 | foo.insert(3); 14 | foo.print(); 15 | 16 | cout << "insert 4" << endl; 17 | foo.insert(4); 18 | foo.print(); 19 | 20 | cout << "insert 5" << endl; 21 | foo.insert(5); 22 | foo.print(); 23 | 24 | cout << "The max value is " << foo.max() << endl; 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /L15-Subtype/3-PosIntSet/IntSet.cpp: -------------------------------------------------------------------------------- 1 | #include "IntSet.h" 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | IntSet::IntSet(): numElts(0){ 8 | } 9 | 10 | int IntSet::indexOf(int v){ 11 | for (int i = 0; i < numElts; i++){ 12 | if (elts[i] == v) 13 | return i; 14 | } 15 | return MAXELTS; 16 | } 17 | 18 | int IntSet::size(){ 19 | return numElts; 20 | } 21 | 22 | bool IntSet::query(int v){ 23 | return (indexOf(v) != MAXELTS); 24 | } 25 | 26 | void IntSet::insert(int v){ 27 | if (indexOf(v) == MAXELTS){ 28 | if (numElts == MAXELTS) 29 | throw MAXELTS; 30 | elts[numElts++] = v; 31 | } 32 | } 33 | 34 | void IntSet::remove(int v){ 35 | int victim = indexOf(v); 36 | if (victim != MAXELTS){ 37 | elts[victim] = elts[numElts-1]; 38 | numElts--; 39 | } else{ 40 | throw v; 41 | } 42 | } 43 | 44 | void IntSet::print(){ 45 | for (int i = 0; i < numElts; i++){ 46 | cout << elts[i] << " " << flush; 47 | } 48 | cout << endl; 49 | } 50 | 51 | void PosIntSet::insert(int v){ 52 | if (v < 0) throw -1; 53 | IntSet::insert(v); 54 | } 55 | -------------------------------------------------------------------------------- /L15-Subtype/3-PosIntSet/IntSet.h: -------------------------------------------------------------------------------- 1 | #ifndef INTSET_H 2 | #define INTSET_H 3 | 4 | const int MAXELTS = 100; 5 | 6 | class IntSet{ 7 | // OVERVIEW: a mutable set of integers, |set| <= MAXELTS 8 | protected: 9 | int elts[MAXELTS]; 10 | int numElts; 11 | int indexOf(int v); 12 | // EFFECTS: returns the index of v if it exists in the 13 | // array, MAXELTS otherwise. 14 | public: 15 | IntSet(); 16 | // EFFECTS: default constructor. Creates an empty IntSet. 17 | 18 | void insert(int v); 19 | // MODIFIES: this 20 | // EFFECTS: this = this + {v} if room, 21 | // throws int MAXELTS otherwise. 22 | void remove(int v); 23 | // MODIFIES: this 24 | // EFFECTS: this = this - {v} if v is in this 25 | // throws int v otherwise. 26 | bool query(int v); 27 | // EFFECTS: returns true if v is in this, false otherwise. 28 | int size(); 29 | // EFFECTS: returns |this|. 30 | void print(); 31 | // MODIFIES: cout 32 | // EFFECTS: print out the integers contained in the set in 33 | // sequence. 34 | }; 35 | 36 | class PosIntSet : public IntSet{ 37 | // OVERVIEW: a subclass, but not a subtype 38 | public: 39 | void insert(int v); 40 | // EFFECTS: if v is non-negative, 41 | // and s has room to include it, 42 | // s = s + {v}. 43 | // if v is negative throw int -1 44 | // if s is full thrown int MAXELTS 45 | }; 46 | 47 | #endif 48 | -------------------------------------------------------------------------------- /L15-Subtype/3-PosIntSet/Makefile: -------------------------------------------------------------------------------- 1 | # variable definition 2 | 3 | CC = g++ 4 | 5 | DEFS = 6 | LIBS = 7 | INCLUDES = #-I. 8 | HEADERS = #int_t.h int_impl.h 9 | MAINSRCS = main.cpp 10 | OTHSRCS = IntSet.cpp 11 | SRCS = $(MAINSRCS) $(OTHSRCS) 12 | OBJS = $(SRCS:.cpp=.o) 13 | TARGETS = $(MAINSRCS:.cpp=) 14 | 15 | CFLAGS = -g -Wall $(INCLUDES) $(DEFS) 16 | 17 | %.o: %.cpp 18 | $(CC) $(CFLAGS) -o $@ -c $< 19 | 20 | all: $(TARGETS) 21 | 22 | $(TARGETS): $(OBJS) 23 | $(CC) $(CFLAGS) -o $(TARGETS) $(OBJS) $(LIBS) 24 | 25 | depend: 26 | makedepend -Y $(INCLUDES) $(SRCS) 27 | 28 | memcheck: $(TARGETS) 29 | valgrind --leak-check=full ./$(TARGETS) 30 | 31 | clean: 32 | rm -f $(OBJS) $(TARGETS) 33 | 34 | .PHONY: all depend memcheck clean 35 | -------------------------------------------------------------------------------- /L15-Subtype/3-PosIntSet/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "IntSet.h" 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | PosIntSet s; 8 | try { 9 | cout << "Insert -1 through s itself" << endl; 10 | s.insert(-1); 11 | cout << "Insertion successful!" << endl; 12 | } catch (int i) { 13 | cout << "Exception thrown" << endl; 14 | cout << "Insertion failed!" << endl; 15 | } 16 | IntSet& r = s; 17 | try { 18 | cout << "Insert -2 through the reference to s" << endl; 19 | r.insert(-2); 20 | cout << "Insertion successful!" << endl; 21 | } catch (int i) { 22 | cout << "Exception thrown" << endl; 23 | cout << "Insertion failed!" << endl; 24 | } 25 | IntSet* p = &s; 26 | try { 27 | cout << "Insert -3 through the pointer to s" << endl; 28 | p->insert(-3); 29 | cout << "Insertion successful!" << endl; 30 | } catch (int i) { 31 | cout << "Exception thrown" << endl; 32 | cout << "Insertion failed!" << endl; 33 | } 34 | 35 | s.print(); 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /L15-Subtype/4-virtual-PosIntSet/IntSet.cpp: -------------------------------------------------------------------------------- 1 | #include "IntSet.h" 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | IntSet::IntSet(): numElts(0){ 8 | } 9 | 10 | int IntSet::indexOf(int v){ 11 | for (int i = 0; i < numElts; i++){ 12 | if (elts[i] == v) 13 | return i; 14 | } 15 | return MAXELTS; 16 | } 17 | 18 | int IntSet::size(){ 19 | return numElts; 20 | } 21 | 22 | bool IntSet::query(int v){ 23 | return (indexOf(v) != MAXELTS); 24 | } 25 | 26 | void IntSet::insert(int v){ 27 | if (indexOf(v) == MAXELTS){ 28 | if (numElts == MAXELTS) 29 | throw MAXELTS; 30 | elts[numElts++] = v; 31 | } 32 | } 33 | 34 | void IntSet::remove(int v){ 35 | int victim = indexOf(v); 36 | if (victim != MAXELTS){ 37 | elts[victim] = elts[numElts-1]; 38 | numElts--; 39 | } else{ 40 | throw v; 41 | } 42 | } 43 | 44 | void IntSet::print(){ 45 | for (int i = 0; i < numElts; i++){ 46 | cout << elts[i] << " " << flush; 47 | } 48 | cout << endl; 49 | } 50 | 51 | void PosIntSet::insert(int v){ 52 | if (v < 0) throw -1; 53 | IntSet::insert(v); 54 | } 55 | -------------------------------------------------------------------------------- /L15-Subtype/4-virtual-PosIntSet/IntSet.h: -------------------------------------------------------------------------------- 1 | #ifndef INTSET_H 2 | #define INTSET_H 3 | 4 | const int MAXELTS = 100; 5 | 6 | class IntSet{ 7 | // OVERVIEW: a mutable set of integers, |set| <= MAXELTS 8 | protected: 9 | int elts[MAXELTS]; 10 | int numElts; 11 | int indexOf(int v); 12 | // EFFECTS: returns the index of v if it exists in the 13 | // array, MAXELTS otherwise. 14 | public: 15 | IntSet(); 16 | // EFFECTS: default constructor. Creates an empty IntSet. 17 | virtual void insert(int v); 18 | // void insert(int v); 19 | // MODIFIES: this 20 | // EFFECTS: this = this + {v} if room, 21 | // throws int MAXELTS otherwise. 22 | void remove(int v); 23 | // MODIFIES: this 24 | // EFFECTS: this = this - {v} if v is in this 25 | // throws int v otherwise. 26 | bool query(int v); 27 | // EFFECTS: returns true if v is in this, false otherwise. 28 | int size(); 29 | // EFFECTS: returns |this|. 30 | void print(); 31 | // MODIFIES: cout 32 | // EFFECTS: print out the integers contained in the set in 33 | // sequence. 34 | }; 35 | 36 | class PosIntSet : public IntSet{ 37 | // OVERVIEW: a subclass, but not a subtype 38 | public: 39 | void insert(int v); 40 | // EFFECTS: if v is non-negative, 41 | // and s has room to include it, 42 | // s = s + {v}. 43 | // if v is negative throw int -1 44 | // if s is full thrown int MAXELTS 45 | }; 46 | 47 | #endif 48 | -------------------------------------------------------------------------------- /L15-Subtype/4-virtual-PosIntSet/Makefile: -------------------------------------------------------------------------------- 1 | # variable definition 2 | 3 | CC = g++ 4 | 5 | DEFS = 6 | LIBS = 7 | INCLUDES = #-I. 8 | HEADERS = #int_t.h int_impl.h 9 | MAINSRCS = main.cpp 10 | OTHSRCS = IntSet.cpp 11 | SRCS = $(MAINSRCS) $(OTHSRCS) 12 | OBJS = $(SRCS:.cpp=.o) 13 | TARGETS = $(MAINSRCS:.cpp=) 14 | 15 | CFLAGS = -g -Wall $(INCLUDES) $(DEFS) 16 | 17 | %.o: %.cpp 18 | $(CC) $(CFLAGS) -o $@ -c $< 19 | 20 | all: $(TARGETS) 21 | 22 | $(TARGETS): $(OBJS) 23 | $(CC) $(CFLAGS) -o $(TARGETS) $(OBJS) $(LIBS) 24 | 25 | depend: 26 | makedepend -Y $(INCLUDES) $(SRCS) 27 | 28 | memcheck: $(TARGETS) 29 | valgrind --leak-check=full ./$(TARGETS) 30 | 31 | clean: 32 | rm -f $(OBJS) $(TARGETS) 33 | 34 | .PHONY: all depend memcheck clean 35 | -------------------------------------------------------------------------------- /L15-Subtype/4-virtual-PosIntSet/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "IntSet.h" 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | PosIntSet s; 8 | try { 9 | cout << "Insert -1 through s itself" << endl; 10 | s.insert(-1); 11 | cout << "Insertion successful!" << endl; 12 | } catch (int i) { 13 | cout << "Exception thrown" << endl; 14 | cout << "Insertion failed!" << endl; 15 | } 16 | IntSet& r = s; 17 | try { 18 | cout << "Insert -2 through the reference to s" << endl; 19 | r.insert(-2); 20 | cout << "Insertion successful!" << endl; 21 | } catch (int i) { 22 | cout << "Exception thrown" << endl; 23 | cout << "Insertion failed!" << endl; 24 | } 25 | IntSet* p = &s; 26 | try { 27 | cout << "Insert -3 through the pointer to s" << endl; 28 | p->insert(-3); 29 | cout << "Insertion successful!" << endl; 30 | } catch (int i) { 31 | cout << "Exception thrown" << endl; 32 | cout << "Insertion failed!" << endl; 33 | } 34 | 35 | s.print(); 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /L15-Subtype/MaxIntSet/IntSet.cpp: -------------------------------------------------------------------------------- 1 | #include "IntSet.h" 2 | #include 3 | 4 | using namespace std; 5 | 6 | IntSet::IntSet(): numElts(0){ 7 | } 8 | 9 | int IntSet::indexOf(int v) const{ 10 | for (int i = 0; i < numElts; i++){ 11 | if (elts[i] == v) return i; 12 | } 13 | return MAXELTS; 14 | } 15 | 16 | int IntSet::size() const{ 17 | return numElts; 18 | } 19 | 20 | bool IntSet::query(int v) const{ 21 | return (indexOf(v) != MAXELTS); 22 | } 23 | 24 | void IntSet::insert(int v){ 25 | if (indexOf(v) == MAXELTS){ 26 | if (numElts == MAXELTS) throw MAXELTS; 27 | elts[numElts++] = v; 28 | } 29 | } 30 | 31 | void IntSet::remove(int v){ 32 | int victim = indexOf(v); 33 | if (victim != MAXELTS){ 34 | elts[victim] = elts[numElts-1]; 35 | numElts--; 36 | } 37 | } 38 | 39 | void IntSet::print() const{ 40 | for (int i = 0; i < numElts; i++){ 41 | cout << elts[i] << " " << flush; 42 | } 43 | cout << endl; 44 | } 45 | -------------------------------------------------------------------------------- /L15-Subtype/MaxIntSet/IntSet.h: -------------------------------------------------------------------------------- 1 | #ifndef INTSET_H 2 | #define INTSET_H 3 | 4 | const int MAXELTS = 1000000; 5 | 6 | class IntSet{ 7 | //protected: // Needed to make MaxIntSet work 8 | // OVERVIEW: a mutable set of integers, |set| <= MAXELTS 9 | int elts[MAXELTS]; 10 | int numElts; 11 | int indexOf(int v) const; 12 | // EFFECTS: returns the index of v if it exists in the 13 | // array, MAXELTS otherwise. 14 | public: 15 | IntSet(); 16 | // EFFECTS: default constructor. Creates an empty IntSet. 17 | void insert(int v); 18 | // MODIFIES: this 19 | // EFFECTS: this = this + {v} if room, 20 | // throws int MAXELTS otherwise. 21 | void remove(int v); 22 | // MODIFIES: this 23 | // EFFECTS: this = this - {v} if v is in this 24 | bool query(int v) const; 25 | // EFFECTS: returns true if v is in this, false otherwise. 26 | int size() const; 27 | // EFFECTS: returns |this|. 28 | void print() const; 29 | // MODIFIES: cout 30 | // EFFECTS: print out the integers contained in the set in 31 | // sequence. 32 | }; 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /L15-Subtype/MaxIntSet/Makefile: -------------------------------------------------------------------------------- 1 | # variable definition 2 | 3 | CC = g++ 4 | 5 | DEFS = 6 | LIBS = 7 | INCLUDES = #-I. 8 | HEADERS = #int_t.h int_impl.h 9 | MAINSRCS = test.cpp 10 | OTHSRCS = IntSet.cpp 11 | SRCS = $(MAINSRCS) $(OTHSRCS) 12 | OBJS = $(SRCS:.cpp=.o) 13 | TARGETS = $(MAINSRCS:.cpp=) 14 | 15 | CFLAGS = -g -Wall $(INCLUDES) $(DEFS) 16 | 17 | %.o: %.cpp 18 | $(CC) $(CFLAGS) -o $@ -c $< 19 | 20 | all: $(TARGETS) 21 | 22 | $(TARGETS): $(OBJS) 23 | $(CC) $(CFLAGS) -o $(TARGETS) $(OBJS) $(LIBS) 24 | 25 | depend: 26 | makedepend -Y $(INCLUDES) $(SRCS) 27 | 28 | memcheck: $(TARGETS) 29 | valgrind --leak-check=full ./$(TARGETS) 30 | 31 | clean: 32 | rm -f $(OBJS) $(TARGETS) 33 | 34 | .PHONY: all depend memcheck clean 35 | -------------------------------------------------------------------------------- /L15-Subtype/MaxIntSet/MaxIntSet.cpp: -------------------------------------------------------------------------------- 1 | #include "MaxIntSet.h" 2 | 3 | // Only works if data members of IntSet are protected 4 | int MaxIntSet::max() const{ 5 | int m = elts[0]; 6 | for (int i=1; i m) 8 | m = elts[i]; 9 | } 10 | return m; 11 | } 12 | -------------------------------------------------------------------------------- /L15-Subtype/MaxIntSet/MaxIntSet.h: -------------------------------------------------------------------------------- 1 | #include "IntSet.h" 2 | 3 | class MaxIntSet: public IntSet{ 4 | 5 | public: 6 | int max() const; 7 | }; 8 | -------------------------------------------------------------------------------- /L15-Subtype/MaxIntSet/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "IntSet.h" 3 | 4 | using namespace std; 5 | 6 | int main(){ 7 | IntSet foo; 8 | cout << "insert 7" << endl; 9 | foo.insert(7); 10 | foo.print(); 11 | 12 | cout << "insert 3" << endl; 13 | foo.insert(3); 14 | foo.print(); 15 | 16 | cout << "insert 4" << endl; 17 | foo.insert(4); 18 | foo.print(); 19 | 20 | cout << "insert 5" << endl; 21 | foo.insert(5); 22 | foo.print(); 23 | 24 | cout << "insert 7" << endl; 25 | foo.insert(7); 26 | foo.print(); 27 | 28 | cout << "insert 8" << endl; 29 | foo.insert(8); 30 | foo.print(); 31 | 32 | cout << "remove 5" << endl; 33 | foo.remove(5); 34 | foo.print(); 35 | 36 | cout << "remove 8" << endl; 37 | foo.remove(8); 38 | foo.print(); 39 | 40 | cout << "remove 6" << endl; 41 | foo.remove(6); 42 | foo.print(); 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /L16-Interface/IntSet-Interface/include/IntSet.h: -------------------------------------------------------------------------------- 1 | #ifndef INTSET_H 2 | #define INTSET_H 3 | 4 | #include 5 | 6 | class IntSetFullException: public std::exception{ 7 | public: 8 | std::string what(); 9 | }; 10 | 11 | class IntSet{ 12 | // OVERVIEW: a mutable set of integers, |set| <= MAXELTS 13 | public: 14 | virtual void insert(int v) = 0; 15 | // MODIFIES: this 16 | // EFFECTS: this = this + {v} if room, 17 | // throws IntSetFullException otherwise. 18 | virtual void remove(int v) = 0; 19 | // MODIFIES: this 20 | // EFFECTS: this = this - {v} if v is in this 21 | virtual bool query(int v) const = 0; 22 | // EFFECTS: returns true if v is in this, false otherwise. 23 | virtual int size() const = 0; 24 | // EFFECTS: returns |this|. 25 | virtual void print() const = 0; 26 | // MODIFIES: cout 27 | // EFFECTS: print out the integers contained in the set in 28 | // sequence. 29 | }; 30 | 31 | IntSet *getIntSet(); 32 | // EFFECTS: returns a pointer to the IntSet 33 | #endif 34 | -------------------------------------------------------------------------------- /L16-Interface/IntSet-Interface/lib/.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore everything in this directory 2 | * 3 | # Except this file 4 | !.gitignore 5 | -------------------------------------------------------------------------------- /L16-Interface/IntSet-Interface/provider/IntSetImpl.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "IntSet.h" 3 | 4 | using namespace std; 5 | 6 | const int MAXELTS = 100; 7 | 8 | string IntSetFullException::what(){ 9 | return "IntSet is full."; 10 | } 11 | 12 | class IntSetImpl: public IntSet{ 13 | // OVERVIEW: a mutable set of integers, |set| <= MAXELTS 14 | int elts[MAXELTS]; 15 | int numElts; 16 | int indexOf(int v) const; 17 | // EFFECTS: returns the index of v if it exists in the 18 | // array, MAXELTS otherwise. 19 | public: 20 | IntSetImpl(); 21 | // EFFECTS: default constructor. Creates an empty IntSet. 22 | void insert(int v); 23 | // MODIFIES: this 24 | // EFFECTS: this = this + {v} if room, 25 | // throws IntSetFullException otherwise. 26 | void remove(int v); 27 | // MODIFIES: this 28 | // EFFECTS: this = this - {v} if v is in this 29 | bool query(int v) const; 30 | // EFFECTS: returns true if v is in this, false otherwise. 31 | int size() const; 32 | // EFFECTS: returns |this|. 33 | void print() const; 34 | // MODIFIES: cout 35 | // EFFECTS: print out the integers contained in the set in 36 | // sequence. 37 | }; 38 | 39 | IntSetImpl::IntSetImpl(): numElts(0){ 40 | } 41 | 42 | int IntSetImpl::indexOf(int v) const{ 43 | for (int i = 0; i < numElts; i++){ 44 | if (elts[i] == v) return i; 45 | } 46 | return MAXELTS; 47 | } 48 | 49 | int IntSetImpl::size() const{ 50 | return numElts; 51 | } 52 | 53 | bool IntSetImpl::query(int v) const{ 54 | return (indexOf(v) != MAXELTS); 55 | } 56 | 57 | void IntSetImpl::insert(int v){ 58 | if (indexOf(v) == MAXELTS){ 59 | if (numElts == MAXELTS) throw MAXELTS; 60 | elts[numElts++] = v; 61 | } 62 | } 63 | 64 | void IntSetImpl::remove(int v){ 65 | int victim = indexOf(v); 66 | if (victim != MAXELTS){ 67 | elts[victim] = elts[numElts-1]; 68 | numElts--; 69 | } 70 | } 71 | 72 | void IntSetImpl::print() const{ 73 | for (int i = 0; i < numElts; i++){ 74 | cout << elts[i] << " " << flush; 75 | } 76 | cout << endl; 77 | } 78 | 79 | static IntSetImpl intSet; 80 | 81 | IntSet *getIntSet(){ 82 | return &intSet; 83 | } 84 | -------------------------------------------------------------------------------- /L16-Interface/IntSet-Interface/provider/Makefile: -------------------------------------------------------------------------------- 1 | CC = g++ 2 | CFLAGS = -g -Wall -std=c++11 3 | INCLUDE = -I../include 4 | 5 | IntSet.o: IntSetImpl.cpp 6 | $(CC) $(CFLAGS) $(INCLUDE) -o IntSet.o -c IntSetImpl.cpp 7 | cp IntSet.o ../lib/IntSet.o 8 | 9 | clean: 10 | rm -f *.o 11 | 12 | .PHONY: all clean 13 | -------------------------------------------------------------------------------- /L16-Interface/IntSet-Interface/user/Makefile: -------------------------------------------------------------------------------- 1 | CC = g++ 2 | CFLAGS = -g -Wall -std=c++11 3 | TARGET = main 4 | INCLUDE = -I../include 5 | OBJS = main.o ../lib/IntSet.o 6 | 7 | %.o: %.cpp 8 | $(CC) $(CFLAGS) $(INCLUDE) -o $@ -c $< 9 | 10 | all: $(TARGET) 11 | 12 | $(TARGET): $(OBJS) 13 | $(CC) $(CFLAGS) -o $(TARGET) $(OBJS) 14 | 15 | clean: 16 | rm -f $(OBJS) $(TARGET) 17 | 18 | .PHONY: all clean 19 | -------------------------------------------------------------------------------- /L16-Interface/IntSet-Interface/user/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "IntSet.h" 3 | 4 | using namespace std; 5 | 6 | int main(){ 7 | IntSet *foo = getIntSet(); 8 | cout << "insert 7" << endl; 9 | foo->insert(7); 10 | foo->print(); 11 | 12 | cout << "insert 3" << endl; 13 | foo->insert(3); 14 | foo->print(); 15 | 16 | cout << "insert 4" << endl; 17 | foo->insert(4); 18 | foo->print(); 19 | 20 | cout << "insert 5" << endl; 21 | foo->insert(5); 22 | foo->print(); 23 | 24 | cout << "insert 7" << endl; 25 | foo->insert(7); 26 | foo->print(); 27 | 28 | cout << "insert 8" << endl; 29 | foo->insert(8); 30 | foo->print(); 31 | 32 | cout << "remove 5" << endl; 33 | foo->remove(5); 34 | foo->print(); 35 | 36 | cout << "remove 8" << endl; 37 | foo->remove(8); 38 | foo->print(); 39 | 40 | cout << "remove 6" << endl; 41 | foo->remove(6); 42 | foo->print(); 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /L17-Dynamic-Memory/Dynamic-Allocation/Makefile: -------------------------------------------------------------------------------- 1 | CXX := g++ 2 | CXXFLAGS := -Wall -std=c++11 3 | LDFLAGS := -L/usr/lib -lstdc++ -lm 4 | INCLUDE := 5 | SRC := $(wildcard *.cpp) 6 | OBJECTS := $(SRC:%.cpp=%.o) 7 | TARGETS := $(SRC:%.cpp=%) 8 | 9 | all: $(TARGETS) 10 | 11 | %: %.o 12 | $(CXX) $(CXXFLAGS) -o $(APP_DIR)/$(TARGET) $^ $(LDFLAGS) 13 | 14 | %.o: %.cpp 15 | $(CXX) $(CXXFLAGS) $(INCLUDE) -c $< -o $@ $(LDFLAGS) 16 | 17 | .PHONY: all clean debug release 18 | 19 | debug: CXXFLAGS += -DDEBUG -g 20 | debug: all 21 | 22 | release: CXXFLAGS += -O2 23 | release: all 24 | 25 | clean: 26 | rm -rvf $(TARGETS) $(OBJECTS) 27 | -------------------------------------------------------------------------------- /L17-Dynamic-Memory/Dynamic-Allocation/delete.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int *p = new int(0); 6 | cout << *p << endl; 7 | (*p) ++; 8 | cout << *p << endl; 9 | delete p; 10 | 11 | int n = 10; 12 | p = &n; 13 | cout << *p << endl; 14 | delete p; 15 | } 16 | -------------------------------------------------------------------------------- /L17-Dynamic-Memory/Dynamic-Allocation/dynamicAllocation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int *p = new int(2019); 6 | cout << "p=" << p << "->" << *p << endl; 7 | 8 | int *q = p; 9 | cout << "q=" << q << "->" << *q << endl; 10 | delete p; 11 | delete q; 12 | 13 | int *r = new int; 14 | cout << "r=" << r << "->" << *r << endl; 15 | cout << "q=" << q << "->" << *q << endl; 16 | delete r; 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /L17-Dynamic-Memory/Dynamic-Allocation/leak.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main(int argc, char *argv[]){ 6 | int n = atoi(argv[1]); 7 | 8 | for(int i=0; i 2 | #include 3 | using namespace std; 4 | 5 | int main(int argc, char *argv[]){ 6 | int n = atoi(argv[1]); 7 | int m = atoi(argv[2]); 8 | 9 | for(int i=0; i 3 | 4 | using namespace std; 5 | 6 | IntSet::IntSet(int size): elts(new int[size]), 7 | sizeElts(size), numElts(0){ 8 | } 9 | 10 | IntSet::~IntSet(){ 11 | delete[] elts; 12 | } 13 | 14 | int IntSet::indexOf(int v){ 15 | for (int i = 0; i < numElts; i++){ 16 | if (elts[i] == v) 17 | return i; 18 | } 19 | return sizeElts; 20 | } 21 | 22 | int IntSet::size(){ 23 | return numElts; 24 | } 25 | 26 | bool IntSet::query(int v){ 27 | return (indexOf(v) != sizeElts); 28 | } 29 | 30 | void IntSet::insert(int v){ 31 | if (indexOf(v) == sizeElts){ 32 | if (numElts == sizeElts) 33 | throw sizeElts; 34 | elts[numElts++] = v; 35 | } 36 | } 37 | 38 | void IntSet::remove(int v){ 39 | int victim = indexOf(v); 40 | if (victim != sizeElts){ 41 | elts[victim] = elts[numElts-1]; 42 | numElts--; 43 | } 44 | } 45 | 46 | void IntSet::print(){ 47 | for (int i = 0; i < numElts; i++){ 48 | cout << elts[i] << " " << flush; 49 | } 50 | cout << endl; 51 | } 52 | -------------------------------------------------------------------------------- /L17-Dynamic-Memory/IntSet/IntSet.h: -------------------------------------------------------------------------------- 1 | #ifndef INTSET_H 2 | #define INTSET_H 3 | 4 | const int MAXELTS = 100; 5 | 6 | 7 | class IntSet{ 8 | // OVERVIEW: a mutable set of integers, |set| <= sizeElts 9 | int *elts; // pointer to dynamic array 10 | int sizeElts; // capacity of array 11 | int numElts; // current occupancy 12 | 13 | int indexOf(int v); 14 | // EFFECTS: returns the index of v if it exists in the 15 | // array, sizeElts otherwise. 16 | 17 | public: 18 | IntSet(int size = MAXELTS); 19 | // EFFECTS: create a set with specified capacity. 20 | // It defaults to MAXELTS if not supplied. 21 | 22 | ~IntSet(); // Destroy this IntSet 23 | 24 | void insert(int v); 25 | // MODIFIES: this 26 | // EFFECTS: this = this + {v} if room, 27 | // throws int sizeElts otherwise. 28 | void remove(int v); 29 | // MODIFIES: this 30 | // EFFECTS: this = this - {v} if v is in this 31 | // throws int v otherwise. 32 | bool query(int v); 33 | // EFFECTS: returns true if v is in this, false otherwise. 34 | int size(); 35 | // EFFECTS: returns |this|. 36 | void print(); 37 | // MODIFIES: cout 38 | // EFFECTS: print out the integers contained in the set in 39 | // sequence. 40 | }; 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /L17-Dynamic-Memory/IntSet/Makefile: -------------------------------------------------------------------------------- 1 | # variable definition 2 | 3 | CC = g++ 4 | 5 | DEFS = 6 | LIBS = 7 | INCLUDES = #-I. 8 | HEADERS = #int_t.h int_impl.h 9 | MAINSRCS = main.cpp 10 | OTHSRCS = IntSet.cpp 11 | SRCS = $(MAINSRCS) $(OTHSRCS) 12 | OBJS = $(SRCS:.cpp=.o) 13 | TARGETS = $(MAINSRCS:.cpp=) 14 | 15 | CFLAGS = -g -Wall $(INCLUDES) $(DEFS) 16 | 17 | %.o: %.cpp 18 | $(CC) $(CFLAGS) -o $@ -c $< 19 | 20 | all: $(TARGETS) 21 | 22 | $(TARGETS): $(OBJS) 23 | $(CC) $(CFLAGS) -o $(TARGETS) $(OBJS) $(LIBS) 24 | 25 | depend: 26 | makedepend -Y $(INCLUDES) $(SRCS) 27 | 28 | memcheck: $(TARGETS) 29 | valgrind --leak-check=full ./$(TARGETS) 30 | 31 | clean: 32 | rm -f $(OBJS) $(TARGETS) 33 | 34 | .PHONY: all depend memcheck clean 35 | -------------------------------------------------------------------------------- /L17-Dynamic-Memory/IntSet/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "IntSet.h" 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | IntSet* ip = new IntSet(50); 8 | cout << "insert 7" << endl; 9 | ip->insert(7); 10 | ip->print(); 11 | 12 | cout << "insert 3" << endl; 13 | ip->insert(3); 14 | ip->print(); 15 | 16 | cout << "insert 5" << endl; 17 | ip->insert(5); 18 | ip->print(); 19 | 20 | cout << "insert 7" << endl; 21 | ip->insert(7); 22 | ip->print(); 23 | 24 | cout << "remove 5" << endl; 25 | ip->remove(5); 26 | ip->print(); 27 | 28 | delete ip; 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /L18-Deep-Copy/1-Dangling-Pointer/IntSet.cpp: -------------------------------------------------------------------------------- 1 | #include "IntSet.h" 2 | #include 3 | 4 | using namespace std; 5 | 6 | IntSet::IntSet(int size): elts(new int[size]), 7 | sizeElts(size), numElts(0){ 8 | } 9 | 10 | IntSet::~IntSet(){ 11 | cout << "Destructor" << endl; 12 | delete[] elts; 13 | } 14 | 15 | int IntSet::indexOf(int v){ 16 | for (int i = 0; i < numElts; i++){ 17 | if (elts[i] == v) 18 | return i; 19 | } 20 | return sizeElts; 21 | } 22 | 23 | int IntSet::size(){ 24 | return numElts; 25 | } 26 | 27 | bool IntSet::query(int v){ 28 | return (indexOf(v) != sizeElts); 29 | } 30 | 31 | void IntSet::insert(int v){ 32 | if (indexOf(v) == sizeElts){ 33 | if (numElts == sizeElts) 34 | throw sizeElts; 35 | elts[numElts++] = v; 36 | } 37 | } 38 | 39 | void IntSet::remove(int v){ 40 | int victim = indexOf(v); 41 | if (victim != sizeElts){ 42 | elts[victim] = elts[numElts-1]; 43 | numElts--; 44 | } 45 | } 46 | 47 | void IntSet::print(){ 48 | for (int i = 0; i < numElts; i++){ 49 | cout << elts[i] << " " << flush; 50 | } 51 | cout << endl; 52 | } 53 | -------------------------------------------------------------------------------- /L18-Deep-Copy/1-Dangling-Pointer/IntSet.h: -------------------------------------------------------------------------------- 1 | #ifndef INTSET_H 2 | #define INTSET_H 3 | 4 | const int MAXELTS = 100; 5 | 6 | class IntSet{ 7 | // OVERVIEW: a mutable set of integers, |set| <= sizeElts 8 | int *elts; // pointer to dynamic array 9 | int sizeElts; // capacity of array 10 | int numElts; // current occupancy 11 | 12 | int indexOf(int v); 13 | // EFFECTS: returns the index of v if it exists in the 14 | // array, sizeElts otherwise. 15 | 16 | public: 17 | IntSet(int size = MAXELTS); 18 | // EFFECTS: create a set with specified capacity. 19 | // It defaults to MAXELTS if not supplied. 20 | ~IntSet(); // Destroy this IntSet 21 | 22 | void insert(int v); 23 | // MODIFIES: this 24 | // EFFECTS: this = this + {v} if room, 25 | // throws int sizeElts otherwise. 26 | void remove(int v); 27 | // MODIFIES: this 28 | // EFFECTS: this = this - {v} if v is in this 29 | // throws int v otherwise. 30 | bool query(int v); 31 | // EFFECTS: returns true if v is in this, false otherwise. 32 | int size(); 33 | // EFFECTS: returns |this|. 34 | void print(); 35 | // MODIFIES: cout 36 | // EFFECTS: print out the integers contained in the set in 37 | // sequence. 38 | }; 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /L18-Deep-Copy/1-Dangling-Pointer/Makefile: -------------------------------------------------------------------------------- 1 | # variable definition 2 | 3 | CC = g++ 4 | 5 | DEFS = 6 | LIBS = 7 | INCLUDES = #-I. 8 | HEADERS = #int_t.h int_impl.h 9 | MAINSRCS = main.cpp 10 | OTHSRCS = IntSet.cpp 11 | SRCS = $(MAINSRCS) $(OTHSRCS) 12 | OBJS = $(SRCS:.cpp=.o) 13 | TARGETS = $(MAINSRCS:.cpp=) 14 | 15 | CFLAGS = -g -Wall $(INCLUDES) $(DEFS) 16 | 17 | %.o: %.cpp 18 | $(CC) $(CFLAGS) -o $@ -c $< 19 | 20 | all: $(TARGETS) 21 | 22 | $(TARGETS): $(OBJS) 23 | $(CC) $(CFLAGS) -o $(TARGETS) $(OBJS) $(LIBS) 24 | 25 | depend: 26 | makedepend -Y $(INCLUDES) $(SRCS) 27 | 28 | memcheck: $(TARGETS) 29 | valgrind --leak-check=full ./$(TARGETS) 30 | 31 | clean: 32 | rm -f $(OBJS) $(TARGETS) 33 | 34 | .PHONY: all depend memcheck clean 35 | -------------------------------------------------------------------------------- /L18-Deep-Copy/1-Dangling-Pointer/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "IntSet.h" 3 | 4 | using namespace std; 5 | 6 | void foo(IntSet x){ 7 | } 8 | 9 | int main(){ 10 | IntSet s; 11 | s.insert(5); 12 | s.print(); 13 | 14 | foo(s); 15 | 16 | // { 17 | // IntSet x; 18 | // x = s; 19 | // } 20 | s.print(); 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /L18-Deep-Copy/2-Copy-Constructor-and-Assignment-Operator/IntSet.cpp: -------------------------------------------------------------------------------- 1 | #include "IntSet.h" 2 | #include 3 | 4 | using namespace std; 5 | 6 | IntSet::IntSet(int size): elts(new int[size]), 7 | sizeElts(size), numElts(0){ 8 | } 9 | 10 | IntSet::IntSet(const IntSet &is): elts(NULL), sizeElts(0), numElts(0){ 11 | cout << "Copy constructor\n"; 12 | copyFrom(is); 13 | } 14 | 15 | IntSet& IntSet::operator= (const IntSet &is){ 16 | if(this != &is) 17 | copyFrom(is); 18 | return *this; 19 | } 20 | 21 | IntSet::~IntSet(){ 22 | cout << "Destructor\n"; 23 | delete[] elts; 24 | } 25 | 26 | int IntSet::indexOf(int v){ 27 | for (int i = 0; i < numElts; i++){ 28 | if (elts[i] == v) 29 | return i; 30 | } 31 | return sizeElts; 32 | } 33 | 34 | void IntSet::copyFrom(const IntSet &is){ 35 | if (is.sizeElts != sizeElts){ // Resize array 36 | delete[] elts; 37 | sizeElts = is.sizeElts; 38 | elts = new int[sizeElts]; 39 | } 40 | // Copy array 41 | for (int i = 0; i < is.sizeElts; i++){ 42 | elts[i] = is.elts[i]; 43 | } 44 | // Establish numElts invariant 45 | numElts = is.numElts; 46 | } 47 | 48 | int IntSet::size(){ 49 | return numElts; 50 | } 51 | 52 | bool IntSet::query(int v){ 53 | return (indexOf(v) != sizeElts); 54 | } 55 | 56 | void IntSet::insert(int v){ 57 | if (indexOf(v) == sizeElts){ 58 | if (numElts == sizeElts) 59 | throw sizeElts; 60 | elts[numElts++] = v; 61 | } 62 | } 63 | 64 | void IntSet::remove(int v){ 65 | int victim = indexOf(v); 66 | if (victim != sizeElts){ 67 | elts[victim] = elts[numElts-1]; 68 | numElts--; 69 | } 70 | } 71 | 72 | void IntSet::print(){ 73 | for (int i = 0; i < numElts; i++){ 74 | cout << elts[i] << " " << flush; 75 | } 76 | cout << endl; 77 | } 78 | -------------------------------------------------------------------------------- /L18-Deep-Copy/2-Copy-Constructor-and-Assignment-Operator/IntSet.h: -------------------------------------------------------------------------------- 1 | #ifndef INTSET_H 2 | #define INTSET_H 3 | 4 | const int MAXELTS = 100; 5 | 6 | class IntSet{ 7 | // OVERVIEW: a mutable set of integers, |set| <= sizeElts 8 | int *elts; // pointer to dynamic array 9 | int sizeElts; // capacity of array 10 | int numElts; // current occupancy 11 | 12 | int indexOf(int v); 13 | // EFFECTS: returns the index of v if it exists in the 14 | // array, sizeElts otherwise. 15 | 16 | void copyFrom(const IntSet &is); 17 | // MODIFIES: this 18 | // EFFECTS: copies is contents to this 19 | 20 | public: 21 | IntSet(int size = MAXELTS); 22 | // EFFECTS: create a set with specified capacity. 23 | // It defaults to MAXELTS if not supplied. 24 | 25 | IntSet(const IntSet &is); // copy constructor 26 | 27 | IntSet& operator= (const IntSet& is); // assigment operator 28 | 29 | ~IntSet(); // Destroy this IntSet 30 | 31 | void insert(int v); 32 | // MODIFIES: this 33 | // EFFECTS: this = this + {v} if room, 34 | // throws int sizeElts otherwise. 35 | void remove(int v); 36 | // MODIFIES: this 37 | // EFFECTS: this = this - {v} if v is in this 38 | // throws int v otherwise. 39 | bool query(int v); 40 | // EFFECTS: returns true if v is in this, false otherwise. 41 | int size(); 42 | // EFFECTS: returns |this|. 43 | void print(); 44 | // MODIFIES: cout 45 | // EFFECTS: print out the integers contained in the set in 46 | // sequence. 47 | }; 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /L18-Deep-Copy/2-Copy-Constructor-and-Assignment-Operator/Makefile: -------------------------------------------------------------------------------- 1 | # variable definition 2 | 3 | CC = g++ 4 | 5 | DEFS = 6 | LIBS = 7 | INCLUDES = #-I. 8 | HEADERS = #int_t.h int_impl.h 9 | MAINSRCS = main.cpp 10 | OTHSRCS = IntSet.cpp 11 | SRCS = $(MAINSRCS) $(OTHSRCS) 12 | OBJS = $(SRCS:.cpp=.o) 13 | TARGETS = $(MAINSRCS:.cpp=) 14 | 15 | CFLAGS = -g -Wall $(INCLUDES) $(DEFS) 16 | 17 | %.o: %.cpp 18 | $(CC) $(CFLAGS) -o $@ -c $< 19 | 20 | all: $(TARGETS) 21 | 22 | $(TARGETS): $(OBJS) 23 | $(CC) $(CFLAGS) -o $(TARGETS) $(OBJS) $(LIBS) 24 | 25 | depend: 26 | makedepend -Y $(INCLUDES) $(SRCS) 27 | 28 | memcheck: $(TARGETS) 29 | valgrind --leak-check=full ./$(TARGETS) 30 | 31 | clean: 32 | rm -f $(OBJS) $(TARGETS) 33 | 34 | .PHONY: all depend memcheck clean 35 | -------------------------------------------------------------------------------- /L18-Deep-Copy/2-Copy-Constructor-and-Assignment-Operator/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "IntSet.h" 3 | 4 | using namespace std; 5 | 6 | void foo(IntSet x){ 7 | } 8 | 9 | int main() { 10 | IntSet s(10); 11 | s.insert(5); 12 | s.print(); 13 | 14 | foo(s); 15 | s.query(5); 16 | s.print(); 17 | 18 | { 19 | IntSet x; 20 | x = s; 21 | x.print(); 22 | } 23 | 24 | s.query(5); 25 | s.print(); 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /L19-Dynamic-Resizing/1-Inc/IntSet.cpp: -------------------------------------------------------------------------------- 1 | #include "IntSet.h" 2 | #include 3 | 4 | using namespace std; 5 | 6 | IntSet::IntSet(int size): elts(new int[size]), 7 | sizeElts(size), numElts(0){ 8 | } 9 | 10 | IntSet::IntSet(const IntSet &is): elts(NULL), sizeElts(0), numElts(0){ 11 | cout << "Copy constructor\n"; 12 | copyFrom(is); 13 | } 14 | 15 | IntSet& IntSet::operator= (const IntSet &is){ 16 | if(this != &is) 17 | copyFrom(is); 18 | return *this; 19 | } 20 | 21 | IntSet::~IntSet(){ 22 | delete[] elts; 23 | } 24 | 25 | int IntSet::indexOf(int v){ 26 | for (int i = 0; i < numElts; i++){ 27 | if (elts[i] == v) 28 | return i; 29 | } 30 | return sizeElts; 31 | } 32 | 33 | void IntSet::copyFrom(const IntSet &is){ 34 | if (is.sizeElts != sizeElts){ // Resize array 35 | delete[] elts; 36 | sizeElts = is.sizeElts; 37 | elts = new int[sizeElts]; 38 | } 39 | // Copy array 40 | for (int i = 0; i < is.sizeElts; i++){ 41 | elts[i] = is.elts[i]; 42 | } 43 | // Establish numElts invariant 44 | numElts = is.numElts; 45 | } 46 | 47 | void IntSet::grow(){ 48 | int *tmp = new int[sizeElts + 1]; 49 | for (int i = 0; i < numElts; i++) { 50 | tmp[i] = elts[i]; 51 | } 52 | delete [] elts; 53 | elts = tmp; 54 | sizeElts += 1; 55 | } 56 | 57 | int IntSet::size(){ 58 | return numElts; 59 | } 60 | 61 | bool IntSet::query(int v){ 62 | return (indexOf(v) != sizeElts); 63 | } 64 | 65 | void IntSet::insert(int v){ 66 | if (indexOf(v) == sizeElts){ 67 | if (numElts == sizeElts) 68 | grow(); 69 | elts[numElts++] = v; 70 | } 71 | } 72 | 73 | void IntSet::remove(int v){ 74 | int victim = indexOf(v); 75 | if (victim != sizeElts){ 76 | elts[victim] = elts[numElts-1]; 77 | numElts--; 78 | } 79 | } 80 | 81 | void IntSet::print(){ 82 | for (int i = 0; i < numElts; i++){ 83 | cout << elts[i] << " " << flush; 84 | } 85 | cout << endl; 86 | } 87 | -------------------------------------------------------------------------------- /L19-Dynamic-Resizing/1-Inc/IntSet.h: -------------------------------------------------------------------------------- 1 | #ifndef INTSET_H 2 | #define INTSET_H 3 | 4 | const int MAXELTS = 100; 5 | 6 | class IntSet{ 7 | // OVERVIEW: a mutable set of integers, |set| <= sizeElts 8 | int *elts; // pointer to dynamic array 9 | int sizeElts; // capacity of array 10 | int numElts; // current occupancy 11 | 12 | int indexOf(int v); 13 | // EFFECTS: returns the index of v if it exists in the 14 | // array, sizeElts otherwise. 15 | 16 | void copyFrom(const IntSet &is); 17 | // MODIFIES: this 18 | // EFFECTS: copies is contents to this 19 | 20 | void grow(); 21 | // MODIFIES: this 22 | // EFFECTS: enlarge the elts array, 23 | // preserving current content 24 | public: 25 | IntSet(int size = MAXELTS); 26 | // EFFECTS: create a set with specified capacity. 27 | // It defaults to MAXELTS if not supplied. 28 | 29 | IntSet(const IntSet &is); // copy constructor 30 | 31 | IntSet& operator= (const IntSet& is); // assigment operator 32 | 33 | ~IntSet(); // Destroy this IntSet 34 | 35 | void insert(int v); 36 | // MODIFIES: this 37 | // EFFECTS: this = this + {v} 38 | void remove(int v); 39 | // MODIFIES: this 40 | // EFFECTS: this = this - {v} if v is in this 41 | // throws int v otherwise. 42 | bool query(int v); 43 | // EFFECTS: returns true if v is in this, false otherwise. 44 | int size(); 45 | // EFFECTS: returns |this|. 46 | void print(); 47 | // MODIFIES: cout 48 | // EFFECTS: print out the integers contained in the set in 49 | // sequence. 50 | }; 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /L19-Dynamic-Resizing/1-Inc/Makefile: -------------------------------------------------------------------------------- 1 | # variable definition 2 | 3 | CC = g++ 4 | 5 | DEFS = 6 | LIBS = 7 | INCLUDES = #-I. 8 | HEADERS = #int_t.h int_impl.h 9 | MAINSRCS = main.cpp 10 | OTHSRCS = IntSet.cpp 11 | SRCS = $(MAINSRCS) $(OTHSRCS) 12 | OBJS = $(SRCS:.cpp=.o) 13 | TARGETS = $(MAINSRCS:.cpp=) 14 | 15 | CFLAGS = -g -Wall $(INCLUDES) $(DEFS) 16 | 17 | %.o: %.cpp 18 | $(CC) $(CFLAGS) -o $@ -c $< 19 | 20 | all: $(TARGETS) 21 | 22 | $(TARGETS): $(OBJS) 23 | $(CC) $(CFLAGS) -o $(TARGETS) $(OBJS) $(LIBS) 24 | 25 | depend: 26 | makedepend -Y $(INCLUDES) $(SRCS) 27 | 28 | memcheck: $(TARGETS) 29 | valgrind --leak-check=full ./$(TARGETS) 30 | 31 | clean: 32 | rm -f $(OBJS) $(TARGETS) 33 | 34 | .PHONY: all depend memcheck clean 35 | -------------------------------------------------------------------------------- /L19-Dynamic-Resizing/1-Inc/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "IntSet.h" 6 | 7 | using namespace std; 8 | 9 | int main(int argc, char *argv[]) { 10 | int n = atoi(argv[1]); 11 | srand(atoi(argv[2])); 12 | IntSet s(1); 13 | 14 | clock_t begin = clock(); 15 | for(int i=1; i 3 | 4 | using namespace std; 5 | 6 | IntSet::IntSet(int size): elts(new int[size]), 7 | sizeElts(size), numElts(0){ 8 | } 9 | 10 | IntSet::IntSet(const IntSet &is): elts(NULL), sizeElts(0), numElts(0){ 11 | cout << "Copy constructor\n"; 12 | copyFrom(is); 13 | } 14 | 15 | IntSet& IntSet::operator= (const IntSet &is){ 16 | if(this != &is) 17 | copyFrom(is); 18 | return *this; 19 | } 20 | 21 | IntSet::~IntSet(){ 22 | delete[] elts; 23 | } 24 | 25 | int IntSet::indexOf(int v){ 26 | for (int i = 0; i < numElts; i++){ 27 | if (elts[i] == v) 28 | return i; 29 | } 30 | return sizeElts; 31 | } 32 | 33 | void IntSet::copyFrom(const IntSet &is){ 34 | if (is.sizeElts != sizeElts){ // Resize array 35 | delete[] elts; 36 | sizeElts = is.sizeElts; 37 | elts = new int[sizeElts]; 38 | } 39 | // Copy array 40 | for (int i = 0; i < is.sizeElts; i++){ 41 | elts[i] = is.elts[i]; 42 | } 43 | // Establish numElts invariant 44 | numElts = is.numElts; 45 | } 46 | 47 | void IntSet::grow(){ 48 | int *tmp = new int[sizeElts * 2]; 49 | for (int i = 0; i < numElts; i++) { 50 | tmp[i] = elts[i]; 51 | } 52 | delete [] elts; 53 | elts = tmp; 54 | sizeElts *= 2; 55 | } 56 | 57 | int IntSet::size(){ 58 | return numElts; 59 | } 60 | 61 | bool IntSet::query(int v){ 62 | return (indexOf(v) != sizeElts); 63 | } 64 | 65 | void IntSet::insert(int v){ 66 | if (indexOf(v) == sizeElts){ 67 | if (numElts == sizeElts) 68 | grow(); 69 | elts[numElts++] = v; 70 | } 71 | } 72 | 73 | void IntSet::remove(int v){ 74 | int victim = indexOf(v); 75 | if (victim != sizeElts){ 76 | elts[victim] = elts[numElts-1]; 77 | numElts--; 78 | } 79 | } 80 | 81 | void IntSet::print(){ 82 | for (int i = 0; i < numElts; i++){ 83 | cout << elts[i] << " " << flush; 84 | } 85 | cout << endl; 86 | } 87 | -------------------------------------------------------------------------------- /L19-Dynamic-Resizing/2-Double/IntSet.h: -------------------------------------------------------------------------------- 1 | #ifndef INTSET_H 2 | #define INTSET_H 3 | 4 | const int MAXELTS = 100; 5 | 6 | class IntSet{ 7 | // OVERVIEW: a mutable set of integers, |set| <= sizeElts 8 | int *elts; // pointer to dynamic array 9 | int sizeElts; // capacity of array 10 | int numElts; // current occupancy 11 | 12 | int indexOf(int v); 13 | // EFFECTS: returns the index of v if it exists in the 14 | // array, sizeElts otherwise. 15 | 16 | void copyFrom(const IntSet &is); 17 | // MODIFIES: this 18 | // EFFECTS: copies is contents to this 19 | 20 | void grow(); 21 | // MODIFIES: this 22 | // EFFECTS: enlarge the elts array, 23 | // preserving current content 24 | public: 25 | IntSet(int size = MAXELTS); 26 | // EFFECTS: create a set with specified capacity. 27 | // It defaults to MAXELTS if not supplied. 28 | 29 | IntSet(const IntSet &is); // copy constructor 30 | 31 | IntSet& operator= (const IntSet& is); // assigment operator 32 | 33 | ~IntSet(); // Destroy this IntSet 34 | 35 | void insert(int v); 36 | // MODIFIES: this 37 | // EFFECTS: this = this + {v} 38 | void remove(int v); 39 | // MODIFIES: this 40 | // EFFECTS: this = this - {v} if v is in this 41 | // throws int v otherwise. 42 | bool query(int v); 43 | // EFFECTS: returns true if v is in this, false otherwise. 44 | int size(); 45 | // EFFECTS: returns |this|. 46 | void print(); 47 | // MODIFIES: cout 48 | // EFFECTS: print out the integers contained in the set in 49 | // sequence. 50 | }; 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /L19-Dynamic-Resizing/2-Double/Makefile: -------------------------------------------------------------------------------- 1 | # variable definition 2 | 3 | CC = g++ 4 | 5 | DEFS = 6 | LIBS = 7 | INCLUDES = #-I. 8 | HEADERS = #int_t.h int_impl.h 9 | MAINSRCS = main.cpp 10 | OTHSRCS = IntSet.cpp 11 | SRCS = $(MAINSRCS) $(OTHSRCS) 12 | OBJS = $(SRCS:.cpp=.o) 13 | TARGETS = $(MAINSRCS:.cpp=) 14 | 15 | CFLAGS = -g -Wall $(INCLUDES) $(DEFS) 16 | 17 | %.o: %.cpp 18 | $(CC) $(CFLAGS) -o $@ -c $< 19 | 20 | all: $(TARGETS) 21 | 22 | $(TARGETS): $(OBJS) 23 | $(CC) $(CFLAGS) -o $(TARGETS) $(OBJS) $(LIBS) 24 | 25 | depend: 26 | makedepend -Y $(INCLUDES) $(SRCS) 27 | 28 | memcheck: $(TARGETS) 29 | valgrind --leak-check=full ./$(TARGETS) 30 | 31 | clean: 32 | rm -f $(OBJS) $(TARGETS) 33 | 34 | .PHONY: all depend memcheck clean 35 | -------------------------------------------------------------------------------- /L19-Dynamic-Resizing/2-Double/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "IntSet.h" 6 | 7 | using namespace std; 8 | 9 | int main(int argc, char *argv[]) { 10 | int n = atoi(argv[1]); 11 | srand(atoi(argv[2])); 12 | IntSet s(1); 13 | 14 | clock_t begin = clock(); 15 | for(int i=1; i 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int a, b; 7 | cout << "Please input two integers: "; 8 | cin >> a >> b; 9 | cout << "Their sum is " << a+b << endl; 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /L2-Linux/my_add/makefile: -------------------------------------------------------------------------------- 1 | CC = g++ 2 | CFLAGS = -std=c++11 -g -Wall 3 | 4 | all: run_add 5 | 6 | run_add: run_add.o add.o 7 | $(CC) $(CFLAGS) -o run_add run_add.o add.o 8 | 9 | run_add.o: run_add.cpp 10 | $(CC) $(CFLAGS) -c run_add.cpp 11 | 12 | add.o: add.cpp 13 | $(CC) $(CFLAGS) -c add.cpp 14 | 15 | clean: 16 | rm -f run_add *.o 17 | -------------------------------------------------------------------------------- /L2-Linux/my_add/my_add: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ve280/demos/13d61b7ae2b307ac629e33ae723ddc028e1df574/L2-Linux/my_add/my_add -------------------------------------------------------------------------------- /L2-Linux/my_add/my_add.dSYM/Contents/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | English 7 | CFBundleIdentifier 8 | com.apple.xcode.dsym.my_add 9 | CFBundleInfoDictionaryVersion 10 | 6.0 11 | CFBundlePackageType 12 | dSYM 13 | CFBundleSignature 14 | ???? 15 | CFBundleShortVersionString 16 | 1.0 17 | CFBundleVersion 18 | 1 19 | 20 | 21 | -------------------------------------------------------------------------------- /L2-Linux/my_add/my_add.dSYM/Contents/Resources/DWARF/my_add: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ve280/demos/13d61b7ae2b307ac629e33ae723ddc028e1df574/L2-Linux/my_add/my_add.dSYM/Contents/Resources/DWARF/my_add -------------------------------------------------------------------------------- /L2-Linux/my_add/my_add.txt: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int a, b; 7 | cout << "Please input two integers: "; 8 | cin >> a >> b; 9 | cout << "Their sum is " << a+b << endl; 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /L2-Linux/my_add/output.txt: -------------------------------------------------------------------------------- 1 | Please input two integers: Their sum is 5 2 | -------------------------------------------------------------------------------- /L2-Linux/my_add/run_add.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "add.h" 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int a, b; 9 | cout << "Please input two integers: "; 10 | cin >> a >> b; 11 | cout << "Their sum is " << add(a, b) << endl; 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /L2-Linux/my_favorite.txt: -------------------------------------------------------------------------------- 1 | apple 2 | cherry 3 | orange 4 | pineapple 5 | strawberry 6 | -------------------------------------------------------------------------------- /L2-Linux/test1.txt: -------------------------------------------------------------------------------- 1 | apple 2 | orange 3 | grape 4 | -------------------------------------------------------------------------------- /L2-Linux/test2.txt: -------------------------------------------------------------------------------- 1 | apple 2 | orange 3 | grape 4 | -------------------------------------------------------------------------------- /L2-Linux/test3.txt: -------------------------------------------------------------------------------- 1 | apple 2 | pear 3 | orange 4 | grape 5 | -------------------------------------------------------------------------------- /L2-Linux/test4.txt: -------------------------------------------------------------------------------- 1 | apple 2 | orange 3 | pear 4 | -------------------------------------------------------------------------------- /L2-Linux/test5.txt: -------------------------------------------------------------------------------- 1 | apple 2 | orange 3 | grape 4 | -------------------------------------------------------------------------------- /L20-Linked-List/IntList.cpp: -------------------------------------------------------------------------------- 1 | #include "IntList.h" 2 | #include 3 | using namespace std; 4 | 5 | bool IntList::isEmpty(){ 6 | return !first; 7 | } 8 | 9 | void IntList::insert(int v){ 10 | node *np = new node; 11 | np->value = v; 12 | np->next = first; 13 | first = np; 14 | } 15 | 16 | int IntList::remove(){ 17 | node *victim = first; 18 | int result; 19 | 20 | if (isEmpty()) 21 | { 22 | listIsEmpty e; 23 | throw e; 24 | } 25 | 26 | first = victim->next; 27 | result = victim->value; 28 | delete victim; 29 | return result; 30 | } 31 | 32 | IntList::IntList(): first(0){ 33 | } 34 | 35 | void IntList::removeAll(){ 36 | while(!isEmpty()){ 37 | remove(); 38 | } 39 | } 40 | 41 | IntList::~IntList(){ 42 | removeAll(); 43 | } 44 | 45 | void IntList::copyList(node *list){ 46 | if (!list) return; // Base case 47 | copyList(list->next); 48 | insert(list->value); 49 | } 50 | 51 | IntList::IntList(const IntList &l): first (0){ 52 | copyList(l.first); 53 | } 54 | 55 | IntList &IntList::operator= (const IntList &l){ 56 | if (this != &l){ 57 | removeAll(); 58 | copyList(l.first); 59 | } 60 | return *this; 61 | } 62 | 63 | void IntList::print(){ 64 | node *p = first; 65 | while(p){ 66 | cout << p->value << " "; 67 | p = p->next; 68 | } 69 | cout << endl; 70 | } 71 | -------------------------------------------------------------------------------- /L20-Linked-List/IntList.h: -------------------------------------------------------------------------------- 1 | #ifndef INTLIST_H 2 | #define INTLIST_H 3 | 4 | class listIsEmpty {}; // An exception class 5 | 6 | struct node{ 7 | node *next; 8 | int value; 9 | }; 10 | 11 | class IntList{ 12 | node *first; 13 | void removeAll(); 14 | void copyList(node *list); 15 | 16 | public: 17 | bool isEmpty(); 18 | // EFFECTS: returns true if list is empty, false otherwise 19 | void insert(int v); 20 | // MODIFIES: this 21 | // EFFECTS: inserts v into the front of the list 22 | int remove(); 23 | // MODIFIES: this 24 | // EFFECTS: if list is empty, throw listIsEmpty. 25 | // Otherwise, remove and return the first 26 | // element of the list 27 | void print(); 28 | // MODIFIES: cout 29 | // EFFECTS: print the int list 30 | 31 | IntList(); 32 | // default constructor 33 | IntList(const IntList& l); 34 | // copy constructor 35 | ~IntList(); 36 | // destructor 37 | IntList &operator=(const IntList &l); 38 | // assignment operator 39 | }; 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /L20-Linked-List/Makefile: -------------------------------------------------------------------------------- 1 | # variable definition 2 | 3 | CC = g++ 4 | 5 | DEFS = 6 | LIBS = 7 | INCLUDES = 8 | HEADERS = IntList.h 9 | MAINSRCS = main.cpp 10 | OTHSRCS = IntList.cpp 11 | SRCS = $(MAINSRCS) $(OTHSRCS) 12 | OBJS = $(SRCS:.cpp=.o) 13 | TARGETS = $(MAINSRCS:.cpp=) 14 | 15 | CFLAGS = -g -Wall $(INCLUDES) $(DEFS) 16 | 17 | %.o: %.cpp 18 | $(CC) $(CFLAGS) -o $@ -c $< 19 | 20 | all: $(TARGETS) 21 | 22 | $(TARGETS): $(OBJS) 23 | $(CC) $(CFLAGS) -o $(TARGETS) $(OBJS) $(LIBS) 24 | 25 | depend: 26 | makedepend -Y $(INCLUDES) $(SRCS) 27 | 28 | memcheck: $(TARGETS) 29 | valgrind --leak-check=full ./$(TARGETS) 30 | 31 | clean: 32 | rm -f $(OBJS) $(TARGETS) 33 | 34 | .PHONY: all depend memcheck clean 35 | -------------------------------------------------------------------------------- /L20-Linked-List/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "IntList.h" 3 | using namespace std; 4 | 5 | void foo(IntList x){ 6 | cout << "Print copy in function foo" << endl; 7 | x.print(); 8 | } 9 | 10 | int main(){ 11 | IntList s; 12 | cout << "Insert 1 at the front" << endl; 13 | s.insert(1); 14 | cout << "Insert 2 at the front" << endl; 15 | s.insert(2); 16 | cout << "Insert 3 at the front" << endl; 17 | s.insert(3); 18 | s.print(); 19 | 20 | foo(s); 21 | 22 | { 23 | IntList x; 24 | x = s; 25 | cout << "Print copy by assignment" << endl; 26 | x.print(); 27 | } 28 | 29 | cout << "Remove the first item" << endl; 30 | s.remove(); 31 | s.print(); 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /L21-Template/1-Template/withTemplate.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | template 4 | const T& max(const T& x, const T& y){ 5 | return (x > y) ? x : y; 6 | } 7 | 8 | int main(){ 9 | std::cout << max(0, 1) << std::endl; 10 | 11 | std::cout << max(3.14, 3.14159) << std::endl; 12 | 13 | std::cout << max('a', ' ') << std::endl; 14 | 15 | std::cout << max("hello", "world") << std::endl; 16 | 17 | return 0; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /L21-Template/1-Template/withoutTemplate.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int& max(const int& x, const int& y){ 6 | return (x > y) ? x : y; 7 | } 8 | 9 | const double& max(const double& x, const double& y){ 10 | return (x > y) ? x : y; 11 | } 12 | 13 | const char& max(const char& x, const char& y){ 14 | return (x > y) ? x : y; 15 | } 16 | 17 | int main(){ 18 | std::cout << max(0, 1) << '\n'; 19 | 20 | std::cout << max(3.14, 3.14159) << '\n'; 21 | 22 | std::cout << max('a', ' ') << '\n'; 23 | 24 | return 0; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /L21-Template/2-List/List.h: -------------------------------------------------------------------------------- 1 | #ifndef LIST_H 2 | #define LIST_H 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class listIsEmpty {}; // An exception class 9 | 10 | template 11 | class List{ 12 | struct node{ 13 | node *next; 14 | T value; 15 | }; 16 | node *first; 17 | void removeAll(); 18 | void copyList(node *list); 19 | 20 | public: 21 | bool isEmpty(); 22 | // EFFECTS: returns true if list is empty, false otherwise 23 | void insert(T v); 24 | // MODIFIES: this 25 | // EFFECTS: inserts v into the front of the list 26 | T remove(); 27 | // MODIFIES: this 28 | // EFFECTS: if list is empty, throw listIsEmpty. 29 | // Otherwise, remove and return the first 30 | // element of the list 31 | void print(); 32 | // MODIFIES: cout 33 | // EFFECTS: print the int list 34 | 35 | List(); 36 | // default constructor 37 | List(const List& l); 38 | // copy constructor 39 | ~List(); 40 | // destructor 41 | List &operator=(const List &l); 42 | // assignment operator 43 | }; 44 | 45 | template 46 | bool List::isEmpty(){ 47 | return !first; 48 | } 49 | 50 | template 51 | void List::insert(T v){ 52 | node *np = new node; 53 | np->value = v; 54 | np->next = first; 55 | first = np; 56 | } 57 | 58 | template 59 | T List::remove(){ 60 | if (isEmpty()){ 61 | listIsEmpty e; 62 | throw e; 63 | } 64 | node *victim = first; 65 | T result; 66 | first = victim->next; 67 | result = victim->value; 68 | delete victim; 69 | return result; 70 | } 71 | 72 | template 73 | List::List(): first(0){ 74 | } 75 | 76 | template 77 | void List::removeAll(){ 78 | while(!isEmpty()){ 79 | remove(); 80 | } 81 | } 82 | 83 | template 84 | List::~List(){ 85 | removeAll(); 86 | } 87 | 88 | template 89 | void List::copyList(node *list){ 90 | if (!list) return; // Base case 91 | copyList(list->next); 92 | insert(list->value); 93 | } 94 | 95 | template 96 | List::List(const List &l): first (0){ 97 | copyList(l.first); 98 | } 99 | 100 | template 101 | List &List::operator= (const List &l){ 102 | if (this != &l){ 103 | removeAll(); 104 | copyList(l.first); 105 | } 106 | return *this; 107 | } 108 | 109 | template 110 | void List::print(){ 111 | node *p = first; 112 | while(p){ 113 | cout << p->value << " "; 114 | p = p->next; 115 | } 116 | cout << endl; 117 | } 118 | #endif 119 | -------------------------------------------------------------------------------- /L21-Template/2-List/Makefile: -------------------------------------------------------------------------------- 1 | # variable definition 2 | 3 | CC = g++ 4 | 5 | DEFS = 6 | LIBS = 7 | INCLUDES = 8 | HEADERS = List.h 9 | MAINSRCS = main.cpp 10 | OTHSRCS = 11 | SRCS = $(MAINSRCS) $(OTHSRCS) 12 | OBJS = $(SRCS:.cpp=.o) 13 | TARGETS = $(MAINSRCS:.cpp=) 14 | 15 | CFLAGS = -g -Wall $(INCLUDES) $(DEFS) 16 | 17 | %.o: %.cpp 18 | $(CC) $(CFLAGS) -o $@ -c $< 19 | 20 | all: $(TARGETS) 21 | 22 | $(TARGETS): $(OBJS) 23 | $(CC) $(CFLAGS) -o $(TARGETS) $(OBJS) $(LIBS) 24 | 25 | depend: 26 | makedepend -Y $(INCLUDES) $(SRCS) 27 | 28 | memcheck: $(TARGETS) 29 | valgrind --leak-check=full ./$(TARGETS) 30 | 31 | clean: 32 | rm -f $(OBJS) $(TARGETS) 33 | 34 | .PHONY: all depend memcheck clean 35 | -------------------------------------------------------------------------------- /L21-Template/2-List/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "List.h" 3 | using namespace std; 4 | 5 | void foo(List x){ 6 | cout << "Print copy in function foo" << endl; 7 | x.print(); 8 | } 9 | 10 | int main(){ 11 | List s; 12 | cout << "Insert 1 at the front" << endl; 13 | s.insert(1); 14 | cout << "Insert 2 at the front" << endl; 15 | s.insert(2); 16 | cout << "Insert 3 at the front" << endl; 17 | s.insert(3); 18 | s.print(); 19 | 20 | foo(s); 21 | 22 | { 23 | List x; 24 | x = s; 25 | cout << "Print copy by assignment" << endl; 26 | x.print(); 27 | } 28 | 29 | cout << "Remove the first item" << endl; 30 | s.remove(); 31 | s.print(); 32 | 33 | List list; 34 | list.insert("world!"); 35 | list.insert("hello"); 36 | list.print(); 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /L21-Template/3-Derived-Class/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class A{ 5 | int a=0; 6 | int b=1; 7 | public: 8 | void set(int _a, int _b){ 9 | a = _a; 10 | b = _b; 11 | } 12 | 13 | virtual void print() const{ 14 | cout << a << " " << b << endl; 15 | } 16 | }; 17 | 18 | class B: public A{ 19 | int c=2; 20 | public: 21 | void print() const{ 22 | A::print(); 23 | cout << c << endl; 24 | } 25 | }; 26 | 27 | int main(){ 28 | A a; 29 | cout << "A: "; 30 | a.print(); 31 | 32 | B b; 33 | cout << "B: "; 34 | b.print(); 35 | 36 | b.set(5, 6); 37 | b.print(); 38 | 39 | A ab=b; 40 | cout << "A: "; 41 | ab.print(); 42 | 43 | A *pa = &b; 44 | cout << "A*: "; 45 | pa->print(); 46 | 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /L22-Operator-Overloading/1-Complex/complex.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Complex{ 6 | double real; 7 | double img; 8 | 9 | public: 10 | Complex(double real=0, double img=0): 11 | real(real), img(img){} 12 | Complex operator +(const Complex &c){ 13 | return Complex(this->real + c.real, 14 | this->img + c.img); 15 | } 16 | void print(){ 17 | cout << this->real << "+" 18 | << this->img << "i" << endl; 19 | } 20 | 21 | friend Complex operator-(const Complex &, 22 | const Complex &); 23 | }; 24 | 25 | Complex operator -(const Complex &c1, 26 | const Complex &c2){ 27 | return Complex(c1.real - c2.real, c1.img - c2.img); 28 | } 29 | 30 | int main(){ 31 | Complex c(3, 2); 32 | c.print(); 33 | 34 | Complex c2 = c + c; 35 | c2.print(); 36 | 37 | (c2 - c).print(); 38 | } 39 | -------------------------------------------------------------------------------- /L22-Operator-Overloading/2-IntSet/IntSet.cpp: -------------------------------------------------------------------------------- 1 | #include "IntSet.h" 2 | #include 3 | 4 | using namespace std; 5 | 6 | IntSet::IntSet(int size): elts(new int[size]), 7 | sizeElts(size), numElts(0){ 8 | } 9 | 10 | int IntSet::indexOf(int v){ 11 | for (int i = 0; i < numElts; i++){ 12 | if (elts[i] == v) 13 | return i; 14 | } 15 | return sizeElts; 16 | } 17 | 18 | int IntSet::size(){ 19 | return numElts; 20 | } 21 | 22 | bool IntSet::query(int v){ 23 | return (indexOf(v) != sizeElts); 24 | } 25 | 26 | void IntSet::insert(int v){ 27 | if (indexOf(v) == sizeElts){ 28 | if (numElts == sizeElts) 29 | throw sizeElts; 30 | elts[numElts++] = v; 31 | } 32 | } 33 | 34 | void IntSet::remove(int v){ 35 | int victim = indexOf(v); 36 | if (victim != sizeElts){ 37 | elts[victim] = elts[numElts-1]; 38 | numElts--; 39 | } else{ 40 | throw v; 41 | } 42 | } 43 | 44 | void IntSet::print(){ 45 | for (int i = 0; i < numElts; i++){ 46 | cout << elts[i] << " " << flush; 47 | } 48 | cout << endl; 49 | } 50 | 51 | int &IntSet::operator [](int i){ 52 | cout << "non const version" << endl; 53 | if(i >= 0 && i < numElts) return elts[i]; 54 | else throw -1; 55 | } 56 | 57 | const int &IntSet::operator[](int i) const{ 58 | cout << "const version" << endl; 59 | if(i >= 0 && i < numElts) return elts[i]; 60 | else throw -1; 61 | } 62 | 63 | -------------------------------------------------------------------------------- /L22-Operator-Overloading/2-IntSet/IntSet.h: -------------------------------------------------------------------------------- 1 | #ifndef INTSET_H 2 | #define INTSET_H 3 | 4 | const int MAXELTS = 100; 5 | 6 | 7 | class IntSet{ 8 | // OVERVIEW: a mutable set of integers, |set| <= sizeElts 9 | int *elts; // pointer to dynamic array 10 | int sizeElts; // capacity of array 11 | int numElts; // current occupancy 12 | 13 | int indexOf(int v); 14 | // EFFECTS: returns the index of v if it exists in the 15 | // array, sizeElts otherwise. 16 | 17 | public: 18 | IntSet(int size = MAXELTS); 19 | // EFFECTS: creates a set with specified capacity. 20 | // It defaults to MAXELTS if not supplied. 21 | void insert(int v); 22 | // MODIFIES: this 23 | // EFFECTS: this = this + {v} if room, 24 | // throws int sizeElts otherwise. 25 | void remove(int v); 26 | // MODIFIES: this 27 | // EFFECTS: this = this - {v} if v is in this 28 | // throws int v otherwise. 29 | bool query(int v); 30 | // EFFECTS: returns true if v is in this, false otherwise. 31 | int size(); 32 | // EFFECTS: returns |this|. 33 | void print(); 34 | // MODIFIES: cout 35 | // EFFECTS: prints out the integers contained in the set in 36 | // sequence. 37 | const int &operator[](int i) const; 38 | // EFFECTS: returns a const reference to the element at index i 39 | // if i 2 | #include "IntSet.h" 3 | 4 | using namespace std; 5 | 6 | int main(){ 7 | //IntSet bar; // capacity is MAXELTS 8 | IntSet foo(50); 9 | cout << "insert 7" << endl; 10 | foo.insert(7); 11 | foo.print(); 12 | 13 | cout << "insert 3" << endl; 14 | foo.insert(3); 15 | foo.print(); 16 | 17 | cout << "insert 4" << endl; 18 | foo.insert(4); 19 | foo.print(); 20 | 21 | cout << "insert 5" << endl; 22 | foo.insert(5); 23 | foo.print(); 24 | 25 | cout << "insert 7" << endl; 26 | foo.insert(7); 27 | foo.print(); 28 | 29 | cout << "insert 8" << endl; 30 | foo.insert(8); 31 | foo.print(); 32 | 33 | cout << "remove 5" << endl; 34 | foo.remove(5); 35 | foo.print(); 36 | 37 | cout << "remove 8" << endl; 38 | foo.remove(8); 39 | foo.print(); 40 | 41 | cout << foo [1] << endl; 42 | foo[1] = 18; 43 | 44 | const IntSet &bar = foo; 45 | cout << bar[1] << endl; 46 | 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /L23-Stack/IntList.cpp: -------------------------------------------------------------------------------- 1 | #include "IntList.h" 2 | #include 3 | using namespace std; 4 | 5 | bool IntList::isEmpty() const{ 6 | return !first; 7 | } 8 | 9 | void IntList::insert(int v){ 10 | node *np = new node; 11 | np->value = v; 12 | np->next = first; 13 | first = np; 14 | } 15 | 16 | int IntList::remove(){ 17 | node *victim = first; 18 | int result; 19 | 20 | if (isEmpty()){ 21 | listIsEmpty e; 22 | throw e; 23 | } 24 | 25 | first = victim->next; 26 | result = victim->value; 27 | delete victim; 28 | return result; 29 | } 30 | 31 | int &IntList::head(){ 32 | return first->value; 33 | } 34 | 35 | IntList::IntList(): first(0){ 36 | } 37 | 38 | void IntList::removeAll(){ 39 | while(!isEmpty()){ 40 | remove(); 41 | } 42 | } 43 | 44 | IntList::~IntList(){ 45 | removeAll(); 46 | } 47 | 48 | void IntList::copyList(node *list){ 49 | if (!list) return; // Base case 50 | copyList(list->next); 51 | insert(list->value); 52 | } 53 | 54 | IntList::IntList(const IntList &l): first (0){ 55 | copyList(l.first); 56 | } 57 | 58 | IntList &IntList::operator= (const IntList &l){ 59 | if (this != &l){ 60 | removeAll(); 61 | copyList(l.first); 62 | } 63 | return *this; 64 | } 65 | 66 | void IntList::print() const{ 67 | node *p = first; 68 | while(p){ 69 | cout << p->value << " "; 70 | p = p->next; 71 | } 72 | cout << endl; 73 | } 74 | -------------------------------------------------------------------------------- /L23-Stack/IntList.h: -------------------------------------------------------------------------------- 1 | #ifndef INTLIST_H 2 | #define INTLIST_H 3 | 4 | class listIsEmpty {}; // An exception class 5 | 6 | struct node{ 7 | node *next; 8 | int value; 9 | }; 10 | 11 | class IntList{ 12 | node *first; 13 | void removeAll(); 14 | void copyList(node *list); 15 | 16 | public: 17 | bool isEmpty() const; 18 | // EFFECTS: returns true if list is empty, false otherwise 19 | void insert(int v); 20 | // MODIFIES: this 21 | // EFFECTS: inserts v into the front of the list 22 | int remove(); 23 | // MODIFIES: this 24 | // EFFECTS: if list is empty, throw listIsEmpty. 25 | // Otherwise, remove and return the first 26 | // element of the list 27 | int &head(); 28 | // EFFECTS: return the value in the first node if it exists 29 | // Otherwise, throw listIsEmpty 30 | void print() const; 31 | // MODIFIES: cout 32 | // EFFECTS: print the int list 33 | 34 | IntList(); 35 | // default constructor 36 | IntList(const IntList& l); 37 | // copy constructor 38 | ~IntList(); 39 | // destructor 40 | IntList &operator=(const IntList &l); 41 | // assignment operator 42 | }; 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /L23-Stack/Stack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "IntList.h" 3 | using namespace std; 4 | 5 | class Stack{ 6 | IntList l; 7 | int n; 8 | 9 | public: 10 | Stack(): l(), n(0){}; 11 | 12 | int size(){ return n; } 13 | 14 | void push(int v){ l.insert(v); n++; } 15 | 16 | void pop(){ l.remove(); n--; } 17 | 18 | int &top(){ return l.head(); } 19 | }; 20 | 21 | int main(int argc, char *argv[]){ 22 | string line; 23 | cout << "Enter an expression:" << endl; 24 | getline(cin, line); 25 | 26 | Stack s; 27 | for(int i=0; i 2 | #include 3 | #include 4 | 5 | int main(){ 6 | std::vector v( 7 | std::numeric_limits::max(), 0); 8 | std::cout << std::numeric_limits::max() << std::endl; 9 | std::cout << std::numeric_limits::max() << std::endl; 10 | 11 | std::cout << "v[0]= " << v[0] << std::endl; 12 | 13 | short ssize = v.size(); 14 | std::cout << "short size: " << ssize << std::endl; 15 | int isize = v.size(); 16 | std::cout << "int size: " << isize << std::endl; 17 | } 18 | -------------------------------------------------------------------------------- /L3-Compilation/add/Makefile: -------------------------------------------------------------------------------- 1 | all: run_add 2 | 3 | run_add: run_add.o add.o 4 | g++ -o run_add run_add.o add.o 5 | 6 | run_add.o: run_add.cpp 7 | g++ -c run_add.cpp 8 | 9 | add.o: add.cpp 10 | g++ -c add.cpp 11 | 12 | clean: 13 | rm -f run_add *.o 14 | -------------------------------------------------------------------------------- /L3-Compilation/add/add.cpp: -------------------------------------------------------------------------------- 1 | int add(int a, int b){ 2 | return (a+b); 3 | } 4 | -------------------------------------------------------------------------------- /L3-Compilation/add/add.h: -------------------------------------------------------------------------------- 1 | #ifndef ADD_H 2 | #define ADD_H 3 | 4 | int add(int a, int b); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /L3-Compilation/add/run_add.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "add.h" 3 | using namespace std; 4 | 5 | int main(){ 6 | int a, b; 7 | cout << "Please input two integers: "; 8 | cin >> a >> b; 9 | cout << "Their sum is " << add(a,b) << endl; 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /L3-Compilation/error.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int v; 5 | 6 | cout << "Hello World!" << endl; 7 | cout << v << endl 8 | 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /L3-Compilation/exercise/add.cpp: -------------------------------------------------------------------------------- 1 | #include "add.h" 2 | 3 | int add(int n, int m){ 4 | int result = n; 5 | for (int i=0; i 2 | #include "add.h" 3 | #include "sub.h" 4 | 5 | using namespace std; 6 | 7 | int main(){ 8 | int n = 10; 9 | int m = 2; 10 | 11 | cout << add(n, m) << endl; 12 | cout << sub(n, m) << endl; 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /L3-Compilation/exercise/sub.cpp: -------------------------------------------------------------------------------- 1 | #include "sub.h" 2 | 3 | int sub(int n, int m){ 4 | int result = n; 5 | for (int i=0; i 2 | #include "add.h" 3 | #include "sub.h" 4 | 5 | using namespace std; 6 | 7 | int main(){ 8 | int n = 10; 9 | int m = 2; 10 | 11 | cout << add(n, m) << endl; 12 | cout << sub(n, m) << endl; 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /L3-Compilation/solution/makefile: -------------------------------------------------------------------------------- 1 | all: my_prog 2 | 3 | my_prog: main.o add.o sub.o incdec.o 4 | g++ -o my_prog main.o add.o sub.o incdec.o 5 | 6 | main.o: main.cpp 7 | g++ -c main.cpp 8 | 9 | add.o: add.cpp 10 | g++ -c add.cpp 11 | 12 | sub.o: sub.cpp 13 | g++ -c sub.cpp 14 | 15 | incdec.o: incdec.cpp 16 | g++ -c incdec.cpp 17 | 18 | clean: 19 | rm -f my_prog *.o 20 | -------------------------------------------------------------------------------- /L3-Compilation/solution/sub.cpp: -------------------------------------------------------------------------------- 1 | #include "sub.h" 2 | 3 | int sub(int n, int m){ 4 | int result = n; 5 | for (int i=0; i 2 | 3 | int f(int a[], int n){ 4 | for (int i=0; i 2 | 3 | int main(){ 4 | int a[] = {0, 1, 2, 3, 4, 5}; 5 | 6 | int *p = a; 7 | 8 | std::cout << "a = " << a << std::endl; 9 | std::cout << "p = " << p << std::endl; 10 | std::cout << "*p= " << *p << std::endl; 11 | std::cout << "p++= " << p++ << std::endl; 12 | std::cout << "p= " << p << std::endl; 13 | std::cout << "*p= " << *p << std::endl; 14 | std::cout << "++p= " << ++p << std::endl; 15 | std::cout << "p= " << p << std::endl; 16 | std::cout << "*p= " << *p << std::endl; 17 | p += 2; 18 | std::cout << "p= " << p << std::endl; 19 | std::cout << "*p= " << *p << std::endl; 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /L4-Review/reference.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | int a = 42; 7 | int &b = a; 8 | 9 | cout << "a=" << a << endl; 10 | cout << "b=" << b << endl; 11 | cout << "&a=" << &a << endl; 12 | cout << "&b=" << &b << endl; 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /L4-Review/rvalue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int i = 5; 5 | if (i = 4){ 6 | std::cout << "equal" << std::endl; 7 | } else{ 8 | std::cout << "not equal" << std::endl; 9 | } 10 | 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /L4-Review/struct.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | typedef struct { 4 | char c; 5 | int i; 6 | long l; 7 | float f; 8 | double d; 9 | char s[3]; 10 | bool b; 11 | bool end; 12 | } S; 13 | 14 | using namespace std; 15 | 16 | int main(){ 17 | S s; 18 | 19 | cout << "&s = " << &s << endl; 20 | cout << "&s.c = " << static_cast (&s.c) << endl; // cast needed 21 | cout << "&s.i = " << &s.i << endl; 22 | cout << "&s.l = " << &s.l << endl; 23 | cout << "&s.f = " << &s.f << endl; 24 | cout << "&s.d = " << &s.d << endl; 25 | cout << "&s.s = " << &s.s << endl; 26 | cout << "&s.b = " << &s.b << endl; 27 | cout << "&s.end = " << &s.end << endl; 28 | 29 | cout << sizeof(char) << endl; 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /L4-Review/structArgument.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const int MAX=10000; 4 | 5 | typedef struct{ 6 | int array[MAX]; 7 | int *p; 8 | } LargeStruct; 9 | 10 | using namespace std; 11 | 12 | void f(LargeStruct ls){ 13 | cout << "debut" << endl; 14 | ls.array[0] = 2020; 15 | cout << "ok" << endl; 16 | ls.p[1] = 2021; 17 | cout << "ek" << endl; 18 | } 19 | 20 | int main(){ 21 | LargeStruct ls; 22 | int array[MAX]; 23 | 24 | for(int i=0; i 2 | #include 3 | 4 | using namespace std; 5 | 6 | void fString(string &s){ 7 | return; 8 | } 9 | 10 | void fStringConst(const string &s){ 11 | return; 12 | } 13 | 14 | int main(){ 15 | int i = 1; 16 | // const can be bound to a non-const variable 17 | const int &iConst = i; 18 | 19 | // '\t' is the tabulation character 20 | cout << "i= \t" << i << endl; 21 | cout << "iConst= " << iConst << endl; 22 | 23 | i = 2; 24 | 25 | cout << "i= \t" << i << endl; 26 | cout << "iConst= " << iConst << endl; 27 | 28 | string s = "hello"; 29 | const string sConst = s; 30 | 31 | fString(s); 32 | fStringConst(s); 33 | 34 | fString(sConst); 35 | fStringConst(sConst); 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /L5-const/constCall.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | // Constants; avoid using #define 7 | const int SIZEMAX=100000; 8 | const int IMAX=1000; 9 | 10 | typedef struct{ 11 | int array[SIZEMAX]; 12 | int *p; 13 | } LargeStruct; 14 | 15 | // Camel case 16 | void fByValue(LargeStruct ls){ 17 | ls.array[0] = 2020; 18 | ls.p[1] = 2021; 19 | } 20 | 21 | // Snake case 22 | void f_with_reference(LargeStruct &ls){ 23 | ls.array[0] = 2022; 24 | ls.p[1] = 2023; 25 | } 26 | 27 | // Mix of the two naming conventions 28 | void f_withPointer(LargeStruct *ls){ 29 | ls->array[0] = 2024; 30 | ls->p[1] = 2025; 31 | } 32 | 33 | int main(){ 34 | // This ls is of course different from the ls in the functions' argument 35 | LargeStruct ls; 36 | int array[SIZEMAX]; 37 | // const can be bound to a non-const variable 38 | const LargeStruct &lsConst = ls; 39 | // Initialization of ls 40 | for(int i=0; ip[1]= " << ls.p[1] << endl; 58 | clock_gettime(CLOCK_MONOTONIC, &finish); 59 | elapsed = (finish.tv_sec - start.tv_sec); 60 | elapsed += (finish.tv_nsec - start.tv_nsec) / 1000000000.0; 61 | cout << "Time needed: " << elapsed << endl; 62 | 63 | // Measure time for call with reference 64 | cout << "Call with reference\n"; 65 | clock_gettime(CLOCK_MONOTONIC, &start); 66 | for(int i=0; ip[1]= " << ls.p[1] << endl; 71 | clock_gettime(CLOCK_MONOTONIC, &finish); 72 | elapsed = (finish.tv_sec - start.tv_sec); 73 | elapsed += (finish.tv_nsec - start.tv_nsec) / 1000000000.0; 74 | cout << "Time needed: " << elapsed << endl; 75 | 76 | // Measure time for call with pointer 77 | cout << "Call with pointer:\n"; 78 | clock_gettime(CLOCK_MONOTONIC, &start); 79 | for(int i=0; ip[1]= " << ls.p[1] << endl; 84 | clock_gettime(CLOCK_MONOTONIC, &finish); 85 | elapsed = (finish.tv_sec - start.tv_sec); 86 | elapsed += (finish.tv_nsec - start.tv_nsec) / 1000000000.0; 87 | cout << "Time needed: " << elapsed << endl; 88 | 89 | return 0; 90 | } 91 | -------------------------------------------------------------------------------- /L7-Function/factorial.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int factorial(int n){ 5 | if (n==0 || n==1){ 6 | return 1; 7 | } 8 | return n*factorial(n-1); 9 | } 10 | 11 | int main(){ 12 | cout << "0!= " << factorial(0) << endl; 13 | cout << "1!= " << factorial(1) << endl; 14 | cout << "10!= " << factorial(10) << endl; 15 | 16 | cout << "16!= " << factorial(16) << endl; 17 | cout << "17!= " << factorial(17) << endl; 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /L7-Function/fp.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int min(int x, int y){ 5 | if (x < y){ 6 | return x; 7 | } else{ 8 | return y; 9 | } 10 | } 11 | 12 | int max(int x, int y){ 13 | if (x < y){ 14 | return y; 15 | } else{ 16 | return x; 17 | } 18 | } 19 | 20 | typedef int (* funp_t)(int, int); 21 | 22 | funp_t choose(int i){ 23 | if (i==0){ 24 | return min; 25 | } else{ 26 | return max; 27 | } 28 | } 29 | 30 | int main(){ 31 | int (* fp)(int, int) = min; 32 | int (* fpArray[])(int, int) = {min, max}; 33 | int (** fpp)(int, int) = fpArray; 34 | cout << fp(1, 2) << endl; 35 | cout << fpArray[0](2, 3) << endl; 36 | cout << fpArray[1](3, 4) << endl; 37 | //fp++; // doesn't make sense; what does it point to after inc? 38 | //fpArray++; // arrays are rvalues 39 | fpp++; 40 | cout << (*fpp)(4, 5) << endl; 41 | cout << (void *)fp << endl; 42 | 43 | cout << choose(0)(6, 7) << endl; 44 | cout << choose(1)(6, 7) << endl; 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /L7-Function/int.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | cout<<"Max value of int: "< 2 | using namespace std; 3 | 4 | const int SIZEMAX=1000; 5 | const int NCOUT = 100; 6 | 7 | struct LargeStruct{ 8 | int a[SIZEMAX]; 9 | }; 10 | 11 | void recursiveFunction(LargeStruct ls, int i){ 12 | if (i % NCOUT == 0){ 13 | cout << i << endl; 14 | } 15 | return recursiveFunction(ls, i+1); 16 | } 17 | 18 | int main(){ 19 | LargeStruct ls; 20 | 21 | recursiveFunction(ls, 0); 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /L8_enum/quiz.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | enum Suit_t {CLUBS, DIAMONDS, HEARTS, SPADES}; 5 | 6 | int main(){ 7 | if (HEARTS == 2*DIAMONDS){ 8 | cout << "ok" << endl; 9 | } 10 | 11 | cout << HEARTS - SPADES + CLUBS * DIAMONDS << endl; 12 | 13 | Suit_t c = 2*HEARTS; 14 | 15 | int array[2]; 16 | cout << array[2*CLUBS] << endl; 17 | 18 | return 0; 19 | } 20 | 21 | -------------------------------------------------------------------------------- /L8_enum/suit.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | enum Suit_t {CLUBS, DIAMONDS, HEARTS, SPADES}; 5 | 6 | bool isRed(Suit_t s) { 7 | switch (s) { 8 | case DIAMONDS: 9 | case HEARTS: 10 | return true; 11 | break; 12 | case CLUBS: 13 | case SPADES: 14 | return false; 15 | break; 16 | default: 17 | assert(0); 18 | break; 19 | } 20 | } 21 | 22 | int main(){ 23 | Suit_t suit = DIAMONDS; 24 | 25 | if (isRed(suit)){ 26 | cout << "red" << endl; 27 | } 28 | 29 | cout << suit << endl; // automatically converted to int 30 | 31 | Suit_t suit2 = 2; // Need to cast to Suit_t 32 | 33 | return 0; 34 | } 35 | 36 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Files used for the demos in class 2 | -------------------------------------------------------------------------------- /docker/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM ubuntu:18.04 2 | RUN sed -i 's:^path-exclude=/usr/share/man:#path-exclude=/usr/share/man:' \ 3 | /etc/dpkg/dpkg.cfg.d/excludes 4 | RUN sed -i 's/archive.ubuntu.com/ftp.sjtu.edu.cn/g' /etc/apt/sources.list 5 | RUN apt-get update && \ 6 | apt-get install -y \ 7 | g++\ 8 | man \ 9 | manpages-posix\ 10 | vim 11 | RUN echo "root:root" | chpasswd 12 | RUN useradd -rm -d /home/ve280-demo -s /bin/bash -g root -G sudo -u 1000 ve280-demo 13 | USER ve280-demo 14 | WORKDIR /home/ve280-demo 15 | --------------------------------------------------------------------------------