Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #define MAX 5
- using namespace std;
- struct Kartu {
- int top, angka[MAX];
- char jenis[8];
- }Tumpukan;
- void init(){
- Tumpukan.top = -1;
- Tumpukan.jenis[8] = -1;
- }
- bool isEmpty() {
- return Tumpukan.top == -1;
- return Tumpukan.jenis[8] == -1;
- }
- bool isFull() {
- return Tumpukan.top == MAX-1;
- return Tumpukan.jenis[8] == MAX-1;
- }
- void push() {
- if (isFull()) {
- cout << "\nTumpukan penuh"<<endl;
- }
- else {
- Tumpukan.top++;
- Tumpukan.jenis[8]++;
- cout << "\nMasukkan Angka = "; cin >> Tumpukan.angka[Tumpukan.top];
- cout << "\nMasukkan Jenis Kartu = "; cin.getline(Tumpukan.jenis,8);
- cout << "Kartu " << Tumpukan.angka[Tumpukan.top] <<Tumpukan.angka[Tumpukan.jenis[8]]<< " masuk ke stack"<<endl;
- }
- }
- void pop() {
- if (isEmpty()) {
- cout << "\nData kosong\n"<<endl;
- }
- else {
- cout << "\nData "<<Tumpukan.angka[Tumpukan.top] <<Tumpukan.angka[Tumpukan.jenis[8]] <<" sudah terambil"<<endl;
- Tumpukan.top--;
- Tumpukan.jenis[8]--;
- }
- }
- void printStack() {
- if (isEmpty()) {
- cout << "Kartu Tumpukan Kosong";
- }
- else {
- cout << "\nKartu Tumpukan : ";
- for (int i = Tumpukan.top; i >= 0; i--){
- cout << Tumpukan.angka[i] << ((i == 0) ? "" : " ");
- }
- for (int j = Tumpukan.jenis[8]; j >= 0; j--){
- cout << Tumpukan.angka[j] << ((j == 0) ? "" : " ");
- }
- }
- int main() {
- int pilihan, angka;
- init();
- do {
- printStack();
- cout<<endl;
- cout << "\n1. Buka Kartu (Pop)\n"
- <<"2. Entri (Push)\n"
- <<"3. Keluar\n"
- <<"Masukkan Pilihan: ";
- cin >> pilihan;
- switch (pilihan)
- {
- case 1:
- pop();
- break;
- case 2:
- push();
- break;
- default:
- cout << "Pilihan tidak tersedia" << endl;
- break;
- }
- } while (pilihan!=3);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement