Program Membalikan Kata dengan Stack C++
#include
#include
#include
#include
#include
#define
maxstack 100
void
inisialisasi();
void
push(char);
void pop();
bool
isfull();
bool
isempty();
void
penutup();
struct STACK
{
                int top;
                char stack[maxstack];
};
STACK
stackbaru;
char wordInput[maxstack];
int a;
main()
{
                int i,j;
                system("cls");
                inisialisasi();
                printf("  
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n");
                printf("   +    
PROGRAM MEMBALIK KALIMAT    
+\n");
                printf("   +        
MENGGUNAKAN STACK       
+\n");
                printf("  
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n");
                printf("Masukkan kalimat
(max.100 karakter) : ");
                gets(wordInput);
                for(i=0; wordInput[i]; i++)
                                push(wordInput[i]);
                printf("\n");
                printf("String awal -->
");
                for(j=0; j<=stackbaru.top;
j++)
                                printf("%c",
stackbaru.stack[j]);
                printf("\n\n");                   
                printf("String setelah
dibalik --> ");                          
                pop();
                cout<
 
                penutup();          
}
void
inisialisasi()
{
                stackbaru.top = -1;
}
bool
isfull()
{
                if(stackbaru.top == maxstack-1)
                                return true;
                else
                                return false;
}
bool
isempty()
{
                if(stackbaru.top==-1)
                                return true;
                else 
                                return false;
}
void push
(char data)
{
                if(isfull()==false)
                {
                                stackbaru.top++;
                                stackbaru.stack[stackbaru.top]=data;
                }
                else
                {
                                printf("\n");
                                printf("Maaf,
jumlah karakter melebihi batas\n");
                                printf("String
yg bisa diproses adalah: ");
                                for(a=0;
a
 
                                                printf("%c",
stackbaru.stack[a]);
                                printf("\n");
                }
}
void pop()
{
                while(isempty()==false)
                {
                                printf("%c",
stackbaru.stack[stackbaru.top]);
                                stackbaru.top--;
                }
}
void
penutup()
{
                printf("
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-\n");
                printf("|     Thank You for Using The Program      |\n");
                printf("|          loddaya  ^_^         
|\n");
                printf("|  This Program Developed by loddaya  |\n");
                printf("
-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n\n");
   getch();
}
Post a Comment