Laman

Powered By Blogger

Selasa, 25 Desember 2012

antrian prioritas (priority queue)

priority queue or a antrian prioritas, where each element of higher value will go and queue in front of the smaller, in the lab, I was asked to menginputkan queue manually, and not consecutively. after which the program will automatically sort the corresponding value of the character in the enqueue. This is similar to the way sorting. ie comparing the elements of the new entrance to the front element, if the element has a value greater then the element will be shifted left fence to meet a higher value.

#include <cstdlib>
#include <iostream>
#define maks 5

using namespace std;

class Queue{
friend ostream& operator<<(ostream&, const Queue&);
public :
Queue();
int penuh(int);
int kosong(int);
void cetak();
void enqueue();
char dequeue();

private :
char A[maks];
int banyak;
char x;
};

ostream& operator<<(ostream& out, const Queue& s){
cout<<”\nIsi Quueue sebanyak : “<<s.banyak<<” yaitu : “;
for(int i=0;i<s.banyak;i++)
out<<s.A[i]<<” “;
return out;
}

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 Queue : “;
for(int i=0;i<banyak;i++)
cout<<A[i]<<” “;
}

void Queue::enqueue(){
cin>>x;
cout<<”Elemen :”<<x<<” masuk antrian”;
if(penuh(banyak))cout<<”queue penuh “;
else if(A[0]==’0′){
A[0]=x;
banyak++;
}
else{
int tempat=0;
while(A[tempat]>x)tempat++;
if(banyak!=tempat)
for(int i=banyak;i>=tempat;i–)
A[i+1] = A[i];
A[tempat]=x;
banyak++;
}
}

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

int main(int argc, char *argv[])
{
Queue p;
for(int i=1;i<=5;i++){
cout<<”masukan elemnt :”;p.enqueue();
cout<<endl;
}
cout<<p;

for(int i=1;i<=5;i++){
p.dequeue();cout<<p;
if(i==5) cout<<”\n\n\n element kosong”;
cout<<endl;
}
system(“PAUSE”);
return EXIT_SUCCESS;
}


Queue (antrian)

Queue (queue) is a data structure where data is first put the data first be removed. Or it could be called a data structure that uses the mechanism of FIFO (First In First Out).
queue

#include <iostream.h>
#include <conio.h>

#define maks 5

class Queue{
friend ostream& operator<<(ostream&, const Queue&);

public :
Queue();
int penuh(int);
int kosong(int);
void cetak();
void enqueue(char);
char dequeue();
private:
char A[maks];
int banyak;
};
ostream& operator<<(ostream& out, const Queue& s){
cout<<”\nIsi Queue : “;
for(int i;i<s.banyak;i++)
out<<s.A[i]<<” “;
return out;}

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 Queue : “;
for(int i=0;i<banyak;i++)
cout<<A[i]<<” “;
}

void Queue::enqueue( char x){
cout<<”\nElemen “<<x<<” masuk 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);
}
q.cetak();
char p=q.dequeue();
q.cetak();
cout<<”\n\nCetak pakai overloading “<<q;
getch();
return 0;

}

Stack atau Tumpukan (stack or the heap)

stack or the heap, this time the task qw just make a check brackets. if the character is a parenthesis then 1 character will be inserted into the stack and remove the parentheses when you meet the character to the stack, so that we can know whether parentheses akhrir we balanced input. Caran do it easily, eg given manually via a keyboard character is (a + (b / c)) we only use the selection, or if. when character = brackets -> no input characters into the stack and show character. or in c + + can be shown if (chart == '(') push (kar) and when the meet mark parentheses -> no characters pop from the stack: if (chart == ')') pop (kar), if it does not meet the second the character then go directly to the next character. Please try.


#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
// file stack.h
// formula-based stack

#ifndef Stack_
#define Stack_

template<class T>
class Stack {
// LIFO objects
public:
Stack(int MaxStackSize = 10);
~Stack() {delete [] stack;}
int IsEmpty() const {return top == -1;}
int IsFull() const {return top == MaxTop;}
T Top() const;
Stack<T>& Add(const T& x);
Stack<T>& Delete(T& x);
private:
int top;    // current top of stack
int MaxTop; // max value for top
T *stack;   // element array
};

