Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Total point: 15 Introduction: For this assignment you have to write a c program that will take an infix expression as input and display the

Total point: 15

Introduction: 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.

What should you submit?

Write all the code in a single file and upload the .c file.

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

  1. Checking the balance of the parenthesis: 2 point

  1. Incorrect postfix expression per test case: -2 points

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

  1. Handling single digit inputs: maximum 13 point

  1. 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

  1. One for parenthesis check [char stack]
  2. One during infix to postfix [char stack]

  1. 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.

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

  1. During evaluation you will need to convert char into integer. Example for single digit: char c = '5';

int x = c - '0';

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

Guide To Client Server Databases

Authors: Joe Salemi

2nd Edition

1562763105, 978-1562763107

More Books

Students also viewed these Databases questions

Question

2. In what ways have your peers and your parents helped shape you?

Answered: 1 week ago