From e0e8ae4481d75f46f1bf934dba14fd2d57ca74bd Mon Sep 17 00:00:00 2001 From: Alessandro Ferro <49845537+xfarrow@users.noreply.github.com> Date: Mon, 17 May 2021 11:51:00 +0200 Subject: [PATCH] Library 4 added menu --- librerie/exercise4/bst/bst.cpp | 2 +- librerie/exercise4/main.cpp | 2 +- librerie/exercise4/zmytest/test.cpp | 719 +++++++++++++++++++++++++++- librerie/exercise4/zmytest/test.hpp | 106 +++- 4 files changed, 823 insertions(+), 6 deletions(-) diff --git a/librerie/exercise4/bst/bst.cpp b/librerie/exercise4/bst/bst.cpp index c0c0368..3cf96fb 100755 --- a/librerie/exercise4/bst/bst.cpp +++ b/librerie/exercise4/bst/bst.cpp @@ -262,7 +262,7 @@ template typename BST::NodeLnk* const& BST::FindPointerToMax(struct BST::NodeLnk* const& node) const noexcept{ NodeLnk* const* ptr = &node; NodeLnk* curr = node; - if(curr!=nullptr){ + if(curr!=nullptr){ while(curr->right != nullptr){ ptr = &curr->right; curr = curr->right; diff --git a/librerie/exercise4/main.cpp b/librerie/exercise4/main.cpp index f111216..ab01017 100755 --- a/librerie/exercise4/main.cpp +++ b/librerie/exercise4/main.cpp @@ -11,6 +11,6 @@ using namespace lasd; int main() { std::cout << "Lasd Libraries 2020" << std::endl; - lasdtest(); // To call in the menu of your library test! + menu(); return 0; } diff --git a/librerie/exercise4/zmytest/test.cpp b/librerie/exercise4/zmytest/test.cpp index 07a921d..f45649d 100755 --- a/librerie/exercise4/zmytest/test.cpp +++ b/librerie/exercise4/zmytest/test.cpp @@ -1,2 +1,719 @@ +#include"test.hpp" +#include +#include +#include +#include "../zlasdtest/test.hpp" -// ... +using namespace lasd; +using namespace std; + +void menu(){ + + unsigned short int choice; + DataType chosenDataType; + + do{ + std::cout< "; + std::cin>>std::ws; + std::cin>>choice; + }while(choice!=1 && choice!=2); + switch(choice){ + case 1: + lasdtest(); + break; + case 2: + chosenDataType = ChooseDataType(); + UseChosenType(chosenDataType); + break; + } +} +DataType ChooseDataType(){ + unsigned short int choice; + do{ + std::cout<<"\nChoose a data type:"< "; + std::cin>>std::ws; + std::cin>>choice; + }while(!(choice>0 && choice<4)); + if(choice==1) + return DataType::integer; + else if(choice==2) + return DataType::ffloat; + else if(choice==3) + return DataType::sstring; +} + +void UseChosenType(DataType chosenDataType){ + if(chosenDataType == DataType::integer){ + BST bst; + bst = GenerateIntegerBST(bst); + IntegerFunctions(bst); + + }else if(chosenDataType == DataType::ffloat){ + BST bst; + bst = GenerateFloatBST(bst); + FloatFunctions(bst); + + }else if(chosenDataType == DataType::sstring){ + BST bst; + bst = GenerateStringsBST(bst); + StringFunctions(bst); + } +} + +/* ----- integer functions ----- */ + +template +void IntegerFunctions(T& bst){ + unsigned short int choice; + do{ + std::cout< "; + std::cin>>std::ws; + std::cin>>choice; + std::cout< +void ProductsElementsLessThan(T& tree){ + int n, acc=1; + void (*func)(const int&, const void*, void*) = AccumulateProduct; + + cout<<"Multipy all elements of the tree whose value is less than "; + cin>>ws>>n; + + tree.FoldBreadth(func, (void*)&n, (void*)&acc); + cout<<"\nThe result is "< +void FloatFunctions(T& bst){ + unsigned short int choice; + do{ + std::cout< "; + std::cin>>std::ws; + std::cin>>choice; + std::cout< +void SumElementsGreaterThan(T& tree){ + float n, acc = 0; + void (*func)(const float&, const void*, void*) = AccumulateSum; + + cout<<"Sum all elements of the tree whose value is greater than "; + cin>>ws>>n; + + tree.FoldBreadth(func, (void*)&n, (void*)&acc); + cout<<"\nThe result is "< (*(float*)par)){ + *(float*)acc += data; + } +} + +/* ----- string functions ----- */ + +template +void StringFunctions(T& bst){ + unsigned short int choice; + do{ + std::cout< "; + std::cin>>std::ws; + std::cin>>choice; + std::cout< +void ConcatLessThan(T& tree){ + int n; + string concatenated = ""; + void (*func)(const string&, const void*, void*) = ConcatAString; + + cout<<"Concatenate all elements of the tree whose length is less or equal than "; + cin>>ws>>n; + + tree.FoldBreadth(func, (void*)&n, (void*)&concatenated); + cout<<"\nThe result is "<< concatenated << endl << endl; +} + +void ConcatAString(const string& data, const void* par, void* acc){ + if( ((int)data.length()) <= ((*(int*)par)) ){ + *(string*)acc = *(string*)acc + "-" + data; + } +} + +/* ----- shared functions ----- */ +template