RSS

postest prak5 sdata, rabu09.00

27 Okt

Implementasi kelas array dinamis

Kelas dipecah menjadi 3 file :

1. file “xcept.h”

<pre>class BadInitializers {

public:

BadInitializers() {}

};

class NoMem {

public:

NoMem() {}

};

void my_new_handler()

{

throw NoMem();

};

class OutOfBounds {

public:

OutOfBounds() {}

};

class SizeMismatch {

public:

SizeMismatch() {}

};

class MustBeZero {

public:

MustBeZero() {}

};

class BadInput {

public:

BadInput() {}

};

2. file “array.h”

 
#include <iostream>
 
#include "xcept.h"
 
using namespace std;
 
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);
 
Array1D& geser_kanan();
 
Array1D& geser_kiri();
 
private:
 
int size;
 
int* element;
 
};
 
Array1D::Array1D(int sz){
 
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{
 
return element[i];
 
}<!--more-->
 
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{
 
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{
 
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{
 
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){
 
delete[]element;
 
size=sz;
 
element=new int[size];
 
return* this;
 
}
 
Array1D& Array1D::geser_kanan()
 
{
 
int n = size;
 
int temp = element[n-1];
 
for (int i=n-1; i>=0; i--)
 
element[i+1]=element[i];
 
element[0]=temp;
 
}
 
Array1D& Array1D::geser_kiri()
 
{
 
int n=size;
 
int temp = element[0];
 
for(int i=0; i<n; i++)
 
element[i]=element[i+1];
 
element[n-1]=temp;
 
}

3. file “main.cpp”

</span>
<span style="font-family: Georgia, 'Times New Roman', 'Bitstream Charter', Times, serif; line-height: 19px; white-space: normal; font-size: 13px;">#include<iostream></span></pre>
#include "array1d.h"

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;

cout<<endl;

Y=X;

cout << "Y is " << Y << endl;

cout<<endl;

X += 2;

cout << "X increment by 2 is " << X << endl;

Z=(Y+X)*Y;

cout << "(Y+X)*Y is " << Z << endl;

cout<<endl;

cout << "-(Y+X)*Y is " << -Z << endl;

cout<<endl;

cout<<"setelah digeser kekanan :"<<(Y-X)*Y<<endl;

cout<<endl;

cout<<"setelah digeser kekiri  :"<<(Y+X)<<endl;

cout<<endl;

}

catch(...){

cerr << "An Exception has occured" << endl;}

system("PAUSE");

return EXIT_SUCCESS;

}

hasil running



 
4 Komentar

Ditulis oleh pada Oktober 27, 2010 in praktikum

 

Tag:

4 responses to “postest prak5 sdata, rabu09.00

  1. iruel

    Oktober 28, 2010 at 6:37 pm

    hasil runningnya mana mbak arrum,,,,,liatin donk,, soalnya prognya ga jalan….!!! bantuin donk…!!!

     
    • faiznoer

      Oktober 29, 2010 at 2:26 pm

      Buat irul…lo pgen prgram yg jlan ikuti cara dr blog : http://faiznoer.wordpress.com/
      hehe..

       
      • aium23

        Oktober 29, 2010 at 2:31 pm

        aseem faizz…
        pnya q jugga jdi kog….huhuhuhu….

         
    • aium23

      Oktober 29, 2010 at 2:37 pm

      kamu buka ajja link “hasil running”

      pi biz itu gag usah banyak koment…hahahahahaha,…

       

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: