1. Write a C program to evaluate postfix expression. E.g., 4 5 2 * + 3 - = 11 by
ID: 3650731 • Letter: 1
Question
1. Write a C program to evaluate postfix expression. E.g., 4 5 2 * + 3 - = 11 by using stack and queue data structure. Follow the instruction given below closely to complete this assignment. The program only accepts single digit operand.
a. Assume the following structure definitions for stack and queue and
b. Create the following functions:
? Initialize stack
? Push stack
? Pop stack ? Initialize queue
? Append to queue
? Serve from queue
Note: You may include other stack and queue functions if required.
c. You are required to create one stack and one queue (postfix) to store and manipulate the operands and operators in the program.
d. The following algorithm should be used:
1. Read a postfix expression from user input.
2. append each of the character (operand/operator) into postfix queue.
3. While the postfix queue is not empty, serve one character from the postfix queue.
3.1. If the current character is a digit then
3.1.1. Push its integer value on the stack.
3.2. Else if the current character is an operator then
3.2.1. Pop the top element of the stack into variable op2.
3.2.2. Pop the next element of the stack into variable op1.
3.2.3. Calculate op1 operator op2.
3.2.4. Push the result of the calculation on the stack.
3.3. Pop the top value off the stack. This is the result of evaluating the expression.
Explanation / Answer
#include 004 #include "stack.h" 005 #include "queue.h" 006 007 008 int main() 009 { 010 Stack myStack; 011 QUEUE myQueue; 012 initialize_stack(&myStack); 013 initialize_queue(&myQueue); 014 int item; 015 int op2; 016 int op1; 017 int total; 018 printf("Please enter a character "); 019 scanf("%d",&item); 020 append(&myQueue,item); 021 while(queue_empty(&myQueue) != 1) 022 { 023 serve(&myQueue); 024 }//while 025 if(item = (item % 1 == 0)) 026 { 027 push(&myStack,item); 028 }//if 029 else if(item == operator) 030 { 031 op2 = pop(&myStack); 032 myStack->top++; 033 op1 = pop(&myStack); 034 myStack->top--; 035 }//else 036 }//main 037 038 This is my queue program 039 #include 040 #include 041 #include "queue.h" 042 043 void initialize_queue(Queue *myQueue) 044 { 045 myQueue->count = 0; 046 myQueue->front = 0; 047 myQueue->rear = 0; 048 } 049 int queue_empty(const Queue *myQueue) 050 { 051 if(myQueue->count count >= MAXQUEUE) 062 { 063 return 1; 064 } 065 else 066 { 067 return 0; 068 } 069 } 070 void append(Queue *myQueue,int item) 071 { 072 if(queue_full(myQueue)) 073 { 074 printf("Queue is full "); 075 } 076 else 077 { 078 myQueue->rear++; 079 if(myQueue->rear == MAXQUEUE) 080 { 081 myQueue->rear = 0; 082 } 083 myQueue->entry[myQueue->rear] = item; 084 myQueue->count++; 085 } 086 } 087 char serve(Queue *myQueue) 088 { 089 int item; 090 if(queue_empty(myQueue)) 091 { 092 printf("Queue is empty "); 093 } 094 else 095 { 096 item = myQueue->entry[myQueue->front]; 097 myQueue->front++; 098 if(myQueue->front == MAXQUEUE) 099 { 100 myQueue->front = 0; 101 } 102 myQueue->count--; 103 } 104 return item; 105 } 106 This is my queue header file 107 //header declaration 108 #ifndef QUEUEH 109 #define QUEUEH 110 #define MAXQUEUE 50 111 struct QUEUE 112 { 113 int count; 114 int front; 115 int rear; 116 char entry[MAXQUEUE]; 117 }; 118 typedef struct QUEUE Queue; 119 120 void initialize_queue(Queue *myQueue); 121 int queue_empty(const Queue *myQueue); 122 int queue_full(const Queue *myQueue); 123 void append(Queue *myQueue,int item); 124 char serve(Queue *myQueue); 125 126 #endif 127 128 this is my stack file 129 #include 130 #include 131 #include "stack.h" 132 133 void initialize_stack(Stack *myStack){ 134 myStack->top = -1; 135 } 136 int stack_empty(const Stack *myStack){ 137 if(myStack->top < 0){ 138 return 1; 139 } 140 else 141 { 142 return 0; 143 } 144 } 145 int stack_full(const Stack *myStack) 146 { 147 if(myStack->top >= MAXSTACK-1) 148 { 149 return 1; 150 } 151 else 152 { 153 return 0; 154 } 155 } 156 void push(Stack *myStack,int item){ 157 if(stack_full(myStack)){ 158 printf("The stack is full "); 159 } 160 else 161 { 162 myStack->top++; 163 myStack->entry[myStack->top] = item; 164 } 165 } 166 char pop(Stack *myStack) 167 { 168 int item; 169 if(stack_empty(myStack)) 170 { 171 printf("Stack is empty "); 172 } 173 else 174 { 175 item = myStack->entry[myStack->top]; 176 myStack->top--; 177 } 178 return item; 179 } 180 181 This is my stack program file 182 //header declaration 183 #ifndef STACKH 184 #define STACKH 185 #define MAXSTACK 50 186 //global type declaration 187 struct Stack{ 188 int top; 189 char entry[MAXSTACK]; 190 }; 191 typedef struct Stack stack; 192 //function declaration 193 void initialize_stack(Stack *myStack); 194 int stack_empty(const Stack *myStack); 195 int stack_full(const Stack *myStack); 196 void push(Stack *myStack,int item); 197 char pop(Stack *myStack);Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.