Skip to content

praktikum 5

Oktober 26, 2010

#include
#include
//#include ‘xcept.h’

using namespace std;
//template
class Array1D {

friend ostream& operator (istream&, Array1D&);

public :

Array1D ( int size= 0);
Array1D ( const Array1D& v);
~Array1D (){delete [] element;}
int& operator [] (int i) const;
int Size() {return size;}
Array1D & operator = (const Array1D& v);
Array1D operator + ()const;
Array1D operator + (const Array1D& v)const;
Array1D operator – ()const;
Array1D operator – (const Array1D& v) const;
Array1D operator * (const Array1D& v) const;
Array1D & operator+=(const int& x);
Array1D & ReSize(int sz);
void geser_kiri();
void geser_kanan();

private :
int size ;
int * element ;
};

//template
Array1D::Array1D(int sz)
{
if ( sz < 0);
size = sz;
element = new int[sz];
}

//template
Array1D::Array1D(const Array1D& v)
{
size = v.size;
element =new int[size]; // get space
for (int i=0; i<size; i++)// copy element
element[i] = v.element[i];
}

//template
int& Array1D::operator[](int i)const
{
if(i=size); //throw OutOfBounds();
return element[i];
}
//template
Array1D & Array1D::operator =(const Array1D& v)
{
if (this != &v){
size= v.size;
delete[] element;
element = new int[size];
for (int i=0; i< size; i++)
element [i] = v.element[i];
}
return *this;
}

//template
Array1D Array1D::operator +(const Array1D& v) const
{
if (size != v.size); //throw SizeMismatch();
Array1D w(size);
for(int i=0; i < size; i++)
w.element[i] = element[i] + v.element [i];
return w;
}

//template
Array1D Array1D::operator-(const Array1D& v) const
{
if (size!= v.size); //throw SizeMistach();
Array1D w(size);
for (int i=0; i<size; i++)
w.element[i] = element [i]- v.element [i];
return w;
}

//template
Array1D Array1D::operator-() const
{
Array1D w(size);
for (int i=0; i <size; i++)
w.element[i] = -element[i];
return w;
}

//template
Array1D Array1D::operator*(const Array1D& v)const
{
if (size != v.size);//throw SizeMistach();
Array1D w(size);
for(int i=0; i < size; i++)
w.element[i] = element [i] * v.element[i];
return w;
}

//tempalte
Array1D& Array1D::operator+=(const int& x)
{
for (int i=0; i < size; i++)
element [i] += x;
return *this;
}

//tempalte
ostream& operator <<( ostream& out, const Array1D& x)
{
for (int i=0; i<x.size; i++)
out << x.element[i] <=0; i–)
element[i+1]=element[i];
element[0]=temp;
}

void Array1D::geser_kiri()
{
int n= 10;
int temp= element[0];
for(int i=0;i<n;i++)
element[i]=element[i+1];
element[n-1]= temp;
}

//template
Array1D& Array1D::ReSize(int sz)
{
if (sz < 0);//throw BadInitializer();
delete [] element;
size = sz;
element = new int [size];
return *this;
}

int main(int argc, char *argv[])
{
try {
Array1D X(10),Y,Z;
for (int i=0; i<10; 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 4 is " << X << endl;
Z = (Y+X)*Y;
cout << "(Y + X)* Y is" << Z << endl;
cout << "-(Y + X)* y is" << -Z << endl;
Z.geser_kiri();
cout<<"isi Array seteleh digeser ke kiri;"<<Z<<endl;
Z.geser_kanan();
cout<<"Isi Array setelah digeser ke kanan:"<<Z<<endl;

}
catch (…) {
cerr << "An execption has occurred" << endl;}

system("PAUSE");
return EXIT_SUCCESS;
}

From → Praktikum

Tinggalkan sebuah Komentar

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: