Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <windows.h>
- int *stack,top=-1,max;
- using namespace std;
- void pause()
- {
- cout << "Tekan ENTER untuk melanjutkan...";
- cin.clear();
- cin.sync();
- cin.get();
- }
- void ClearScreen() //Fungsi diambil dari https://cplusplus.com/articles/4z18T05o/#Windows
- {
- HANDLE hStdOut;
- CONSOLE_SCREEN_BUFFER_INFO csbi;
- DWORD count;
- DWORD cellCount;
- COORD homeCoords = { 0, 0 };
- hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
- if (hStdOut == INVALID_HANDLE_VALUE) return;
- /* Get the number of cells in the current buffer */
- if (!GetConsoleScreenBufferInfo( hStdOut, &csbi )) return;
- cellCount = csbi.dwSize.X *csbi.dwSize.Y;
- /* Fill the entire buffer with spaces */
- if (!FillConsoleOutputCharacter(
- hStdOut,
- (TCHAR) ' ',
- cellCount,
- homeCoords,
- &count
- )) return;
- /* Fill the entire buffer with the current colors and attributes */
- if (!FillConsoleOutputAttribute(
- hStdOut,
- csbi.wAttributes,
- cellCount,
- homeCoords,
- &count
- )) return;
- /* Move the cursor home */
- SetConsoleCursorPosition( hStdOut, homeCoords );
- }
- void push(){
- int data;
- for(;;){
- if(top>::max-1){
- cout<<"Stack sudah penuh.\n";
- break;
- } else{
- cout<<"Top: "<<top<<"\n";
- cout<<"Max: "<<::max<<"\n";
- cout<<"Masukkan nilai yang ingin di push: ";cin>>data;
- if(data==0){
- break;
- }else{
- top++;
- stack[top]=data;
- cout<<"Berhasil.\n";
- }
- }
- }
- }
- void pop (){
- if(top<=-1){
- cout<<"Stack kosong.\n";
- } else{
- cout<<"Data yang dikeluarkan adalah: "<<stack[top]<<"\n";
- top--;
- }
- }
- void isEmpty(){
- if (top==-1){
- cout<<"Stack kosong.\n";
- }else{
- cout<<"Stack tidak kosong\n";
- }
- }
- void clear(){
- top=-1;
- cout<<"Stack telah diclear.\n";
- }
- void topEl(){
- if(top==-1){
- cout<<"Stack kosong.";
- } else {
- cout<<"Data di stack teratas adalah: "<<stack[top]<<"\n";
- }
- }
- int main(){
- char pilihan;
- cout<<"Masukkan jumlah stack: ";cin>>::max;
- stack=new int[::max];
- while(pilihan != '0'){
- try{
- ClearScreen();
- cout<<"PROGRAM STACKS"<<"\n"
- <<"1. Tambah data"<<"\n"
- <<"2. Keluarkan data"<<"\n"
- <<"3. Tampilkan data paling atas"<<"\n"
- <<"4. Bersihkan stack"<<"\n"
- <<"5. Check apakah stack kosong"<<"\n"
- <<"6. Tampilkan isi stack"<<"\n"
- <<"0. Keluar"<<"\n";
- cout<<"Masukkan pilihan anda: ";cin>>pilihan;
- switch(pilihan){
- case '1':
- push();
- pause();
- break;
- case '2':
- pop();
- pause();
- break;
- case '3':
- topEl();
- pause();
- break;
- case '4':
- clear();
- pause();
- break;
- case '5':
- isEmpty();
- pause();
- break;
- case '6':
- if(top==-1){
- cout<<"Stack kosong.\n";
- }else{
- for(int x=top;x>-1;x--){
- cout<<"Stack ke - "<<x<<": "<<stack[x]<<"\n";
- }
- }
- pause();
- break;
- case 0:
- break;
- }
- } catch(...){
- cout<<"Maaf, terjadi kesalahan. Silahkan coba kembali."<<"\n";
- pause();
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement