fork download
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <sstream>
  5. #include <vector>
  6. using std::string;
  7.  
  8. struct node
  9. {
  10. node *p, *left, *right;
  11. int key;
  12. };
  13.  
  14. void insert(node *&root, const int key)
  15. {
  16. node *newElement = new node();
  17. newElement->key = key;
  18. newElement->left = newElement->right = NULL;
  19.  
  20. node *y = NULL, *x = root;
  21. while(x)
  22. {
  23. if(key == x->key) exit(EXIT_FAILURE);
  24. y = x;
  25. x = (key < x->key) ? x->left : x->right;
  26. }
  27.  
  28. newElement->p = y;
  29.  
  30. if(!y) root = newElement;
  31. else if(key < y->key) y->left = newElement;
  32. else y->right = newElement;
  33. }
  34.  
  35. void insert(node *&root, std::vector<int> keys)
  36. {
  37. for(int i = 0; i < keys.size(); i++) insert(root, keys[i]);
  38. }
  39.  
  40. node* search(node *root, const int key)
  41. {
  42. while( (root) && (root->key != key) )
  43. root = (key < root->key) ? root->left : root->right;
  44. if(root == NULL) exit(EXIT_FAILURE);
  45. return root;
  46. }
  47.  
  48. node* minNode(node *root)
  49. {
  50. while(root->left) root = root->left;
  51. return root;
  52. }
  53.  
  54. node* maxNode(node *root)
  55. {
  56. while(root->right) root = root->right;
  57. return root;
  58. }
  59.  
  60. node* prev(node *root)
  61. {
  62. if(root->left) return maxNode(root->left);
  63. else exit(EXIT_FAILURE);
  64. }
  65.  
  66. node* next(node *root)
  67. {
  68. if(root->right) return minNode(root->right);
  69. else exit(EXIT_FAILURE);
  70. }
  71.  
  72. void inorder(node *root, std::ostream& os)
  73. {
  74. if(root) {
  75. inorder(root->left, os);
  76. os << root->key << " ";
  77. inorder(root->right, os);
  78. }
  79. }
  80.  
  81. void postorder(node *root, std::ostream& os)
  82. {
  83. if(root) {
  84. inorder(root->left, os);
  85. inorder(root->right, os);
  86. os << root->key << " ";
  87. }
  88. }
  89.  
  90. void preorder(node *root, std::ostream& os)
  91. {
  92. if(root) {
  93. os << root->key << " ";
  94. inorder(root->left, os);
  95. inorder(root->right, os);
  96. }
  97. }
  98.  
  99. node* remove(node *&root, const int key)
  100. {
  101. node *x = search(root, key); // usuwany wezel
  102. node * y = x->p; // rodzic usuwanego wezla
  103. node *z;
  104.  
  105. if((x->left) && (x->right))
  106. {
  107. z = (rand() % 2) ? remove(root, prev(x)->key) : remove(root, next(x)->key);
  108.  
  109. z->left = x->left;
  110. if(z->left) z->left->p = z;
  111. z->right = x->right;
  112. if(z->right) z->right->p = z;
  113. }
  114. else z = (x->left) ? x->left : x->right;
  115.  
  116. if(z) z->p = y;
  117.  
  118. if(!y) root = z;
  119. else if(y->left == x) y->left = z; else y->right = z;
  120.  
  121. return x;
  122. }
  123.  
  124. void remove(node *&root, std::vector<int> keys)
  125. {
  126. for(int i = 0; i < keys.size(); i++) remove(root, keys[i]);
  127. }
  128.  
  129. int getNumberOfElements(node *root)
  130. {
  131. if(root)
  132. return 1 + getNumberOfElements(root->left) + getNumberOfElements(root->right);
  133. else
  134. return 0;
  135. }
  136.  
  137.  
  138. // ---------------- Wyswietlanie danych, operacje na plikach ----------------------
  139.  
  140.  
  141. std::vector<int> loadNumbersFromFile(string nazwa)
  142. {
  143. std::ifstream file;
  144. file.open(nazwa);
  145. if(!file) {
  146. std::cout << "\nNie udalo sie otworzyc pliku\n\n";
  147. exit(EXIT_FAILURE);
  148. }
  149.  
  150. std::vector<int> numbers;
  151. int current;
  152. while(file >> current)
  153. {
  154. numbers.push_back(current);
  155. file.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // ignoruj to co nie jest liczba
  156. }
  157.  
  158. return numbers;
  159. }
  160.  
  161. void buildTree(node *&root, std::vector<int> &numbers)
  162. {
  163. numbers.size() == 1 ? insert(root, numbers[0]) : insert(root, numbers);
  164. }
  165.  
  166. void displayTreeInfo(node *root, std::ostream& file)
  167. {
  168. if(root == NULL)
  169. {
  170. file << "Brak wezlow";
  171. }
  172. else
  173. {
  174. file << "Elementy drzewa: ";
  175. inorder(root, file);
  176. file << "\nIlosc elementow: " << getNumberOfElements(root) << "\n\n";
  177. file << "Korzen drzewa: " << root->key << std::endl;
  178. file << "Najmniejszy element: " << minNode(root)->key << std::endl;
  179. file << "Najwiekszy element: " << maxNode(root)->key << std::endl << "\n\n";
  180. file << "Elementy drzewa w porzadku postorder: ";
  181. postorder(root, file);
  182. file << std::endl << "Elementy drzewa w porzadku preorder: ";
  183. preorder(root, file);
  184. }
  185. }
  186.  
  187. void exportTreeToFile(node *root, string nazwa)
  188. {
  189. std::ofstream file;
  190. file.open(nazwa);
  191. if(!file)
  192. {
  193. std::cout << "\nNie udalo sie otworzyc pliku, lub plik zawiera niepoprawne dane\n\n";
  194. exit(EXIT_FAILURE);
  195. }
  196. displayTreeInfo(root, file);
  197. file.close();
  198. }
  199.  
  200. int main()
  201. {
  202. using namespace std;
  203. node *root = NULL;
  204. char ch = '0';
  205.  
  206. while(ch != '5')
  207. {
  208. cout << "1. Dodaj nowe elementy\n"
  209. "2. Wyswietl informacje o obecnym drzewie\n"
  210. "3. Usun wezly\n"
  211. "4. Eksportuj informacje do pliku\n"
  212. "5. Zakoncz\n";
  213. cin >> ch; cin.get();
  214.  
  215. switch(ch)
  216. {
  217. case '1': case '3':
  218. {
  219. vector<int> numbers;
  220. string line;
  221. cout << "\nPodawaj elementy po spacji: ";
  222. getline(cin, line);
  223. istringstream in(line, istringstream::in);
  224. int current;
  225. while (in >> current) numbers.push_back(current);
  226.  
  227. (ch == '1') ? insert(root, numbers) : remove(root, numbers);
  228. cout << "\nWcisnij, aby wrocic do menu";
  229. cin.get();
  230. break;
  231. }
  232. case '2':
  233. {
  234. displayTreeInfo(root, cout);
  235. cout << "\n\nWcisnij, aby wrocic do menu";
  236. cin.get();
  237. break;
  238. }
  239. case '4':
  240. {
  241. string nazwa;
  242. cout << "Podaj nazwe pliku: ";
  243. cin >> nazwa;
  244.  
  245. buildTree(root, loadNumbersFromFile(nazwa));
  246. exportTreeToFile(root, nazwa);
  247. std::cout << "\nDane zostaly wyexportowane do podanego pliku.\n\n";
  248. cin.get(); cin.get();
  249. break;
  250. }
  251. }
  252. system("cls");
  253. }
  254.  
  255. exit(EXIT_SUCCESS);
  256. }
  257.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘void insert(node*&, int)’:
prog.cpp:23:26: error: ‘EXIT_FAILURE’ was not declared in this scope
prog.cpp:23:38: error: ‘exit’ was not declared in this scope
prog.cpp: In function ‘void insert(node*&, std::vector<int>)’:
prog.cpp:37:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
prog.cpp: In function ‘node* search(node*, int)’:
prog.cpp:44:24: error: ‘EXIT_FAILURE’ was not declared in this scope
prog.cpp:44:36: error: ‘exit’ was not declared in this scope
prog.cpp: In function ‘node* prev(node*)’:
prog.cpp:63:12: error: ‘EXIT_FAILURE’ was not declared in this scope
prog.cpp:63:24: error: ‘exit’ was not declared in this scope
prog.cpp: In function ‘node* next(node*)’:
prog.cpp:69:12: error: ‘EXIT_FAILURE’ was not declared in this scope
prog.cpp:69:24: error: ‘exit’ was not declared in this scope
prog.cpp: In function ‘node* remove(node*&, int)’:
prog.cpp:107:13: error: ‘rand’ was not declared in this scope
prog.cpp: In function ‘void remove(node*&, std::vector<int>)’:
prog.cpp:126:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
prog.cpp: In function ‘std::vector<int> loadNumbersFromFile(std::string)’:
prog.cpp:144:17: error: no matching function for call to ‘std::basic_ifstream<char>::open(std::string&)’
prog.cpp:144:17: note: candidate is:
In file included from prog.cpp:2:0:
/usr/include/c++/4.7/fstream:531:7: note: void std::basic_ifstream<_CharT, _Traits>::open(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode = std::_Ios_Openmode]
/usr/include/c++/4.7/fstream:531:7: note:   no known conversion for argument 1 from ‘std::string {aka std::basic_string<char>}’ to ‘const char*’
prog.cpp:147:8: error: ‘EXIT_FAILURE’ was not declared in this scope
prog.cpp:147:20: error: ‘exit’ was not declared in this scope
prog.cpp:155:15: error: ‘numeric_limits’ is not a member of ‘std’
prog.cpp:155:50: error: expected primary-expression before ‘>’ token
prog.cpp:155:51: error: ‘::max’ has not been declared
prog.cpp:155:51: note: suggested alternative:
In file included from /usr/include/c++/4.7/bits/char_traits.h:41:0,
                 from /usr/include/c++/4.7/ios:41,
                 from /usr/include/c++/4.7/ostream:40,
                 from /usr/include/c++/4.7/iostream:40,
                 from prog.cpp:1:
/usr/include/c++/4.7/bits/stl_algobase.h:254:5: note:   ‘std::max’
prog.cpp: In function ‘void exportTreeToFile(node*, std::string)’:
prog.cpp:190:17: error: no matching function for call to ‘std::basic_ofstream<char>::open(std::string&)’
prog.cpp:190:17: note: candidate is:
In file included from prog.cpp:2:0:
/usr/include/c++/4.7/fstream:702:7: note: void std::basic_ofstream<_CharT, _Traits>::open(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode = std::_Ios_Openmode]
/usr/include/c++/4.7/fstream:702:7: note:   no known conversion for argument 1 from ‘std::string {aka std::basic_string<char>}’ to ‘const char*’
prog.cpp:194:8: error: ‘EXIT_FAILURE’ was not declared in this scope
prog.cpp:194:20: error: ‘exit’ was not declared in this scope
prog.cpp: In function ‘int main()’:
prog.cpp:245:47: error: invalid initialization of non-const reference of type ‘std::vector<int>&’ from an rvalue of type ‘std::vector<int>’
prog.cpp:161:6: error: in passing argument 2 of ‘void buildTree(node*&, std::vector<int>&)’
prog.cpp:252:15: error: ‘system’ was not declared in this scope
prog.cpp:255:7: error: ‘EXIT_SUCCESS’ was not declared in this scope
prog.cpp:255:19: error: ‘exit’ was not declared in this scope
prog.cpp: In function ‘node* next(node*)’:
prog.cpp:70:1: warning: control reaches end of non-void function [-Wreturn-type]
prog.cpp: In function ‘node* prev(node*)’:
prog.cpp:64:1: warning: control reaches end of non-void function [-Wreturn-type]
stdout
Standard output is empty