From c32e658308d905a0daaf603e0a694edad8628bbc Mon Sep 17 00:00:00 2001 From: Alessandro Ferro <49845537+xfarrow@users.noreply.github.com> Date: Fri, 7 May 2021 12:24:13 +0200 Subject: [PATCH] Library 3 added menu --- librerie/exercise3/main.cpp | 1 - librerie/exercise3/zmytest/test.cpp | 419 +++++++++++++++++++++++++++- librerie/exercise3/zmytest/test.hpp | 75 +++-- 3 files changed, 474 insertions(+), 21 deletions(-) diff --git a/librerie/exercise3/main.cpp b/librerie/exercise3/main.cpp index 61e37e2..bfc751c 100755 --- a/librerie/exercise3/main.cpp +++ b/librerie/exercise3/main.cpp @@ -12,6 +12,5 @@ int main() { std::cout << "Lasd Libraries 2020" << std::endl; menu(); - lasdtest(); // To call in the menu of your library test! return 0; } diff --git a/librerie/exercise3/zmytest/test.cpp b/librerie/exercise3/zmytest/test.cpp index 07a921d..affaf0e 100755 --- a/librerie/exercise3/zmytest/test.cpp +++ b/librerie/exercise3/zmytest/test.cpp @@ -1,2 +1,419 @@ -// ... +#include"test.hpp" +#include +#include +#include +#include "../zlasdtest/test.hpp" + +using namespace lasd; + +void menu(){ + + unsigned short int choice; + + DataType chosenDataType; + Implementation chosenImplementation; + + do{ + std::cout<<"1. Use your tests (to be used by the professor)"<>std::ws; + std::cin>>choice; + }while(choice!=1 && choice!=2); + switch(choice){ + case 1: + lasdtest(); + break; + case 2: + chosenImplementation = ChooseImplementation(); + chosenDataType = ChooseDataType(); + UseChosenType(chosenImplementation, chosenDataType); + break; + } +} + +DataType ChooseDataType(){ + unsigned short int choice; + do{ + std::cout<<"\nChoose a data type:"<>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; +} +Implementation ChooseImplementation(){ + unsigned short int choice; + do{ + std::cout<<"\nChoose an implementation for the binary tree:"<>std::ws; + std::cin>>choice; + }while(!(choice>0 && choice<3)); + if(choice==1) + return Implementation::vector; + else if(choice==2) + return Implementation::pointers; +} + +void UseChosenType(Implementation chosenImplementation, DataType chosenDataType){ + + if(chosenImplementation == Implementation::vector){ + if(chosenDataType == DataType::integer){ + BinaryTreeVec bt; + bt = GenerateIntegerBT(bt); + IntegerFunctions(bt); + + }else if(chosenDataType == DataType::ffloat){ + BinaryTreeVec bt; + bt = GenerateFloatBT(bt); + FloatFunctions(bt); + + }else if(chosenDataType == DataType::sstring){ + BinaryTreeVec bt; + bt = GenerateStringsBT(bt); + StringFunctions(bt); + } + }else if(chosenImplementation == Implementation::pointers){ + if(chosenDataType == DataType::integer){ + BinaryTreeLnk bt; + bt = GenerateIntegerBT(bt); + IntegerFunctions(bt); + + }else if(chosenDataType == DataType::ffloat){ + BinaryTreeLnk bt; + bt = GenerateFloatBT(bt); + FloatFunctions(bt); + + }else if(chosenDataType == DataType::sstring){ + BinaryTreeLnk bt; + bt = GenerateStringsBT(bt); + StringFunctions(bt); + } + } +} + +template +void IntegerFunctions(T& bt){ + unsigned short int choice; + do{ + std::cout<>std::ws; + std::cin>>choice; + std::cout< +void FloatFunctions(T& bt){ + unsigned short int choice; + do{ + std::cout<>std::ws; + std::cin>>choice; + std::cout< +void StringFunctions(T& bt){ + unsigned short int choice; + do{ + std::cout<>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 MultiplyByThree(T& tree){ + void (*fun)(int&, void*) = MultiplyAnElement; + int par = 3; + tree.MapBreadth(fun,(void*)&par); + cout<<"The tree is:\n\n"; + PrintTree(tree); +} +void MultiplyAnElement(int& data, void* par){ + data *= *(int*)par; +} + +/* ----- float functions ----- */ +template +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; + } +} + +template +void CubeElements(T& tree){ + void (*fun)(float&, void*) = Exponentiation; + float par = 3; + tree.MapBreadth(fun,(void*)&par); + cout<<"The tree is:\n\n"; + PrintTree(tree); +} +void Exponentiation(float& data, void* par){ + data = pow(data , *(float*)par ); +} + +/* ----- string functions ----- */ +template +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; + } +} + +template +void HeadConcat(T& tree){ + void (*fun)(string&, void*) = HeadConcatMapAux; + string par; + + cout<<"Concatenate in front the following string: "; + cin>>ws; + cin>>par; + + tree.MapBreadth(fun,(void*)&par); + cout<<"The tree is:\n\n"; + PrintTree(tree); +} +void HeadConcatMapAux(string& data, void* par){ + data = *(string*)par + data; +} + +/* ----- shared functions ----- */ +template