Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdlib>
- #include <iostream>
- using namespace std;
- template<typename T>class Tablica{
- private:
- T* tablica;
- int IloscZapisanychElementow;
- int IloscElementow;
- public:
- Tablica(int n){
- if(n>0){
- IloscElementow =n;
- tablica = new T[IloscElementow];
- for(int i=0;i<IloscElementow;i++){
- tablica[i]=0;
- }
- }
- }
- void WstawElement(T element){
- if(IloscZapisanychElementow == IloscElementow){
- string wyjatek="Brak Miejsca w Tablicy";
- throw wyjatek;
- }else{
- for(int i=0;i<IloscElementow;i++){
- if(tablica[i] == 0){
- tablica[i]= element;
- IloscZapisanychElementow++;
- break;
- }
- }
- }
- }
- int Ile(){
- return IloscElementow;
- }
- T Max(){
- T temp = tablica[0];
- for(int i=1;i<IloscElementow;i++){
- if(temp<tablica[i]){
- temp = tablica[i];
- }
- }
- cout<<"Najwiekszy element to "<< temp<<endl;
- return temp;
- }
- T Min(){
- T temp = tablica[0];
- for(int i=1;i<IloscElementow;i++){
- if(tablica[i] != 0){
- if(temp>tablica[i]){
- temp = tablica[i];
- }
- }
- }
- cout<<"Najmniejszy element to "<< temp<<endl;
- return temp;
- }
- void Wyswietl(){
- for(int i=0;i<IloscElementow;i++){
- cout<<"Element nr "<<i+1<<" = "<<tablica[i]<<endl;
- }
- }
- void Sortuj()
- {
- for( int i = 0; i < IloscElementow; i++ )
- {
- for( int j = 0; j < IloscElementow - 1; j++ )
- {
- if( tablica[ j ] > tablica[ j + 1 ] )
- swap( tablica[ j ], tablica[ j + 1 ] );
- }
- }
- }
- T operator[](const int idx)
- {
- if (idx >= 0 && idx < IloscElementow)
- {
- return tablica[idx];
- }else{
- string wyjatek ="Nie poprawny argument";
- throw wyjatek;
- }
- }
- };
- int main()
- {
- Tablica<int> tabInt(10);
- tabInt.WstawElement(5);
- tabInt.WstawElement(7);
- tabInt.Wyswietl();
- tabInt.Max();
- cout<<tabInt[1]; //operator indeksowania
- Tablica<double> tabDouble(10);
- tabDouble.WstawElement(9.14);
- tabDouble.WstawElement(3.14);
- tabDouble.WstawElement(8.14);
- tabDouble.Sortuj();
- tabDouble.Wyswietl();
- tabDouble.Min();
- system("PAUSE");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement