Library 4

improvements
This commit is contained in:
Alessandro Ferro 2021-05-17 15:21:13 +02:00
parent e0e8ae4481
commit aa71e72169
3 changed files with 122 additions and 121 deletions

View File

@ -1,7 +1,7 @@
#! /bin/bash #! /bin/bash
g++-10 -O3 -o main \ g++-10 -o main \
zlasdtest/exercise1/simpletest.cpp zlasdtest/exercise1/fulltest.cpp \ zlasdtest/exercise1/simpletest.cpp zlasdtest/exercise1/fulltest.cpp \
zlasdtest/exercise2/simpletest.cpp zlasdtest/exercise2/fulltest.cpp \ zlasdtest/exercise2/simpletest.cpp zlasdtest/exercise2/fulltest.cpp \
zlasdtest/exercise3/simpletest.cpp zlasdtest/exercise3/fulltest.cpp \ zlasdtest/exercise3/simpletest.cpp zlasdtest/exercise3/fulltest.cpp \

View File

@ -30,6 +30,7 @@ void menu(){
break; break;
} }
} }
DataType ChooseDataType(){ DataType ChooseDataType(){
unsigned short int choice; unsigned short int choice;
do{ do{
@ -69,8 +70,8 @@ void UseChosenType(DataType chosenDataType){
/* ----- integer functions ----- */ /* ----- integer functions ----- */
template <typename T> template <typename Data>
void IntegerFunctions(T& bst){ void IntegerFunctions(BST<Data>& bst){
unsigned short int choice; unsigned short int choice;
do{ do{
std::cout<<std::endl<<std::endl; std::cout<<std::endl<<std::endl;
@ -165,8 +166,8 @@ void IntegerFunctions(T& bst){
}while(choice!=19 && choice!=20); }while(choice!=19 && choice!=20);
} }
template <typename T> template <typename Data>
void ProductsElementsLessThan(T& tree){ void ProductsElementsLessThan(BST<Data>& tree){
int n, acc=1; int n, acc=1;
void (*func)(const int&, const void*, void*) = AccumulateProduct; void (*func)(const int&, const void*, void*) = AccumulateProduct;
@ -185,8 +186,8 @@ void AccumulateProduct(const int& data, const void* par, void* acc){
} }
/* ----- float functions ----- */ /* ----- float functions ----- */
template <typename T> template <typename Data>
void FloatFunctions(T& bst){ void FloatFunctions(BST<Data>& bst){
unsigned short int choice; unsigned short int choice;
do{ do{
std::cout<<std::endl<<std::endl; std::cout<<std::endl<<std::endl;
@ -281,8 +282,8 @@ void FloatFunctions(T& bst){
}while(choice!=19 && choice!=20); }while(choice!=19 && choice!=20);
} }
template <typename T> template <typename Data>
void SumElementsGreaterThan(T& tree){ void SumElementsGreaterThan(BST<Data>& tree){
float n, acc = 0; float n, acc = 0;
void (*func)(const float&, const void*, void*) = AccumulateSum; void (*func)(const float&, const void*, void*) = AccumulateSum;
@ -301,8 +302,8 @@ void AccumulateSum(const float& data, const void* par, void* acc){
/* ----- string functions ----- */ /* ----- string functions ----- */
template <typename T> template <typename Data>
void StringFunctions(T& bst){ void StringFunctions(BST<Data>& bst){
unsigned short int choice; unsigned short int choice;
do{ do{
std::cout<<std::endl<<std::endl; std::cout<<std::endl<<std::endl;
@ -397,8 +398,8 @@ void StringFunctions(T& bst){
}while(choice!=19 && choice!=20); }while(choice!=19 && choice!=20);
} }
template <typename T> template <typename Data>
void ConcatLessThan(T& tree){ void ConcatLessThan(BST<Data>& tree){
int n; int n;
string concatenated = ""; string concatenated = "";
void (*func)(const string&, const void*, void*) = ConcatAString; void (*func)(const string&, const void*, void*) = ConcatAString;
@ -417,9 +418,9 @@ void ConcatAString(const string& data, const void* par, void* acc){
} }
/* ----- shared functions ----- */ /* ----- shared functions ----- */
template <template <typename...> class Tree, typename DTType> template <typename Data>
void PrintTree(Tree<DTType>& tree){ void PrintTree(BST<Data>& tree){
void (*PrinterFunction) (DTType&, void*) = PrintSingleElement; void (*PrinterFunction) (Data&, void*) = PrintSingleElement;
cout<<"Pre order:\n"; cout<<"Pre order:\n";
tree.MapPreOrder(PrinterFunction, nullptr); tree.MapPreOrder(PrinterFunction, nullptr);
@ -443,88 +444,88 @@ void PrintSingleElement(Data& data, void* _){
std::cout << data << " "; std::cout << data << " ";
} }
template <template <typename...> class Tree, typename DTType> template <typename Data>
void CheckExistence(Tree<DTType>& tree){ void CheckExistence(BST<Data>& tree){
DTType elementToLookFor; Data elementToLookFor;
cout<<"\n\nCheck existence in the tree of: "; cout<<"\n\nCheck existence in the tree of: ";
cin>>ws; cin>>ws;
cin>>elementToLookFor; cin>>elementToLookFor;
cout<<"The element " << ( (!tree.Exists(elementToLookFor))? "does not " : "") << "exists\n\n"; cout<<"The element " << ( (!tree.Exists(elementToLookFor))? "does not " : "") << "exists\n\n";
} }
template <template <typename...> class Tree, typename DTType> template <typename Data>
void InsertElement(Tree<DTType>& bst){ void InsertElement(BST<Data>& bst){
DTType elementToInsert; Data elementToInsert;
cout<<"\n\nInsert in the BST the following element: "; cout<<"\n\nInsert in the BST the following element: ";
cin>>ws; cin>>ws;
cin>>elementToInsert; cin>>elementToInsert;
bst.Insert(elementToInsert); bst.Insert(elementToInsert);
} }
template <template <typename...> class Tree, typename DTType> template <typename Data>
void RemoveElement(Tree<DTType>& bst){ void RemoveElement(BST<Data>& bst){
DTType elementToRemove; Data elementToRemove;
cout<<"\n\nRemove from the BST the following element: "; cout<<"\n\nRemove from the BST the following element: ";
cin>>ws; cin>>ws;
cin>>elementToRemove; cin>>elementToRemove;
bst.Remove(elementToRemove); bst.Remove(elementToRemove);
} }
template <typename T> template <typename Data>
void PrintMinimum(T& bst){ void PrintMinimum(BST<Data>& bst){
if(bst.Size()>0) if(bst.Size()>0)
cout<<"\n\nThe minimum element in the BST is "<<bst.Min(); cout<<"\n\nThe minimum element in the BST is "<<bst.Min();
else else
cout<<"\n\nThe tree is empty."; cout<<"\n\nTree is empty.";
} }
template <typename T> template <typename Data>
void PrintMinimumNDelete(T& bst){ void PrintMinimumNDelete(BST<Data>& bst){
if(bst.Size()>0) if(bst.Size()>0)
cout<<"The minimum element in the BST ( "<<bst.MinNRemove()<<" ) has been removed"; cout<<"The minimum element in the BST ( "<<bst.MinNRemove()<<" ) has been removed";
else else
cout<<"\n\nThe tree is empty."; cout<<"\n\nTree is empty.";
} }
template <typename T> template <typename Data>
void RemoveMin(T& bst){ void RemoveMin(BST<Data>& bst){
if(bst.Size()>0){ if(bst.Size()>0){
bst.RemoveMin(); bst.RemoveMin();
cout<<"\n\nThe minimum element has been deleted"; cout<<"\n\nThe minimum element has been deleted";
} }
else else
cout<<"\n\nThe tree is empty"; cout<<"\n\nTree is empty.";
} }
template <typename T> template <typename Data>
void PrintMaximum(T& bst){ void PrintMaximum(BST<Data>& bst){
if(bst.Size()>0) if(bst.Size()>0)
cout<<"\n\nThe maximum element in the BST is "<<bst.Max(); cout<<"\n\nThe maximum element in the BST is "<<bst.Max();
else else
cout<<"\n\nThe tree is empty."; cout<<"\n\nTree is empty.";
} }
template <typename T> template <typename Data>
void PrintMaximumNDelete(T& bst){ void PrintMaximumNDelete(BST<Data>& bst){
if(bst.Size()>0) if(bst.Size()>0)
cout<<"The maximum element in the BST ( "<<bst.MaxNRemove()<<" ) has been removed"; cout<<"The maximum element in the BST ( "<<bst.MaxNRemove()<<" ) has been removed";
else else
cout<<"\n\nThe tree is empty."; cout<<"\n\nTree is empty.";
} }
template <typename T> template <typename Data>
void RemoveMax(T& bst){ void RemoveMax(BST<Data>& bst){
if(bst.Size()>0){ if(bst.Size()>0){
bst.RemoveMax(); bst.RemoveMax();
cout<<"\n\nThe maximum element has been deleted"; cout<<"\n\nThe maximum element has been deleted";
} }
else else
cout<<"\n\nThe tree is empty"; cout<<"\n\nTree is empty.";
} }
template <template <typename...> class Tree, typename DTType> template <typename Data>
void PrintPredecessor(Tree<DTType>& bst){ void PrintPredecessor(BST<Data>& bst){
DTType lookForPredecessor; Data lookForPredecessor;
cout<<"Print the predecessor of "; cout<<"Print the predecessor of ";
cin>>ws; cin>>ws;
cin>>lookForPredecessor; cin>>lookForPredecessor;
@ -535,9 +536,9 @@ void PrintPredecessor(Tree<DTType>& bst){
} }
} }
template <template <typename...> class Tree, typename DTType> template <typename Data>
void PredecessorNRemove(Tree<DTType>& bst){ void PredecessorNRemove(BST<Data>& bst){
DTType lookForPredecessor; Data lookForPredecessor;
cout<<"Print and delete the predecessor of "; cout<<"Print and delete the predecessor of ";
cin>>ws; cin>>ws;
cin>>lookForPredecessor; cin>>lookForPredecessor;
@ -548,9 +549,9 @@ void PredecessorNRemove(Tree<DTType>& bst){
} }
} }
template <template <typename...> class Tree, typename DTType> template <typename Data>
void RemovePredecessor(Tree<DTType>& bst){ void RemovePredecessor(BST<Data>& bst){
DTType lookForPredecessor; Data lookForPredecessor;
cout<<"Delete the predecessor of "; cout<<"Delete the predecessor of ";
cin>>ws; cin>>ws;
cin>>lookForPredecessor; cin>>lookForPredecessor;
@ -562,9 +563,9 @@ void RemovePredecessor(Tree<DTType>& bst){
} }
} }
template <template <typename...> class Tree, typename DTType> template <typename Data>
void PrintSuccessor(Tree<DTType>& bst){ void PrintSuccessor(BST<Data>& bst){
DTType lookForSuccessor; Data lookForSuccessor;
cout<<"Print the successor of "; cout<<"Print the successor of ";
cin>>ws; cin>>ws;
cin>>lookForSuccessor; cin>>lookForSuccessor;
@ -575,9 +576,9 @@ void PrintSuccessor(Tree<DTType>& bst){
} }
} }
template <template <typename...> class Tree, typename DTType> template <typename Data>
void SuccessorNRemove(Tree<DTType>& bst){ void SuccessorNRemove(BST<Data>& bst){
DTType lookForSuccessor; Data lookForSuccessor;
cout<<"Print and delete the successor of "; cout<<"Print and delete the successor of ";
cin>>ws; cin>>ws;
cin>>lookForSuccessor; cin>>lookForSuccessor;
@ -588,9 +589,9 @@ void SuccessorNRemove(Tree<DTType>& bst){
} }
} }
template <template <typename...> class Tree, typename DTType> template <typename Data>
void RemoveSuccessor(Tree<DTType>& bst){ void RemoveSuccessor(BST<Data>& bst){
DTType lookForSuccessor; Data lookForSuccessor;
cout<<"Delete the successor of "; cout<<"Delete the successor of ";
cin>>ws; cin>>ws;
cin>>lookForSuccessor; cin>>lookForSuccessor;
@ -642,8 +643,7 @@ void NodeOperations(T& currentNode){
/* ----- generator functions ----- */ /* ----- generator functions ----- */
template <typename T> BST<int> GenerateIntegerBST(BST<int>& bst){
T GenerateIntegerBST(T& bst){
ulong dim = getDimension(); ulong dim = getDimension();
Vector<int> tmp(dim); Vector<int> tmp(dim);
@ -657,12 +657,11 @@ T GenerateIntegerBST(T& bst){
} }
cout<<endl<<endl; cout<<endl<<endl;
T tree(tmp); BST<int> tree(tmp);
return tree; return tree;
} }
template <typename T> BST<float> GenerateFloatBST(BST<float>& bst){
T GenerateFloatBST(T& bst){
ulong dim = getDimension(); ulong dim = getDimension();
Vector<float> tmp(dim); Vector<float> tmp(dim);
@ -676,12 +675,11 @@ T GenerateFloatBST(T& bst){
} }
cout<<endl<<endl; cout<<endl<<endl;
T tree(tmp); BST<float> tree(tmp);
return tree; return tree;
} }
template <typename T> BST<string> GenerateStringsBST(BST<string>& bst){
T GenerateStringsBST(T& bst){
ulong dim = getDimension(); ulong dim = getDimension();
Vector<string> tmp(dim); Vector<string> tmp(dim);
@ -695,7 +693,7 @@ T GenerateStringsBST(T& bst){
} }
cout<<endl<<endl; cout<<endl<<endl;
T tree(tmp); BST<string> tree(tmp);
return tree; return tree;
} }

View File

@ -9,87 +9,87 @@ enum class DataType{integer,ffloat,sstring};
/* ---- integer functions ---- */ /* ---- integer functions ---- */
template <typename T> template <typename Data>
void IntegerFunctions(T&); void IntegerFunctions(lasd::BST<Data>&);
template <typename T> template <typename Data>
void ProductsElementsLessThan(T&); void ProductsElementsLessThan(lasd::BST<Data>&);
void AccumulateProduct(const int&, const void*, void*); void AccumulateProduct(const int&, const void*, void*);
/* ----- float functions ----- */ /* ----- float functions ----- */
template <typename T> template <typename Data>
void FloatFunctions(T&); void FloatFunctions(lasd::BST<Data>&);
template <typename T> template <typename Data>
void SumElementsGreaterThan(T&); void SumElementsGreaterThan(lasd::BST<Data>&);
void AccumulateSum(const float&, const void*, void*); void AccumulateSum(const float&, const void*, void*);
/* ----- string functions ----- */ /* ----- string functions ----- */
template <typename T> template <typename Data>
void StringFunctions(T&); void StringFunctions(lasd::BST<Data>&);
template <typename T> template <typename Data>
void ConcatLessThan(T&); void ConcatLessThan(lasd::BST<Data>&);
void ConcatAString(const std::string&, const void*, void*); void ConcatAString(const std::string&, const void*, void*);
/* ---- shared functions ---- */ /* ---- shared functions ---- */
void menu(); void menu();
template <template <typename...> class Tree, typename DTType> template <typename Data>
void PrintTree(Tree<DTType>&); void PrintTree(lasd::BST<Data>&);
template <typename Data> template <typename Data>
void PrintSingleElement(Data& data, void* _); void PrintSingleElement(Data& data, void* _);
template <template <typename...> class Tree, typename DTType> template <typename Data>
void CheckExistence(Tree<DTType>& tree); void CheckExistence(lasd::BST<Data>& tree);
template <template <typename...> class Tree, typename DTType> template <typename Data>
void InsertElement(Tree<DTType>&); void InsertElement(lasd::BST<Data>&);
template <template <typename...> class Tree, typename DTType> template <typename Data>
void RemoveElement(Tree<DTType>&); void RemoveElement(lasd::BST<Data>&);
template <typename T> template <typename Data>
void PrintMinimum(T&); void PrintMinimum(lasd::BST<Data>&);
template <typename T> template <typename Data>
void PrintMinimumNDelete(T&); void PrintMinimumNDelete(lasd::BST<Data>&);
template <typename T> template <typename Data>
void RemoveMin(T&); void RemoveMin(lasd::BST<Data>&);
template <typename T> template <typename Data>
void PrintMaximum(T&); void PrintMaximum(lasd::BST<Data>&);
template <typename T> template <typename Data>
void PrintMaximumNDelete(T&); void PrintMaximumNDelete(lasd::BST<Data>&);
template <typename T> template <typename Data>
void RemoveMax(T&); void RemoveMax(lasd::BST<Data>&);
template <template <typename...> class Tree, typename DTType> template <typename Data>
void PrintPredecessor(Tree<DTType>&); void PrintPredecessor(lasd::BST<Data>&);
template <template <typename...> class Tree, typename DTType> template <typename Data>
void PredecessorNRemove(Tree<DTType>&); void PredecessorNRemove(lasd::BST<Data>&);
template <template <typename...> class Tree, typename DTType> template <typename Data>
void RemovePredecessor(Tree<DTType>&); void RemovePredecessor(lasd::BST<Data>&);
template <template <typename...> class Tree, typename DTType> template <typename Data>
void PrintSuccessor(Tree<DTType>&); void PrintSuccessor(lasd::BST<Data>&);
template <template <typename...> class Tree, typename DTType> template <typename Data>
void SuccessorNRemove(Tree<DTType>&); void SuccessorNRemove(lasd::BST<Data>&);
template <template <typename...> class Tree, typename DTType> template <typename Data>
void RemoveSuccessor(Tree<DTType>&); void RemoveSuccessor(lasd::BST<Data>&);
template <typename T> template <typename T>
void NodeOperations(T&); void NodeOperations(T&);
@ -97,15 +97,18 @@ void NodeOperations(T&);
/* ----- generator functions ----- */ /* ----- generator functions ----- */
DataType ChooseDataType(); //choose data type DataType ChooseDataType();
void UseChosenType(DataType); void UseChosenType(DataType);
template <typename T>
T GenerateIntegerBST(T&); lasd::BST<int> GenerateIntegerBST(lasd::BST<int>&);
template <typename T>
T GenerateFloatBST(T&); lasd::BST<float> GenerateFloatBST(lasd::BST<float>&);
template <typename T>
T GenerateStringsBST(T&); lasd::BST<std::string> GenerateStringsBST(lasd::BST<std::string>&);
std::string generateRandomString(ulong); std::string generateRandomString(ulong);
ulong getDimension(); ulong getDimension();
#endif #endif