Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribedimage text in transcribedimage text in transcribed

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: Problem printf("Parenthesis imbalanced"); We as humans write math expression in infix notation, eg 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. 52. 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. 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: Input Description: a) char* menu(): This function displays a menu e for entering an infix, x for exiting the program. - 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: 1, If the user choosese, it takes an infix as input, copy it into a dynamically allocated string and retum 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 otherwise, 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 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) vold 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. Base Requirements for the assignment: 1. You must have to use Stack during the conversion and evolution process the way we learned in the class. e) int is Operator(char e): this function takes a char and retums 1 if the chur is an operator Otherwise, it returns 0; D) int getOperator Priority (char e): this function takes an operator and returns its priority 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, slr, 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, ctc.: ) int conved Teint(char e)this function converts a char digit into int digil int in void) b) int calculatefint , int , 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 while(strcmp(strmenu). "exit")-0) Some Example test expression if(isBalancedParenthesis(str)) postfix - Co o Postfix print "Output %s".posti): evaluatiepostFix Example 1 Infis expression: (7-3)/(2+2)) Parenthesis imbalanced Example 2 (5+6)*7-899 output: 56.789. 3) 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 4) Incorrect postfix expression per test case:-2 points there will be test cases with single and multiple digits. 5) Correct postfix but incorrect evaluation per test case: -1 points 6) Handling single digit inputs:maximum 10 point 7) Handling two or more-digit inputs: 100 percent (if pass all test cases) evaluation: 5 Example 3: Infix expression: (7-3)/(2+2) Postfix expression: 73.2 2 +/ evaluation: 1 Read it: Some hints (but it is not the only way) Example 4: Infix expression: 3+[4*5- (6/7^B)"9)*10, output: 3 4 5 6 7 8 ^/9. 10. evaluation: 203 Example 5: 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 stocks in three places a. One for the parenthesis check (char slack) b. One during infix to postfix char slackl c. One during evaluation fint stack] For a and b above, you can use some 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 nush int, pop int, etc. Or find other strategy to utilize existing push pop method 4. You can use isdigit function to check whether a charis a digit or not 5. During evaluation you will need to convert char into integer. Example for single digit: chure intx- .0: Intix expression: 1000 + 2000 output: 1000 2000 + evaluation: 3000 Rubric: See uploaded code String2 Integer Example.e file for more example and testing. Also, the string exercise solution has a similar example of the end The code will be compiled and tested in Hustis server while gracing. 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 fils all the test cases. However, if your code passes all the test cases, then-2 penalties will be applied for nor implementing each finction mentioned abowe. In summary: in order to get full credit for this assignment, you have to implement all the required functions and the oude has to pass all the test cases Please see the lecture slides for the explanation, steps and more test examples. Good Luck 1) If the code does not compile in Fustis server: 0. 2) If your code has missing any function explained above but passes all our test cases. -2 will be doducted for cach missing function. However, there is no additional grade just for implementing all the functions. The code will be tested with varieties of inputs) 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: Problem printf("Parenthesis imbalanced"); We as humans write math expression in infix notation, eg 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. 52. 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. 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: Input Description: a) char* menu(): This function displays a menu e for entering an infix, x for exiting the program. - 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: 1, If the user choosese, it takes an infix as input, copy it into a dynamically allocated string and retum 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 otherwise, 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 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) vold 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. Base Requirements for the assignment: 1. You must have to use Stack during the conversion and evolution process the way we learned in the class. e) int is Operator(char e): this function takes a char and retums 1 if the chur is an operator Otherwise, it returns 0; D) int getOperator Priority (char e): this function takes an operator and returns its priority 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, slr, 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, ctc.: ) int conved Teint(char e)this function converts a char digit into int digil int in void) b) int calculatefint , int , 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 while(strcmp(strmenu). "exit")-0) Some Example test expression if(isBalancedParenthesis(str)) postfix - Co o Postfix print "Output %s".posti): evaluatiepostFix Example 1 Infis expression: (7-3)/(2+2)) Parenthesis imbalanced Example 2 (5+6)*7-899 output: 56.789. 3) 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 4) Incorrect postfix expression per test case:-2 points there will be test cases with single and multiple digits. 5) Correct postfix but incorrect evaluation per test case: -1 points 6) Handling single digit inputs:maximum 10 point 7) Handling two or more-digit inputs: 100 percent (if pass all test cases) evaluation: 5 Example 3: Infix expression: (7-3)/(2+2) Postfix expression: 73.2 2 +/ evaluation: 1 Read it: Some hints (but it is not the only way) Example 4: Infix expression: 3+[4*5- (6/7^B)"9)*10, output: 3 4 5 6 7 8 ^/9. 10. evaluation: 203 Example 5: 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 stocks in three places a. One for the parenthesis check (char slack) b. One during infix to postfix char slackl c. One during evaluation fint stack] For a and b above, you can use some 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 nush int, pop int, etc. Or find other strategy to utilize existing push pop method 4. You can use isdigit function to check whether a charis a digit or not 5. During evaluation you will need to convert char into integer. Example for single digit: chure intx- .0: Intix expression: 1000 + 2000 output: 1000 2000 + evaluation: 3000 Rubric: See uploaded code String2 Integer Example.e file for more example and testing. Also, the string exercise solution has a similar example of the end The code will be compiled and tested in Hustis server while gracing. 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 fils all the test cases. However, if your code passes all the test cases, then-2 penalties will be applied for nor implementing each finction mentioned abowe. In summary: in order to get full credit for this assignment, you have to implement all the required functions and the oude has to pass all the test cases Please see the lecture slides for the explanation, steps and more test examples. Good Luck 1) If the code does not compile in Fustis server: 0. 2) If your code has missing any function explained above but passes all our test cases. -2 will be doducted for cach missing function. However, there is no additional grade just for implementing all the functions. The code will be tested with varieties of inputs)

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_2

Step: 3

blur-text-image_3

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

Question

=+b. All plants are completed by the contract date.

Answered: 1 week ago

Question

3. Contrast relational contexts in organizations

Answered: 1 week ago

Question

2. Describe ways in which organizational culture is communicated

Answered: 1 week ago

Question

1. Describe and compare approaches to managing an organization

Answered: 1 week ago