Advertisement
neogz

TEM - template klase niz - unos ispis i najmanji clan

Sep 3rd, 2014
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.19 KB | None | 0 0
  1. /*
  2.     Napišite  program  u  kojem  ćete  korisniku  omogućiti  da  unese  i  ispiše  elemente  niza
  3.         različitih  tipova,  te  da  pronađe  najmanji  element  u  nizu.  Omogućite  korisniku  da    sam
  4.     odluči o broju elemenata niza, te da izabere između:
  5.         -  niza cijelih brojeva
  6.         -  niza realnih brojeva
  7.         -  niza karaktera
  8.     Upotrijebite predloške funkcija.
  9. */
  10. #ifdef _MSC_VER
  11. #define _CRT_SECURE_NO_WARNINGS
  12. #endif
  13. #include <iostream>
  14. using namespace std;
  15.  
  16. char crt[] = "\n--------------------------------------------------\n";
  17.  
  18. template <class T>
  19. void unos(T niz[], int max)
  20. {
  21.     cout << crt;
  22.     for (size_t i = 0; i < max; i++)
  23.     {
  24.         cout << i + 1 << " -> ";
  25.         cin >> niz[i];
  26.     }
  27.  
  28. }
  29. template <class T>
  30. void ispis(T niz[], int max){
  31.     for (int i = 0; i < max; i++)
  32.     {
  33.         cout << niz[i] << "\t";
  34.     }
  35.     cout << crt;
  36. }
  37. template <class T>
  38. T najmanjiunizu(T niz[], int max){
  39.     T * naj = &niz[0];
  40.     for (int i = 1; i < max; i++){
  41.         if (niz[i] < *naj)
  42.             naj = &niz[i];
  43.     }
  44.     return *naj;
  45. }
  46. int main() {
  47.    
  48.     int max;
  49.     char izbor;
  50.     cout << "Koliko elemenata ce imati niz: ";
  51.     cin >> max;
  52.  
  53.     cout << "c- char; i - int; f - float" << endl;
  54.     cin >> izbor;
  55.  
  56.     switch (izbor){
  57.     case 'c':
  58.     case 'C':
  59.     {
  60.                 char * pok = new char[max];
  61.                 cout << "Unesi elemente niza: ";
  62.                 unos(pok, max);
  63.  
  64.                 cout << crt << "Vas niz je: " << crt;
  65.                 ispis(pok, max);
  66.                    
  67.                 cout << crt << "Najmanji clan niza je: " << najmanjiunizu(pok,max);
  68.                 delete pok;
  69.                 pok = nullptr;
  70.                 break;
  71.     }
  72.         case 'i':
  73.         case 'I':
  74.         {
  75.                     int * pok = new int[max];
  76.                     cout << "Unesi elemente niza: ";
  77.                     unos(pok, max);
  78.  
  79.                     cout << crt << "Vas niz je: " << crt;
  80.                     ispis(pok, max);
  81.  
  82.                     cout << crt << "Najmanji clan niza je: " << najmanjiunizu(pok, max);
  83.                     delete pok;
  84.                     pok = nullptr;
  85.                     break;
  86.         }
  87.         case 'f':
  88.         case 'F':
  89.         {
  90.                     float * pok = new float[max];
  91.                     cout << "Unesi elemente niza: ";
  92.                     unos(pok, max);
  93.  
  94.                     cout << crt << "Vas niz je: " << crt;
  95.                     ispis(pok, max);
  96.  
  97.                     cout << crt << "Najmanji clan niza je: " << najmanjiunizu(pok, max);
  98.                     delete pok;
  99.                     pok = nullptr;
  100.                     break;
  101.         }
  102.     }
  103.     system("pause>null");
  104.     return 0;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement