Question
Language C Since there are a couple tricky parts about reading and parsing an RPN expressions, you will be given code that will prompt the
Language C
Since there are a couple tricky parts about reading and parsing an RPN expressions, you will be given code that will prompt the user, and parse the expression into a linked list. You will be responsible for writing code to process the linked list which will evaluate the expression. You will also be given code to manipulate a linked list.
Your program needs to handle the same operators that you used in assignment 1, addition (+), subtraction (-), multiplication (*), division (/), and exponentiation (^).
What to submit
A zip file of all of your files, including the starting code.
Example Output
Use input is in bold.
$ ./a.out Please enter an RPN expression: 1 0 / Interpreting input as: (1.00 / 0.00) Error: Requested division by 0 Result: 0.00 $ ./a.out Please enter an RPN expression: 0 0 ^ Interpreting input as: (0.00 ^ 0.00) Error: 0^0 is indeterminate Result: 0.00 $ ./a.out Please enter an RPN expression: 1 1 + Interpreting input as: (1.00 + 1.00) Result: 2.00 $ ./a.out Please enter an RPN expression: 1 1 - Interpreting input as: (1.00 - 1.00) Result: 0.00 $ ./a.out Please enter an RPN expression: 1 2 * Interpreting input as: (1.00 * 2.00) Result: 2.00 $ ./a.out Please enter an RPN expression: 1 2 / Interpreting input as: (1.00 / 2.00) Result: 0.50 $ ./a.out Please enter an RPN expression: 2 3 ^ Interpreting input as: (2.00 ^ 3.00) Result: 8.00 $ ./a.out Please enter an RPN expression: 1 2 3 + * Interpreting input as: (1.00 * (2.00 + 3.00)) Result: 5.00 $
Hints
Make sure to look at the provided header file RPN_linked_list.h. It includes a definition of a RPN_Node struct, and several functions to manipulate a linked list.
To push a number onto a linked list, use the function push. It can be used like:
push(&stack, value);
To pop a number off a linked list, use the function pop. It can be used like:
double value = pop(&stack);
The function prompt_and_parse() will correctly validate an RPN expression, so you do not need to worry about handling input that is invalid. You only need to error check situations like division by 0 and 0^0.
The functions push and pop will correctly manage memory for you, however you still need to free memory for any linked list you use or create. An example of how to properly free the memory is in the starting code, in particularly it correctly frees the linked list named head in the main function.
To help you understand what is in a linked list at any time, use the provided print function. It can be used like:
print_list(head);
It is recommended you put some of these into your program while working so you can understand how the linked lists is structured.
There is a provided function print_as_infix which will take in a linked list in the format that prompt_and_parse gives you. This will print out the expression in the more familiar infix notation so that you can verify your calculator works correctly. The starting code calls this function.
Don't forget that when using multiple files, all .c files must be included on the command line. For example, you should use
clang -Wall file1.c file2.c
or
gcc -Wall file1.c file2.c
depending on the operating system you are using.
Starting Code
In addition to the below code, you will want two files: RPN_linked_list.h and RPN_linked_list.c. Put them into the same directory as your assignment code. You should not modify either of those files.
#include#include #include #include "RPN_linked_list.h" double evaluate_rpn_expression(RPN_Node *head); int main() { RPN_Node *head = prompt_and_parse(); printf("Interpreting input as: "); print_as_infix(head); double result = evaluate_rpn_expression(head); printf("Result: %.2lf ", result); free_list(head); head = NULL; return 0; } double evaluate_rpn_expression(RPN_Node *head) { // This function should walk through the linked list specified by head. // It should also declare another linked list, named stack. // // For each term in the input list, it should check if it is a value or // an operator. If it is a value, it should push it on the stack. // If it is an operator, it should pop two terms off the stack and // use those as the operands to the operator. Be careful of the order of // the terms since for some operators that matters. // // After processing the list, the stack will have one element, which is // the answer. Store it in a double, free the stack and return the answer. return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started