0% found this document useful (0 votes)
52 views10 pages

Tutorial C++

This document contains code examples in C++ for animation of a stack, queue, bubble sort algorithm, and reversing a word. It includes the full source code for each program with comments and explanations. The purpose is to provide examples of programming concepts and functions in C++ to help others learn.

Uploaded by

Itaparal Nayyr
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views10 pages

Tutorial C++

This document contains code examples in C++ for animation of a stack, queue, bubble sort algorithm, and reversing a word. It includes the full source code for each program with comments and explanations. The purpose is to provide examples of programming concepts and functions in C++ to help others learn.

Uploaded by

Itaparal Nayyr
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Ini merupakan contoh contoh program pada c++ mohon dicoba agar tau maksud dan fungsi setiap

perintah – perintahnya, mungkin ada kesalahan juga bagi yang bisa memperbaiki menjadi lebih baik
maka akan lebih berguna lagi bagi yang lainnya…

Animatation stuck c++


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

int top,i;
char key,temp,e;
char stack[10];
void delay();

void pushanim()
{
 for(i=0;i<=17; i++)
 {
  gotoxy(22+i,7);cout<<" ";
  gotoxy(23+i,7); cout<<temp; delay();

 }
 for(i=1;i<=(14-top);i++)
 {
  delay();
  gotoxy(40,6+i); cout<<" ";
  gotoxy(40,7+i); cout<<temp;
 }
}

void popanim(char temp)


{
 for(i=1;i<=(14-top);i++)
 {
  delay();
  gotoxy(40,22-i-top); cout<<" ";
  gotoxy(40,21-i-top); cout<<temp;
 }
 for(i=1;i<=19;i++)
 {
  delay();
  gotoxy(38+i,7); cout<<" ";
  gotoxy(39+i,7); cout<<temp; delay();
 }
 gotoxy(58,7);cout<<" ";
}

void push(char e)
{
 top=top+1;
 stack[top]=e;
 pushanim();
}
void pop(char e)
{
 if(top !=0)
 {
  e=stack[top]; popanim(e);
  top=top-1;
 }
 else
 {
  gotoxy(1,7); cout<<"stack telah kosong"<<endl;
  gotoxy(1,7);
 }
}

void main()
{
   clrscr();
  cout<<"Animasi Stack"<<endl;
  cout<<"1.Push"<<endl;
  cout<<"2.Pop"<<endl;
  cout<<"3.quit"<<endl;
  //cout<<"pilih [1/2/3] ="<<endl;
  gotoxy(59,6); cout<<"=";
   gotoxy(59,8); cout<<"=";
   gotoxy(37,9); cout<<"||   ||";
  for(i=1;i<=11;i++)
 {
   gotoxy(38,10+i);
   if(i==11)
   cout<<"|___|";
   else
   cout<<"|   |";
 }
   top=0;
   do
   {
   input:
   gotoxy(1,5);
  cout<<"masukkan pilihan anda[1/2/3] : ";
   key=getche();
   if(int(key)==27 || key=='3')
   break;
   else if(key=='1')
     {
       if(top != 10)
       {
       gotoxy(1,7); cout<<"masukkan suatu huruf : ";
       cin>>temp;
            push(temp);
       gotoxy(1,7); cout<<"                    ";
       }
       else
       {
       gotoxy(1,7); cout<<"stack penuh";
            getch();
       gotoxy(1,7); cout<<" ";
     }
     }
   else if(key=='2')
       pop(temp);
   else
   goto input;
  }while(1);

 getch();
}

void delay()
{
 for(int y=1;y<100;y++)
 for(int x=1;x<100;x++)
 for(int p=1;p<100;p++)
 cout<<"";
}

Animation queue
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>

char pil;
int jml;
void delay();

struct node
{
   char kar;
   node *next;
};
   node *tail;   //ekor
   node *now;
   node *head;
void buatnodebaru()
{
  tail=NULL;
  head=NULL;
}
void push(char ch)
{
 now=new node;
 if(head==NULL)
 {
   now->kar=ch;
   now->next=NULL;
   tail=now;
  head=now;

 }
 else
 {
   now->kar=ch;
   now->next=NULL;
  tail->next = now;
   tail=now;
 }
}

void enqueue()
{
 int i;
 char temp;
 gotoxy(1,6);
 cout<<"   ";
 gotoxy(1,8);
 cout<<" ";
 gotoxy(1,6);
 cout<<"masukkan @ karakter : ";
 cin>>temp;
 gotoxy(1,6);
 cout<<" ";

 push(temp);
   for(i=1;i<= 75-jml*6;i++)
   {
     gotoxy(i+1,20);cout<<" O";
     gotoxy(i,21); cout<<"=("<<now->kar<<")=";
   gotoxy(i+1,22);cout<<"| |";
   delay();
   if(i!=75-jml*6)
   {
   gotoxy(i+1,20);cout<<"  ";
       gotoxy(i,21);cout<<"      ";
       gotoxy(i+1,22);cout<<"   ";
  }

   }
}

void dequeue()
{
 if(head==NULL)
 {
  gotoxy(1,8); cout<<"antrean kosong !";
   delay();

 }
 else
 {
   jml=jml-1;
   char delete_element=head->kar;
   node *temp;
   temp=head;
   head=head->next;
   delete temp;

 for(int i=69;i<=76;i++)
 {
  gotoxy(i+1,20); cout<<" O";
  gotoxy(i,21); cout<<"=("<<delete_element<<")=";
  gotoxy(i+1,22); cout<<"| |";
  delay();
  gotoxy(i+1,20); cout<<"  ";
  gotoxy(i,21); cout<<"      ";
  gotoxy(i+1,22); cout<<"   ";
 }
 int byk=0;

 while(byk!=jml)
 {
   byk=byk+1;
  for(int i=69-byk*6;i<=75-byk*6;i++)
 {
     gotoxy(i+1,20); cout<<" O";
   gotoxy(i,21); cout<<"=("<<head->kar<<")=";
   gotoxy(i+1,22); cout<<"| |";
   delay();
      if(i!=75-byk*6)
      {
       gotoxy(i+1,20); cout<<"  ";
   gotoxy(i,21); cout<<"      ";
   gotoxy(i+1,22); cout<<"   ";
      }
 }
 }
 }
}

void input()
{
 gotoxy(1,1);cout<<"1.Enqueue";
 gotoxy(1,2);cout<<"2.Dequeue";
 gotoxy(1,3);cout<<"3.Exit";
 do
 {
   gotoxy(1,4);cout<<"masukkan pilihan anda[1/2/3] : ";
   gotoxy(32,4);cin>>pil;
   gotoxy(32,4);cout<<"  ";
   switch(pil)
   {
   case '1':
   {
     if(jml<10)
     {
       jml=jml+1;
       enqueue();
     }
     else
     {
      gotoxy(1,8); cout<<"antrean penuh !";
      delay();
      gotoxy(1,8); cout<<" ";
     }
     break;
    }
   case '2':
   {
     dequeue();
    break;
   }
   case '3':
   {
      exit(1);
   break;
   }
  }
 }while(1);
}
void main()
{
  buatnodebaru();
  jml=0;
  clrscr();
  input();
  getch();
}
void delay()
{
 for(int y=1;y<100;y++)
 for(int x=1;x<100;x++)
 for(int p=1;p<100;p++)
 cout<<"";
}

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

int data[10],data2[10];
int n;

void tukar(int a, int b)


{
 int t;
 t = data[b];
 data[b] = data[a];
 data[a] = t;
}

void bubble_sort()
{
 for(int i=1;i<=n;i++)
 {
  for(int j=n; j>=i; j--)
 {
   if(data[j] < data[j-1]) tukar(j,j-1);
 }
 }
}
void main()
{
 cout<<"===PROGRAM BUBBLE SORT==="<<endl;

 //Input Data
 cout<<"Masukkan Jumlah Data : ";
 cin>>n;
 for(int i=1;i<=n;i++)
 {
  cout<<"Masukkan data ke "<<i<<" : ";
  cin>>data[i];
  data2[i]=data[i];
 }

 bubble_sort();

 cout<<"\n\n";
 //tampilkan data
 cout<<"Data Setelah di Sort : ";
 for(int i=1; i<=n; i++)
 {
  cout<<" "<<data[i];
 }
 cout<<"\n\nSorting Selesai";
 getch();
}

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

void main()
{
 char kata[50];
 cout<<"Masukkan Kata Yang Akan Dibalik : ";
 cin>>kata;
 cout<<"\n\n";
 strrev(kata);
 cout<<"Kata Setelah Dibalik : "<<kata;
 getch();
}

You might also like