creating tree containers: one that uses a vector to hold your trees (class VectorContainer). Each of these container classes should be able to hold any amount of different expressions each of which can be of any size.see example CODE GIVEN class Container { protected: Sort* sort_function; public: /* Constructors */ Container() : sort_function(nullptr) { }; Container(Sort* function) : sort_function(function) { }; /* Non Virtual Functions */ void set_sort_function(Sort* sort_function); // set the type of sorting algorithm /* Pure Virtual Functions */ // push the top pointer of the tree into container virtual void add_element(Base* element) = 0; // iterate through trees and output the expressions (use stringify()) virtual void print() = 0; // calls on the previously set sorting-algorithm. Checks if sort_function is not null, throw exception if otherwise virtual void sort() = 0; /* Essentially the only functions needed to sort */ //switch tree locations virtual void swap(int i, int j) = 0; // get top ptr of tree at index i virtual Base* at(int i) = 0; // return container size virtual int size() = 0; }; #endif //__CONTAINER_HPP__ Example for list as reference #ifndef __LISTCONTAINER_HPP__ #define __LISTCONTAINER_HPP__ #include "container.hpp" #include #include #include class Sort; class ListContainer: public Container{ public: std::list baseList; //Container() : sort_function(nullptr){}; //Container(Sort* function) : sort_Function(function){}; void set_sort_funtion(Sort* sort_function){ this -> sort_function = sort_function; } void add_element(Base* element){ baseList.push_back(element); } void print(){ for(std::list::iterator i = baseList.begin(); i != baseList.end(); ++i){ std::cout << ' ' << (*i) -> stringify(); } } void sort(){ try{ if(sort_function != nullptr){ sort_function -> sort(this); } else{ throw std::logic_error("invalid sort_function"); } } catch(std::exception &exp){ std::cout << "ERROR : " << exp.what() << "\n"; } } //sorting functions void swap(int i, int j){ std::list::iterator first = baseList.begin(); for(int f = 0; f < i; f++){ first++; } Base* temp = *first; std::list::iterator second = baseList.begin(); for(int s = 0; s < j; s++){ second++; } *first = *second; *second = temp; } Base* at(int i){ std::list::iterator x = baseList.begin(); for(int a = 0; a < i; a++){ x++; } return *x; } int size(){ return baseList.size(); } }; #endif //__LISTCONTAINER_HPP__
creating tree containers: one that uses a
CODE GIVEN
class Container {
protected:
Sort* sort_function;
public:
/* Constructors */
Container() : sort_function(nullptr) { };
Container(Sort* function) : sort_function(function) { };
/* Non Virtual Functions */
void set_sort_function(Sort* sort_function); // set the type of sorting
/* Pure Virtual Functions */
// push the top pointer of the tree into container
virtual void add_element(Base* element) = 0;
// iterate through trees and output the expressions (use stringify())
virtual void print() = 0;
// calls on the previously set sorting-algorithm. Checks if sort_function is not null, throw exception if otherwise
virtual void sort() = 0;
/* Essentially the only functions needed to sort */
//switch tree locations
virtual void swap(int i, int j) = 0;
// get top ptr of tree at index i
virtual Base* at(int i) = 0;
// return container size
virtual int size() = 0;
};
#endif //__CONTAINER_HPP__
Example for list as reference
#ifndef __LISTCONTAINER_HPP__
#define __LISTCONTAINER_HPP__
#include "container.hpp"
#include <list>
#include <iostream>
#include <stdexcept>
class Sort;
class ListContainer: public Container{
public:
std::list<Base*> baseList;
//Container() : sort_function(nullptr){};
//Container(Sort* function) : sort_Function(function){};
void set_sort_funtion(Sort* sort_function){
this -> sort_function = sort_function;
}
void add_element(Base* element){
baseList.push_back(element);
}
void print(){
for(std::list<Base*>::iterator i = baseList.begin(); i != baseList.end(); ++i){
std::cout << ' ' << (*i) -> stringify();
}
}
void sort(){
try{
if(sort_function != nullptr){
sort_function -> sort(this);
}
else{
throw std::logic_error("invalid sort_function");
}
}
catch(std::exception &exp){
std::cout << "ERROR : " << exp.what() << "\n";
}
}
//sorting functions
void swap(int i, int j){
std::list<Base*>::iterator first = baseList.begin();
for(int f = 0; f < i; f++){
first++;
}
Base* temp = *first;
std::list<Base*>::iterator second = baseList.begin();
for(int s = 0; s < j; s++){
second++;
}
*first = *second;
*second = temp;
}
Base* at(int i){
std::list<Base*>::iterator x = baseList.begin();
for(int a = 0; a < i; a++){
x++;
}
return *x;
}
int size(){
return baseList.size();
}
};
#endif //__LISTCONTAINER_HPP__
Trending now
This is a popular solution!
Step by step
Solved in 3 steps