template<class T>
Stack<T>::Stack(int MaxStackSize)
{// Stack constructor.
MaxTop = MaxStackSize – 1;
stack = new T[MaxStackSize];
top = -1;
}

template<class T>
T Stack<T>::Top() const
{// Return top element.
if (IsEmpty()) throw OutOfBounds(); // Top fails
else return stack[top];
}

template<class T>
Stack<T>& Stack<T>::Add(const T& x)
{// Add x to stack.
if (IsFull()) throw NoMem(); // add fails
stack[++top] = x;
return *this;
}

template<class T>
Stack<T>& Stack<T>::Delete(T& x)
{// Delete top element and put in x.
if (IsEmpty()) throw OutOfBounds(); // delete fails
x = stack[top--];
return *this;
}

#endif
// match parentheses

#include <iostream.h>
#include <string.h>
#include <stdio.h>

const int MaxLength = 100; // max expression length

void cek_tandakurung(char *expr)
{// Parenthesis matching.
Stack<int> s(MaxLength);
int j, length = strlen(expr);

// scan expression expr for ( and )
for (int i = 1; i <= length; i++) {
if (expr[i - 1] == ‘(‘) s.Add(i);
else if (expr[i - 1] == ‘)’)
try {s.Delete(j);  // unstack match
cout <<”(buka kurung at “<< j <<”)”<< ‘ ‘ <<”(tutup kurung at “<< i <<”)”<< endl;}
catch (OutOfBounds)
{cout << “tanda kurung kelebihan di no”
<< i << endl;}
}

// remaining ( in stack are unmatched
while (!s.IsEmpty()) {
s.Delete(j);
cout << “tanda kurung kelebihan di no “
<< j << endl;}
}

void main(void)
{
char expr[MaxLength];
cout << “masukan sembarang karekter yang mempunyai tanda kurung max (“
<< MaxLength <<”)”<< endl;
cin.getline(expr, MaxLength);
cout <<”karakter yang anda masukan adalah”
<< endl;
puts(expr);
cout<<endl;
cek_tandakurung(expr);
cout<<”\ntanda kurung seimbang “;
}

Array 1 Dimensi

Static array is an array of array [0] is shifted to the left then the element will be lost and the next element will be the first element, for the reason that, in panning slider either left or right, dilai array [0] if geserkiri or array [max] if geserkanan values ​​should be saved first, and newly allocated back to the first element and the last element.

#include <iostream.h>
#define maks 5

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

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

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

ostream& operator<<(ostream& out, const Array1D& x){
for(int i=0;i<maks;i++)
cout<<x.A[i]<<” “;
cout<<endl;
return out;
}

istream& operator>>(istream& in, Array1D& x){
int posisi;
cout<<”Mengisi array pada posisi ke : “;
in>>posisi;
if(posisi>0&&posisi<=maks){
cout<<”Masukan elemen array-nya : “;
in>>x.A[posisi-1];
}
else
cout<<”Anda memasukan posisi diluar range….”;
return in;
}

void Array1D::geser_kanan(){
int n=maks;
int temp=A[n-1];
for(int i=n-1;i>=0;i–)
A[i+1]=A[i];
A[0]=temp;
}

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

int main(){
Array1D x;
cout<<”Array masih kosong : “<<x;
cin>>x;
cout<<”isi array saat ini : “<<x;
x.geser_kiri();
cout<<”isi array setelah digeser kekiri :”<<x;
x.geser_kanan();
cout<<”isi array setelah digeser kekanan :”<<x;
return 0;
}

inheritance or inheritance

inheritance or inheritance, a class can have kids classes. and properties owned or inherited by the father of her children, which is in the public and protected, private parts can only be used by classes that have it.

class Bil_float : public Bilangan {
friend istream& operator>>(istream&, Bil_float&);
friend ostream& operator<<(ostream&, const Bil_float&);
private :
float a;
public :
Bil_float(float x=0.0) : a(x){}
void banding_float(const Bil_float& y, const Bil_float& z){
if(y.a>z.a) cout<<y.a<<”:: y lebih besar dari “<<z.a<<”::z”;
else cout<<y.a<<”::y lebih kecil dari”<<z.a<<”::z”;
}
};

istream& operator>>(istream& in, Bil_float& masuk){
cout<<”masukan bilangan”; in>>masuk.a;
return in;
}

ostream& operator<<(ostream& out, const Bil_float& keluar){
out<<”tampilkan bilangan”<<keluar.a;
return out;
}



operators - operator overloading

at this time I will discuss the use of operators - operator overloading.

Overloading is the use of multiple methods or properties of the same name, but has a list of parameters / arguments are different. The difference in question is a different number of parameters, different data types, or different from both (the number of parameters and data types). Methods or properties are just different return value (return value) can not be said to be overloading.












#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;
private:
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;
}

void Bilangan::banding_int(const Bilangan& x, const Bilangan& y)
{
if (x.a > y.a) cout << x.a << “::x lebih besar dari ” << y.a << “::y”;
else cout << x.a << “::x lebih kecil dari ” << y.a << “::y”;
}

Bilangan& Bilangan::operator=(const Bilangan& x)
{   a = x.a;
b = x.b;
return *this;
}

istream& operator>>(istream& in, Bilangan& x)
{
cout <<”\nMasukkan bagian integer : “;
in >> x.a;
cout <<”Masukkan bagian float   : “;
in >> x.b;
return in;
}

Bilangan Bilangan::operator+(const Bilangan& x) const
{   Bilangan cc;
cc.a = a + x.a;
cc.b = b + x.b;
return cc;
}

Bilangan Bilangan::operator-() const
{   Bilangan x;
x.a = -a;
x.b = -b;
return x;
}

int main(int argc, char *argv[])
{
Bilangan s, t(-2,3.14), d;
cout << “Nilai awal s\n” << s;
cout << “Nilai awal t dari deklarasi\n” << t;
s = t;
cout << “Setelah s di-assign t\n”;
cout << “Nilai s\n” << s;
cout << “Masukkan nilai-nilai objek d”;
cin >> d;
cout << “Setelah d + t => \n” << d+t;
cout << “Nilai d dinegatifkan\n” << -d;

system(“PAUSE”);
return EXIT_SUCCESS;
}

Link List

Link List is a form of dynamic data structures and inter-connect - connect.
Link List is more flexible to use than arrays, because it is dynamic (no limit data), in contrast to the data array has a maximum limit. Link List can be linked to a variable of type pointer.
and below is a sample program:

#include <cstdlib>

#include <iostream>

using namespace std;

class Node{

friend class List;

friend ostream& operator<<(ostream&, const List&);

public:

 Node(char& t, Node* p) : info(t), berikut(p){}

protected:

 char info;

 Node *berikut;

 };

class List{

friend ostream& operator<<(ostream&, const List&);

public:

 List() : kepala(0){}

 ~List();

 void sisip(char t);

 int hapus(char& t);

 int kosong() {return (kepala == 0);}

 void cetak();

protected:

 Node* kepala;

 Node* nodeBaru(char& t,Node* p)

 {Node* q = new Node(t,p); return q;}

 };

ostream& operator<<(ostream& out, const List& k)

{

for(Node* p=k.kepala;p;p=p->berikut)

out << p->info <<" ->";

out << "*\n";

return out;

}

List::~List()

{

Node* temp;

for(Node* p=kepala;p;)

{

temp=p;

p=p->berikut;

delete temp;

}

}

void List::sisip(char t)

{

cout << t << "masuk list:";

Node* p=nodeBaru(t,kepala);

kepala=p;

}

int List::hapus(char& t)

{

if(kosong()) return 0;

t=kepala->info;

Node *p = kepala;

kepala=kepala->berikut;

delete p;

return 1;

}

void List::cetak()

{

for (Node* p = kepala; p; p=p->berikut)

cout << p->info <<" ->";

cout << "*\n";

}

int main(int argc, char *argv[])

{

List x;

char data;

x.sisip('a');

cout << x;

x.sisip('b');

cout << x;

x.sisip('c');

cout << x;

x.sisip('d');

cout << x;

for (int i=0; i<5; i++){

x.hapus(data);

cout << data << " dihapus dari list :";

cout << x;

}

system("PAUSE");

return EXIT_SUCCESS;

}

semoga bermanfaat (^_^)