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

Part 1: The Problem Code written in C You are going to use a stack to keep track

ID: 3680472 • Letter: P

Question

Part 1: The Problem

Code written in C

You are going to use a stack to keep track of numbers used with a postfix calculator. With postfix notation the operators follow the operand(s). So the infix expression:

12 + (9 * 2)

Would be written in postfix format as:

12 9 2 * +

The * would apply to the previous two values 9 and 2. The result then replaces the two values used in the multiple. So we end up with

12 18 +

So the 12 and 18 would apply to the + operation which would give a result of 30.

30

If you have an operator such as sqrt that takes only one parameter it would be as follows (assume s is the operator for square root:

144 s

The result would be

12

How to start:

So the header structure is:

struct Stack

{

struct NumberEntry *firstEntry;

};

The second structure is used for the stack entries:

struct NumberEntry

{

double value;

struct NumberEntry *nextEntry;

};

Assuming these two structures the general processing we will be as follows: Create structure Stack and set the firstEntry field to NULL. Call this the stack

Open a file (inputFile)

If (input file is not open) Display an error message

Else

While (not end of file for inputFile)

Read the next input-token from the file into a character array value called token

If (token is operator sqrt)

Get the top value from the stack pop the value from the stack

Take the sqrt of the value

Push the result onto the stack

Else if (token is +, -, *, / or p) Get the top value as value1

Pop the value from the stack

Get the top value as value2

Pop the value from the stack

Do the operation on the values (which order should the

values be?)

Push the result onto the stack

Else if (token is q)

Get the top value and display it

Pop off all remaining values from the stack

Else

Convert the text to a number (of type double) Push the double number onto the stack

End if

End while

Close the file

End if

For the Stack implementation you need to write the following functions:

// stack functions

// top returns the value from the top of the stack.

// Results are invalid if the stack is empty double top(struct Stack *stack);

// put a new value at the top of the stack

void push(struct Stack *stack, double number);

// remove the top entry from the stack void pop(struct Stack *stack);

// is the stack empty? Returns true if it is empty and false otherwise

bool empty(struct Stack *stack);

// remove any remaining values from the stack void clear(struct Stack *stack);

// display the results of the stack. This function has been written

// for you                

void dumpStack(struct Stack *stack);

// use this to test the stack

You should structure the rest of your program to implement the calculator. The q command must output the result from the stack and then clear out any remaining entries (there should not be any if all is going well).

Explanation / Answer

I have written the c code for the same:

working c code:

#include <stdio.h>
#include <string.h>

int top = -1;
int stack[100];

/* push the given data into the stack */
void push (int data) {
stack[++top] = data;
}

/* Pop the top element from the stack */
int pop () {
int data;
if (top == -1)
return -1;
data = stack[top];
stack[top] = 0;
top--;
return (data);
}

int main() {
char str[100];
int i, data = -1, operand1, operand2, result;
/* Get the postfix expression from the user */
printf("Enter ur postfix expression:");
fgets(str, 100, stdin);
for (i = 0; i < strlen(str); i++) {
if (isdigit(str[i])) {
/*
* if the i/p char is digit, parse
* character by character to get
* complete operand
*/
data = (data == -1) ? 0 : data;
data = (data * 10) + (str[i] - 48);
continue;
}

if (data != -1) {
/* if the i/p is operand, push it into the stack */
push(data);
}

if (str[i] == '+' || str[i] == '-'
|| str[i] == '*' || str[i] == '/') {
/*
* if the i/p is an operator, pop 2 elements
* from the stack and apply the operator
*/
operand2 = pop();
operand1 = pop();
if (operand1 == -1 || operand2 == -1)
break;
switch (str[i]) {
case '+':
result = operand1 + operand2;
/* push the result into the stack */
push(result);
break;
case '-':
result = operand1 - operand2;
push(result);
break;
case '*':
result = operand1 * operand2;
push(result);
break;
case '/':
result = operand1 / operand2;
push(result);
break;
}
}
data = -1;
}
if (top == 0)
printf("Output:%d ", stack[top]);
else
printf("u have given wrong postfix expression ");
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