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

Project #2: Stack Arithmetic See the RUBRIC Below before starting the design For

ID: 3821688 • Letter: P

Question

Project #2: Stack Arithmetic See the RUBRIC Below before starting the design For this circuit, you wi need to implement a simple finite state machine to control a stack of 4-bit unsigned integers. If you are not familiar with the stack abstract data type, check htt n.wikipedia.or wiki/Stack (abstract data t You have to design this stack so that its contents can be used to perform some simple arithmetic. The user must be able to perform four operations: Push A value is added to the top of the stack. Pop The top-most value is removed from the stack Pop with Add The top two values on the stack are popped from the stack, added together, and the result is pushed back onto the top of the stack. Pop with subtract The top two values on the stack are popped from the stack used to perform subtraction, and the result is pushed back onto the top of the stack (The first value popped is subtracted from the second value popped Pop and exchange The top two values on the stack are popped and inserted back on the stack in reverse order To keep things simple, the maximum stack depth will be four. Your circuit should display the complete contents of the stack on HEX3 through HEX0. To be clear, HEX3 should display the first value pushed onto the stack, HEX2 should display the second value, and so on. The seven-segment display should have no lit LEDs if it does not contain a value Some notes about error handling: Stack overflow If a push is performed, but there is no more room on the top of the stack, the contents of the stack should not be changed and the stack overflow register should be set. Hook this register up to an LED to notify the user. This register and LED should remain set and lit until the board is reset Stack Underflow If any of the three pop operations are performed without enough values on the stack to perform that operation, the contents of the stack should not be changed and the stack underflow register should be set. Hook this register up to an LED to notify the user. This register and LED should remain set and lit until the board is reset.

Explanation / Answer

#include #define MAXSIZE 5 struct stack { int stk[MAXSIZE]; int top; }; typedef struct stack STACK; STACK s; void push(void); int pop(void); void display(void); void main () { int choice; int option = 1; s.top = -1; printf ("STACK OPERATION "); while (option) { printf ("------------------------------------------ "); printf (" 1 --> PUSH "); printf (" 2 --> POP "); printf (" 3 --> DISPLAY "); printf (" 4 --> EXIT "); printf ("------------------------------------------ "); printf ("Enter your choice "); scanf ("%d", &choice); switch (choice) { case 1: push(); break; case 2: pop(); break; case 3: display(); break; case 4: return; } fflush (stdin); printf ("Do you want to continue(Type 0 or 1)? "); scanf ("%d", &option); } } /* Function to add an element to the stack */ void push () { int num; if (s.top == (MAXSIZE - 1)) { printf ("Stack is Full "); return; } else { printf ("Enter the element to be pushed "); scanf ("%d", &num); s.top = s.top + 1; s.stk[s.top] = num; } return; } /* Function to delete an element from the stack */ int pop () { int num; if (s.top == - 1) { printf ("Stack is Empty "); return (s.top); } else { num = s.stk[s.top]; printf ("poped element is = %dn", s.stk[s.top]); s.top = s.top - 1; } return(num); } /* Function to display the status of the stack */ void display () { int i; if (s.top == -1) { printf ("Stack is empty "); return; } else { printf (" The status of the stack is "); for (i = s.top; i >= 0; i--) { printf ("%d ", s.stk[i]); } } printf (" "); }