Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.32 KB | None | 0 0
  1. #include <string>
  2. #include <fstream>
  3. #include <sstream>
  4. #include <iostream>
  5. #include <cstdlib>
  6. #include <vector>
  7.  
  8. using namespace std;
  9.  
  10. struct data {
  11. int id;
  12. string namePL;
  13. string nameLA;
  14. string date;
  15. string collector;
  16. string comment;
  17. string family;
  18. string place;
  19. string status;
  20. };
  21.  
  22. struct node {
  23. struct data data;
  24. struct node* next;
  25. struct node* prev;
  26. };
  27.  
  28. struct node* head = NULL;
  29. struct node* last = NULL;
  30.  
  31.  
  32.  
  33. void append(struct data newKarta)
  34. {
  35. struct node* new_node = (struct node*)malloc(sizeof(struct node));
  36. last = head;
  37. new_node->data = newKarta;
  38. new_node->next = NULL;
  39. if (head == NULL) {
  40. new_node->prev = NULL;
  41. head = new_node;
  42. return;
  43. }
  44. while (last->next != NULL)
  45. last = last->next;
  46. last->next = new_node;
  47. new_node->prev = last;
  48. return;
  49. } ;
  50.  
  51.  
  52. struct node* usun(int id) {
  53. struct node* current = head;
  54. struct node* previous = NULL;
  55.  
  56. if (head == NULL) {
  57. return NULL;
  58. }
  59. while ((current->data).id != id) {
  60.  
  61. if (current->next == NULL) {
  62. return NULL;
  63. }
  64. else {
  65. previous = current;
  66. current = current->next;
  67. }
  68. }
  69. if (current == head) {
  70. head = head->next;
  71. }
  72. else {
  73. current->prev->next = current->next;
  74. }
  75. if (current == last) {
  76. last = current->prev;
  77. }
  78. else {
  79. current->next->prev = current->prev;
  80. }
  81.  
  82. return current;
  83. };
  84.  
  85. string getNamePL() {
  86. string namePL;
  87. cout << "Wprowadz polska nazwe zebranego osobnika: " << endl;
  88. cin >> namePL;
  89. return namePL;
  90. };
  91. string getNameLA() {
  92. string nameLA;
  93. cout << "wprowadz lacinska nazwe zebranego osobinka: " << endl;
  94. cin >> nameLA;
  95. return nameLA;
  96. };
  97. string getDate() {
  98. string date;
  99. cout << "Data zebrania (format rrrr-mm-dd): " << endl;
  100. cin >> date;
  101. return date;
  102. };
  103. string getComment() {
  104. string comment;
  105. cout << "Komentarz: " << endl;
  106. cin >> comment;
  107. return comment;
  108. };
  109. string getFamily() {
  110. string family;
  111. cout << "Rodzina (lac.) do ktorej nalezy osobnik: " << endl;
  112. cin >> family;
  113. return family;
  114. };
  115. string getCollector() {
  116. string collector;
  117. cout << "Imie i nazwisko osoby ktora zebrala osobnika: " << endl;
  118. cin >> collector;
  119. return collector;
  120. };
  121. string getPlace() {
  122. string place;
  123. cout << "Miejsce zebrania: " << endl;
  124. cin >> place;
  125. return place;
  126. };
  127. string getStatus() {
  128. string status;
  129. cout << "Wprowadz status gatunku osobnika (zagrozony, rzadki, powszechny, brak danych); " << endl;
  130. cin >> status;
  131. return status;
  132. };
  133. /*void printData(){
  134. struct node *ptr = head;
  135.  
  136. while (ptr != NULL) {
  137. cout << "\n[ ";
  138. cout << "id: ";
  139. cout << ptr->data.id << endl;
  140. cout << "Nazwa polska: ";
  141. cout << ptr->data.namePL << endl;
  142. cout << "Nazwa lacinska: ";
  143. cout << ptr->data.nameLA << endl;
  144. cout << "Data zebrania: ";
  145. cout << ptr->data.date << endl;
  146. cout << "Osoba ktora zebrala osobnika: ";
  147. cout << ptr->data.collector << endl;
  148. cout << "Rodzina: ";
  149. cout << ptr->data.family << endl;
  150. cout << "Miejsce zebrania: ";
  151. cout << ptr->data.place << endl;
  152. cout << "Status wystepowania: ";
  153. cout << ptr->data.status << endl;
  154. cout << "Komentarz: ";
  155. cout << ptr->data.comment << endl;
  156. cout << " ]" << endl;
  157.  
  158. }
  159. */
  160.  
  161. void displayListForward(){
  162. struct node *ptr = head;
  163.  
  164. while (ptr != NULL) {
  165. cout << "\n[ ";
  166. cout << "id: ";
  167. cout << ptr->data.id << endl;
  168. cout << "Nazwa polska: ";
  169. cout << ptr->data.namePL << endl;
  170. cout << "Nazwa lacinska: ";
  171. cout << ptr->data.nameLA << endl;
  172. cout << "Data zebrania: ";
  173. cout << ptr->data.date << endl;
  174. cout << "Osoba ktora zebrala osobnika: ";
  175. cout << ptr->data.collector << endl;
  176. cout << "Rodzina: ";
  177. cout << ptr->data.family << endl;
  178. cout << "Miejsce zebrania: ";
  179. cout << ptr->data.place << endl;
  180. cout << "Status wystepowania: ";
  181. cout << ptr->data.status << endl;
  182. cout << "Komentarz: ";
  183. cout << ptr->data.comment << endl;
  184. cout << " ]" << endl;
  185. }
  186.  
  187. };
  188.  
  189.  
  190.  
  191. /*bool searchId( int x)
  192. {
  193. struct node* current = head;
  194. while (current != NULL)
  195. {
  196. if ((current->data).id == x)
  197. printData(current->data);
  198.  
  199. return true;
  200. current = current->next;
  201. }
  202. return false;
  203. }
  204. */
  205.  
  206. /*
  207. bool searchNamePL(string x)
  208. {
  209.  
  210. struct node* current = head;
  211. while (current != NULL)
  212. {
  213. if ((current->data).namePL == x)
  214. printData(current->data);
  215. return true;
  216. current = current->next;
  217. }
  218. return false;
  219. }
  220. bool searchNameLA(string x)
  221. {
  222.  
  223. struct node* current = head;
  224. while (current != NULL)
  225. {
  226. if ((current->data).nameLA == x) {
  227. printData(current->data);
  228. return true;
  229. }
  230. current = current->next;
  231. }
  232. return false;
  233. }
  234. bool searchDate(string x)
  235. {
  236.  
  237. struct node* current = head; // Initialize current
  238. while (current != NULL)
  239. {
  240. if ((current->data).date == x)
  241. printData(current->data);
  242. return true;
  243. current = current->next;
  244. }
  245. return false;
  246. }
  247. bool searchFamily(string x)
  248. {
  249.  
  250. struct node* current = head; // Initialize current
  251. while (current != NULL)
  252. {
  253. if ((current->data).family == x)
  254. printData(current->data);
  255. return true;
  256. current = current->next;
  257. }
  258. return false;
  259. }
  260. bool searchCollector(string x)
  261. {
  262.  
  263. struct node* current = head; // Initialize current
  264. while (current != NULL)
  265. {
  266. if ((current->data).collector == x)
  267. printData(current->data);
  268. return true;
  269. current = current->next;
  270. }
  271. return false;
  272. }
  273. bool searchPlace(string x)
  274. {
  275.  
  276. struct node* current = head; // Initialize current
  277. while (current != NULL)
  278. {
  279. if ((current->data).place == x)
  280. printData(current->data);
  281. return true;
  282. current = current->next;
  283. }
  284. return false;
  285. }
  286. bool searchStatus(string x)
  287. {
  288.  
  289. struct node* current = head; // Initialize current
  290. while (current != NULL)
  291. {
  292. if ((current->data).status == x)
  293. printData(current->data);
  294. return true;
  295. current = current->next;
  296. }
  297. return false;
  298. }
  299.  
  300. */
  301.  
  302. /*
  303.  
  304. void szukajPo() {
  305. int choice;
  306. do
  307. {
  308.  
  309.  
  310.  
  311. cout << endl
  312. << " Szukaj rosliny po: \n"
  313. << " 1 - id\n"
  314. << " 2 - Nazwie polskiej\n"
  315. << " 3 - Nazwie lacinskiej.\n"
  316. << " 4 - Dacie.\n"
  317. << " 5 - Rodzinie\n"
  318. << " 6 - Osobie która zebrała osobnika.\n"
  319. << " 7 - Miejscu zebrania.\n"
  320. << " 8 - Statusie zagrożenia.\n"
  321. << " Enter your choice and press return: ";
  322. cin >> choice;
  323. int temp;
  324. string temps;
  325. switch (choice)
  326. {
  327. case 1:
  328.  
  329. cout << "Wprowadz id: ";
  330. cin >> temp;
  331. searchId(temp);
  332. break;
  333. case 2:
  334.  
  335. cout << "Wprowadz nazwę polską: ";
  336. cin >> temps;
  337. searchNamePL(temps);
  338.  
  339. break;
  340. case 3:
  341. cout << "Wprowadz nazwę lacinska: ";
  342. cin >> temps;
  343. searchNameLA(temps);
  344. break;
  345. case 4:
  346. cout << "Wprowadz date: ";
  347. cin >> temps;
  348. searchDate(temps);
  349.  
  350. break;
  351. case 5:
  352. cout << "Wprowadz family: ";
  353. cin >> temps;
  354. searchFamily(temps);
  355. case 6:
  356. cout << "Wprowadz osobę ktora zebrala: ";
  357. cin >> temps;
  358. searchCollector(temps);
  359. break;
  360. case 7:
  361. cout << "Wprowadz miejsce zebrania: ";
  362. cin >> temps;
  363. searchPlace(temps);
  364. break;
  365. case 8:
  366. cout << "Wprowadz status (zagrozony, rzadki, powszechny, brak danych): ";
  367. cin >> temps;
  368. searchStatus(temps);
  369. break;
  370. default:
  371. cout << "Wybierz ponownie. \n";
  372.  
  373. break;
  374. }
  375.  
  376. } while (choice != 9);
  377. }
  378.  
  379. */
  380.  
  381. struct data getDataFromUser() {
  382. struct data temp;
  383. temp.namePL = getNamePL();
  384. temp.nameLA = getNameLA();
  385. temp.family = getFamily();
  386. temp.status = getStatus();
  387. temp.date = getDate();
  388. temp.place = getPlace();
  389. temp.collector = getCollector();
  390. temp.comment = getComment();
  391.  
  392. return temp;
  393.  
  394. };
  395.  
  396. void importList();
  397. void exportList();
  398.  
  399. int main() {
  400. int choice;
  401. do
  402. {
  403.  
  404.  
  405. cout << endl
  406. << " 1 - Wprowadz nowa roslinie\n"
  407. << " 2 - Usun rosline z albumu\n"
  408. << " 3 - Edytuj dane o roslinie\n"
  409. << " 4 - Wyszukaj element po id, "
  410. << " 5 - Wyswietl cala baze\n"
  411. << " 6 - Wyswietl rosliny o zadanym statusie\n"
  412. << " 7 - Zapisz do pliku\n"
  413. << " 8 - Wczytaj z pliku\n"
  414. << " 9 - Zakoncz program\n"
  415. << " Enter your choice and press return: ";
  416. cin >> choice;
  417.  
  418. switch (choice)
  419. {
  420. case 1:
  421. append(getDataFromUser());
  422. break;
  423. case 2:
  424. int toDelete;
  425. cout << "podaj pozycje która chcesz usunać: " << endl;
  426. cin >> toDelete;
  427. usun(toDelete);
  428. break;
  429. case 3:
  430.  
  431. break;
  432. case 4:
  433.  
  434. break;
  435. case 5:
  436. displayListForward();
  437. break;
  438. case 6:
  439.  
  440. break;
  441. case 7:
  442. exportList();
  443.  
  444. break;
  445. case 8:
  446.  
  447. break;
  448. case 9:
  449. break;
  450. default:
  451. cout << "Niepoprawny wybor. \n"
  452. << "Wybierz ponownie.\n";
  453. break;
  454.  
  455. }
  456.  
  457. } while (choice != 9);
  458.  
  459.  
  460. return 0;
  461. }
  462.  
  463. void exportList() {
  464. struct node *ptr = head;
  465. ofstream dataFile;
  466. dataFile.open("zielnik.txt", ofstream::out | ofstream::trunc);
  467. if (!dataFile.is_open() || dataFile.fail())
  468. {
  469. dataFile.close();
  470. printf("\nBląd z plikiem zielnik.txt");
  471. };
  472.  
  473. while (ptr != NULL) {
  474. dataFile << (ptr->data).id << ","
  475. << (ptr->data).namePL << ","
  476. << (ptr->data).nameLA << ","
  477. << (ptr->data).date << ","
  478. << (ptr->data).collector << ","
  479. << (ptr->data).comment << ","
  480. << (ptr->data).family << ","
  481. << (ptr->data).place << ","
  482. << (ptr->data).status << endl;
  483. ptr = ptr->next;
  484.  
  485.  
  486. }
  487. dataFile.close();
  488. };
  489.  
  490. void importList() {
  491. node *head = NULL;
  492. node *last = NULL;
  493. ifstream dataFile;
  494. dataFile.open("zielnik.txt", ifstream::in);
  495. if (!dataFile.is_open() || dataFile.fail())
  496. {
  497. dataFile.close();
  498. printf("\nBląd z plikiem zielnik.txt");
  499. };
  500. vector<string> matrix;
  501. string line;
  502.  
  503. while (getline(dataFile, line, '\n'))
  504. {
  505. istringstream iss(line);
  506. string data;
  507. while (getline(iss, data, ','))
  508. {
  509. matrix.push_back(data);
  510. }
  511. }
  512. dataFile.close();
  513.  
  514.  
  515. //tworzenie tymczas struktury i id
  516. struct data tmp_karta;
  517. int tmp_id;
  518.  
  519. tmp_karta;
  520. int i = 0;
  521. while (i < matrix.size()) {
  522. tmp_karta.id = sstream::stoi(matrix[i]);
  523. tmp_karta.namePL = matrix[i + 1];
  524. tmp_karta.nameLA = matrix[i + 2];
  525. tmp_karta.date = matrix[i + 3];
  526. tmp_karta.collector = matrix[i + 4];
  527. tmp_karta.comment = matrix[i + 5];
  528. tmp_karta.family = matrix[i + 6];
  529. tmp_karta.place = matrix[i + 7];
  530. tmp_karta.status = matrix[i + 8];
  531. i += 9; // rekord ma 9pol
  532. append(tmp_karta);
  533. }
  534.  
  535.  
  536.  
  537.  
  538. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement