Wednesday, 27 October 2010

ARRAY DINAMIS

prak 5
s

// exception classes for various error types

#ifndef Xcept_
#define Xcept_

#include <except.h>
#include <new.h>

// bad initializers
class BadInitializers {
   public:
      BadInitializers() {}
};

// insufficient memory
class NoMem {
   public:
      NoMem() {}
};

// change new to throw NoMem instead of xalloc
void my_new_handler()
{
   throw NoMem();
};

new_handler Old_Handler_ = set_new_handler(my_new_handler);

// improper array, find, insert, or delete index
// or deletion from empty structure
class OutOfBounds {
   public:
      OutOfBounds() {}
};

// use when operands should have matching size
class SizeMismatch {
   public:
      SizeMismatch() {}
};

// use when zero was expected
class MustBeZero {
   public:
      MustBeZero() {}
};

// use when zero was expected
class BadInput {
   public:
      BadInput() {}
};

#endif




program awal



#include <cstdlib>
#include <iostream>
#include "xcept.h"

using namespace std;
template<class T>
class Array1D
{
     // friend ostream& operator<<(ostream&, const Array1D<T>&);

      public:
             Array1D(int size = 0);
             Array1D(const Array1D<T>& v);
             ~Array1D() {delete[] element;}
             T& operator [](int i) const;
             int Size(){return size;}
             Array1D<T>& operator=(const Array1D<T>& v);
             Array1D<T> operator+()const;
             Array1D<T> operator+(const Array1D<T>& v)const;
             Array1D<T> operator-()const;
             Array1D<T> operator-(const Array1D<T>& v)const;
             Array1D<T> operator*(const Array1D<T>& v)const;
             Array1D<T> operator+=(const T& x);
             Array1D<T>& ReSize (int sz);
             void  geser_kiri();
             void  geser_kanan();
      private:
            int size;
            T*element;
            };
            template<class T>
            Array1D<T>::Array1D(int sz)
            {
            if (sz < 0)throw BadInitializers();
              size =sz;
            element = new T[sz];
            }
            template<class T>
            Array1D<T>::Array1D(const Array1D<T>& v)
            {
            size = v.size;
            element = new T[size];
            for (int i = 0; i<size; i++)
            element [i]= v.element[i];
            }
            template<class T>
            T& Array1D<T>::operator[](int i) const
            {
            if (i<0 || i >=size)throw OutOfBounds();
            return element[i];
            }
            template<class T>
            Array1D<T>&Array1D<T>::operator=(const Array1D<T>& v)
            {
            if (this != &v){
            size= v.size;
            delete[] element;
            element = new T[size];
            for (int i=0 ;i<size; i++)
            element[i]= v.element[i];
            }
            return*this;
            }
            template<class T>
            Array1D<T>Array1D<T>::operator+(const Array1D<T>& v)const
            {
            if (size != v.size) throw SizeMismatch();
            Array1D<T> w(size);
            for (int i =0; i<size;i++)
            w.element[i]=element[i]+v.element[i];
            return w;
            }
            template<class T>
            Array1D<T>Array1D<T>::operator-(const Array1D<T>& v)const
            {
            if (size != v.size) throw SizeMismatch();
            Array1D<T> w(size);
            for (int i =0; i<size;i++)
            w.element[i]=element[i]-v.element[i];
            return w;
            }
            template<class T>
            Array1D<T>Array1D<T>::operator-()const
            {
            Array1D<T> w(size);
            for (int i =0; i<size;i++)
            w.element[i]=-element[i];
            return w;
            }
            template<class T>
            Array1D<T>Array1D<T>::operator*(const Array1D<T>& v)const
            {
            if (size != v.size) throw SizeMismatch();
            Array1D<T> w(size);
            for (int i =0; i<size;i++)
            w.element[i]=element[i]* v.element[i];
            return w;
            }
            //template<class T>
            Array1D<T>Array1D<T>::operator+=(const T& x)
            {
            for (int i =0; i<size; i++)
            element[i] + = x;
            return* this;
            }
            //template<class T>
            ostream& operator<<(ostream& out, const Array1D<T>& x)
            {
            for (int i =0; i< x.size; i++)
            out<<x.element[i]<<" ";
            return out;
            }
            //template<class T>
            Array1D<T>Array1D<T>::ReSize (int sz)
            {
            if (sz<0) throw BadInitializers();
            size = sz;
            element = new T[size];
            return* this;
            }
          
            void main(void)
            {
            try {
            Array1D<int>x(10),y,z;
            for(int i=0;i<20;i++)
            x[i]=i;
            cout<<"X[3]="<<x[3]<<endl;
            cout<<"X is"<<x<<endl;
            y=x;
            cout<<"Y is"<<y<<endl;
            x+=2;
            cout<<"x incremented by 2 is"<<x<<endl;
            z=(y+x)*y;
            cout<<"(y+x)*y is"<<z<<endl;
            cout<<"-(y+x)*y is"<<-z<<endl;
            }
            catch(...){
                 cerr<<"An exception has occured"<<endl;}
                 }
                                   
{
    system("PAUSE");
    return EXIT_SUCCESS;
}

No comments:

Post a Comment