Advertisement
sellmmaahh

tut7-zad1

Jul 24th, 2015
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.93 KB | None | 0 0
  1. #include <cstring>
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5.  
  6.  
  7.  
  8.  
  9. int main () {
  10. cout<<"Unesite broj recenica: ";
  11. int n;
  12. cin>>n;
  13.  
  14. char **niz=nullptr;
  15. try {
  16. niz=new char*[n];
  17. for (int i=0; i<n; i++) {
  18.     niz[i]=nullptr;
  19. }
  20. string temp;
  21. for (int i=0; i<n; i++) {
  22.     getline (cin, temp);
  23.  
  24. niz[i]=new char[temp.length()+1];
  25. *copy(temp.begin(), temp.end(), niz[i])='\0';
  26. }
  27.  
  28. for (int i=0; i<n; i++) {
  29.     for (int j=i+1; j<n; j++)
  30.     {
  31.         if (strcmp(niz[j], niz[i])<0) {
  32.             char *temp(niz[i]);
  33.             niz[i]=niz[j];
  34.             niz[j]=temp;
  35.         }
  36.     }
  37. }
  38. for (int i=0; i<n; i++) cout<<niz[i]<<" ";
  39.  
  40. for (int i=0; i<n; i++)
  41. {
  42.     delete [] niz[i];
  43. }
  44. delete [] niz;
  45. niz=nullptr;
  46. }
  47. catch (bad_alloc)
  48. {
  49.     if (niz!=nullptr) {
  50.             for (int i=0; i<n; i++)
  51. {
  52.     delete [] niz[i];
  53. }
  54. delete [] niz;
  55.  
  56.     }
  57.     cout<<"Greska pri alokaciji.";
  58. }
  59. return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement