第十三周。

This commit is contained in:
unlockable
2023-05-17 09:48:55 +08:00
parent 5e6fa305e4
commit 5a2f889bf1
9 changed files with 537 additions and 5 deletions

58
OOP/12/Excercise01.cpp Normal file
View File

@@ -0,0 +1,58 @@
#include <iostream>
using std::cout;
using std::endl;
template <class T>
void swap(T list[], const int left, const int right) {
T tmp = list[left];
list[left] = list[right];
list[right] = tmp;
}
template <class T>
T *sort(T originList[], const int left, const int right) {
int l = left, r = right;
int pivot = left;
if (left >= right) {
return originList;
}
while (l < r) {
while (originList[r] >= originList[pivot] && r > l) {
r--;
}
swap(originList, pivot, r);
pivot = r;
while (originList[l] <= originList[pivot] && l < r) {
l++;
}
swap(originList, pivot, l);
pivot = l;
}
sort(originList, left, pivot - 1);
sort(originList, pivot + 1, right);
return originList;
}
int main() {
int intList[6] = {1, 6, 4, 5, 2, 3};
char charList[14] = "Muelsyse.com!";
for (int i = 0; i < 6; i++) {
cout << intList[i] << ' ';
}
cout << endl;
for (int i = 0; i < 13; i++) {
cout << charList[i] << ' ';
}
cout << endl;
sort(intList, 0, 5);
sort(charList, 0, 9);
for (int i = 0; i < 6; i++) {
cout << intList[i] << ' ';
}
cout << endl;
for (int i = 0; i < 13; i++) {
cout << charList[i] << ' ';
}
cout << endl;
return 0;
}

96
OOP/12/Exercise02.cpp Normal file
View File

@@ -0,0 +1,96 @@
#include <iostream>
using std::cout;
using std::endl;
template <class E> class Storage {
private:
E *objectEptr;
int length;
void swap(int left, int right) {
E tmp = objectEptr[left];
objectEptr[left] = objectEptr[right];
objectEptr[right] = tmp;
}
void quickSort(const int left, const int right) {
if (left >= right) {
return;
}
int l = left, r = right;
int pivot = left;
while (l < r) {
while (objectEptr[r] >= objectEptr[pivot] && r > l) {
r--;
}
this->swap(pivot, r);
pivot = r;
while (objectEptr[l] <= objectEptr[pivot] && l < r) {
l++;
}
this->swap(pivot, l);
pivot = l;
}
this->quickSort(left, pivot - 1);
this->quickSort(pivot + 1, right);
}
public:
Storage() : objectEptr(NULL){};
Storage(const int
_length) : objectEptr(new E[_length]), length(_length){};
Storage(const E *originptr, const int _n)
: objectEptr(new E[_n]), length(_n) {
for (int i = 0; i < _n; i++) {
objectEptr[i] = originptr[i];
}
};
~Storage() {
delete[] objectEptr;
}
Storage &sort() {
quickSort(0, length - 1);
return *this;
}
Storage &sort(const int leftIndex, const int rightIndex) {
quickSort(leftIndex, rightIndex);
return *this;
}
E *getList() {
return this->objectEptr;
}
int getLength() {
return this->length;
}
Storage &printOut() {
for (int i = 0; i < this->length; i++) {
cout << objectEptr[i] << ' ';
}
cout << endl;
return *this;
}
E &operator[](int index) {
return objectEptr[index];
}
int search(E target) {
for (int i = 0; i < this->length; i++) {
if (target == objectEptr[i]) {
return i;
}
}
return -1;
}
};
int main() {
int list[6] = {1, 4, 6, 5, 3, 2};
std::string str = "HelloWorld!";
Storage<int> aList(list, 6);
Storage<char> charList(str.c_str(), str.length() + 1);
aList.printOut().sort().printOut();
charList.printOut().sort(0, charList.getLength() - 2).printOut();
aList[2] = 8;
aList.printOut();
cout << "8 is at index: " << aList.search(8) << endl;
cout << "H is at index: " << charList.search('H') << endl;
return 0;
}

View File

@@ -0,0 +1,8 @@
#pragma once
class BaseListExceptions {};
class ValueError : public BaseListExceptions {};
class IndexError : public BaseListExceptions {};
class DuplicateError : public BaseListExceptions {};

163
OOP/12/Optional01/listE.h Executable file
View File

@@ -0,0 +1,163 @@
#pragma once
#include "ListExceptions.h"
#include "node.h"
template <class E> class List {
private:
Node<E> *head;
int _length;
int getIndexInRange(int index);
public:
List();
List(const E &newItem);
List(const E *newItemList, const int itemCount);
~List();
// Append _newItem at last. Returns the List itself.
List<E> &append(const E &_newItem);
// Insert _newItem at index, and move all items at and after [index] 1 step.
// Throws IndexError if index is not in the range of [-_length, _length -
// 1]. Returns the List itself.
List<E> &insert(int index, const E &_newItem);
// Pop the item at given index, default is the to pop the last one.
E pop(int index = -1);
// Remove the first occurance of target. Throws ValueError if target does
// not exist. Returns the List itself.
List<E> &remove(const E &target);
// Clear all nodes.
List<E> &clear();
E &operator[](const int index);
int length();
bool contains(const E &target);
// Give the index of target in the list on its first appearance. Throws
// ValueError if target does not exist.
int index(const E &target);
};
template <class E> int List<E>::getIndexInRange(int index) {
if (index < 0) {
index += this->_length;
}
if (index < 0 || index >= _length) {
throw(IndexError());
}
return index;
}
template <class E> List<E>::List() : head(NULL), _length(0){};
template <class E>
List<E>::List(const E &newItem) : head(new Node<E>(newItem)), _length(1){};
template <class E> List<E>::List(const E *newItemList, const int itemCount) {
for (int i = 0; i < itemCount; i++) {
this->append(newItemList[i]);
}
};
template <class E> List<E>::~List() {
if (this->head != NULL) {
this->head->destruct();
}
head = NULL;
}
template <class E> List<E> &List<E>::append(const E &_newItem) {
if (this->head == NULL) {
this->head = new Node<E>(_newItem);
}
else {
this->head->getTail().setNext(new Node<E>(_newItem));
}
this->_length++;
return *this;
}
template <class E> List<E> &List<E>::insert(int index, const E &_newItem) {
index = this->getIndexInRange(index);
if (index == 0) {
this->head = new Node<E>(_newItem, this->head);
}
else {
Node<E> &prev = this->head->getByIndex(index - 1);
prev.setNext(new Node<E>(_newItem, prev.getNextPtr()));
}
this->_length++;
return *this;
}
template <class E> List<E> &List<E>::remove(const E &target) {
this->pop(this->index(target));
return *this;
}
template <class E> E List<E>::pop(int index) {
if (_length == 0) {
throw(IndexError());
}
index = this->getIndexInRange(index);
Node<E> removed = this->head->getByIndex(index);
this->_length--;
if (index == 0) {
Node<E> *newHead = this->head->getNextPtr();
delete this->head;
this->head = newHead;
}
else {
Node<E> &prev = this->head->getByIndex(index - 1);
delete prev.getNextPtr();
prev.setNext(removed.getNextPtr());
}
return removed.getContent();
}
template <class E> List<E> &List<E>::clear() {
this->head->destruct();
this->head = NULL;
this->_length = 0;
return *this;
}
template <class E> E &List<E>::operator[](const int index) {
return this->head->getByIndex(getIndexInRange(index)).getContent();
}
template <class E> int List<E>::length() {
return this->_length;
}
template <class E> bool List<E>::contains(const E &target) {
if (this->_length == 0) {
return false;
}
Node<E> *current = this->head;
do {
if (current->getContent() == target) {
return true;
}
current = current->getNextPtr();
} while (current != NULL);
return false;
}
template <class E> int List<E>::index(const E &target) {
if (this->_length == 0) {
throw(ValueError());
}
Node<E> *current = this->head;
int pos = 0;
do {
if (current->getContent() == target) {
return pos;
}
current = current->getNextPtr();
pos++;
} while (current != NULL);
throw(ValueError());
}

65
OOP/12/Optional01/main.cpp Executable file
View File

@@ -0,0 +1,65 @@
#include "listE.h"
#include "setE.h"
#include <iostream>
using std::cout;
using std::endl;
template <class E> void output(List<E>& list) {
for (int i = 0; i < list.length(); i++) {
cout << list[i] << ' ';
}
if (list.length() == 0) {
cout << "(Empty)";
}
cout << endl;
}
int main() {
int nums[5] = {1, 4, 6, 8, 3};
List<int> aList;
List<int> bList(nums, 5);
output(bList);
try {
aList.remove(0);
}
catch(ValueError) {
std::cerr << "There is an ValueError" << std::endl;
}
aList.append(3);
aList.append(8);
aList.append(9);
aList.append(12);
aList.insert(1, 20);
output(aList);
cout << aList.pop() << endl;
output(aList);
aList.pop(2);
output(aList);
cout << aList.contains(3) << ' ' << aList.contains(15) << endl;
try {
aList.index(15);
}
catch(ValueError) {
std::cerr << "There's no 15 in this list." << std::endl;
}
aList.clear();
output(aList);
cout << "\n----------\n" << endl;
Set<int> aSet;
aSet.show();
aSet.add(2);
aSet.add(3);
aSet.show();
aSet.remove(2);
aSet.show();
return 0;
}

67
OOP/12/Optional01/node.h Executable file
View File

@@ -0,0 +1,67 @@
#pragma once
#include <iostream>
template <class E> class Node {
private:
E content;
Node *nextNode;
public:
Node();
Node(E _content, Node<E> *_nextNode = NULL);
~Node();
// Returns a reference to the content.
E &getContent();
// Returns a reference to the next Node. May return NULL if this Node is the
// last one.
Node<E> *getNextPtr();
// Set the next Node _nextNode. Returns THIS Node.
Node<E> &setNext(Node<E> *_nextNode);
// Returns a reference to the Tail.
Node<E> &getTail();
// Returns a reference to the Node of given index.
Node<E> &getByIndex(const int index);
void destruct();
};
template <class E> Node<E>::Node() : content(0), nextNode(NULL){};
template <class E>
Node<E>::Node(E _content, Node *_nextNode)
: content(_content), nextNode(_nextNode){};
template <class E> Node<E>::~Node(){};
template <class E> E &Node<E>::getContent() {
return this->content;
}
template <class E> Node<E> *Node<E>::getNextPtr() {
return this->nextNode;
}
template <class E> Node<E> &Node<E>::setNext(Node<E> *_nextNode) {
this->nextNode = _nextNode;
return *this;
}
template <class E> Node<E> &Node<E>::getTail() {
if (this->nextNode == NULL) {
return *this;
}
return this->nextNode->getTail();
}
template <class E> Node<E> &Node<E>::getByIndex(const int index) {
if (index == 0) {
return *this;
}
return this->nextNode->getByIndex(index - 1);
}
template <class E> void Node<E>::destruct() {
if (this->nextNode != NULL) {
this->nextNode->destruct();
}
delete this;
}

69
OOP/12/Optional01/setE.h Normal file
View File

@@ -0,0 +1,69 @@
#include "ListExceptions.h"
#include "listE.h"
template <class E> class Set {
private:
List<E> _list;
public:
Set();
Set(const E &_newItem);
Set(const E *_newItem, const int count);
// Add an element.
Set<E> &add(const E &_newItem);
// Remove an element.
Set<E> &remove(const E &target);
// Clear all elements.
Set<E> &clear();
int size();
void show();
};
template <class E> Set<E>::Set() : _list(){};
template <class E> Set<E>::Set(const E &_newItem) : _list(_newItem){};
template <class E> Set<E>::Set(const E *_newItem, const int count) : _list() {
for (int i = 0; i < count; i++) {
try {
this->add(_newItem[i]);
}
catch (DuplicateError) {
continue;
}
}
};
template <class E> Set<E> &Set<E>::add(const E &_newItem) {
if (_list.contains(_newItem)) {
throw(DuplicateError());
}
_list.append(_newItem);
return *this;
};
template <class E> Set<E> &Set<E>::remove(const E &target) {
_list.remove(target);
return *this;
}
template <class E> Set<E> &Set<E>::clear() {
_list.clear();
return *this;
}
template <class E> int Set<E>::size() {
return _list.length();
}
template <class E> void Set<E>::show() {
for (int i = 0; i < _list.length(); i++) {
std::cout << _list[i] << ' ';
}
if (_list.length() == 0) {
std::cout << "(Empty)";
}
std::cout << std::endl;
}

View File

@@ -59,6 +59,12 @@ public:
int main() {
// D objD(1, 2, 3, 4);
// objD.display();
cout << "---\n" << sizeof(A) << endl << sizeof(B) << endl << sizeof(C) << endl;
// cout << "---\n" << sizeof(A) << endl << sizeof(B) << endl << sizeof(C) << endl;
// cout << -13 % 6 << endl;
cout << "---";
endl(cout);
endl(cout);
cout << "---";
endl(cout);
return 0;
}

View File

@@ -60,7 +60,7 @@ NODE* insert(NODE* head, int index, int value) {
NODE* newNodePtr;
currentNode = head;
if ((*currentNode).index > index) {
newNodePtr = malloc(LEN);
newNodePtr = (NODE*) malloc(LEN);
(*newNodePtr).index = index;
(*newNodePtr).value = value;
(*newNodePtr).prev = NULL;
@@ -77,7 +77,7 @@ NODE* insert(NODE* head, int index, int value) {
currentNode = (*currentNode).next;
continue;
}
newNodePtr = malloc(LEN);
newNodePtr = (NODE*)malloc(LEN);
(*newNodePtr).index = index;
(*newNodePtr).value = value;
(*newNodePtr).prev = currentNode;
@@ -86,7 +86,7 @@ NODE* insert(NODE* head, int index, int value) {
(*currentNode).next = newNodePtr;
return newNodePtr;
}
newNodePtr = malloc(LEN);
newNodePtr = (NODE*)malloc(LEN);
(*newNodePtr).index = index;
(*newNodePtr).value = value;
(*newNodePtr).prev = currentNode;
@@ -97,7 +97,7 @@ NODE* insert(NODE* head, int index, int value) {
NODE* create(int index, int value) {
NODE* newNodePtr;
newNodePtr = malloc(LEN);
newNodePtr = (NODE*)malloc(LEN);
(*newNodePtr).index = index;
(*newNodePtr).value = value;
(*newNodePtr).prev = NULL;