Advertisement
sellmmaahh

t7-zad1-b

Apr 23rd, 2015
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.17 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <string>
  4. #include <cstring>
  5.  
  6. using namespace std;
  7.  
  8. bool fjakriterija(char *a, char *b)
  9. {
  10.     return std::strcmp(a,b)<0;
  11. }
  12.  
  13. int main(){
  14.  
  15.  
  16.     int n;
  17.     cout << "Koliko recenica zelite unijeti: ";
  18.     cin >> n;
  19.     cin.ignore(10000,'\n');
  20.  
  21.     char **niz(nullptr);
  22.     try{
  23.  
  24.         niz = new char*[n];
  25.         for(int i = 0; i < n; i++)
  26.             niz[i] = nullptr;
  27.  
  28.         string temp;
  29.  
  30.         for(int i = 0; i < n; i++){
  31.             getline(cin, temp);
  32.  
  33.             niz[i] = new char[temp.length()+1];
  34.  
  35.             *copy(temp.begin(), temp.end(), niz[i]) = '\0';
  36.         }
  37.  
  38.  
  39.  
  40.  
  41.  
  42.                 std::sort (niz, niz+n, fjakriterija);
  43.  
  44.  
  45.  
  46.         for(int i = 0; i< n; i++){
  47.             cout << i << ": " << niz[i] << endl;
  48.         }
  49.  
  50.         for(int i = 0; i < n; i++)
  51.             delete[] niz[i];
  52.         delete[] niz;
  53.         niz = nullptr;
  54.  
  55.     }catch(bad_alloc){
  56.  
  57.         if(niz != nullptr){
  58.             for(int i = 0; i < n; i++)
  59.                 delete[] niz[i];
  60.  
  61.             delete[] niz;
  62.         }
  63.  
  64.         cout << "Doslo je do greske s memorijom. Hvala!";
  65.     }
  66.  
  67.  
  68.  
  69.  
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement