Advertisement
Guest User

hellohaha

a guest
Feb 16th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.55 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <sstream>
  5. #include <iomanip>
  6. #include "ArgumentManager.h"
  7.  
  8. using namespace std;
  9.  
  10.  
  11.  
  12. struct book {
  13. book *prev;
  14. int book_id;
  15. string book_name;
  16. string book_author;
  17. book *next;
  18. };
  19.  
  20.  
  21. class book_lib {
  22. private:
  23. book *head;
  24. book *tail;
  25. int size;
  26. public:
  27. book_lib() {
  28. head = 0;
  29. tail = 0;
  30. }
  31.  
  32. int getSize()
  33. {
  34. return size;
  35. }
  36.  
  37. bool isempty() {
  38. return (head == 0 ? 1 : 0);
  39. };
  40.  
  41.  
  42. void insertbegin(int real_book_id, string real_book_name, string real_book_author) {
  43. if (book_search("book_id", to_string(real_book_id)) != -1)
  44. return;
  45.  
  46. book *new_book = new book;
  47.  
  48. new_book->prev = nullptr;
  49. new_book->book_id = real_book_id;
  50. new_book->book_name = real_book_name;
  51. new_book->book_author = real_book_author;
  52. new_book->next = head;
  53. if (head != nullptr)
  54. head->prev = new_book;
  55. head = new_book;
  56. if (tail == nullptr) {
  57. tail = new_book;
  58. }
  59. };
  60.  
  61. void insertend(int real_book_id, string real_book_name, string real_book_author) {
  62. if (book_search("book_id", to_string(real_book_id)) != -1)
  63. return;
  64.  
  65. book *new_book = new book;
  66. new_book->prev = tail;
  67. new_book->book_id = real_book_id;
  68. new_book->book_name = real_book_name;
  69. new_book->book_author = real_book_author;
  70. new_book->next = nullptr;
  71.  
  72. if (tail != nullptr) {
  73. tail->next = new_book;
  74. }
  75. tail = new_book;
  76. if (head == nullptr) {
  77. head = new_book;
  78. }
  79. size++;
  80. };
  81.  
  82. void insert(int pos, int real_book_id, string real_book_name, string real_book_author) {
  83. if (book_search("book_id", to_string(real_book_id)) != -1)
  84. return;
  85.  
  86. book *new_book = new book;
  87. new_book = head;
  88. int ncounter = 0;
  89.  
  90. if (pos >= 0 && head == NULL)
  91. {
  92. insertend(real_book_id, real_book_name, real_book_author);
  93. size++;
  94. return;
  95. }
  96. if (pos == 0) {
  97. insertbegin(real_book_id, real_book_name, real_book_author);
  98. return;
  99.  
  100. }
  101. else {
  102. while (ncounter != pos && new_book->next != nullptr) {
  103. new_book = new_book->next;
  104. ncounter++;
  105. };
  106. if (new_book->next == nullptr) {
  107. insertend(real_book_id, real_book_name, real_book_author);
  108. size++;
  109. return;
  110. }
  111.  
  112. else {
  113. book *copy_book = new book;
  114. copy_book->prev = new_book->prev;
  115. copy_book->next = new_book;
  116. new_book->prev->next = copy_book;
  117. new_book->prev = copy_book;
  118. copy_book->book_id = real_book_id;
  119. copy_book->book_name = real_book_name;
  120. copy_book->book_author = real_book_author;
  121. };
  122. };
  123. size++;
  124. };
  125.  
  126. void remove(int pos) {
  127.  
  128. if (pos >= size)
  129. {
  130. return;
  131. }
  132. book *book_to_remove = head;
  133. int ncounter2 = 0;
  134. while (ncounter2 != pos && book_to_remove != nullptr) {
  135. book_to_remove = book_to_remove->next;
  136. ncounter2++;
  137. };
  138. if (book_to_remove != nullptr) {
  139. if (book_to_remove->prev != nullptr) {
  140. book_to_remove->prev->next = book_to_remove->next;
  141. }
  142. else {
  143. head = book_to_remove->next;
  144. };
  145. if (book_to_remove->next != nullptr) {
  146. book_to_remove->next->prev = book_to_remove->prev;
  147. }
  148. else {
  149. tail = book_to_remove->prev;
  150. };
  151. delete book_to_remove;
  152. };
  153. size--;
  154. };
  155.  
  156. void sort(string key) {
  157. int swap_val;
  158. book *book_to_swap;
  159. if (isempty())
  160. return;
  161. do
  162. {
  163. swap_val = 0;
  164. book_to_swap = head;
  165.  
  166. while (book_to_swap->next != nullptr)
  167. {
  168.  
  169. if (key == "book_id") {
  170. if (book_to_swap->book_id > book_to_swap->next->book_id) {
  171. swap(book_to_swap->book_id, book_to_swap->next->book_id);
  172. swap(book_to_swap->book_name, book_to_swap->next->book_name);
  173. swap(book_to_swap->book_author, book_to_swap->next->book_author);
  174. swap_val = 1;
  175. }
  176. }
  177.  
  178. else if (key == "book_name") {
  179. if (book_to_swap->book_name > book_to_swap->next->book_name) {
  180. swap(book_to_swap->book_id, book_to_swap->next->book_id);
  181. swap(book_to_swap->book_name, book_to_swap->next->book_name);
  182. swap(book_to_swap->book_author, book_to_swap->next->book_author);
  183. swap_val = 1;
  184. }
  185. }
  186.  
  187. else if (key == "book_author") {
  188. if (book_to_swap->book_author > book_to_swap->next->book_author) {
  189. swap(book_to_swap->book_id, book_to_swap->next->book_id);
  190. swap(book_to_swap->book_name, book_to_swap->next->book_name);
  191. swap(book_to_swap->book_author, book_to_swap->next->book_author);
  192. swap_val = 1;
  193. }
  194. }
  195.  
  196. book_to_swap = book_to_swap->next;
  197. }
  198. } while (swap_val);
  199. }
  200.  
  201.  
  202. int book_search(string key, string value) {
  203. int ncounter3 = 0;
  204. book *book_to_find = head;
  205.  
  206. if (key == "pos") {
  207. return stoi(value);
  208. }
  209.  
  210. while (book_to_find != nullptr) {
  211. if (key == "book_id") {
  212. if (book_to_find->book_id == stoi(value)) {
  213. return ncounter3;
  214. }
  215. }
  216. else if (key == "book_name") {
  217. if (book_to_find->book_name == value) {
  218. return ncounter3;
  219. }
  220. }
  221. else if (key == "book_author") {
  222. if (book_to_find->book_author == value) {
  223. return ncounter3;
  224. }
  225. };
  226. ncounter3++;
  227. book_to_find = book_to_find->next;
  228. };
  229. return -1;
  230. }
  231.  
  232. void print(bool flag = false, string file = "") {
  233. book *book_to_print = new book;
  234. book_to_print = head;
  235. ofstream ofile;
  236. if (flag == true)
  237. ofile.open(file);
  238.  
  239. while (book_to_print != nullptr) {
  240. if (flag == false) {
  241. cout << "book_id:" << book_to_print->book_id
  242. << ", book_name:" << book_to_print->book_name
  243. << ", book_author:" << book_to_print->book_author << endl;
  244. }
  245. else {
  246. ofile << "book_id:" << book_to_print->book_id
  247. << ", book_name:" << book_to_print->book_name
  248. << ", book_author:" << book_to_print->book_author << endl;
  249. }
  250. book_to_print = book_to_print->next;
  251. };
  252. if (ofile.is_open())
  253. ofile.close();
  254. };
  255. };
  256.  
  257.  
  258.  
  259. int main(int argc, char** argv) {
  260. if (argc < 2) {
  261. std::cout <<
  262. "Usage: ListOperation input=input1.txt command=command1.txt output=output1.txt"
  263. << std::endl;
  264. return -1;
  265. }
  266. ArgumentManager am(argc, argv);
  267.  
  268. const string A = am.get("input");
  269. const string B = am.get("command");
  270. const string C = am.get("output");
  271.  
  272.  
  273. int n = 0;
  274. string line = "";
  275. book_lib list;
  276.  
  277. ifstream ifileone(A);
  278. if (ifileone.is_open())
  279. {
  280. while (getline(ifileone, line))
  281. {
  282. if (line.length() != 0) {
  283. int book_id = stoi(line.substr(line.find("book_id:") + 8, 5));
  284. string book_name = line.substr(line.find("book_name:") + 10, line.find(", book_author") - 25);
  285. string book_author = line.substr(line.find("book_author:") + 12);
  286. list.insertend(book_id, book_name, book_author);
  287. n++;
  288. }
  289. }
  290. ifileone.close();
  291. }
  292.  
  293. ifstream ifiletwo(B);
  294. if (ifiletwo.is_open())
  295. {
  296. while (getline(ifiletwo, line))
  297. {
  298. if (line.length() != 0) {
  299. if (line.substr(0, line.find(" ")) == "add") {
  300. //int book_id = stoi(line.substr(line.find("book_id:") + 8, 5));
  301. //string book_name = line.substr(line.find("book_name:") + 10, line.find(", book_author") - 35);
  302. //string book_author = line.substr(line.find("book_author:") + 12);
  303. //int name_pos = stoi(line.substr(line.find("pos:") + 4, line.find("book_name:") + 10));
  304. string a = line.substr(line.find("book_author"));
  305. string book_author = a.substr(12);
  306. string n = line.substr(line.find("book_name"));
  307. string book_name = n.substr(10, n.length() - a.length() - 12);
  308. string c = line.substr(line.find("book_id"));
  309. string e = c.substr(8, c.length() - n.length() - 10);
  310. int book_id = stoi(e);
  311. string d = line.substr(line.find("pos:"));
  312. int name_pos = stoi(d.substr(4, d.length() - c.length() - 5));
  313. list.insert(name_pos, book_id, book_name, book_author);
  314. }
  315. else if (line.substr(0, line.find(" ")) == "sort") {
  316. list.sort(line.substr(line.find(" ") + 1));
  317. }
  318. else if (line.substr(0, line.find(" ")) == "remove") {
  319. int temp = 0;
  320. while (list.book_search(line.substr(line.find(" ") + 1, line.find(":") - 7),
  321. line.substr(line.find(":") + 1)) != -1 && temp != list.getSize() && list.getSize() != 0) {
  322. list.remove(list.book_search(line.substr(line.find(" ") + 1, line.find(":") - 7),
  323. line.substr(line.find(":") + 1)));
  324. temp++;
  325. }
  326. }
  327. n++;
  328. }
  329. }
  330. ifiletwo.close();
  331. }
  332. list.print(true, C);
  333. return 0;
  334. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement