Advertisement
Guest User

Untitled

a guest
May 24th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.15 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <sstream>
  5.  
  6. using namespace std;
  7.  
  8. struct element
  9. {
  10. string slowo;
  11. element *next;
  12. };
  13. struct kolejka
  14. {
  15. element* head;
  16. element* tail;
  17. };
  18. void zadanie1(string &wiadomosc);
  19. void dodaj_s(element* &stos, string bufor);
  20. void usun_s(element* &stos, string bufor);
  21. void zadanie2(string &wiadomosc);
  22. void dodaj_k(element* &head, element* &tail, string pom);
  23. void usun_k(element* &head, element* &tail);
  24.  
  25. int main()
  26. {
  27. string wiadomosc="";
  28.  
  29. cout<<"Zadanie 1: "<<endl;
  30. cout<<endl;
  31. zadanie1(wiadomosc);
  32. cout<<"------------------------------------------";
  33. cout<<endl;
  34. cout<<"Zadanie 2: "<<endl;
  35. zadanie2(wiadomosc);
  36. cout<<"------------------------------------------";
  37.  
  38.  
  39. return 0;
  40. }
  41. void zadanie1(string &wiadomosc)
  42. {
  43. string sciezka;
  44. int dl;
  45. string bufor="", znak;
  46. ifstream plik;
  47. element *stos = nullptr;
  48. bool koniec;
  49. sciezka="dane.txt";
  50. plik.open(sciezka);
  51.  
  52. while(!plik.eof())
  53. {
  54. koniec=0;
  55. while(!koniec && !plik.eof())
  56. {
  57. getline(plik, bufor, ' ');
  58. dl=bufor.size();
  59.  
  60. for(int i=0;i<dl;i++)
  61. {
  62. if(bufor[i] =='.' || bufor[i] =='!' || bufor[i] =='?')//sprawdzanie ostatniego znaku bufora
  63. {
  64. znak=bufor[i];
  65. koniec=1;
  66. bufor.pop_back();//usuniecie ostatniego znaku bufora (znaki konca zdania sa czescia innych slow)
  67. }
  68. }
  69. bufor=bufor + " ";
  70. dodaj_s(stos, bufor);//dodawanie elementu do stosu
  71. }
  72.  
  73. while(stos!=nullptr)//usuwanie elementow ze stosu
  74. {
  75. bufor=stos -> slowo;
  76. usun_s(stos,bufor);
  77. wiadomosc=wiadomosc+bufor;
  78. }
  79. wiadomosc.pop_back();//usuniecie dodatkowych spacji
  80. wiadomosc=wiadomosc+znak;//dodanie na sam koniec danego znaku konca zdania
  81. wiadomosc=wiadomosc+" "+"\n";
  82. znak="";
  83. }
  84. cout<<wiadomosc;
  85. }
  86. void dodaj_s(element* &stos, string bufor)
  87. {
  88. element *el= new element;
  89. el -> slowo = bufor;
  90. el -> next = stos;
  91. stos = el;
  92. }
  93. void usun_s(element* &stos, string bufor)
  94. {
  95. element *temp=stos;
  96. stos=stos ->next;
  97. delete temp;
  98. }
  99. void zadanie2(string &wiadomosc)
  100. {
  101. string bufor="", zdanie="", pom="";
  102. kolejka k;
  103. k.head=nullptr;
  104. k.tail=nullptr;
  105.  
  106. int dl;
  107.  
  108. bool koniec;
  109. bool czy_p;//do sprawdzenia, czy bufor bedzie podejrzany
  110.  
  111. stringstream strumien;
  112. strumien.str(wiadomosc);
  113.  
  114. while(!strumien.eof())
  115. {
  116. koniec=0;
  117. czy_p=0;
  118.  
  119. while(!koniec && !strumien.eof())
  120. {
  121.  
  122. getline(strumien, bufor, ' ');
  123. dl=bufor.size();
  124. zdanie=zdanie+bufor+" ";
  125.  
  126. if(bufor=="promotion" || bufor=="discount" || bufor=="sale" || bufor=="offer")
  127. {
  128. czy_p=1;
  129. }
  130. for(int i=0;i<dl;i++)
  131. {
  132. if(bufor[i] =='.' || bufor[i] =='!' || bufor[i] =='?')//sprawdzanie ostatniego znaku bufora
  133. {
  134. koniec=1;
  135. pom=zdanie;
  136. zdanie="";
  137. }
  138. }
  139. }
  140.  
  141. if(czy_p==1)
  142. {
  143. dodaj_k(k.head, k.tail, pom);//dodawanie elementow do kolejki
  144. }
  145. pom="";
  146. }
  147.  
  148. while(k.head != nullptr)//usuwanie elementow z kolejki
  149. {
  150. cout<<k.head -> slowo;//wyswietlanie elementow kolejki
  151. usun_k(k.head, k.tail);
  152. }
  153. cout<<endl;
  154. }
  155. void dodaj_k(element* &head, element* &tail, string pom)
  156. {
  157. element *el=new element;
  158. el -> slowo=pom;
  159. el -> next=nullptr;
  160.  
  161. if(tail != nullptr)
  162. {
  163. tail->next=el;
  164. }
  165. tail=el;
  166.  
  167. if(head==nullptr)
  168. {
  169. head=el;
  170. }
  171. }
  172. void usun_k(element* &head, element* &tail)
  173. {
  174. if(head == nullptr)
  175. {
  176. return;
  177. }
  178. element* temp=head;
  179. head=head -> next;
  180. delete temp;
  181.  
  182. if(head==nullptr)
  183. {
  184. tail=nullptr;
  185. }
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement