diff options
author | zedarider <ymherklotz@gmail.com> | 2016-03-10 15:58:54 +0000 |
---|---|---|
committer | zedarider <ymherklotz@gmail.com> | 2016-03-10 15:58:54 +0000 |
commit | f52b1d4a370f1720995337a1eb3e01f9383af2e4 (patch) | |
tree | 60dd9ca907da80881959e6749bddb4f38a375805 | |
parent | e7978d38642d83bfaea85f637cc594037ea5ca3a (diff) | |
download | imperial_2015-f52b1d4a370f1720995337a1eb3e01f9383af2e4.tar.gz imperial_2015-f52b1d4a370f1720995337a1eb3e01f9383af2e4.zip |
Adding the testExample
-rw-r--r-- | C++/BinaryTree.cpp | 87 | ||||
-rwxr-xr-x | C++/Recursion | bin | 0 -> 10616 bytes | |||
-rwxr-xr-x | C++/final_test/ExampleTest | bin | 0 -> 19240 bytes | |||
-rw-r--r-- | C++/final_test/ExampleTest.cpp | 130 | ||||
-rw-r--r-- | C++/final_test/log.txt | 5 |
5 files changed, 222 insertions, 0 deletions
diff --git a/C++/BinaryTree.cpp b/C++/BinaryTree.cpp new file mode 100644 index 0000000..68ebefa --- /dev/null +++ b/C++/BinaryTree.cpp @@ -0,0 +1,87 @@ +#include <iostream> + +using namespace std; + +typedef int tree_t; + +struct treeNode { + tree_t element; + treeNode* left; + treeNode* right; + + treeNode(tree_t el) { + element = el; + left = NULL; + right = NULL; + } +}; + +void addElement(treeNode*&, tree_t&); +void deallocateTree(treeNode*&); +void printTree(treeNode*); +void deleteFromRoot(treeNode*&, tree_t&); +int treeHeight(treeNode*); + +int main() { + treeNode* firstTree = NULL; + tree_t newElement; + int n; + + cout << "Enter number of elements: "; + cin >> n; + + for(int i = 0; i < n; ++i) { + cin >> newElement; + addElement(firstTree, newElement); + } + + cout << "printing tree: " << endl; + + deallocateTree(firstTree); + return 0; +} + +void addElement(treeNode* &hdTree, tree_t &el) { + if(hdTree == NULL) { + treeNode* newTreeNode = new treeNode(el); + hdTree = newTreeNode; + } else if(el < hdTree->element) { + addElement(hdTree->left, el); + } else { + addElement(hdTree->right, el); + } +} + +void deallocateTree(treeNode* &hdTree) { + if(hdTree != NULL) { + deallocateTree(hdTree->left); + deallocateTree(hdTree->right); + delete hdTree; + } +} + +void printTree(treeNode* hdTree) { + if(hdTree != NULL) { + printTree(hdTree->left); + cout << hdTree->element; + printTree(hdTree->right); + } +} + +void deleteFromRoot(treeNode* &hdTree, tree_t &el) { + if(el < hdTree->element) { + deleteFromRoot(hdTree->left, el); + } else if(el > hdTree->element) { + deleteFromRoot(hdTree->right, el); + } else { + // Do something + } +} + +int treeHeight(treeNode* hdTree) { + // Do something +} + +bool elementInTree(treeNode* hdTree, tree_t &el) { + +} diff --git a/C++/Recursion b/C++/Recursion Binary files differnew file mode 100755 index 0000000..9d1f633 --- /dev/null +++ b/C++/Recursion diff --git a/C++/final_test/ExampleTest b/C++/final_test/ExampleTest Binary files differnew file mode 100755 index 0000000..e82c913 --- /dev/null +++ b/C++/final_test/ExampleTest diff --git a/C++/final_test/ExampleTest.cpp b/C++/final_test/ExampleTest.cpp new file mode 100644 index 0000000..ccab6e2 --- /dev/null +++ b/C++/final_test/ExampleTest.cpp @@ -0,0 +1,130 @@ +#include <iostream> +#include <cstdlib> +#include <fstream> +#include <string> +#include <sstream> + +using namespace std; + +struct element { + string name; + double weight; + double value; + + double evaluateRatio() { + return value / weight; + } + + string formatString() { + stringstream ss; + ss << name << " w: " << weight << " v: " << value; + return ss.str(); + } +}; + +void sortItems(element*&, int&, int&); +void selectItems(element*&, int&, element*&, int&, double&); +bool inArray(element*&, int&, element&); + +int main() { + ifstream inputFile; + string fileName; + int arraySize, count; + element tmpEl; + + element* elArray; + + count = 0; + + cout << "Enter the name of the input file:" << endl; + cin >> fileName; + + inputFile.open(fileName.c_str()); + if(!inputFile.is_open()) { + cout << "Error opening file... Exiting now" << endl; + exit(EXIT_FAILURE); + } + + inputFile >> arraySize; + + elArray = new element[arraySize]; + + while(inputFile >> tmpEl.name >> tmpEl.weight >> tmpEl.value) { + elArray[count++] = tmpEl; + } + + cout << endl << "input:" << endl; + for(int i = 0; i < count; ++i) { + cout << elArray[i].formatString() << endl; + } + + sortItems(elArray, arraySize, count); + cout << endl << "Sorted input by value / weight ratio:" << endl; + for(int i = 0; i < count; ++i) { + cout << elArray[i].formatString() << endl; + } + + double usrIn = 0; + element* targetArray = new element[arraySize]; + int targetCount; + double wght, vlue; + + while(usrIn >= 0) { + wght = 0; + vlue = 0; + cout << endl << "Enter target maximum weight (negative to exit): "; + cin >> usrIn; + if(usrIn >= 0) { + selectItems(elArray, count, targetArray, targetCount, usrIn); + cout << endl << "The best approximation for value and given weight is: " << endl << endl; + for(int i = 0; i < targetCount; ++i) { + cout << targetArray[i].formatString() << endl; + wght += targetArray[i].weight; + vlue += targetArray[i].value; + } + cout << endl << "weight: " << wght << endl << "value: " << vlue << endl; + } + } + return 0; +} + +void sortItems(element* &elArr, int &maxSize, int &count) { + element* tmpArr = new element[maxSize]; + element tmpEl; + + for(int i = 0; i < count; ++i) { + for(int j = 0; j < count; ++j) { + if(!inArray(tmpArr, i, elArr[j])) { + tmpEl = elArr[j]; + } + } + for(int j = 0; j < count; ++j) { + if(!inArray(tmpArr, i, elArr[j]) && elArr[j].evaluateRatio() > tmpEl.evaluateRatio()) { + tmpEl = elArr[j]; + } + } + tmpArr[i] = tmpEl; + } + + delete[] elArr; + elArr = tmpArr; +} + +void selectItems(element* &elArr, int &count, element* &output, int &outCount, double &tarWeight) { + outCount = 0; + for(int i = 0; i < count; ++i) { + if(elArr[i].weight < tarWeight) { + output[outCount++] = elArr[i]; + tarWeight -= elArr[i].weight; + } + } +} + +bool inArray(element* &elArr, int &count, element &el) { + for(int i = 0; i < count; ++i) { + if(elArr[i].name == el.name) { + return true; + } + } + return false; +} diff --git a/C++/final_test/log.txt b/C++/final_test/log.txt new file mode 100644 index 0000000..74b6183 --- /dev/null +++ b/C++/final_test/log.txt @@ -0,0 +1,5 @@ +10 +Iridium 60.5 120.0 +Platinium 80 12.4 +Iron 31 29.3 +Something 30 15 |