reating 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. Please create vectorContainer.hpp all I need sort.hpp #ifndef __SORT_HPP__ #define __SORT_HPP__ #include "container.hpp" class Container; class Sort { public: /* Constructors */ Sort(); /* Pure Virtual Functions */ virtual void sort(Container* container) = 0; }; #endif //__SORT_HPP__ base.hpp #ifndef __BASE_HPP__ #define __BASE_HPP__ #include class Base { public: /* Constructors */ Base() { }; /* Pure Virtual Functions */ virtual double evaluate() = 0; virtual std::string stringify() = 0; }; #endif //__BASE_HPP__ container.hpp #ifndef __CONTAINER_HPP__ #define __CONTAINER_HPP__ #include "sort.hpp"
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.
Please create vectorContainer.hpp all I need
sort.hpp
#ifndef __SORT_HPP__
#define __SORT_HPP__
#include "container.hpp"
class Container;
class Sort {
public:
/* Constructors */
Sort();
/* Pure Virtual Functions */
virtual void sort(Container* container) = 0;
};
#endif //__SORT_HPP__
base.hpp
#ifndef __BASE_HPP__
#define __BASE_HPP__
#include <string>
class Base {
public:
/* Constructors */
Base() { };
/* Pure Virtual Functions */
virtual double evaluate() = 0;
virtual std::string stringify() = 0;
};
#endif //__BASE_HPP__
container.hpp
#ifndef __CONTAINER_HPP__
#define __CONTAINER_HPP__
#include "sort.hpp"
#include "base.hpp"
class Sort;
class Base;
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
#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){
if(i == baseList.begin()){
std::cout <<(*i) -> stringify();
}
else{
std::cout << ", " << (*i) -> stringify();
}
}
std::cout << std::endl;
}
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 2 steps