Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For this assignment you have to write a c program that will take an infix expression as input and display the postfix expression of the

For this assignment you have to write a c program that will take an infix expression as input and display the postfix expression of the input. After converting the postfix

expression, the program should evaluate the expression from the postfix and display the result.

Problem

We as humans write math expression in infix notation, e.g. 5 + 2 (the operators are written in-between the operands). In computers language however, it is preferred to

have the operators on the right side of the operands, i.e. 5 2 +. For more complex expressions that include parenthesis and multiple operators, a compiler has to convert the expression into postfix first and then evaluate the resulting postfix.

Write a program that takes an infix expression as input, uses stacks to convert it into postfix expression, and finally evaluates it. It must support the following operations:

+ - / * ^ % (

Example

Infix expression:

(7 3) / (2 + 2)

Postfix expression:

7 3 - 2 2 + /

Result: 1

Rubric:

1) If code does not compile: 0

2) Checking the balance of the parenthesis: 2 point

3) Incorrect postfix expression per test case: - 2 points

4) Correct postfix but incorrect value per test case: - 1 points

5) Handling single digit inputs: maximum 13 point

6) Handling two-digit inputs: 100 percent (if pass all test cases)

Some hints (but it is not the only way):

1. You will need to use stacks in three places

a.One for parenthesis check [char stack]

b.One during infix to postfix [char stack]

c. One during evaluation [int stack]

For a and b above, you can use same array and same push, pop method as both of them are char. But for evaluation you have int stack and you might consider to create

another push pop method to handle it. Maybe push_int, pop_int, etc.

2.You can create a function for obtaining operator priority. It should take an operator as input and return its precedence as an integer.

3. During evaluation you will need to convert char into integer.

Example for single digit:

char c = '5';

int x = c - '0';

The code I have below does convert infix expressions into postfix expressions but it doesn't evaluate the answer

what should i add or change in order for the code to evaluate the answer of the expression after finding the postfix expression

here's the code

#include

char stack[20];

int top = -1;

void push(char x)

{

stack[++top] = x;

}

char pop()

{

if(top == -1)

return -1;

else

return stack[top--];

}

int priority(char x)

{

if(x == '(')

return 0;

if(x == '+' || x == '-')

return 1;

if(x == '*' || x == '/')

return 2;

}

main()

{

char exp[20];

char *e, x;

printf("Enter the expression :: ");

scanf("%s",exp);

e = exp;

while(*e != '\0')

{

if(isalnum(*e))

printf("%c",*e);

else if(*e == '(')

push(*e);

else if(*e == ')')

{

while((x = pop()) != '(')

printf("%c", x);

}

else

{

while(priority(stack[top]) >= priority(*e))

printf("%c",pop());

push(*e);

}

e++;

}

while(top != -1)

{

printf("%c",pop());

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Mastering Apache Cassandra 3 X An Expert Guide To Improving Database Scalability And Availability Without Compromising Performance

Authors: Aaron Ploetz ,Tejaswi Malepati ,Nishant Neeraj

3rd Edition

1789131499, 978-1789131499

More Books

Students also viewed these Databases questions