Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Computer Architecture & Assembly Language Class: I need a stack based C program

ID: 3751393 • Letter: C

Question

Computer Architecture & Assembly Language Class:

I need a stack based C program, and would like to use an array list for this. Please note what you did so I can learn from it. Please make sure it works, I will rate right after.

The specifics are below. The user should be able to type one of the characters below (left side) and insert a integer according to those commands (right side). Please let me know if you want further clarification on what needs to be done.

Example runs for reference

Write and test a C program that implements a stack based integer-based calculator. The program accepts input until q is entered. Inputs control the calculator as follows Input Action Non-zero integer Pushed on the stack Pop the top stack item Quit the program Clear the stack Display the stack top down Print the top stack item Replace the top 2 stack items by their sum Replace the top 2 stack items by their product Replace the top 2 stack items by their difference /Replace the top 2 stack items by their quotient Replace the top 2 stack items by second raised to the power top Your program should check for error conditions - see example program run. You can make the maximum stack size small in order to test for errors.

Explanation / Answer

#include <stdio.h>
#include <math.h>
int stack[100];
int pointer=0;
void pop();
void clear();
void display();
void print();
void add();
void multiply();
void subtract();
void division();
void power();
void pop()
{
   if(pointer==0)
       printf("Stack is empty, nothing to pop ");
   else
       pointer--;
}
void clear()
{
   pointer=0;
}
void display()
{
   int i;
   if(pointer==0)
       printf("Error: Stack is empty, nothing to display ");
   else
   {  
       for(i=0;i<pointer;i++)
           printf("%d ",stack[i] );
   }
}
void print()
{
   printf("%d ",stack[pointer-1] );
}

void add()
{
   if(pointer<2)
       printf("Error: Nor enough numbers present in the stack to perform this operation ");
   else
   {
       int ans=stack[pointer-1]+stack[pointer-2];
       stack[pointer-2]=ans;
       pointer--;
   }
}
void multiply()
{
   int ans=stack[pointer-1]*stack[pointer-2];
   stack[pointer-2]=ans;
   pointer--;
}
void subtract()
{
   if(pointer<2)
       printf("Error: Nor enough numbers present in the stack to perform this operation ");
   else
   {
       int ans=abs(stack[pointer-1]-stack[pointer-2]);
       stack[pointer-2]=ans;
       pointer--;
   }
}
void division()
{
   if(pointer<2)
       printf("Error: Nor enough numbers present in the stack to perform this operation ");
   else
   {
       if(stack[pointer-1]==0)
           printf("Error: Attept to divide by 0 ");
       else
       {
           int ans=stack[pointer-2]/stack[pointer-1];
           stack[pointer-2]=ans;
           pointer--;
       }
   }
}
void power()
{
   int ans=pow(stack[pointer-2],stack[pointer-1]);
   stack[pointer-2]=ans;
   pointer--;
}
int main()
{

   char temp;

   while(1)
   {
       scanf("%c",&temp);
       if(((int)temp>=48)&&((int)temp<=57))
       {
           if(pointer<100)
           {
               stack[pointer]=(int)temp-48;
               pointer++;
           }
           else
               printf("Error: stack overflow ");
       }
       else if(temp=='p')
           pop();
       else if(temp=='c')
           clear();
       else if(temp=='d')
           display();
       else if(temp=='=')
           print();
       else if(temp=='+')
           add();
       else if(temp=='*')
           multiply();
       else if(temp=='-')
           subtract();
       else if(temp=='/')
           division();
       else if(temp=='^')
           power();
       else if(temp=='q')
           break;
      

   }
   printf("Goodbye! ");
   return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote