Showing posts with label Source Code. Show all posts
Showing posts with label Source Code. Show all posts

Try Catch


//file : Try Catch.cpp
//Author : Jemmy R. Mongi

#include <iostream>
#include <exception>
using namespace std;

struct MyException : public exception
{
  const char * what () const throw ()
  {
    return "C++ Exception";
  }
};

int main()
{
  try
  {
    throw MyException();
  }
  catch(MyException& e)
  {
    std::cout << "MyException caught" << std::endl;
    std::cout << e.what() << std::endl;
  }
  catch(std::exception& e)
  {
    //Other errors
  }
}

Queue Pointer

/*
  File : queue_pointer.cpp
  Author : Jemmy R. Mongi
  Date : 18 September 2012
*/
#include <iostream>
#include <conio.h>
#include <stdio.h>
using namespace std;

struct node
{
    int data;
    node *link;
};
node *front = NULL;
node *rear = NULL;

void Insertion()
{
     node *temp;
     cout<< "Masukan nilai : ";
     cin>>temp->data;
     temp->link = NULL;
         //if(front == NULL)
         //{
              //front = temp;
              //rear = temp;
         //}
         //else
         //{
     rear->link = temp;
     temp = rear->link;
         //}   
}//end insertion
void Deletion()
{
     node *temp;
     if(front == NULL && rear == NULL)
     {
          system("cls");
          cout<<"Error, cannot delete, because Queue is empty\n";
     }
     else
     {
          temp = front;
          front = front->link;
          cout << "Number to be delete is : \n"<< temp->data;
          free(temp);
          if(front == NULL)
          {
               rear = NULL;
          }
     }
}//end deletion

void Display()
{
     node *temp;
     if(front == NULL && rear == NULL)
     {
          system("cls");
          cout<< "error cannot display, because Queue is Empty\n";
     }   
     else
     {
         temp = front;
         cout<< "elements in queue is : ";
         while(temp != NULL)   
         {
              cout<data;
         }
         temp = temp->link;
     }
}
//fungsi utama
int main()
{
    int pilih;
    while(pilih != 4)
    {
        cout<<"Queue Pointer\n";
        cout<<"1. Insert\n";
        cout<<"2. Delete\n";
        cout<<"3. Display\n";
        cout<<"4. Exit\n";
        cout<<"Masukan pilihan anda : ";
        cin>>pilih;
        switch(pilih)
        {
             case 1 :
                  Insertion();
                  break;
             case 2 :
                  Deletion();
                  break;
             case 3 :
                  Display();
                  break;
             case 4 :
                  exit(0);
                  break;
             default :
                     system("cls");
                     cout<<"maaf, pilihan tidak tersedia\n";
        }
    }
    system("pause");
    return 0;
}//end fungsi utama

Stack

#include <conio.h>
#include <iostream>
using namespace std;

int main()
{
    int stack[5];
    for(int top = 0; top < 5; top++)
    {
            for(int i = top; i < 0; i++)
            {
                    if (top == -1)
                    cout<< "error";
                    else
                    {
                        cin>>stack[i];
                    }
            }
           
    }
    system("pause");
    return 0;
}

Selection

//File : Selection.cpp
//Author : Jemmy R. Mongi

#include <stdio.h>
#include <stdlib.h>
#define n 6
int main() {
    int A[n];
   
    /* input nilai pada array A */
    for (int x=0; x
        printf("A[%d]: ", x);
        scanf("%d", &A[x]);
    }
   
    system("cls");
    /* tampilkan nilai pada array A sebelum disortir */
    printf("Nilai A sebelum disortir \n");
    for (int y=0; y
        printf("A[%d] = %d \n", y, A[y]);
   
    /* algoritma "selection sort" */
    /* sortir dimulai dari elemen terbesar ke elemen terkecil */
    /* sortir dimulai dengan membandingkan elemen paling akhir dengan elemen-elemen sebelumnya */
    for (int i=0; i<=n-1; i++) {
        int big = n-1;
        for (int j=n-2; j>=i; j--) {
            if (A[big] < A [j])
               big = j;
        }/* end for j */
        int temp = A[big];
        A[big] = A[i];
        A[i] = temp;
    }/* end for i */
   
    /* tampilkan nilai array A setelah disortir */
    printf("\n\nNilai A setelah disortir \n");
    for (int z=0; z
        printf("A[%d] = %d \n", z, A[z]);
       
    system("pause");
    return 0;
}

Bubble

/*
   Author :  Jemmy R. Mongi
   File :  Bubble.cpp
*/
#include <stdio.h>
#include &ltstdlib.h>

/* prototipe fungsi bubbleSort */
int bubbleSort(int *, int);

int main() {
    printf("***BUBBLE SORT***\n\n");
   
    /* minta user menginput jumlah elemen array */
    printf("Masukan jumlah elemen: ");
    int n;
    scanf("%d", &n);
   
    /* deklarasi array dengan jumlah elemen sebanyak input user */
    int A[n];
   
    /* input nilai pada elemen-elemen array */
    int input;
    for (input = 0; input < n; input++) {
        printf("A[%d]: ", input);
        scanf("%d", &A[input]);
    }
   
    system("cls");
    /* tampilkan nilai elemen-elemen array sebelum disortir */
    printf("Nilai elemen-elemen array sebelum disortir\n");
    int before;
    for (before = 0; before < n; before++)
        printf("A[%d] = %d\n", before, A[before]);
   
    /* panggil fungsi bubbleSort */
    bubbleSort(A, n);
   
    /* tampilkan nilai elemen-elemen array setelah disortir */
    printf("\nNilai elemen-elemen array setelah disortir\n");
    int after;
    for (after = 0; after < n; after++)
        printf("A[%d] = %d\n", after, A[after]);
   
    system("pause");
    return 0;
}

/* body fungsi bubbleSort */
int bubbleSort(int *A, int n) {
    int i;
    for (i = 1; i <= n-1; i++) {
        int j;
        for (j = n-1; j >= i; j--) {
            if (A[j] < A[j-1]) {
               int temp = A[j];
               A[j] = A[j-1];
               A[j-1] = temp;
            }/* if */
        }/* for j */
    }/* for i */
}/* fungsi bubbleSort() */

Tutorial Lengkap Hosting Website Gratis Dengan Gambar

Hallo Guys.. sudah sangat lama sekali sejak terakhir kali saya melakukan posting di blogger ini... kalau saya tidak salah terakhir saya ...