Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <utility>
- #include <cstdlib>
- using namespace std;
- class przepis
- {
- public:
- char* rodzaj = "";
- char* nazwa;
- int czas = 0;
- char* skladniki;
- char* tresc;
- przepis(char*, char*, int, char*, char*);
- przepis() = default;
- przepis(const przepis &) = default;
- przepis & operator=(const przepis &) = default;
- int operator>(const przepis &);
- friend ostream & operator<<(ostream &, const przepis &);
- friend istream & operator >> (istream &, przepis &);
- int nazwam(string);
- };
- przepis::przepis(char* rodzaj, char* nazwa, int czas, char* skladniki, char* tresc) : czas(czas)
- {
- przepis::rodzaj = new char[strlen(rodzaj) + 1];
- strcpy_s(przepis::rodzaj, strlen(rodzaj) + 1, rodzaj);
- przepis::nazwa = new char[strlen(nazwa) + 1];
- strcpy_s(przepis::nazwa, strlen(nazwa) + 1, nazwa);
- przepis::skladniki = new char[strlen(skladniki) + 1];
- strcpy_s(przepis::skladniki, strlen(skladniki) + 1, skladniki);
- przepis::tresc = new char[strlen(tresc) + 1];
- strcpy_s(przepis::tresc, strlen(tresc) + 1, tresc);
- }
- int przepis::operator>(const przepis &rt)
- {
- string str1(nazwa);
- string str2(rt.nazwa);
- return str1 > str2;
- }
- ostream & operator<<(ostream &os, const przepis &rt)
- {
- os << "Rodzaj: " << rt.rodzaj << endl;
- os << "Nazwa: " << rt.nazwa << endl;
- os << "Czas przygotowania: " << rt.czas << " minut" << endl;
- os << "Skladniki: " << rt.skladniki << endl;
- os << "Tresc przepisu: " << rt.tresc << endl << endl;
- return os;
- };
- istream & operator >> (istream &is, przepis &rt)
- {
- return is >> rt.rodzaj >> rt.nazwa >> rt.czas >> rt.skladniki >> rt.tresc;
- };
- int przepis::nazwam(string nazwa2)
- {
- if (nazwa == nazwa2)
- return 0;
- else
- return 1;
- }
- struct q_elem
- {
- przepis t;
- q_elem * next;
- q_elem * pop;
- q_elem(const przepis &ctr) :t(ctr), next(nullptr), pop(nullptr) {};
- q_elem(char* rodzaj, char* nazwa, int czas, char* skladniki, char* tresc) : t(rodzaj, nazwa, czas, skladniki, tresc), next(nullptr), pop(nullptr) {};
- };
- class queue
- {
- protected:
- q_elem *head;
- q_elem *tail = head;
- public:
- queue() :head(nullptr) {}
- virtual queue & operator+=(const przepis &rq)
- {
- q_elem *tmp = head;
- while (tmp)
- {
- if (strcmp(tmp->t.nazwa, rq.nazwa) == 0)
- {
- cout << "Przepis na podane dane juz wystepuje w ksiazce" << endl << endl;
- return *this;
- }
- tmp = tmp->next;
- }
- q_elem *qep = new q_elem(rq);
- if (head)
- {
- tail->next = qep;
- qep->pop = tail;
- }
- else
- head = qep;
- tail = qep;
- return *this;
- }
- queue deleteElem(char* nazwa)
- {
- q_elem *qep = head;
- if (qep == NULL)
- {
- cout << "Lista jest pusta" << endl;
- return *this;
- }
- else
- {
- while (qep != nullptr)
- {
- if (qep->t.nazwam(nazwa) == 0)
- {
- if (qep == head)
- {
- if (head->next == NULL)
- {
- head->next = head->pop = nullptr;
- delete qep;
- return *this;
- }
- else
- {
- head->next->pop = head->pop;
- head = head->next;
- delete qep;
- return *this;
- }
- }
- else if (qep->next == nullptr)
- {
- qep->pop->next = qep->next;
- delete qep;
- return *this;
- }
- else
- {
- qep->pop->next = qep->next;
- qep->next->pop = qep->pop;
- delete qep;
- return *this;
- }
- }
- qep = qep->next;
- }
- cout << "W ksiazce nie ma danego przepisu" << endl << endl;
- return *this;
- }
- }
- queue(const queue &rq) :head(nullptr)
- {
- q_elem *qep = rq.head;
- while (qep)
- {
- (*this) += qep->t;
- qep = qep->next;
- }
- }
- queue(queue &&rrq) : head(rrq.head), tail(rrq.tail)
- {
- rrq.head = rrq.tail = nullptr;
- }
- ~queue()
- {
- while (head)
- {
- const q_elem * qep = head;
- head = head->next;
- delete qep;
- }
- }
- void empty() // pozostawia obiekt pusty (head==nullptr)
- {
- while (head)
- {
- const q_elem * qep = head;
- head = head->next;
- delete qep;
- }
- }
- queue & operator=(const queue &rq)
- {
- if (&rq == this)
- return *this;
- empty();
- q_elem *qep = rq.head;
- while (qep)
- {
- (*this) += qep->t;
- qep = qep->next;
- }
- return *this;
- }
- queue operator +(const queue &rq)
- {
- q_elem *qep = rq.head;
- while (qep)
- {
- (*this) += qep->t;
- qep = qep->next;
- }
- return *this;
- }
- queue & operator=(queue &&rq)
- {
- swap(head, rq.head);
- swap(tail, rq.tail);
- return *this;
- }
- int wypisz(char* nazwa)
- {
- q_elem *qep = head;
- while (qep)
- {
- if (qep->t.nazwam(nazwa) == 0)
- {
- cout << qep->t << endl;
- return 0;
- }
- else
- qep = qep->next;
- }
- cout << "Nie ma takiego przepisu w ksiazce" << endl << endl;
- return 0;
- }
- void mozliwe(char* nazwa)
- {
- int maxskl = 0;
- queue tmp1;
- int dlugosc = strlen(nazwa);
- int rozmiar = 0;
- for (int i = 0; i < dlugosc; i++)
- {
- if (nazwa[i] == ',')
- rozmiar++;
- }
- char** tab1 = new char*[rozmiar+1];
- char* wszyscy = new char[dlugosc];
- strcpy_s(wszyscy, dlugosc + 1, nazwa);
- char *token1, *token2;
- int a = 0;
- cout << wszyscy << endl;
- while (a < (rozmiar+1))
- {
- token = strtok(wszyscy, ",");
- tab1[a] = token;
- a++;
- }
- /*
- for (int j = 0; j < (rozmiar+1); j++)
- {
- cout << tab1[j] << endl;
- }
- */
- }
- friend istream & operator >> (istream &is, queue &rq)
- {
- przepis t;
- rq.empty();
- while (is >> t)
- rq += t;
- return is;
- }
- friend ostream & operator<<(ostream &os, const queue &rq)
- {
- q_elem *qep = rq.head;
- while (qep)
- {
- os << qep->t;
- qep = qep->next;
- }
- return os;
- }
- int zapisz(char *fileName)
- {
- ofstream zapis;
- zapis.open(fileName);
- if (!zapis.is_open())
- {
- cout << "Pliku nie udalo sie otworzyc" << endl;
- return 1;
- }
- q_elem *qep = head;
- while (qep)
- {
- zapis << qep->t.nazwa << endl;
- zapis << qep->t.czas << endl;
- zapis << qep->t.skladniki << endl;
- zapis << qep->t.tresc << endl;
- qep = qep->next;
- }
- zapis.close();
- return 0;
- }
- int wczytaj(char *fileName)
- {
- ifstream czytanie;
- czytanie.open(fileName);
- if (!czytanie.is_open())
- {
- cout << "Pliku nie udalo sie otworzyc" << endl;
- return 1;
- }
- string wiersz1;
- string wiersz2;
- string wiersz3;
- string wiersz4;
- while (!czytanie.eof())
- {
- getline(czytanie, wiersz1);
- if (wiersz1 == "")
- break;
- getline(czytanie, wiersz2);
- int i = atoi(wiersz2.c_str());
- getline(czytanie, wiersz3);
- getline(czytanie, wiersz4);
- char *s1 = new char[wiersz1.size() + 1];
- memcpy(s1, wiersz1.c_str(), wiersz1.size() + 1);
- char *s3 = new char[wiersz3.size() + 1];
- memcpy(s3, wiersz3.c_str(), wiersz3.size() + 1);
- char *s4 = new char[wiersz4.size() + 1];
- memcpy(s4, wiersz4.c_str(), wiersz4.size() + 1);
- q_elem *qep;
- if(fileName == "Ciasta.txt")
- qep = new q_elem("Ciasto",s1, i, s3, s4);
- else
- qep = new q_elem("Salatka", s1, i, s3, s4);
- free(s1);
- free(s3);
- free(s4);
- if (head!=nullptr)
- {
- tail->next = qep;
- qep->pop = tail;
- }
- else
- head = qep;
- tail = qep;
- }
- czytanie.close(); // zamykam połączenie wejściowe z plikiem
- return 0;
- }
- queue najkrocej()
- {
- q_elem *qep = head;
- if (qep == NULL)
- {
- cout << "Lista jest pusta" << endl << endl;
- return *this;
- }
- else
- {
- q_elem *tmp = head;
- int czasmin = tmp->t.czas;
- while (qep)
- {
- if (qep->t.czas < czasmin)
- {
- czasmin = qep->t.czas;
- tmp = qep;
- qep = qep->next;
- }
- else
- qep = qep->next;
- }
- cout << tmp->t << endl;
- return *this;
- }
- }
- };
- class sorted_q :public queue
- {
- public:
- sorted_q & operator+=(const przepis &rq)
- {
- q_elem *tmp = head;
- while (tmp)
- {
- if (strcmp(tmp->t.nazwa, rq.nazwa)==0)
- {
- cout << "Przepis na podane dane juz wystepuje w ksiazce" << endl << endl;
- return *this;
- }
- tmp = tmp->next;
- }
- q_elem * const newel = new q_elem(rq);
- if ((!head) || (head->t > rq))
- {
- newel->next = head;
- if (head) head->pop = newel;
- head = newel;
- }
- else
- {
- q_elem * pq = head;
- while (pq->next && !(pq->next->t > rq))
- pq = pq->next;
- if (pq->next == nullptr)
- {
- newel->next = pq->next;
- newel->pop = pq;
- pq->next = newel;
- tail = newel;
- return *this;
- }
- else
- {
- newel->next = pq->next;
- newel->pop = pq;
- pq->next->pop = newel;
- pq->next = newel;
- }
- }
- return *this;
- }
- using queue::operator=;
- };
- int main()
- {
- queue q1;
- q1.wczytaj("Ciasta.txt");
- queue q2;
- q2.wczytaj("Salatki.txt");
- q1.mozliwe("jeden,dwa,trzy,cztery");
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement