Advertisement
Guest User

Untitled

a guest
Jan 18th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.79 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <string>
  4. #include <fstream>
  5. #include <utility>
  6. #include <cstdlib>
  7.  
  8. using namespace std;
  9.  
  10. class przepis
  11. {
  12. public:
  13. char* rodzaj = "";
  14. char* nazwa;
  15. int czas = 0;
  16. char* skladniki;
  17. char* tresc;
  18.  
  19. przepis(char*, char*, int, char*, char*);
  20.  
  21. przepis() = default;
  22. przepis(const przepis &) = default;
  23. przepis & operator=(const przepis &) = default;
  24.  
  25. int operator>(const przepis &);
  26. friend ostream & operator<<(ostream &, const przepis &);
  27. friend istream & operator >> (istream &, przepis &);
  28.  
  29. int nazwam(string);
  30. };
  31.  
  32. przepis::przepis(char* rodzaj, char* nazwa, int czas, char* skladniki, char* tresc) : czas(czas)
  33. {
  34. przepis::rodzaj = new char[strlen(rodzaj) + 1];
  35. strcpy_s(przepis::rodzaj, strlen(rodzaj) + 1, rodzaj);
  36. przepis::nazwa = new char[strlen(nazwa) + 1];
  37. strcpy_s(przepis::nazwa, strlen(nazwa) + 1, nazwa);
  38. przepis::skladniki = new char[strlen(skladniki) + 1];
  39. strcpy_s(przepis::skladniki, strlen(skladniki) + 1, skladniki);
  40. przepis::tresc = new char[strlen(tresc) + 1];
  41. strcpy_s(przepis::tresc, strlen(tresc) + 1, tresc);
  42. }
  43.  
  44. int przepis::operator>(const przepis &rt)
  45. {
  46. string str1(nazwa);
  47. string str2(rt.nazwa);
  48. return str1 > str2;
  49. }
  50.  
  51. ostream & operator<<(ostream &os, const przepis &rt)
  52. {
  53. os << "Rodzaj: " << rt.rodzaj << endl;
  54. os << "Nazwa: " << rt.nazwa << endl;
  55. os << "Czas przygotowania: " << rt.czas << " minut" << endl;
  56. os << "Skladniki: " << rt.skladniki << endl;
  57. os << "Tresc przepisu: " << rt.tresc << endl << endl;
  58. return os;
  59. };
  60.  
  61. istream & operator >> (istream &is, przepis &rt)
  62. {
  63. return is >> rt.rodzaj >> rt.nazwa >> rt.czas >> rt.skladniki >> rt.tresc;
  64. };
  65.  
  66. int przepis::nazwam(string nazwa2)
  67. {
  68. if (nazwa == nazwa2)
  69. return 0;
  70. else
  71. return 1;
  72. }
  73.  
  74. struct q_elem
  75. {
  76. przepis t;
  77. q_elem * next;
  78. q_elem * pop;
  79. q_elem(const przepis &ctr) :t(ctr), next(nullptr), pop(nullptr) {};
  80. q_elem(char* rodzaj, char* nazwa, int czas, char* skladniki, char* tresc) : t(rodzaj, nazwa, czas, skladniki, tresc), next(nullptr), pop(nullptr) {};
  81. };
  82.  
  83. class queue
  84. {
  85. protected:
  86. q_elem *head;
  87. q_elem *tail = head;
  88.  
  89. public:
  90. queue() :head(nullptr) {}
  91.  
  92. virtual queue & operator+=(const przepis &rq)
  93. {
  94. q_elem *tmp = head;
  95. while (tmp)
  96. {
  97. if (strcmp(tmp->t.nazwa, rq.nazwa) == 0)
  98. {
  99. cout << "Przepis na podane dane juz wystepuje w ksiazce" << endl << endl;
  100. return *this;
  101. }
  102. tmp = tmp->next;
  103. }
  104.  
  105. q_elem *qep = new q_elem(rq);
  106.  
  107. if (head)
  108. {
  109. tail->next = qep;
  110. qep->pop = tail;
  111. }
  112. else
  113. head = qep;
  114.  
  115. tail = qep;
  116.  
  117. return *this;
  118. }
  119.  
  120. queue deleteElem(char* nazwa)
  121. {
  122. q_elem *qep = head;
  123. if (qep == NULL)
  124. {
  125. cout << "Lista jest pusta" << endl;
  126. return *this;
  127. }
  128. else
  129. {
  130. while (qep != nullptr)
  131. {
  132. if (qep->t.nazwam(nazwa) == 0)
  133. {
  134. if (qep == head)
  135. {
  136. if (head->next == NULL)
  137. {
  138. head->next = head->pop = nullptr;
  139. delete qep;
  140. return *this;
  141. }
  142. else
  143. {
  144. head->next->pop = head->pop;
  145. head = head->next;
  146. delete qep;
  147. return *this;
  148. }
  149. }
  150. else if (qep->next == nullptr)
  151. {
  152. qep->pop->next = qep->next;
  153. delete qep;
  154. return *this;
  155. }
  156. else
  157. {
  158. qep->pop->next = qep->next;
  159. qep->next->pop = qep->pop;
  160. delete qep;
  161. return *this;
  162. }
  163. }
  164. qep = qep->next;
  165. }
  166. cout << "W ksiazce nie ma danego przepisu" << endl << endl;
  167. return *this;
  168. }
  169. }
  170.  
  171. queue(const queue &rq) :head(nullptr)
  172. {
  173. q_elem *qep = rq.head;
  174.  
  175. while (qep)
  176. {
  177. (*this) += qep->t;
  178. qep = qep->next;
  179. }
  180. }
  181.  
  182. queue(queue &&rrq) : head(rrq.head), tail(rrq.tail)
  183. {
  184. rrq.head = rrq.tail = nullptr;
  185. }
  186.  
  187. ~queue()
  188. {
  189. while (head)
  190. {
  191. const q_elem * qep = head;
  192. head = head->next;
  193. delete qep;
  194. }
  195. }
  196.  
  197. void empty() // pozostawia obiekt pusty (head==nullptr)
  198. {
  199. while (head)
  200. {
  201. const q_elem * qep = head;
  202. head = head->next;
  203. delete qep;
  204. }
  205. }
  206.  
  207. queue & operator=(const queue &rq)
  208. {
  209. if (&rq == this)
  210. return *this;
  211.  
  212. empty();
  213.  
  214. q_elem *qep = rq.head;
  215. while (qep)
  216. {
  217. (*this) += qep->t;
  218. qep = qep->next;
  219. }
  220.  
  221. return *this;
  222. }
  223.  
  224. queue operator +(const queue &rq)
  225. {
  226. q_elem *qep = rq.head;
  227. while (qep)
  228. {
  229. (*this) += qep->t;
  230. qep = qep->next;
  231. }
  232.  
  233. return *this;
  234. }
  235.  
  236. queue & operator=(queue &&rq)
  237. {
  238. swap(head, rq.head);
  239. swap(tail, rq.tail);
  240. return *this;
  241. }
  242.  
  243. int wypisz(char* nazwa)
  244. {
  245. q_elem *qep = head;
  246. while (qep)
  247. {
  248. if (qep->t.nazwam(nazwa) == 0)
  249. {
  250. cout << qep->t << endl;
  251. return 0;
  252. }
  253. else
  254. qep = qep->next;
  255. }
  256.  
  257. cout << "Nie ma takiego przepisu w ksiazce" << endl << endl;
  258. return 0;
  259. }
  260.  
  261. void mozliwe(char* nazwa)
  262. {
  263. int maxskl = 0;
  264. queue tmp1;
  265. int dlugosc = strlen(nazwa);
  266. int rozmiar = 0;
  267. for (int i = 0; i < dlugosc; i++)
  268. {
  269. if (nazwa[i] == ',')
  270. rozmiar++;
  271. }
  272. char** tab1 = new char*[rozmiar+1];
  273. char* wszyscy = new char[dlugosc];
  274. strcpy_s(wszyscy, dlugosc + 1, nazwa);
  275. char *token1, *token2;
  276.  
  277. int a = 0;
  278. cout << wszyscy << endl;
  279. while (a < (rozmiar+1))
  280. {
  281. token = strtok(wszyscy, ",");
  282. tab1[a] = token;
  283. a++;
  284. }
  285.  
  286. /*
  287. for (int j = 0; j < (rozmiar+1); j++)
  288. {
  289. cout << tab1[j] << endl;
  290. }
  291. */
  292. }
  293.  
  294. friend istream & operator >> (istream &is, queue &rq)
  295. {
  296. przepis t;
  297.  
  298. rq.empty();
  299.  
  300. while (is >> t)
  301. rq += t;
  302.  
  303. return is;
  304. }
  305.  
  306. friend ostream & operator<<(ostream &os, const queue &rq)
  307. {
  308. q_elem *qep = rq.head;
  309. while (qep)
  310. {
  311. os << qep->t;
  312. qep = qep->next;
  313. }
  314.  
  315. return os;
  316. }
  317.  
  318. int zapisz(char *fileName)
  319. {
  320. ofstream zapis;
  321. zapis.open(fileName);
  322. if (!zapis.is_open())
  323. {
  324. cout << "Pliku nie udalo sie otworzyc" << endl;
  325. return 1;
  326. }
  327. q_elem *qep = head;
  328. while (qep)
  329. {
  330. zapis << qep->t.nazwa << endl;
  331. zapis << qep->t.czas << endl;
  332. zapis << qep->t.skladniki << endl;
  333. zapis << qep->t.tresc << endl;
  334. qep = qep->next;
  335. }
  336.  
  337. zapis.close();
  338. return 0;
  339. }
  340.  
  341.  
  342. int wczytaj(char *fileName)
  343. {
  344. ifstream czytanie;
  345. czytanie.open(fileName);
  346. if (!czytanie.is_open())
  347. {
  348. cout << "Pliku nie udalo sie otworzyc" << endl;
  349. return 1;
  350. }
  351.  
  352. string wiersz1;
  353. string wiersz2;
  354. string wiersz3;
  355. string wiersz4;
  356.  
  357. while (!czytanie.eof())
  358. {
  359. getline(czytanie, wiersz1);
  360. if (wiersz1 == "")
  361. break;
  362. getline(czytanie, wiersz2);
  363. int i = atoi(wiersz2.c_str());
  364. getline(czytanie, wiersz3);
  365. getline(czytanie, wiersz4);
  366.  
  367. char *s1 = new char[wiersz1.size() + 1];
  368. memcpy(s1, wiersz1.c_str(), wiersz1.size() + 1);
  369. char *s3 = new char[wiersz3.size() + 1];
  370. memcpy(s3, wiersz3.c_str(), wiersz3.size() + 1);
  371. char *s4 = new char[wiersz4.size() + 1];
  372. memcpy(s4, wiersz4.c_str(), wiersz4.size() + 1);
  373.  
  374. q_elem *qep;
  375.  
  376. if(fileName == "Ciasta.txt")
  377. qep = new q_elem("Ciasto",s1, i, s3, s4);
  378. else
  379. qep = new q_elem("Salatka", s1, i, s3, s4);
  380.  
  381. free(s1);
  382. free(s3);
  383. free(s4);
  384.  
  385. if (head!=nullptr)
  386. {
  387. tail->next = qep;
  388. qep->pop = tail;
  389. }
  390. else
  391. head = qep;
  392.  
  393. tail = qep;
  394. }
  395.  
  396. czytanie.close(); // zamykam połączenie wejściowe z plikiem
  397. return 0;
  398. }
  399.  
  400. queue najkrocej()
  401. {
  402. q_elem *qep = head;
  403. if (qep == NULL)
  404. {
  405. cout << "Lista jest pusta" << endl << endl;
  406. return *this;
  407. }
  408. else
  409. {
  410. q_elem *tmp = head;
  411. int czasmin = tmp->t.czas;
  412. while (qep)
  413. {
  414. if (qep->t.czas < czasmin)
  415. {
  416. czasmin = qep->t.czas;
  417. tmp = qep;
  418. qep = qep->next;
  419. }
  420. else
  421. qep = qep->next;
  422. }
  423. cout << tmp->t << endl;
  424. return *this;
  425. }
  426. }
  427.  
  428. };
  429.  
  430. class sorted_q :public queue
  431. {
  432. public:
  433. sorted_q & operator+=(const przepis &rq)
  434. {
  435. q_elem *tmp = head;
  436. while (tmp)
  437. {
  438. if (strcmp(tmp->t.nazwa, rq.nazwa)==0)
  439. {
  440. cout << "Przepis na podane dane juz wystepuje w ksiazce" << endl << endl;
  441. return *this;
  442. }
  443. tmp = tmp->next;
  444. }
  445.  
  446. q_elem * const newel = new q_elem(rq);
  447.  
  448. if ((!head) || (head->t > rq))
  449. {
  450. newel->next = head;
  451. if (head) head->pop = newel;
  452. head = newel;
  453. }
  454. else
  455. {
  456. q_elem * pq = head;
  457. while (pq->next && !(pq->next->t > rq))
  458. pq = pq->next;
  459.  
  460. if (pq->next == nullptr)
  461. {
  462. newel->next = pq->next;
  463. newel->pop = pq;
  464. pq->next = newel;
  465. tail = newel;
  466. return *this;
  467. }
  468. else
  469. {
  470. newel->next = pq->next;
  471. newel->pop = pq;
  472. pq->next->pop = newel;
  473. pq->next = newel;
  474. }
  475. }
  476. return *this;
  477. }
  478.  
  479. using queue::operator=;
  480. };
  481.  
  482. int main()
  483. {
  484. queue q1;
  485. q1.wczytaj("Ciasta.txt");
  486. queue q2;
  487. q2.wczytaj("Salatki.txt");
  488.  
  489. q1.mozliwe("jeden,dwa,trzy,cztery");
  490.  
  491. system("pause");
  492. return 0;
  493. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement