Description
Problem:
Write a program that reads an infix or postfix expression from an input file,
evaluates the expression and outputs the result.
Objectives:
The objective of this assignment is for students to demonstrate a working
knowledge of:
Stack implementation using a linked list
Evaluation of postfix notation
Evaluation of infix notation
Requirements:
When your program is run, it should read input from an input file one line at a time.
Each line will begin with the number 1 or 2 followed by a space. 1 indicates that the
expression which follows, is in infix notation; while 2 indicates that the expression
which follows is in post fix notation. Your program should read and process each
line of input, outputting the postfix notation and the value that the expression
evaluates to. Output should be written to a file called output.txt the output file will
consist of all of the calculated output, each written on a new line in the file. Your
program should continue to read and process data from the input file, until a line
beginning with the number 0 is encountered.
The name of your input file will be passed as an argument to main so it should not
be hardcoded in your program
Required functions:
Your program should include the following functions
For the stack implemented as a linked list
1. stack* initStack() : this creates and initializes the values of the stack
implemented as a linked list
2. stack* push(stack *s, char c): takes a stack pointer as a parameter and
adds the character parameter to the top of the stack and returns a pointer to
the updated stack
3. char pop(stack *s): pops the stack and returns the character popped out
4. char peek (stack *s): looks at the top value on the stack and returns the
value seen
5. int IsEmpty(stack *s)checks to see if the stack is empty returning 0 for false
and 1 for true.
6. void evaluatePostfix(char *) which takes a postfix expression as a char
array and an integer indicating the length of the array. The function should:
– write the expression to the output file
– evaluate the expression using a stack
– and write the integer value obtained to the output file as shown in the
sample output
7. void evaluateInfix(char *) which takes an infix expression,
– converts it to the postfix form
– and then evaluates the expression.
– Both the postfix notation and the value it evaluates to, are written to the
output file (as shown in the sample output).
Note that these are the required functions but you are allowed to create additional
helping functions if you wish.
What do you submit?
For this assignment you are required to submit your source code as a single .c file
called ExpEvaluator.c. All submissions are to be made via Webcourses by the
deadline. Email Assignments will not be accepted. There will be NO EXTENSION
FOR THIS ASSIGNMENT. You may choose to submit up to 24 hours late, with
15% grade penalty.
Note: If your program does not compile on Eustis you will receive a maximum score
of 40 if your code has all of the functions. If your code does not compile and there
are missing functions, then you will receive a grade lower than 40.
Expectations for code testing:
Your code will be tested on Eustis. The test file used by the TA will not be the one
given in the sample input. However, it will match the specifications given in the
assignment. For example there may be more or less lines of input but the file
format will be the same and the data will end with a 0.
Sample input.txt
1 (5 + 4)
2 3 4 +
1 2 * 2 /(1 + 3)
0
Sample output file from running code
5 4 + = 9
3 4 + = 7
2 2 * 1 3 + / = 1
Sample2 input.txt
2 7 5 – 2 +
1 3 * (5 – 2 )
1 5/1 + 3
0
Sample output2 file from running code
7 5 – 2 + = 4
3 5 2 – * = 9
5 1 / 3 + = 8
Grading Rubric
Comments and formatting of code……………..…5
Stack methods……………………………………………40
Appropriate naming of variables ……………….. 5
Correct file I/O………………………………………… 5
Correct evaluation of Postfix……………………….10
Correct Evaluation of Infix…………………………..10
Correct output format…………………………………5
Total ………………………………………………………80