Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <sstream>
- using namespace std;
- struct element
- {
- string slowo;
- element *next;
- };
- struct kolejka
- {
- element* head;
- element* tail;
- };
- void zadanie1(string &wiadomosc);
- void dodaj_s(element* &stos, string bufor);
- void usun_s(element* &stos, string bufor);
- void zadanie2(string &wiadomosc);
- void dodaj_k(element* &head, element* &tail, string pom);
- void usun_k(element* &head, element* &tail);
- int main()
- {
- string wiadomosc="";
- cout<<"Zadanie 1: "<<endl;
- cout<<endl;
- zadanie1(wiadomosc);
- cout<<"------------------------------------------";
- cout<<endl;
- cout<<"Zadanie 2: "<<endl;
- zadanie2(wiadomosc);
- cout<<"------------------------------------------";
- return 0;
- }
- void zadanie1(string &wiadomosc)
- {
- string sciezka;
- int dl;
- string bufor="", znak;
- ifstream plik;
- element *stos = nullptr;
- bool koniec;
- sciezka="dane.txt";
- plik.open(sciezka);
- while(!plik.eof())
- {
- koniec=0;
- while(!koniec && !plik.eof())
- {
- getline(plik, bufor, ' ');
- dl=bufor.size();
- for(int i=0;i<dl;i++)
- {
- if(bufor[i] =='.' || bufor[i] =='!' || bufor[i] =='?')//sprawdzanie ostatniego znaku bufora
- {
- znak=bufor[i];
- koniec=1;
- bufor.pop_back();//usuniecie ostatniego znaku bufora (znaki konca zdania sa czescia innych slow)
- }
- }
- bufor=bufor + " ";
- dodaj_s(stos, bufor);//dodawanie elementu do stosu
- }
- while(stos!=nullptr)//usuwanie elementow ze stosu
- {
- bufor=stos -> slowo;
- usun_s(stos,bufor);
- wiadomosc=wiadomosc+bufor;
- }
- wiadomosc.pop_back();//usuniecie dodatkowych spacji
- wiadomosc=wiadomosc+znak;//dodanie na sam koniec danego znaku konca zdania
- wiadomosc=wiadomosc+" "+"\n";
- znak="";
- }
- cout<<wiadomosc;
- }
- void dodaj_s(element* &stos, string bufor)
- {
- element *el= new element;
- el -> slowo = bufor;
- el -> next = stos;
- stos = el;
- }
- void usun_s(element* &stos, string bufor)
- {
- element *temp=stos;
- stos=stos ->next;
- delete temp;
- }
- void zadanie2(string &wiadomosc)
- {
- string bufor="", zdanie="", pom="";
- kolejka k;
- k.head=nullptr;
- k.tail=nullptr;
- int dl;
- bool koniec;
- bool czy_p;//do sprawdzenia, czy bufor bedzie podejrzany
- stringstream strumien;
- strumien.str(wiadomosc);
- while(!strumien.eof())
- {
- koniec=0;
- czy_p=0;
- while(!koniec && !strumien.eof())
- {
- getline(strumien, bufor, ' ');
- dl=bufor.size();
- zdanie=zdanie+bufor+" ";
- if(bufor=="promotion" || bufor=="discount" || bufor=="sale" || bufor=="offer")
- {
- czy_p=1;
- }
- for(int i=0;i<dl;i++)
- {
- if(bufor[i] =='.' || bufor[i] =='!' || bufor[i] =='?')//sprawdzanie ostatniego znaku bufora
- {
- koniec=1;
- pom=zdanie;
- zdanie="";
- }
- }
- }
- if(czy_p==1)
- {
- dodaj_k(k.head, k.tail, pom);//dodawanie elementow do kolejki
- }
- pom="";
- }
- while(k.head != nullptr)//usuwanie elementow z kolejki
- {
- cout<<k.head -> slowo;//wyswietlanie elementow kolejki
- usun_k(k.head, k.tail);
- }
- cout<<endl;
- }
- void dodaj_k(element* &head, element* &tail, string pom)
- {
- element *el=new element;
- el -> slowo=pom;
- el -> next=nullptr;
- if(tail != nullptr)
- {
- tail->next=el;
- }
- tail=el;
- if(head==nullptr)
- {
- head=el;
- }
- }
- void usun_k(element* &head, element* &tail)
- {
- if(head == nullptr)
- {
- return;
- }
- element* temp=head;
- head=head -> next;
- delete temp;
- if(head==nullptr)
- {
- tail=nullptr;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement