logo Use CA10RAM to get 10%* Discount.
Order Nowlogo
(5/5)

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 to the postfix expression.

INSTRUCTIONS TO CANDIDATES
ANSWER ALL QUESTIONS

Total points: 13

Read all the pages before starting to write your code

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 to the postfix expression, the program should evaluate the expression from the postfix and display the result. Your solution should follow a set of requirements to get credit.

What should you submit?

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

Please include the following commented lines in the beginning of your code to declare your authorship of the code:

Compliance with Rules: UCF Golden rules apply towards this assignment and submission. Assignment rules mentioned in syllabus, are also applied in this submission. The TA and Instructor can call any students for explaining any part of the code in order to better assess your authorship and for further clarification if needed.

Remember: Sharing code to anyone is a violation of the policy. Also, getting a part of code from anywhere will be considered as cheating.

 

Deadline:

See the deadline in Webcourses. The assignment will accept late submission up to 24 hours after the due date time with 20% penalty. After that the assignment submission will be locked. An assignment submitted by email will not be graded and will not be replied according to the course policy.

What to do if you need clarification?

Write an email to the TA and put the course teacher in the cc for clarification on the requirements.

How to get help if you are stuck?

According to the course policy, all the helps should be taken during office hours (not by email).

Problem

We as humans write math expression in infix notation, e.g. 5 + 2 (the operators are written in- between the operands). In computer’s 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, the computer 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. Read further to understand the requirements.

Input Description:

 

  • Input will be an infix expression as a string with maximum length of

  • The expression will contain only positive integer numbers (no negative or floating point number)

  • The numbers can be single or multiple digits

  • The expression will contain only the following operators and parenthesis: +, - ,      / , * , ^ ,

% , (, )

  • The expression might contain imbalance parenthesis (so you have to check it before starting the conversion. If it is not balanced, you should print a message and stop the conversion process for that incorrect infix expression)

  • The expression can have whitespace between symbols, but not within a multiple digit number. For example, 56 + 7, or, 56+7, both are valid input., But 5 6+7 will not be used in input

Base Requirements for the assignment:

  1. You must have to use Stack during the conversion and evaluation process the way we learned in the

  2. The main function of the code should look like this with some modification. Note that you will need to declare some variables appropriately (for example, str, postfix, result, etc.). You can add maximum 10-15 more lines of codes in the main function as needed for your logic, free any allocated memory, :

int main(void)

{

while(strcmp(str = menu(), "exit")!=0)

{

if (isBalancedParenthesis(str))

{

postFix = convertToPostfix(str); printf("Output: %s", postfix); evaluate(postFix);

 

}

 

else

printf("Parenthesis imbalanced");

 

}

return 0;

}

 

 

In addition to the other functions for multiple stacks or any other function as you wish, you have to write and utilize at least the following functions in your solution:

 

  1. char* menu(): This function displays a menu. e for entering an infix, x for exiting the program.

 

If the user chooses e, it takes an infix as input, copy it into a dynamically allocated string and return it to the main function. If the user chooses x, it will copy ‘exit’ to a dynamically allocated string and return it.

 

  1. int isBalancedParenthesis(char *): This function takes an infix expression and check the balance of the parenthesis. It returns 1, if it is balanced and 0

 

  1. char* convertToPostfix(char *): This function takes the infix expression as string and converts it in to postfix. Note that this function can call other functions too to take help during this

 

  1. void evaluateix(char *): This function takes the postfix expression as the parameter and evaluate the expression. At the end, it prints the result of the evaluation in a new line. Note that this function can call other functions too, to take help during this

 

  1. int isOperator(char c): this function takes a char and returns 1 if the char is an operator. Otherwise, it returns 0;

 

  1. int getOperatorPriority(char c): this function takes an operator and returns its priority

 

  1. int convetToInt(char c): this function converts a char digit into int digit

 

  1. int calculate(int a, int b, char op): this function takes to operand and one operator and returns the result of the operation based on op; Example: calculate( 5, 6, ‘+’) will return 11

 

Some Example test expression Example 1

Infix expression: (7 - 3) / (2 + 2)) Parenthesis imbalanced

 

Example 2

(5+6)*7-8*9

 

output: 5 6 + 7 * 8 9 * -

 

evaluation: 5

 

Example 3:

Infix expression: (7 - 3) / (2 + 2)

 

Postfix expression: 7 3 - 2 2 + /

 

evaluation: 1

 

Example 4:

Infix expression: 3+(4*5-(6/7^8)*9)*10, output: 3 4 5 * 6 7 8 ^ /9 * - 10 * +

evaluation: 203

 

Example 5:

Infix expression: 1000 + 2000

output: 1000 2000 +

evaluation: 3000

 

Rubric:

The code will be compiled and tested in Eustis server while grading. If your code does not compile in Eustis, we conclude that your code is not compiling and it will be graded accordingly. We will apply a set of test cases to check whether your code can produce the expected output or not. Failing each test case will reduce some grade based on the rubric given bellow. There is no grade just for writing the above functions, if your code fails all the test cases. However, if your code passes all the test cases, then -2 penalties will be applied for not implementing each function mentioned above. In summary: in order to get full credit for this assignment, you have to implement all the required functions and the code has to pass all the test cases.

 

  • If the code does not compile in Eustis server:

  • If your code has missing any function explained above but passes all our test cases, -2 will be deducted for each missing function. However, there is no additional grade just for implementing all the functions. The code will be tested with varieties of inputs).

 

  • Checking the balance of the parenthesis: 2 points // Not displaying incorrect parenthesis - 2 points. You should check and stop your processing an infix if the parenthesis is imbalanced

  • Incorrect postfix expression per test case: -2 points //there will be test cases with single and multiple

  • Correct postfix but incorrect evaluation per test case: -1 points

  • Handling single digit inputs: maximum 10 point

  • Handling two or more-digit inputs: 100 percent (if pass all test cases)

 

 

 

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

 

  1. Implementing all the above functions will help you to solve the problems in a structured way.

  2. Use the uploaded multi stack code for stack implementation and modify the code as you need.

  3. You will need to use stacks in three places

    1. One for the parenthesis check [char stack]

    2. One during infix to postfix [char stack]

    3. 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. Or find other strategy to utilize existing push pop method

  1. You can use isdigit function to check whether a char is a digit or not

  2. During evaluation you will need to convert char into Example for single digit

(5/5)
Attachments:

Related Questions

. Introgramming & Unix Fall 2018, CRN 44882, Oakland University Homework Assignment 6 - Using Arrays and Functions in C

DescriptionIn this final assignment, the students will demonstrate their ability to apply two ma

. The standard path finding involves finding the (shortest) path from an origin to a destination, typically on a map. This is an

Path finding involves finding a path from A to B. Typically we want the path to have certain properties,such as being the shortest or to avoid going t

. Develop a program to emulate a purchase transaction at a retail store. This program will have two classes, a LineItem class and a Transaction class. The LineItem class will represent an individual

Develop a program to emulate a purchase transaction at a retail store. Thisprogram will have two classes, a LineItem class and a Transaction class. Th

. SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of Sea Ports. Here are the classes and their instance variables we wish to define:

1 Project 1 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of

. Project 2 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of Sea Ports. Here are the classes and their instance variables we wish to define:

1 Project 2 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of

Ask This Question To Be Solved By Our ExpertsGet A+ Grade Solution Guaranteed

expert
Um e HaniScience

500 Answers

Hire Me
expert
Muhammad Ali HaiderFinance

588 Answers

Hire Me
expert
Husnain SaeedComputer science

647 Answers

Hire Me
expert
Atharva PatilComputer science

846 Answers

Hire Me
August
January
February
March
April
May
June
July
August
September
October
November
December
2025
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
SunMonTueWedThuFriSat
27
28
29
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
5
6
00:00
00:30
01:00
01:30
02:00
02:30
03:00
03:30
04:00
04:30
05:00
05:30
06:00
06:30
07:00
07:30
08:00
08:30
09:00
09:30
10:00
10:30
11:00
11:30
12:00
12:30
13:00
13:30
14:00
14:30
15:00
15:30
16:00
16:30
17:00
17:30
18:00
18:30
19:00
19:30
20:00
20:30
21:00
21:30
22:00
22:30
23:00
23:30