From 07cdae9a033336d579d240ec887240fceaaab5b8 Mon Sep 17 00:00:00 2001 From: Alessandro Ferro <49845537+xfarrow@users.noreply.github.com> Date: Wed, 9 Jun 2021 08:13:58 +0200 Subject: [PATCH] Library 5 --- librerie/exercise5/matrix/csr/matrixcsr.cpp | 2 +- librerie/exercise5/zmytest/test.cpp | 201 ++++++++++---------- librerie/exercise5/zmytest/test.hpp | 69 +++++-- 3 files changed, 158 insertions(+), 114 deletions(-) diff --git a/librerie/exercise5/matrix/csr/matrixcsr.cpp b/librerie/exercise5/matrix/csr/matrixcsr.cpp index 4abaddd..d3dbfb5 100755 --- a/librerie/exercise5/matrix/csr/matrixcsr.cpp +++ b/librerie/exercise5/matrix/csr/matrixcsr.cpp @@ -45,7 +45,7 @@ MatrixCSR::MatrixCSR(MatrixCSR&& toMove) noexcept{ std::swap(head, toMove.head); std::swap(R, toMove.R); - toMove.R.Resize(1); + toMove.R.Resize(1); // the moved matrix must be empty toMove.R[0] = &toMove.head; Node** oldHead = R[0]; diff --git a/librerie/exercise5/zmytest/test.cpp b/librerie/exercise5/zmytest/test.cpp index e59fe76..0eef580 100755 --- a/librerie/exercise5/zmytest/test.cpp +++ b/librerie/exercise5/zmytest/test.cpp @@ -50,7 +50,7 @@ Implementation ChooseImplementation(){ do{ std::cout<<"\nChoose an implementation for the binary tree:"< "; std::cin>>std::ws; std::cin>>choice; @@ -103,6 +103,7 @@ void UseChosenType(Implementation chosenImplementation, DataType chosenDataType) } } +/* ----- integer functions ----- */ template void IntegerFunctions(T& mtx){ @@ -152,6 +153,38 @@ void IntegerFunctions(T& mtx){ }while(choice!=8 && choice!=9); } +template +void ProductsElementsLessThan(T& mtx){ + int n, acc=1; + void (*func)(const int&, const void*, void*) = AccumulateProduct; + + cout<<" Multipy all elements of the matrix whose value is less than "; + cin>>ws>>n; + + mtx.FoldPreOrder(func, (void*)&n, (void*)&acc); + cout<<"\n The result is "< +void Double(T& mtx){ + void (*fun)(int&, void*) = MultiplyAnElement; + int par = 2; + mtx.MapPreOrder(fun,(void*)&par); + cout<<" Done.\n"; +} + +void MultiplyAnElement(int& data, void* par){ + data *= *(int*)par; +} + +/* ----- float functions ----- */ + template void FloatFunctions(T& mtx){ unsigned short int choice; @@ -200,6 +233,38 @@ void FloatFunctions(T& mtx){ }while(choice!=8 && choice!=9); } +template +void SumElementsGreaterThan(T& mtx){ + float n, acc = 0; + void (*func)(const float&, const void*, void*) = AccumulateSum; + + cout<<" Sum all elements of the matrix whose value is greater than "; + cin>>ws>>n; + + mtx.FoldPreOrder(func, (void*)&n, (void*)&acc); + cout<<"\n The result is "< (*(float*)par)){ + *(float*)acc += data; + } +} + +template +void CubeElements(T& mtx){ + void (*fun)(float&, void*) = Exponentiation; + float par = 3; + mtx.MapPreOrder(fun,(void*)&par); + cout<<" Done.\n"; +} + +void Exponentiation(float& data, void* par){ + data = (-1.0F) * pow(data , *(float*)par ); +} + +/* ----- string functions ----- */ + template void StringFunctions(T& mtx){ unsigned short int choice; @@ -248,6 +313,44 @@ void StringFunctions(T& mtx){ }while(choice!=8 && choice!=9); } +template +void ConcatLessThan(T& mtx){ + 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; + + mtx.FoldPreOrder(func, (void*)&n, (void*)&concatenated); + cout<<"\n The 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& mtx){ + void (*fun)(string&, void*) = HeadConcatMapAux; + string par; + + cout<<" Concatenate in front the following string: "; + cin>>ws; + cin>>par; + + mtx.MapPreOrder(fun,(void*)&par); + cout<<" Done.\n"; +} + +void HeadConcatMapAux(string& data, void* par){ + data = *(string*)par + data; +} + +/* ----- shared functions ----- */ + template void Resize(T& mtx){ ulong rows, columns; @@ -297,99 +400,6 @@ void Insert(T& mtx){ } } -template -void ConcatLessThan(T& mtx){ - 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; - - mtx.FoldPreOrder(func, (void*)&n, (void*)&concatenated); - cout<<"\n The 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& mtx){ - void (*fun)(string&, void*) = HeadConcatMapAux; - string par; - - cout<<" Concatenate in front the following string: "; - cin>>ws; - cin>>par; - - mtx.MapPreOrder(fun,(void*)&par); - cout<<" Done.\n"; -} -void HeadConcatMapAux(string& data, void* par){ - data = *(string*)par + data; -} - -template -void SumElementsGreaterThan(T& mtx){ - float n, acc = 0; - void (*func)(const float&, const void*, void*) = AccumulateSum; - - cout<<" Sum all elements of the matrix whose value is greater than "; - cin>>ws>>n; - - mtx.FoldPreOrder(func, (void*)&n, (void*)&acc); - cout<<"\n The result is "< (*(float*)par)){ - *(float*)acc += data; - } -} - -template -void CubeElements(T& mtx){ - void (*fun)(float&, void*) = Exponentiation; - float par = 3; - mtx.MapPreOrder(fun,(void*)&par); - cout<<" Done.\n"; -} -void Exponentiation(float& data, void* par){ - data = (-1.0F) * pow(data , *(float*)par ); -} - -template -void Double(T& mtx){ - void (*fun)(int&, void*) = MultiplyAnElement; - int par = 2; - mtx.MapPreOrder(fun,(void*)&par); - cout<<" Done.\n"; -} -void MultiplyAnElement(int& data, void* par){ - data *= *(int*)par; -} - -template -void ProductsElementsLessThan(T& mtx){ - int n, acc=1; - void (*func)(const int&, const void*, void*) = AccumulateProduct; - - cout<<" Multipy all elements of the matrix whose value is less than "; - cin>>ws>>n; - - mtx.FoldPreOrder(func, (void*)&n, (void*)&acc); - cout<<"\n The result is "< class Matrix, typename Data> void CheckExistence(Matrix& mtx){ Data elementToLookFor; @@ -446,7 +456,6 @@ void Print(T& mat){ template T GenerateIntegerMat(T& mat){ - default_random_engine gen(random_device{}()); uniform_int_distribution random_dimension(1,20); // generates the dimensions of the matrix uniform_int_distribution dist(0,100); // generates values inside the matrix @@ -472,9 +481,7 @@ T GenerateIntegerMat(T& mat){ matrix(row,column) = dist(gen); } - return matrix; - } template diff --git a/librerie/exercise5/zmytest/test.hpp b/librerie/exercise5/zmytest/test.hpp index db9d91d..a04848c 100755 --- a/librerie/exercise5/zmytest/test.hpp +++ b/librerie/exercise5/zmytest/test.hpp @@ -13,55 +13,92 @@ enum class DataType{integer,ffloat,sstring}; enum class Implementation{vector,yale}; void menu(); -template -void Print(T&); + +DataType ChooseDataType(); //choose data type + +Implementation ChooseImplementation(); + +/* ----- integer functions ----- */ + template void IntegerFunctions(T&); -template