Explain the working of this code   #include #include using namespace std; class Pancake {   private:     int top;   int arr[10];   public:     Pancake() {       top = -1;       for (int i = 0; i < 5; i++) {         arr[i] = 0;       }     }   bool isEmpty() {     if (top == -1)       return true;     else       return false;   }   bool isFull() {     if (top == 4)       return true;     else       return false;   }   void push(int val) {     if (isFull()) {       cout << "pancakes overflow" << endl;     } else {       top++; // 1       arr[top] = val;     }   }   int pop() {     if (isEmpty()) {       cout << "pancakes underflow" << endl;       return 0;     } else {       int popValue = arr[top];       arr[top] = 0;       top--;       return popValue;     }   }   int count() {     return (top + 1);   }   int peek(int pos) {     if (isEmpty()) {       cout << "pancake underflow" << endl;       return 0;     } else {       return arr[pos];     }   }   void display() {     cout << "All values in the pancake are " << endl;     for (int i = 4; i >= 0; i--) {       cout << arr[i] << endl;     }   } }; int main() {   Pancake s1;   int option, postion, value;   do {     cout << "What operation do you want to perform? Select Option number. Enter 0 to exit." << endl;     cout << "1. Push()" << endl;     cout << "2. Pop()" << endl;     cout << "3. isEmpty()" << endl;     cout << "4. isFull()" << endl;     cout << "5. peek()" << endl;     cout << "6. count()" << endl;     cout << "7. display()" << endl;     cout << "8. Clear Screen" << endl << endl;     cin >> option;     switch (option) {     case 0:       break;     case 1:       cout << "Enter an item to push in the Pancake" << endl;       cin >> value;       s1.push(value);       break;     case 2:       cout << "Pop Function Called - Poped Value: " << s1.pop() << endl;       break;     case 3:       if (s1.isEmpty())         cout << "Pancake is Empty" << endl;       else         cout << "Pancake is not Empty" << endl;       break;     case 4:       if (s1.isFull())         cout << "Pancake is Full" << endl;       else         cout << "Pancake is not Full" << endl;       break;     case 5:       cout << "Enter position of item you want to peek: " << endl;       cin >> postion;       cout << "Peek Function Called - Value at position " << postion << " is " << s1.peek(postion) << endl;       break;     case 6:       cout << "Count Function Called - Number of Items in the Pancake are: " << s1.count() << endl;       break;     case 7:       cout << "Display Function Called - " << endl;       s1.display();       break;     case 8:       system("cls");       break;     default:       cout << "Enter Proper Option number " << endl;     }   } while (option != 0);   return 0; }

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
100%

Explain the working of this code

 

#include<iostream>

#include<string>

using namespace std;

class Pancake {
  private:
    int top;
  int arr[10];

  public:
    Pancake() {
      top = -1;
      for (int i = 0; i < 5; i++) {
        arr[i] = 0;
      }
    }

  bool isEmpty() {
    if (top == -1)
      return true;
    else
      return false;
  }
  bool isFull() {
    if (top == 4)
      return true;
    else
      return false;
  }

  void push(int val) {
    if (isFull()) {
      cout << "pancakes overflow" << endl;
    } else {
      top++; // 1
      arr[top] = val;
    }
  }

  int pop() {
    if (isEmpty()) {
      cout << "pancakes underflow" << endl;
      return 0;
    } else {
      int popValue = arr[top];
      arr[top] = 0;
      top--;
      return popValue;
    }
  }

  int count() {
    return (top + 1);
  }

  int peek(int pos) {
    if (isEmpty()) {
      cout << "pancake underflow" << endl;
      return 0;
    } else {
      return arr[pos];
    }
  }

  void display() {
    cout << "All values in the pancake are " << endl;
    for (int i = 4; i >= 0; i--) {
      cout << arr[i] << endl;
    }
  }
};

int main() {
  Pancake s1;
  int option, postion, value;

  do {
    cout << "What operation do you want to perform? Select Option number. Enter 0 to exit." << endl;
    cout << "1. Push()" << endl;
    cout << "2. Pop()" << endl;
    cout << "3. isEmpty()" << endl;
    cout << "4. isFull()" << endl;
    cout << "5. peek()" << endl;
    cout << "6. count()" << endl;
    cout << "7. display()" << endl;
    cout << "8. Clear Screen" << endl << endl;

    cin >> option;
    switch (option) {
    case 0:
      break;
    case 1:
      cout << "Enter an item to push in the Pancake" << endl;
      cin >> value;
      s1.push(value);
      break;
    case 2:
      cout << "Pop Function Called - Poped Value: " << s1.pop() << endl;
      break;
    case 3:
      if (s1.isEmpty())
        cout << "Pancake is Empty" << endl;
      else
        cout << "Pancake is not Empty" << endl;
      break;
    case 4:
      if (s1.isFull())
        cout << "Pancake is Full" << endl;
      else
        cout << "Pancake is not Full" << endl;
      break;
    case 5:
      cout << "Enter position of item you want to peek: " << endl;
      cin >> postion;
      cout << "Peek Function Called - Value at position " << postion << " is " << s1.peek(postion) << endl;
      break;
    case 6:
      cout << "Count Function Called - Number of Items in the Pancake are: " << s1.count() << endl;
      break;
    case 7:
      cout << "Display Function Called - " << endl;
      s1.display();
      break;
    case 8:
      system("cls");
      break;
    default:
      cout << "Enter Proper Option number " << endl;
    }

  } while (option != 0);

  return 0;
}

Expert Solution
steps

Step by step

Solved in 5 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY