This is default featured post 1 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured post 2 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured post 3 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured post 4 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured post 5 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

Minggu, 31 Oktober 2010

Program Stack

#include <cstdlib>
#include <iostream>
using namespace std;
class Stack{
public:
Stack(){
jum = 5;
last = 0;
}
void push(int data){
if(jum > last){
a[last] = data;
last = last + 1;
}
}
int pop(){
if(last > 0){
int temp = a[last - 1];
a[last - 1] = 0;
last = last – 1;
return temp;
}

Tugas Struktur Data ( Antrian Queue)

#include <cstdlib>
#include <iostream.h>
#define maks 5
//using namespace std;

class Queue{
friend ostream& operator << (ostream&, const Queue&);
//friend istream& operator >> (istream&, Queue&);
public :
Queue();
int penuh(int);
int kosong(int);
void cetak();
void enqueue(char);
char dequeue();
private :
int banyak;
char A[maks];
};

ostream& operator << (ostream& out, const Queue& s)
{
cout <<”\nIsi Queue :”;

for (int i=0; i<s.banyak; i++)
out << s.A[i] << ” “;
cout<<endl;
}

Queue::Queue(){
banyak = 0;
for (int i=0; i<maks; i++)
A[i]=’0′;
}


int Queue::penuh(int s)
{return s==maks?1:0;}

int Queue::kosong(int s)
{return s==0?1:0;}

void Queue::cetak()
{cout <<”\nIsi dalam Queue sekarang   :”;
for (int i=0; i<banyak; i++)
cout <<A[i]<<” “;
}
void Queue::enqueue(char x)
{
cout <<”\nElemen :” <<x<<”  Masuk pada antrian”;
if (penuh(banyak)) cout <<”  queue penuh”;
else if(A[0]==’0′){
A[0]=x;
banyak++;
}
else{
for (int i=banyak; i>=0; i–)
A[i+1]=A[i];
A[0]=x;
banyak++;
}
}

char Queue::dequeue()
{
char temp=A[--banyak];
cout <<”\nDequeue elemen –>” <<temp;
A[banyak]=’0′;
return temp;
}

main()
{
Queue q;
for (char c=’a'; c<’d'; c++)
{
q.enqueue(c);
cout <<q;
}
char p=q.dequeue();
q.cetak();
cout <<”\n\nCetak menggunakan overloading:” <<q;
cout<<endl;

system(“PAUSE”);
return EXIT_SUCCESS;
}

Rabu, 27 Oktober 2010

Praktikum SDATA 5 , rabu jam 09.00

#include <iostream.h>
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);
       Array1D<T>& geser_kanan();
       Array1D<T>& geser_kiri();
private:
        int size;
        T*element;
};
template <class T>
Array1D<T>::Array1D(int sz)
{
                         size =sz;
                         element=new T[sz];
}
template <class T>
Array1D<T>::Array1D(const Array1D<T>& v)
{
                           size = v.size;
                           element=new T[size]; // get space
                           for (int i=0;i<size;i++)// copy elements
                           element[i]=v.element[i];
}
template <class T>
T& Array1D<T>::operator[](int i)const
{
                          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
{
             Array1D<T>w(size);
             for(int i=0; i<size;i++)
             w.element[i]=element[i]- v.element[i];
             return w;
}

Praktikum SDATA 4 , rabu jam 09.00

#include <cstdlib>

#include <iostream>
#define maks5

using namespace std;

class Array1D{
      friend ostream& operator<<(ostream&, const Array1D&);
      friend istream& operator>>(istream&, Array1D&);
public:
       Array1D();
       void cetak();
       void geser_kiri();
       void geser_kanan();
       void hapus_elemen();
       void urut();
private:
       char A[5];
};

//konstrukstor
Array1D::Array1D(){
       for(int i=0;i<5;i++)
       A[i]='O';
}


void Array1D::cetak(){
       for(int i=0;i<5;i++)
       cout<<A[i]<<" ";
}

//overloading fungsi cetak
ostream& operator<<(ostream& out, const Array1D& x){
         for(int i=0;i<5;i++)
         out<<x.A[i]<<" ";
         out<<endl;
         return out;
}

//overloading input
istream& operator>>(istream& in, Array1D& x){
       
        for(int i=0;i<5;i++){
        cout<<"masukkan nilai array ke-"<<i+1<<" : ";
        in>>x.A[i];
}
                
        return in;
}

//fungsi untuk menggeser semua nilai 1 indeks ke kanan
void Array1D::geser_kanan(){
     int n=5;
     int temp=A[n-1];
     for(int i=n-1;i>=0;i--)
     A[i+1]=A[i];
     A[0]=temp;
}

//fungsi untuk menggeser semua nilai 1 indeks ke kiri
void Array1D::geser_kiri(){
     int n=5;
     int temp=A[0];
     for(int i=0;i<n;i++)
     A[i]=A[i+1];
     A[n-1]=temp;
}

//menthods yang di tambahkan untuk menghapus nilai dari indeks tertentu
void Array1D::hapus_elemen(){
     int posisi;
     cout<<"\npilih indeks berapa yang akan di hapus :\n";
     cin>>posisi;
     if(posisi>0 && posisi<=5)
     A[posisi-1]='O';
     else cout<<"indeks hanya terdiri dari 1 - 5\n";
}

//main fingsi
int main(int argc, char *argv[])
{
    Array1D x;
    cout<<"Array masih kosong : "<<x; //pemanggilan overloading output
    cin>>x; //penggunaan overloading input
    cout<<"Isi Array saat ini : "<<x;
    x.geser_kiri();  //penggunaan fingsi
    cout<<"Isi Array setelah di geser kiri : "<<x;
    x.geser_kanan();
    cout<<"Isi Array setelah di geser kanan : "<<x;
    cout<<"Urutan elemen pada indeksnya saat ini : "<<x;
    x.hapus_elemen();
    cout<<"Setelah dihapus menjadi : "<<x;
  
    system("PAUSE");
    return EXIT_SUCCESS;
}

PRAKTIKUM SDATA 3 , rabu jam 09.00

#include <cstdlib>
#include <iostream>

using namespace std;

class Bilangan{
friend ostream& operator << (ostream&, const Bilangan&);
friend istream& operator >> (istream&, Bilangan&);

public:
Bilangan(int a0=0, float b0=0.0) : a(a0), b(b0){}
void banding_int(const Bilangan&, const Bilangan&);
Bilangan& operator=(const Bilangan&);
Bilangan operator+(const Bilangan&) const;
Bilangan operator-()const;
// protected:
int a;
float b;
};

ostream& operator << (ostream&out, const Bilangan& x){
out << “Bagian integer : ” << x.a << endl;
out << “Bagian float : ” << x.b << endl;
return out;
}

Praktikum SDATA 2 , rabu jam 09.00

#include <iostream.h>
template <class T>
class kompleks {
 friend class operasi;
 friend ostream& operator<<(ostream&, const kompleks<T>&) ;
 friend istream& operator>>(istream&, kompleks<T>&) ;
public :
 kompleks(T s=0, T +=0): a(s), b(+){};
 void cetak();
 kompleks operator-();
 kompleks operator-(const kompleks<T>&);
 kompleks operator+(const kompleks<T>&);
private :
 T a;
 T b;
};

template <class T>
void kompleks<T>::cetak()
{
 if(b>0)cout<<"bilangan kompleks : " << a<<"+"<<b<<"i";
 else cout<<"bilangan kompleks : " <<a<<b<<"i";
 cout<< endl;
 }

template <class T>
kompleks<T>kompleks<T>::operator-()
{
kompleks x;
x.a=a;
x.b=-b;
return x;
}

template <class T>
kompleks<T>kompleks<T>::operator-(const kompleks<T>& m)
{
kompleks x;
x.a=a - m.a;
x.b=b - m.b;
return x;
}

template <class T>
kompleks<T> kompleks<T>::operator+(const kompleks<T>& m)
{
kompleks x;
x.a = a + m.a;
x.b = b + m.b;
return x;
}

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More