Advertisement
lucasblack29

31200091_ANDRE VINCENTIUS SANTOSA_3MSI1

Dec 6th, 2021
930
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.97 KB | None | 0 0
  1. #include <iostream>
  2. #include <windows.h>
  3. int *stack,top=-1,max;
  4. using namespace std;
  5. void pause()
  6. {
  7.     cout << "Tekan ENTER untuk melanjutkan...";
  8.     cin.clear();
  9.     cin.sync();
  10.     cin.get();
  11. }
  12. void ClearScreen() //Fungsi diambil dari https://cplusplus.com/articles/4z18T05o/#Windows
  13. {
  14.     HANDLE                     hStdOut;
  15.     CONSOLE_SCREEN_BUFFER_INFO csbi;
  16.     DWORD                      count;
  17.     DWORD                      cellCount;
  18.     COORD                      homeCoords = { 0, 0 };
  19.  
  20.     hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
  21.     if (hStdOut == INVALID_HANDLE_VALUE) return;
  22.  
  23.     /* Get the number of cells in the current buffer */
  24.     if (!GetConsoleScreenBufferInfo( hStdOut, &csbi )) return;
  25.     cellCount = csbi.dwSize.X *csbi.dwSize.Y;
  26.  
  27.     /* Fill the entire buffer with spaces */
  28.     if (!FillConsoleOutputCharacter(
  29.     hStdOut,
  30.     (TCHAR) ' ',
  31.     cellCount,
  32.     homeCoords,
  33.     &count
  34.     )) return;
  35.  
  36.     /* Fill the entire buffer with the current colors and attributes */
  37.     if (!FillConsoleOutputAttribute(
  38.     hStdOut,
  39.     csbi.wAttributes,
  40.     cellCount,
  41.     homeCoords,
  42.     &count
  43.     )) return;
  44.  
  45.     /* Move the cursor home */
  46.     SetConsoleCursorPosition( hStdOut, homeCoords );
  47. }
  48. void push(){
  49.     int data;
  50.     for(;;){
  51.         if(top>::max-1){
  52.             cout<<"Stack sudah penuh.\n";
  53.             break;
  54.         } else{
  55.             cout<<"Top: "<<top<<"\n";
  56.             cout<<"Max: "<<::max<<"\n";
  57.             cout<<"Masukkan nilai yang ingin di push: ";cin>>data;
  58.             if(data==0){
  59.                 break;
  60.             }else{
  61.                 top++;
  62.                 stack[top]=data;
  63.                 cout<<"Berhasil.\n";
  64.             }
  65.         }
  66.     }
  67. }
  68. void pop (){
  69.     if(top<=-1){
  70.         cout<<"Stack kosong.\n";
  71.     } else{
  72.         cout<<"Data yang dikeluarkan adalah: "<<stack[top]<<"\n";
  73.         top--;
  74.     }
  75. }
  76. void isEmpty(){
  77.     if (top==-1){
  78.         cout<<"Stack kosong.\n";
  79.     }else{
  80.         cout<<"Stack tidak kosong\n";
  81.     }
  82. }
  83. void clear(){
  84.     top=-1;
  85.     cout<<"Stack telah diclear.\n";
  86. }
  87. void topEl(){
  88.     if(top==-1){
  89.         cout<<"Stack kosong.";
  90.     } else {
  91.         cout<<"Data di stack teratas adalah: "<<stack[top]<<"\n";
  92.     }
  93. }
  94. int main(){
  95.     char pilihan;
  96.     cout<<"Masukkan jumlah stack: ";cin>>::max;
  97.     stack=new int[::max];
  98.     while(pilihan != '0'){
  99.         try{
  100.             ClearScreen();
  101.             cout<<"PROGRAM STACKS"<<"\n"
  102.                 <<"1. Tambah data"<<"\n"
  103.                 <<"2. Keluarkan data"<<"\n"
  104.                 <<"3. Tampilkan data paling atas"<<"\n"
  105.                 <<"4. Bersihkan stack"<<"\n"
  106.                 <<"5. Check apakah stack kosong"<<"\n"
  107.                 <<"6. Tampilkan isi stack"<<"\n"
  108.                 <<"0. Keluar"<<"\n";
  109.             cout<<"Masukkan pilihan anda: ";cin>>pilihan;
  110.             switch(pilihan){
  111.                 case '1':
  112.                     push();
  113.                     pause();
  114.                 break;
  115.                 case '2':
  116.                     pop();
  117.                     pause();
  118.                 break;
  119.                 case '3':
  120.                     topEl();
  121.                     pause();
  122.                 break;
  123.                 case '4':
  124.                     clear();
  125.                     pause();
  126.                 break;
  127.                 case '5':
  128.                     isEmpty();
  129.                     pause();
  130.                 break;
  131.                 case '6':
  132.                     if(top==-1){
  133.                         cout<<"Stack kosong.\n";
  134.                     }else{
  135.                         for(int x=top;x>-1;x--){
  136.                             cout<<"Stack ke - "<<x<<": "<<stack[x]<<"\n";
  137.                         }
  138.                     }
  139.                 pause();
  140.                 break;
  141.                 case 0:
  142.                 break;
  143.             }
  144.         } catch(...){
  145.             cout<<"Maaf, terjadi kesalahan. Silahkan coba kembali."<<"\n";
  146.             pause();
  147.         }
  148.     }
  149.     return 0;
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement