Kamis, 31 Mei 2018

Program Menggunakan Binary Tree C++

Program Menggunakan Binary Tree C++

Script Program
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
using namespace std;

/* A <span id="mvuhno2x61oe_1" class="mvuhno2x61oe">binary tree</span> node has data, pointer to left child
   and a pointer to right child */
struct node
{
   int data;
   struct node* left;
   struct node* right;
};

/* Prototypes for funtions needed in printPaths() */
void printPathsRecur(struct node* node, int path[], int pathLen);
void printArray(int ints[], int len);

/*Given a binary tree, print out all of its root-to-leaf
 paths, one per line. Uses a recursive helper to do the work.*/
void printPaths(struct node* node)
{
  int path[1000];
  printPathsRecur(node, path, 0);
}

/* Recursive helper function -- given a node, and an array containing
 the path from the root node up to but not including this node,
 print out all the root-leaf paths.*/
void printPathsRecur(struct node* node, int path[], int pathLen)
{
  if (node==NULL)
    return;

  /* append this node to the path array */
  path[pathLen] = node->data;
  pathLen++;

  /* it's a leaf, so print the path that led to here  */
  if (node->left==NULL && node->right==NULL)
  {
    printArray(path, pathLen);
  }
  else
  {
    /* otherwise try both subtrees */
    printPathsRecur(node->left, path, pathLen);
    printPathsRecur(node->right, path, pathLen);
  }
}


/* UTILITY FUNCTIONS */
/* Utility that prints out an array on a line. */
void printArray(int ints[], int len)
{
  int i;
  for (i=0; i<len; i++)
  {
    printf("%d ", ints[i]);
  }
  printf("\n");
}

/* utility that allocates a new node with the
   given data and NULL left and right pointers. */
struct node* newnode(int data)
{
  struct node* node = (struct node*)
                       malloc(sizeof(struct node));
  node->data = data;
  node->left = NULL;
  node->right = NULL;

  return(node);
}

/* Driver program to test above functions*/
int main()
{

 struct node *root = newnode(5);
  root->left        = newnode(4);
  root->right       = newnode(8);
  root->left->left  = newnode(11);
  root->right->left  = newnode(13);
  root->right->right  = newnode(4);
  root->left->left->left  = newnode(7);
  root->left->left->right = newnode(2);
  root->right->right->right = newnode(1);

  printPaths(root);

   double number1 = 0.0;
 double number2 = 0.0;
 double number3 = 0.0;
 double number4 = 0.0;
 double answer;
 cout <<"masukkan 4 angka dari path di atas"<<endl;
 cin>>number1;
 cin>>number2;
 cin>>number3;
 cin>>number4;

 answer = (number1 + number2 + number3 + number4) /4;
 cout<<"rata-ratanya adalah "<<answer<<endl;



  system ("pause");
  getchar();

  return 0;}

Referensi :
https://teknobloggerid.blogspot.com/2016/06/contoh-coding-binary-tree-c.html

Rabu, 30 Mei 2018

Finite State Machine (FSM) Game Survival

  • FSM Game Survival

  • FSM Pseudocode
Void RunLogic(int * state)
{
switch( *state )
{
case 0: //Wander
Wander();
if(SeeEnemy() ) { *Attack}
break;
case 1: //Attack
Attack();
if( with() ) { *Range;*Melee;}
if( NoEnemy() ) { *Wander;}
if( Hide() ) { *Flee;}
if( EnemyDeath() ) { *Loot;}
if( PlayerLowHP() ) { *FindAid;}
break;
case 2: //Range
rRange();
if( use() ) { *Gun;}
Break;
case 3: //Melee
Melee();
if( use() ) { *Knife;*Punch;}
Break;
case 4: //FindAid
FindAid();
if(HPFull() ) { *Flee;}
case 5: //Flee
Flee();
if( NoEnemy() ) { *Wander;}
if( PlayerDeath() ) { *GameOver;}
break;
case 6: //loot
loot();
if( search() ) { *gun;*knife;}
break;
case 7: //Gun
Gun();
if( Reload() ) { *Attack;}
case 8: //GameOver
GameOver();
if( respawn() ) { *Player;}
}
}




Senin, 28 Mei 2018

Program Antrian Kereta Api Menggunakan Queue C++

Program Antrian Kereta Api Menggunakan Queue C++

Script Program
#include<iostream>l
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<ctype.h>

using namespace std;

//struct node
struct Node{
 int data;
 Node *next;
};
Node *head, *tail;

//fungsi
void menu();
void LinkedList();
void penutup();
void inisialisasi();
void insertData();
void removeData();
void bersih();
void tampil();

//pemanggilan fungsi
//fungsi utama
int main(){
 char MENU;
 int ulangMetode = 1;

 do
 {
        printf("||= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =||\n");
        printf("||              PROGRAM ANTRIAN KEBERANGKATAN KERETA API                 ||\n");
        printf("||_______________________________________________________________________||\n");


        //menu pilihan
                        cout<<"||+++++++++++++++++++++++++++++++++++||\n";
                        cout<<"||***          MENU UTAMA         ***||\n";
                        cout<<"||-----------------------------------||\n";
                        cout<<"|| 1. Pendaftaran Kereta Api         ||\n";
                        cout<<"|| 2. Exit                           ||\n";
                        cout<<"||+++++++++++++++++++++++++++++++++++||\n";
                        cout<<endl;

  printf("Masukkan Pilihan Anda ?: ");
  MENU = getche();
  printf("\n");
  getch();
        system("cls");

  switch(MENU)
  {
  case '1' :
   LinkedList();
   break;
        case '2' :
            penutup();
   break;
  default :
   {
    puts("\nKode yang dimasukkan salah!\n");
    puts("Press any key for back to the menu");
    getch();
   }
   break;
  }
 }
 while(ulangMetode == 1);
}

//menu dari metode
void menu()
{

    cout<<"||+++++++++++++++++++++++++++++++++++++++++++||\n";
 cout<<"||         ***** PILIHAN MENU *****          ||\n";
    cout<<"||-------------------------------------------||\n";
 cout<<"|| 1. Daftar Antrian Kereta Api<KODE KERETA> ||\n";
 cout<<"|| 2. Keberangkatan                          ||\n";
 cout<<"|| 3. Memberangkatkan Semua Antrian          ||\n";
 cout<<"|| 4. Lihat Daftar Antrian Kereta            ||\n";
 cout<<"|| 5. Exit                                   ||\n";
    cout<<"||+++++++++++++++++++++++++++++++++++++++++++||\n";
 cout<<endl;
}

//metode linked list
void LinkedList()
{
 char pilihMenu;
 int ulang = 1;

 do
 {
  menu();
  printf("Input Menu  : ");
  pilihMenu = getche();
  printf("\n");
  switch(pilihMenu)
  {
  case '1' :
   insertData();
   break;
  case '2' :
   removeData();
   break;
  case '3' :
   bersih();
   break;
  case '4' :
            tampil();
   break;
  case '5' :
      penutup();
   break;
  default :
   {
    puts("Input menu salah!\n");
    puts("Press any key for back to the menu");
    getch();
   }
   break;
  }
 }
 while(ulang == 1);
}

/*fungsi insialisasi bahwa
head dan tail bernilai NULL*/
void inisialisasi()
{
 head = NULL;
 tail = NULL;
}

//fungsi untuk memasukkan data
void insertData()
{
 int angka;
 Node *nodeBaru;
 nodeBaru = new Node;

 cout<<"Masukkan Data : ";
 cin>>angka;

 nodeBaru->data = angka;
 nodeBaru->next = NULL;

 if(tail == NULL)
 {
  head = tail = nodeBaru;
  tail->next = NULL;
 }
 else
 {
  tail->next = nodeBaru;
  tail = nodeBaru;
 }

 printf("Data %i masuk!\n\n", angka);
 puts("Press any key for back to the menu");
 getch();
 system("cls");
}

/*untuk hapus elemen belakang
      untuk metode LIFO*/
void removeData()
{
 int elDel;
 Node *del, *prevTail;
 del = new Node;

 if(tail != NULL)
 {
  del = tail;
  elDel = del->data;

  if(tail == head)
  {
   inisialisasi();
  }
  else
  {
   prevTail = head;

   while(prevTail->next != tail)
    prevTail = prevTail->next;

   tail = prevTail;
   tail->next = NULL;
  }

  delete del;

  printf("Data %i Diberangkatkan!\n\n", elDel);
 }
 else
 {
  puts("Data Kosong! Tidak ada data yang dapat diberangkatkan!\n");
 }

 puts("Press any key for back to the menu");
 getch();
 system("cls");
}

//untuk hapus semua elemen
void bersih()
{
 Node *clear, *point;

 if(tail != NULL)
 {
  point = head;

  while(point != NULL)
  {
   clear = point;
   point = point->next;

   delete clear;
  }

  inisialisasi();

  puts("Semua Data Diberangkatkan!\n");
 }
 else
  puts("Data Masih Kosong!\n");

 puts("Press any key for back to the menu");
 getch();
 system("cls");
}

//untuk menampilkan list data
void tampil()
{
 Node *see;

 see = head;

 if(tail != NULL)
 {
  puts("Daftar Data Antrian Kereta Api:");
  puts("");

  while(see != NULL)
  {
   printf("%i ", see->data);
   see = see->next;
  }
  puts("\n");
 }
 else
  puts("Data masih dalam keadaan kosong!\n");

 puts("Press any key for back to the menu");
 getch();
 system("cls");
}

//fungsi penutup / keluar
void penutup()
{
    cout<<"\n\tPROGRAM EXIT"<<endl;
    exit(0);
}

Referensi:
https://www.esokharinanti.com/2014/04/program-queue-linked-list-pada-antrian.html