//
you're reading...
struktur data

Praktikum V Struktur Data

pada praktikum kali ini membutuhkan file header “xcept.h”, berikut adalah source code-nya :

#ifndef __xcept

#define __xcept

// exception classes for various error types

// 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

 

Berikut ini adalah source code kelas array 1D

class Array1D{

friend ostream& operator<<(ostream&, const 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);

private:

int size;

int* element;

};

Array1D::Array1D(int sz){

if(sz<0)throw BadInitializers();

size=sz;

element=new int[sz];

}

Array1D::Array1D(const Array1D& v){

size=v.size;

element=new int[size];

for(int i=0; i<size; i++)

element[i]=element[i];

}

int& Array1D::operator[](int i)const{

if(i<0||i>=size)throw OutOfBounds();

return element[i];

}

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]=element[i];

}

return* this;

}

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;

}

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;

}

Array1D Array1D::operator-()const{

Array1D w(size);

for(int i=0; i<size; i++)

w.element[i]=-element[i];

return w;

}

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;

}

Array1D& Array1D::operator+=(const int& x){

for(int i=0; i<size;i++)

element[i]+=x;

return* this;

}

ostream& operator<<(ostream& out, const Array1D& x){

for(int i=0; i<x.size; i++)

out << x.element[i] << ” “;

return out;

}

Array1D& Array1D::Resize(int sz){

if(sz<0) throw BadInitializers();

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 increment 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;

}

About kusukaitu

nothing special..

Diskusi

Belum ada 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: