Answer to Question #233672 in C++ for Ahmad

Question #233672
Implement the following function. You may use the stack template class and the
stack operations of push, pop, peek, is_empty, and size. You may also use cin.peek( )
and use "cin>>i" to read an integer.
int evaluate_postfix_from_cin( )
// Precondition (Which is not checked): The next input line of cin is a
// properly formed postfix expression consisting of integers,
// the binary operations + and -, and spaces.
// Postcondition: The function has read the next input line (including
// the newline) and returned the value of the postfix expression.
{
int i;
stack<int> s;
1
Expert's answer
2021-09-07T23:22:23-0400
#include<iostream>
#include<conio.h>
#define MAX 3
using namespace std;
int a[MAX], top = -1;
void push();
void pop();
void peep();
void change();
void size();


int main()
{
  int n;
  while(1) {
  cout<<"\n1. PUSH";
  cout<<"\n2. POP";
  cout<<"\n3. PEEP";
  cout<<"\n4. CHANGE";
  cout<<"\n5. is_empty";
  cout<<"\n6. size";
     cout<<"\nEnter Choice : ";
     cin>>n;
     switch(n)
     {
       case 1:
 {
    push();
    break;
 }
       case 2:
 {
    pop();
    break;
 }
 case 3:
       {
   peep();
   break;
       }
       case 4:
       {
   change();
   break;
       }
       case 5:
 {
    size();
    break;
 }
       case 6:
 {
    exit(0);
 }
       default:
 cout<<"\ninvalid choice !!!";
     } 
     getch();
  }
}


void push(){
  int data;
  if(top==MAX-1)
  {
     cout<<"\noverflow or stack is full !!!";
  }
  else
  {
     cout<<"\nEnter the element : ";
     cin>>data;
     top++;
     a[top]=data;
  }
}


void pop()
{
  if(top==-1)
  {
         cout<<"\nunder flow STACK or STACK is empty";
  }


  else
  {
    cout<<"\nPOP or DELETE element :  ",a[top];
    top--;
  }
}


void size()
{
   int i;
   if(top>=0)
   {
       cout<<"\nElemets : ";
       for(i=top; i>=0; i--)
       {
  cout<<"\n",a[i];
       }
   }
   else
   {
       cout<<"\nThe STACK is Empty";
   }
}
void peep()
{
   int m;
   cout<<"\nEnter the position : ";
   cin>>m;
   if(top-m<=-1)
   {
      cout<<"\nSTACK is overflow !";
   }
   else
   {
      cout<<"\nThe Elements is : %d",a[top-m];
   }
}
void change()
{
  int x,y;
  cout<<"\nEnter Position for change : ";
  cin>>x;
  cout<<"\nEnter the Number for change : ";
  cin>>y;
  if(top-x<=-1)
  {
     cout<<"\nSTACK is overflow !!!";
  }
  else
  {
    a[top-x]=y;
    cout<<"\nCHANGE successfull !!!";
  }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS