Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

In the C Programming language please help me solve this assignment. I am looking not only how to code this but also lots of comments

In the C Programming language please help me solve this assignment. I am looking not only how to code this but also lots of comments explaining the purpose and function of your code. I understand that this is a lot of work but I am looking to learn while reviewing the code so that I can understand it better. Please remember to code in C only.

Problem (Write in C) We as humans write math expression in infix notation, e.g. 5 + 2 (the operators are written inbetween 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, 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 50. - 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 use Stack during the conversion and evaluation process. 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, etc.:

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:

a) 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. b) 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 otherwise. c) 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 process. d) 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 process. e) int isOperator(char c): this function takes a char and returns 1 if the char is an operator. Otherwise, it returns 0; f) int getOperatorPriority(char c): this function takes an operator and returns its priority g) int convetToInt(char c): this function converts a char digit into int digit h) 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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions