Saturday, 26 March 2016

Check your skill in C (Dynamical Stack)

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 thesample 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.

No comments:

Post a Comment