#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< "; std::cin>>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::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; } Implementation ChooseImplementation(){ unsigned short int choice; do{ std::cout<<"\nChoose an implementation for the binary tree:"< "; std::cin>>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::cin>>std::ws; std::cin>>choice; std::cout< 0) NodeOperations(bt.Root()); else cout<<"\nThe tree does not have any nodes..."; break; case 7: menu(); break; } }while(choice!=7 && choice!=8); } template void FloatFunctions(T& bt){ unsigned short int choice; do{ std::cout< "; std::cin>>std::ws; std::cin>>choice; std::cout< 0) NodeOperations(bt.Root()); else cout<<"\nThe tree does not have any nodes..."; break; case 7: menu(); break; } }while(choice!=7 && choice!=8); } template void StringFunctions(T& bt){ unsigned short int choice; do{ std::cout< "; std::cin>>std::ws; std::cin>>choice; std::cout< 0) NodeOperations(bt.Root()); else cout<<"\nThe tree does not have any nodes..."; break; case 7: menu(); break; } }while(choice!=7 && choice!=8); } /* ----- integer functions ----- */ template 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