Problem: Write a program that reads an infix or postfix expression from an input
ID: 3567193 • Letter: P
Question
Problem:
Write a program that reads an infix or postfix expression from an input file, evaluates the expression and outputs the result. Must be written in C
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 values 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
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
5. int IsEmpty(stack *s)checks to see if the stack is empty returning O for false and 1 for true.
6. void evaluatePostfix(char[]) which takes a postfix expression as a char array - and writes the expression to the output file
- evaluates the expression using a stack - and writes the integer value obtained to the output files 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).
Explanation / Answer
#include #include #include #include #define MAX 50 struct infix { char target[MAX] ; char stack[MAX] ; char *s, *t ; int top ; } ; void initinfix ( struct infix * ) ; void setexpr ( struct infix *, char * ) ; void push ( struct infix *, char ) ; char pop ( struct infix * ) ; void convert ( struct infix * ) ; int priority ( char ) ; void show ( struct infix ) ; void main( ) { struct infix p ; char expr[MAX] ; initinfix ( &p ) ; clrscr( ) ; printf ( " Enter an expression in infix form: " ) ; gets ( expr ) ; setexpr ( &p, expr ) ; convert ( &p ) ; printf ( " The postfix expression is: " ) ; show ( p ) ; getch( ) ; } /* initializes structure elements */ void initinfix ( struct infix *p ) { p -> top = -1 ; strcpy ( p -> target, "" ) ; strcpy ( p -> stack, "" ) ; p -> t = p -> target ; p -> s = "" ; } /* sets s to point to given expr. */ void setexpr ( struct infix *p, char *str ) { p -> s = str ; } /* adds an operator to the stack */ void push ( struct infix *p, char c ) { if ( p -> top == MAX ) printf ( " Stack is full. " ) ; else { p -> top++ ; p -> stack[p -> top] = c ; } } /* pops an operator from the stack */ char pop ( struct infix *p ) { if ( p -> top == -1 ) { printf ( " Stack is empty. " ) ; return -1 ; } else { char item = p -> stack[p -> top] ; p -> top-- ; return item ; } } /* converts the given expr. from infix to postfix form */ void convert ( struct infix *p ) { char opr ; while ( *( p -> s ) ) { if ( *( p -> s ) == ' ' || *( p -> s ) == ' ' ) { p -> s++ ; continue ; } if ( isdigit ( *( p -> s ) ) || isalpha ( *( p -> s ) ) ) { while ( isdigit ( *( p -> s ) ) || isalpha ( *( p -> s ) ) ) { *( p -> t ) = *( p -> s ) ; p -> s++ ; p -> t++ ; } } if ( *( p -> s ) == '(' ) { push ( p, *( p -> s ) ) ; p -> s++ ; } if ( *( p -> s ) == '*' || *( p -> s ) == '+' || *( p -> s ) == '/' || *( p -> s ) == '%' || *( p -> s ) == '-' || *( p -> s ) == '$' ) { if ( p -> top != -1 ) { opr = pop ( p ) ; while ( priority ( opr ) >= priority ( *( p -> s ) ) ) { *( p -> t ) = opr ; p -> t++ ; opr = pop ( p ) ; } push ( p, opr ) ; push ( p, *( p -> s ) ) ; } else push ( p, *( p -> s ) ) ; p -> s++ ; } if ( *( p -> s ) == ')' ) { opr = pop ( p ) ; while ( ( opr ) != '(' ) { *( p -> t ) = opr ; p -> t++ ; opr = pop ( p ) ; } p -> s++ ; } } while ( p -> top != -1 ) { char opr = pop ( p ) ; *( p -> t ) = opr ; p -> t++ ; } *( p -> t ) = '' ; } /* returns the priority of an operator */ int priority ( char c ) { if ( c == '$' ) return 3 ; if ( c == '*' || c == '/' || c == '%' ) return 2 ; else { if ( c == '+' || c == '-' ) return 1 ; elsereturn 0 ; } } /* displays the postfix form of given expr. */ void show ( struct infix p ) { printf ( " %s", p.target ) ; }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.