Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.28 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. template<typename T>class Tablica{
  7.     private:
  8.         T* tablica;
  9.         int IloscZapisanychElementow;
  10.         int IloscElementow;
  11.     public:
  12.         Tablica(int n){
  13.             if(n>0){
  14.                 IloscElementow =n;
  15.                 tablica = new T[IloscElementow];
  16.                
  17.                 for(int i=0;i<IloscElementow;i++){
  18.                     tablica[i]=0;
  19.                 }                  
  20.             }
  21.         }
  22.        
  23.         void WstawElement(T element){
  24.             if(IloscZapisanychElementow == IloscElementow){
  25.                
  26.                 string wyjatek="Brak Miejsca w Tablicy";
  27.                 throw wyjatek; 
  28.                
  29.             }else{
  30.                
  31.                 for(int i=0;i<IloscElementow;i++){
  32.                     if(tablica[i] == 0){
  33.                         tablica[i]= element;
  34.                         IloscZapisanychElementow++;
  35.                         break;
  36.                     }
  37.                 }                      
  38.             }
  39.         }
  40.        
  41.         int Ile(){
  42.             return IloscElementow;
  43.         }
  44.        
  45.         T Max(){
  46.             T temp = tablica[0];
  47.             for(int i=1;i<IloscElementow;i++){
  48.                 if(temp<tablica[i]){
  49.                     temp = tablica[i];
  50.                 }  
  51.             }
  52.             cout<<"Najwiekszy element to "<< temp<<endl;
  53.             return temp;
  54.         }
  55.        
  56.         T Min(){
  57.             T temp = tablica[0];
  58.             for(int i=1;i<IloscElementow;i++){
  59.                 if(tablica[i] != 0){
  60.                     if(temp>tablica[i]){
  61.                         temp = tablica[i];
  62.                     }                      
  63.                 }
  64.             }
  65.             cout<<"Najmniejszy element to "<< temp<<endl;
  66.             return temp;
  67.         }
  68.        
  69.        
  70.         void Wyswietl(){
  71.             for(int i=0;i<IloscElementow;i++){
  72.                 cout<<"Element nr "<<i+1<<" = "<<tablica[i]<<endl;
  73.             }
  74.         }
  75.        
  76.         void Sortuj()
  77.         {
  78.             for( int i = 0; i < IloscElementow; i++ )
  79.             {
  80.                 for( int j = 0; j < IloscElementow - 1; j++ )
  81.                 {
  82.                     if( tablica[ j ] > tablica[ j + 1 ] )
  83.                          swap( tablica[ j ], tablica[ j + 1 ] );
  84.                 }
  85.             }
  86.         }
  87.        
  88.         T operator[](const int idx)
  89.         {
  90.             if (idx >= 0 && idx < IloscElementow)
  91.             {
  92.               return tablica[idx];
  93.             }else{
  94.                 string wyjatek ="Nie poprawny argument";
  95.                  throw wyjatek;
  96.           }
  97.         }
  98.        
  99. };
  100.  
  101.  
  102. int main()
  103. {
  104.     Tablica<int> tabInt(10);
  105.     tabInt.WstawElement(5);
  106.     tabInt.WstawElement(7);
  107.     tabInt.Wyswietl();
  108.     tabInt.Max();
  109.     cout<<tabInt[1];     //operator indeksowania
  110.    
  111.     Tablica<double> tabDouble(10);
  112.     tabDouble.WstawElement(9.14);
  113.     tabDouble.WstawElement(3.14);
  114.     tabDouble.WstawElement(8.14);
  115.     tabDouble.Sortuj();
  116.     tabDouble.Wyswietl();
  117.     tabDouble.Min();
  118.    
  119.     system("PAUSE");
  120.     return 0;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement