Sabtu, 05 April 2014

Contoh Program C++ QUEUE

===============================================================





==============================================================
#include <cstdlib>
#include <iostream>
#include <stdio.h>
#define true 1
#define false 0
#define max 5

using namespace std;

struct queue
{
       char info[max];
       int awal,akhir;
};
struct queue antri;

void init(void)
{
 antri.awal=0;
 antri.akhir=0;
}


int full(void)
{
if (antri.akhir==max)
   return(true);
else
    return(false);
}
int empty(void)
{
if (antri.akhir==0)
   return(true);
else
    return(false);
}


void inQueue(char elemen)
{
if(empty()==true)
{
antri.awal=1;
antri.akhir=1;
antri.info[antri.awal]=elemen;
}
else
{
if(full()!=true)
{
antri.akhir++;
antri.info[antri.akhir]=elemen;
}
else
printf("QUEUE overflow");
}
}
char deQueue()
{
char isi;
int i;
if(empty()!=true)
{
isi=antri.info[antri.awal];
for(i=antri.awal;i<=antri.akhir;i++)
antri.info[i]=antri.info[i+1];
antri.akhir--;
return(isi);
}
else
printf("queue underflow.\n");
}

void baca()
{
int i;
if(antri.awal>0&& antri.akhir>0)
{
cout<<"isi antrian:"<<endl;
for (i=antri.awal;i<=antri.akhir;i++)
{
    cout<<antri.info[i];
    cout<<endl;
}
}
else
{
    cout<<"isi antrian Kosong"<<endl;
}
}

void clear()
{ antri.awal=0;
  antri.akhir=0;
}

main()

{   int pil;
    char elm;
    while(1){
    cout<<"Program QUEUE"<<endl;
    cout<<"1.INQUEUE"<<endl;
    cout<<"2.DENQUEUE"<<endl;
    cout<<"3.READ"<<endl;
    cout<<"4.CLEAR"<<endl;
    cout<<"5.EXIT"<<endl;
    cout<<"Input Pilihan =";cin>>pil;
    system("cls");
    if (pil == 1)
    {  cout<<"Input Antrian =";cin>>elm;
       inQueue(elm);}
    else if (pil == 2)
    {  deQueue();}
    else if (pil == 3)
    {baca();}
    else if (pil == 4)
    {clear();}
    else if (pil == 5)
    {   return EXIT_SUCCESS;}
    else
    {cout<<"Inputan ERROR"<<endl;}
    }
    system("pause");
 return 0;
}
==========================================================
SEMOGA MEMBANTU

Jumat, 04 April 2014

PROGRAM SORTING C++ SEDERHANA

Contoh ASCENDING





=======================================================
Contoh DESCENDING



======================================================
#include <cstdlib>
#include <iostream>

using namespace std;

main()
{ int a[5],i,j,n,tampung,terkecil,pil;
cout<<"Input Banyak Data =";cin>>n;
for(i=0;i<n;i++){
cout<<"Input data ke "<<i<<" = ";cin>>a[i];
}
cout<<"Sebelum sorting: "<<endl;
for(i=0;i<n;i++){
cout<<a[i]<<" ";
}
cout<<endl;
cout<<"1.ASC"<<endl;
cout<<"2.DSC"<<endl;
cout<<"Input Pilihan =";cin>>pil;
if (pil ==1)
{
for(i=0;i<n-1;i++){
for (j=i;j<n-1;j++){
if(a[i]>a[j+1]){
tampung=a[j+1];
a[j+1]=a[i];
a[i]=tampung;

}
}
}
for (i=0;i<n;i++){
cout<<a[i]<<" ";
}
}
if (pil ==2)
{ for(i=0;i>n-1;i++){
for (j=i;j>n-1;j++){
if(a[i]<a[j+1]){
tampung=a[j+1];
a[j+1]=a[i];
a[i]=tampung;

}
}
}
for (i=0;i<n;i++){
cout<<a[i]<<" ";
}
}
system("pause");
return 0;
}
====================================================
Semoga membantu