Advertisement
Guest User

non-complete

a guest
Jun 25th, 2019
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.43 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <functional>
  6. #include <fstream>
  7. #include <sstream>
  8. template <typename T, typename R = T>
  9. class AVLTree {
  10. struct Node {
  11. T e;
  12. Node* l;
  13. Node* r;
  14. int h;
  15. Node* father;
  16. long long ID;
  17. Node(T e, long long ID = 0, Node* father = nullptr) : ID(ID), e(e), l(nullptr), r(nullptr), father(father), h(0) { }
  18.  
  19. static int height(Node* n) {
  20. return n == nullptr ? -1 : n->h;
  21. }
  22.  
  23. void updateH() {
  24. h = std::max(Node::height(l), Node::height(r)) + 1;
  25. }
  26. };
  27. long long cont;
  28. std::vector<T>* safer;
  29. Node* root;
  30. int length;
  31. std::function<R(T)> key;
  32.  
  33. void destroy(Node* n) {
  34. if (n != nullptr) {
  35. destroy(n->l);
  36. destroy(n->r);
  37. delete n;
  38. }
  39. }
  40. void rotAB(Node*& n) {
  41. Node* aux = n->l;
  42. n->l = aux->r;
  43. n->updateH();
  44. aux->r = n;
  45. n = aux;
  46. n->updateH();
  47. }
  48. void rotBA(Node*& n) {
  49. Node* aux = n->r;
  50. n->r = aux->l;
  51. n->updateH();
  52. aux->l = n;
  53. n = aux;
  54. n->updateH();
  55. }
  56.  
  57. void balance(Node*& n) {
  58. int delta = Node::height(n->l) - Node::height(n->r);
  59. if (delta < -1) {
  60. if (Node::height(n->r->l) > Node::height(n->r->r)) {
  61. rotAB(n->r);
  62. }
  63. rotBA(n);
  64. }
  65. else if (delta > 1) {
  66. if (Node::height(n->l->r) > Node::height(n->l->l)) {
  67. rotBA(n->l);
  68. }
  69. rotAB(n);
  70. }
  71. }
  72.  
  73. void add(Node*& n, T e, Node* padre, std::vector<T>*& vec) {
  74. if (n == nullptr) {
  75. n = new Node(e,cont++, padre);
  76. vec->push_back(e);
  77. }
  78. else if (key(e) < key(n->e)) {
  79. add(n->l, e, n, vec);
  80. }
  81. else {
  82. add(n->r, e, n, vec);
  83. }
  84. balance(n);
  85. actualizarPadres(root, nullptr);
  86. n->updateH();
  87. }
  88.  
  89. void actualizarPadres(Node*&n, Node* padre)
  90. {
  91. if (n != nullptr)
  92. {
  93. n->father = padre;
  94. actualizarPadres(n->l, n);
  95. actualizarPadres(n->r, n);
  96.  
  97. }
  98.  
  99. }
  100.  
  101. Node *minimum(Node* n)
  102. {
  103. if (n == nullptr)
  104. {
  105. return nullptr;
  106. }
  107. else if (n->l)
  108. {
  109. return minimum(n->l);
  110. }
  111. else
  112. {
  113. return n;
  114. }
  115. }
  116.  
  117. void replace(Node* n, Node* newNode)
  118. {
  119. if (n->father)
  120. {
  121. if (n->e == n->father->l->e)
  122. {
  123. n->father->l = newNode;
  124. }
  125. else if (n->e == n->father->r->e)
  126. {
  127. n->father->r = newNode;
  128. }
  129. }
  130. if (newNode)
  131. {
  132. newNode->father = n->father;
  133. }
  134. }
  135.  
  136. void destroyNode(Node* n)
  137. {
  138. n->l = nullptr;
  139. n->r = nullptr;
  140. delete n;
  141. }
  142.  
  143. void removeNode(Node* nodeToRemove)
  144. {
  145. if (nodeToRemove->l && nodeToRemove->r)
  146. {
  147. Node* min = minimum(nodeToRemove->r);
  148. nodeToRemove->e = min->e;
  149. removeNode(min);
  150. }
  151. else if (nodeToRemove->l)
  152. {
  153. replace(nodeToRemove, nodeToRemove->l);
  154. destroyNode(nodeToRemove);
  155. }
  156. else if (nodeToRemove->r)
  157. {
  158. replace(nodeToRemove, nodeToRemove->r);
  159. destroyNode(nodeToRemove);
  160. }
  161. else
  162. {
  163. replace(nodeToRemove, nullptr);
  164. }
  165. }
  166.  
  167. void remove(Node* n, T e)
  168. {
  169. if (n == nullptr)
  170. {
  171. return;
  172. }
  173. else if (e < n->e)
  174. {
  175. remove(n->l, e);
  176. }
  177. else if (e > n->e)
  178. {
  179. remove(n->r, e);
  180. }
  181. else if (e == n->e) {
  182. removeNode(n);
  183. }
  184. }
  185.  
  186. void showInOrder(Node* n, std::vector<T>*& vec)
  187. {
  188. if (n != nullptr)
  189. {
  190. showInOrder(n->l, vec);
  191. vec->push_back(n->e);
  192. showInOrder(n->r, vec);
  193. }
  194. }
  195.  
  196. void showInO(Node* n)
  197. {
  198. if (n != nullptr)
  199. {
  200. showInO(n->l);
  201. std::cout << n->e << "-";
  202. showInO(n->r);
  203. }
  204. }
  205.  
  206.  
  207. public:
  208. AVLTree(std::function<R(T)> key = [](T a) {return a; }) : root(nullptr), length(0), key(key)
  209. {
  210. safer = new std::vector<T>();
  211. cont = 0;
  212. }
  213. ~AVLTree() { destroy(root); delete safer; }
  214.  
  215. int Height() {
  216. return Node::height(root);
  217. }
  218. int Size() {
  219. return length;
  220. }
  221. void Add(T e, std::vector<T>*& vec) {
  222. add(root, e, nullptr, vec);
  223. length++;
  224. }
  225.  
  226. void InOrder(std::vector<T>*& vec)
  227. {
  228. showInOrder(root, vec);
  229. }
  230.  
  231. void DRemove(T e)
  232. {
  233. remove(root, e);
  234. }
  235.  
  236. void moscout()
  237. {
  238. showInO(root);
  239. }
  240.  
  241. Node* getRoot()
  242. {
  243. return this->root;
  244. }
  245. };
  246.  
  247. template <typename T>
  248. class CVector
  249. {
  250. private:
  251. std::vector<T>* safer;
  252. AVLTree<T>* tree;
  253. public:
  254. CVector()
  255. {
  256. safer = new std::vector<T>();
  257. tree = new AVLTree<T>();
  258. }
  259. ~CVector()
  260. {
  261. delete safer;
  262. delete tree;
  263. }
  264.  
  265. void addToTree(T elem)
  266. {
  267. tree->Add(elem, safer);
  268. }
  269.  
  270. std::vector<T>* getSafer()
  271. {
  272. return this->safer;
  273. }
  274.  
  275. std::vector<T>* safe()
  276. {
  277. std::vector<T>* aux = new std::vector<T>();
  278. tree->InOrder(aux);
  279. safer = aux;
  280. return safer;
  281. }
  282.  
  283. AVLTree<T>*& getTree()
  284. {
  285. return this->tree;
  286. }
  287. };
  288.  
  289. class CDetector
  290. {
  291. long long numero;
  292. std::string palabra;
  293. bool request;
  294. double doub;
  295.  
  296. std::string type;
  297. public:
  298. CDetector(std::string word)
  299. {
  300. type = detectar(word);
  301. if (type == "string")
  302. {
  303. palabra = word, numero = NULL, request = NULL, doub = NULL;
  304. }
  305. else if (type == "bool")
  306. {
  307. palabra = " ", numero = NULL, doub = NULL;
  308. if (word == "SI")
  309. request = true;
  310. else
  311. request = false;
  312. }
  313. else if (type == "long")
  314. {
  315. palabra = " ", doub = NULL, request = NULL;
  316. numero = std::stoll(word);
  317. }
  318. else if (type == "double")
  319. {
  320. palabra = " ", numero = NULL, request = NULL;
  321. doub = std::stod(word);
  322. }
  323. else if (type == "Desconocido")
  324. {
  325. palabra = " ", numero = NULL, doub = NULL, request = NULL;
  326. }
  327. else
  328. {
  329. palabra = "ERROR :(", numero = NULL, request = NULL, doub = NULL;
  330. }
  331. }
  332.  
  333. ~CDetector() {}
  334.  
  335. std::string getType()
  336. {
  337. return this->type;
  338. }
  339.  
  340. std::string detectar(std::string word)
  341. {
  342. if (word[0] >= 65 && word[0] <= 122)
  343. return "string";
  344. else if (word == "SI" && word == "NO")
  345. return "bool";
  346. else if (word[0] >= 48 && word[0] <= 57)
  347. {
  348. if ((double)std::stoll(word) != std::stod(word))
  349. return "double";
  350. else if ((double)std::stoll(word) == std::stod(word))
  351. return "long";
  352. }
  353. else return "Desconocido";
  354. }
  355. };
  356.  
  357.  
  358. class CColumna abstract
  359. {
  360.  
  361. public:
  362. CColumna(){}
  363. ~CColumna(){}
  364. };
  365.  
  366.  
  367. class CColumnaString : public CColumna
  368. {
  369. CVector<std::string>* vec;
  370. public:
  371. CColumnaString() : CColumna()
  372. {
  373. vec = new CVector<std::string>();
  374. }
  375.  
  376. ~CColumnaString()
  377. {
  378. delete vec;
  379. }
  380.  
  381. void agregar(std::string elem)
  382. {
  383. vec->addToTree(elem);
  384. }
  385. };
  386.  
  387. class CColumnaDouble : public CColumna
  388. {
  389. CVector<double>* vec;
  390. public:
  391. CColumnaDouble() : CColumna()
  392. {
  393. vec = new CVector<double>();
  394. }
  395.  
  396. ~CColumnaDouble()
  397. {
  398. delete vec;
  399. }
  400.  
  401. void agregar(double elem)
  402. {
  403. vec->addToTree(elem);
  404. }
  405. };
  406.  
  407. class CColumnaLong : public CColumna
  408. {
  409. CVector<long long>* vec;
  410. public:
  411. CColumnaLong() : CColumna()
  412. {
  413. vec = new CVector<long long>();
  414. }
  415.  
  416. ~CColumnaLong()
  417. {
  418. delete vec;
  419. }
  420.  
  421. void agregar(long long elem)
  422. {
  423. vec->addToTree(elem);
  424. }
  425. };
  426.  
  427. class CColumnaBool : public CColumna
  428. {
  429. CVector<bool>* vec;
  430. public:
  431. CColumnaBool() : CColumna()
  432. {
  433. vec = new CVector<bool>();
  434. }
  435.  
  436. ~CColumnaBool()
  437. {
  438. delete vec;
  439. }
  440.  
  441. void agregar(bool elem)
  442. {
  443. vec->addToTree(elem);
  444. }
  445. };
  446.  
  447. class CArchivo
  448. {
  449. private:
  450. CDetector* det;
  451. public:
  452. CArchivo()
  453. {
  454.  
  455. }
  456. ~CArchivo() { delete det; }
  457.  
  458. void escribirArchivo()
  459. {
  460. std::ofstream archivoJoc("ArchivoJoc.txt", std::ios::out);
  461. if (!archivoJoc.is_open())
  462. {
  463. std::cout << "El archivo no se ha podido abrir, verifique el nombre del archivo." << std::endl;
  464. exit(1);
  465. }
  466. std::string linea;
  467. std::cout << "Ingrese lo que desea ingresar en el archivo: "; std::getline(std::cin, linea);
  468. archivoJoc << linea;
  469. archivoJoc.close();
  470. }
  471.  
  472. void leerArchivo(std::vector<CColumna&>*& vec)
  473. {
  474. int cont = 0;
  475. std::string nombreArchivo;
  476. std::cout << "Ingrese el nombre del archivo que desea abrir";
  477. std::cin >> nombreArchivo;
  478. std::ifstream archivoJoc(nombreArchivo.c_str(), std::ios::in);
  479. if (!archivoJoc.is_open())
  480. {
  481. std::cout << "El archivo no pudo abrirse, por favor verifique el nombre del archivo.";
  482. exit(1);
  483. }
  484. std::string linea, num;
  485. while (archivoJoc >> linea)
  486. {
  487. std::stringstream ss(linea);
  488. int sumTotal = 0;
  489. while (std::getline(ss, num, ','))
  490. {
  491. //sumTotal += atoi(num.c_str());
  492. det->detectar(num.c_str());
  493. if (det->getType() == "long")
  494. {
  495. if (cont == 0)
  496. {
  497. CColumnaLong lon;
  498. lon.agregar(std::stoll(num.c_str()));
  499. vec->push_back(&lon);
  500.  
  501. }
  502. }
  503. }
  504. cont++;
  505. std::cout << '[' << linea << "] Suma total: " << sumTotal << std::endl;
  506. }
  507. archivoJoc.close();
  508. }
  509.  
  510. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement