mituri

[C++] Odwroc kolejke

Oct 1st, 2015
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. /*
  2. Uzyj klasy queue. Opracować funkcję OdwrocKol(…), która odwraca kolejność elementów w kolejce. Napisać program sprawdzający tę funkcję. W kolejce mają być ustawiane elementy typu string, wczytywane z dołączonego pliku
  3.  
  4. */
  5.  
  6. #include <iostream>
  7. #include <string>
  8. #include <queue>
  9. #include <fstream>
  10. #include <stdlib>
  11.  
  12. using namespace std;
  13.  
  14. queue<string> odwrocKolejke(queue <string> kolejka){
  15. queue<string> tmp = kolejka, tmp2;
  16. vector<string> wynik;
  17. while(!tmp.empty()){
  18. wynik.push_back(tmp.front());
  19. tmp.pop();
  20. }
  21.  
  22. while(!wynik.empty()){
  23. tmp2.push(wynik.back());
  24. wynik.pop_back();
  25. }
  26.  
  27. return tmp2;
  28. }
  29.  
  30. void pokazKolejke(queue <string> kolejka){
  31. queue<string> tmp = kolejka;
  32. while(!tmp.empty()){
  33. cout << tmp.front() << " ";
  34. tmp.pop();
  35. }
  36. cout << endl;
  37. }
  38.  
  39.  
  40. int main(){
  41. queue <string> kolejka, kolejka2;
  42.  
  43. fstream file;
  44. file.open( "text.txt" , std::ios::in | std::ios::out);
  45. if( file.good() == false )exit (0);
  46.  
  47. string line;
  48. while(getline(file,line)){
  49. kolejka.push(line);
  50. }
  51. pokazKolejke(kolejka);
  52.  
  53. kolejka2 = odwrocKolejke(kolejka);
  54. pokazKolejke(kolejka2);
  55.  
  56. system("PAUSE");
  57. }
Advertisement
Add Comment
Please, Sign In to add comment