Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- #include <algorithm>
- #include <functional>
- #include <fstream>
- #include <sstream>
- template <typename T, typename R = T>
- class AVLTree {
- struct Node {
- T e;
- Node* l;
- Node* r;
- int h;
- Node* father;
- long long ID;
- Node(T e, long long ID = 0, Node* father = nullptr) : ID(ID), e(e), l(nullptr), r(nullptr), father(father), h(0) { }
- static int height(Node* n) {
- return n == nullptr ? -1 : n->h;
- }
- void updateH() {
- h = std::max(Node::height(l), Node::height(r)) + 1;
- }
- };
- long long cont;
- std::vector<T>* safer;
- Node* root;
- int length;
- std::function<R(T)> key;
- void destroy(Node* n) {
- if (n != nullptr) {
- destroy(n->l);
- destroy(n->r);
- delete n;
- }
- }
- void rotAB(Node*& n) {
- Node* aux = n->l;
- n->l = aux->r;
- n->updateH();
- aux->r = n;
- n = aux;
- n->updateH();
- }
- void rotBA(Node*& n) {
- Node* aux = n->r;
- n->r = aux->l;
- n->updateH();
- aux->l = n;
- n = aux;
- n->updateH();
- }
- void balance(Node*& n) {
- int delta = Node::height(n->l) - Node::height(n->r);
- if (delta < -1) {
- if (Node::height(n->r->l) > Node::height(n->r->r)) {
- rotAB(n->r);
- }
- rotBA(n);
- }
- else if (delta > 1) {
- if (Node::height(n->l->r) > Node::height(n->l->l)) {
- rotBA(n->l);
- }
- rotAB(n);
- }
- }
- void add(Node*& n, T e, Node* padre, std::vector<T>*& vec) {
- if (n == nullptr) {
- n = new Node(e,cont++, padre);
- vec->push_back(e);
- }
- else if (key(e) < key(n->e)) {
- add(n->l, e, n, vec);
- }
- else {
- add(n->r, e, n, vec);
- }
- balance(n);
- actualizarPadres(root, nullptr);
- n->updateH();
- }
- void actualizarPadres(Node*&n, Node* padre)
- {
- if (n != nullptr)
- {
- n->father = padre;
- actualizarPadres(n->l, n);
- actualizarPadres(n->r, n);
- }
- }
- Node *minimum(Node* n)
- {
- if (n == nullptr)
- {
- return nullptr;
- }
- else if (n->l)
- {
- return minimum(n->l);
- }
- else
- {
- return n;
- }
- }
- void replace(Node* n, Node* newNode)
- {
- if (n->father)
- {
- if (n->e == n->father->l->e)
- {
- n->father->l = newNode;
- }
- else if (n->e == n->father->r->e)
- {
- n->father->r = newNode;
- }
- }
- if (newNode)
- {
- newNode->father = n->father;
- }
- }
- void destroyNode(Node* n)
- {
- n->l = nullptr;
- n->r = nullptr;
- delete n;
- }
- void removeNode(Node* nodeToRemove)
- {
- if (nodeToRemove->l && nodeToRemove->r)
- {
- Node* min = minimum(nodeToRemove->r);
- nodeToRemove->e = min->e;
- removeNode(min);
- }
- else if (nodeToRemove->l)
- {
- replace(nodeToRemove, nodeToRemove->l);
- destroyNode(nodeToRemove);
- }
- else if (nodeToRemove->r)
- {
- replace(nodeToRemove, nodeToRemove->r);
- destroyNode(nodeToRemove);
- }
- else
- {
- replace(nodeToRemove, nullptr);
- }
- }
- void remove(Node* n, T e)
- {
- if (n == nullptr)
- {
- return;
- }
- else if (e < n->e)
- {
- remove(n->l, e);
- }
- else if (e > n->e)
- {
- remove(n->r, e);
- }
- else if (e == n->e) {
- removeNode(n);
- }
- }
- void showInOrder(Node* n, std::vector<T>*& vec)
- {
- if (n != nullptr)
- {
- showInOrder(n->l, vec);
- vec->push_back(n->e);
- showInOrder(n->r, vec);
- }
- }
- void showInO(Node* n)
- {
- if (n != nullptr)
- {
- showInO(n->l);
- std::cout << n->e << "-";
- showInO(n->r);
- }
- }
- public:
- AVLTree(std::function<R(T)> key = [](T a) {return a; }) : root(nullptr), length(0), key(key)
- {
- safer = new std::vector<T>();
- cont = 0;
- }
- ~AVLTree() { destroy(root); delete safer; }
- int Height() {
- return Node::height(root);
- }
- int Size() {
- return length;
- }
- void Add(T e, std::vector<T>*& vec) {
- add(root, e, nullptr, vec);
- length++;
- }
- void InOrder(std::vector<T>*& vec)
- {
- showInOrder(root, vec);
- }
- void DRemove(T e)
- {
- remove(root, e);
- }
- void moscout()
- {
- showInO(root);
- }
- Node* getRoot()
- {
- return this->root;
- }
- };
- template <typename T>
- class CVector
- {
- private:
- std::vector<T>* safer;
- AVLTree<T>* tree;
- public:
- CVector()
- {
- safer = new std::vector<T>();
- tree = new AVLTree<T>();
- }
- ~CVector()
- {
- delete safer;
- delete tree;
- }
- void addToTree(T elem)
- {
- tree->Add(elem, safer);
- }
- std::vector<T>* getSafer()
- {
- return this->safer;
- }
- std::vector<T>* safe()
- {
- std::vector<T>* aux = new std::vector<T>();
- tree->InOrder(aux);
- safer = aux;
- return safer;
- }
- AVLTree<T>*& getTree()
- {
- return this->tree;
- }
- };
- class CDetector
- {
- long long numero;
- std::string palabra;
- bool request;
- double doub;
- std::string type;
- public:
- CDetector(std::string word)
- {
- type = detectar(word);
- if (type == "string")
- {
- palabra = word, numero = NULL, request = NULL, doub = NULL;
- }
- else if (type == "bool")
- {
- palabra = " ", numero = NULL, doub = NULL;
- if (word == "SI")
- request = true;
- else
- request = false;
- }
- else if (type == "long")
- {
- palabra = " ", doub = NULL, request = NULL;
- numero = std::stoll(word);
- }
- else if (type == "double")
- {
- palabra = " ", numero = NULL, request = NULL;
- doub = std::stod(word);
- }
- else if (type == "Desconocido")
- {
- palabra = " ", numero = NULL, doub = NULL, request = NULL;
- }
- else
- {
- palabra = "ERROR :(", numero = NULL, request = NULL, doub = NULL;
- }
- }
- ~CDetector() {}
- std::string getType()
- {
- return this->type;
- }
- std::string detectar(std::string word)
- {
- if (word[0] >= 65 && word[0] <= 122)
- return "string";
- else if (word == "SI" && word == "NO")
- return "bool";
- else if (word[0] >= 48 && word[0] <= 57)
- {
- if ((double)std::stoll(word) != std::stod(word))
- return "double";
- else if ((double)std::stoll(word) == std::stod(word))
- return "long";
- }
- else return "Desconocido";
- }
- };
- class CColumna abstract
- {
- public:
- CColumna(){}
- ~CColumna(){}
- };
- class CColumnaString : public CColumna
- {
- CVector<std::string>* vec;
- public:
- CColumnaString() : CColumna()
- {
- vec = new CVector<std::string>();
- }
- ~CColumnaString()
- {
- delete vec;
- }
- void agregar(std::string elem)
- {
- vec->addToTree(elem);
- }
- };
- class CColumnaDouble : public CColumna
- {
- CVector<double>* vec;
- public:
- CColumnaDouble() : CColumna()
- {
- vec = new CVector<double>();
- }
- ~CColumnaDouble()
- {
- delete vec;
- }
- void agregar(double elem)
- {
- vec->addToTree(elem);
- }
- };
- class CColumnaLong : public CColumna
- {
- CVector<long long>* vec;
- public:
- CColumnaLong() : CColumna()
- {
- vec = new CVector<long long>();
- }
- ~CColumnaLong()
- {
- delete vec;
- }
- void agregar(long long elem)
- {
- vec->addToTree(elem);
- }
- };
- class CColumnaBool : public CColumna
- {
- CVector<bool>* vec;
- public:
- CColumnaBool() : CColumna()
- {
- vec = new CVector<bool>();
- }
- ~CColumnaBool()
- {
- delete vec;
- }
- void agregar(bool elem)
- {
- vec->addToTree(elem);
- }
- };
- class CArchivo
- {
- private:
- CDetector* det;
- public:
- CArchivo()
- {
- }
- ~CArchivo() { delete det; }
- void escribirArchivo()
- {
- std::ofstream archivoJoc("ArchivoJoc.txt", std::ios::out);
- if (!archivoJoc.is_open())
- {
- std::cout << "El archivo no se ha podido abrir, verifique el nombre del archivo." << std::endl;
- exit(1);
- }
- std::string linea;
- std::cout << "Ingrese lo que desea ingresar en el archivo: "; std::getline(std::cin, linea);
- archivoJoc << linea;
- archivoJoc.close();
- }
- void leerArchivo(std::vector<CColumna&>*& vec)
- {
- int cont = 0;
- std::string nombreArchivo;
- std::cout << "Ingrese el nombre del archivo que desea abrir";
- std::cin >> nombreArchivo;
- std::ifstream archivoJoc(nombreArchivo.c_str(), std::ios::in);
- if (!archivoJoc.is_open())
- {
- std::cout << "El archivo no pudo abrirse, por favor verifique el nombre del archivo.";
- exit(1);
- }
- std::string linea, num;
- while (archivoJoc >> linea)
- {
- std::stringstream ss(linea);
- int sumTotal = 0;
- while (std::getline(ss, num, ','))
- {
- //sumTotal += atoi(num.c_str());
- det->detectar(num.c_str());
- if (det->getType() == "long")
- {
- if (cont == 0)
- {
- CColumnaLong lon;
- lon.agregar(std::stoll(num.c_str()));
- vec->push_back(&lon);
- }
- }
- }
- cont++;
- std::cout << '[' << linea << "] Suma total: " << sumTotal << std::endl;
- }
- archivoJoc.close();
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement