C program Requirements: When your program is run, it should read input from an i
ID: 3682093 • Letter: C
Question
C program
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.
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
Explanation / Answer
#include<stdio.h> //For standard input/output functions
#include<stdlib.h> //For standard input/output functions
#include<conio.h> //For Console related functions
char stack[40]; //Stack is being defined
int top=-1; //Setting the initial value of stack top element to '-1'
char pop() //Declaring a function to pop from the stack
{
char a;
a=stack[top];
top--;
return a; //Function pop() will return the popped value
}
void push(char item) //Function for pushing the value in stack
{
top++;
stack[top]=item;
}
int prcd(char symbol) //For finding the precedence value of the given arithmetic operation
{
switch(symbol)
{
case '+':
case '-':return 2;
break;
case '*':
case '/':return 4;
break;
case '^':
case '$':return 6;
break;
case '(':
case ')':
case '#':return 1;
break;
}
}
int isoperator(char symbol) //function to check the item is a operator or not
{
switch(symbol)
{
case '+':
case '-':
case '*':
case '/':
case '^':
case '$':
case '(':
case ')':return 1;
break;
default:return 0;
}
}
void convertip(char infix[],char postfix[]) //Function to convert infix expression to postfix
{
int i,symbol,j=0;
stack[++top]='#';
for(i=0;i{
symbol=infix[i];
if(isoperator(symbol)==0)
{
postfix[j]=symbol;
j++;
}
else{
if(symbol=='(')push(symbol);
else if(symbol==')')
{
while(stack[top]!='(')
{
postfix[j]=pop();
j++;
}
pop();
}
else{
if(prcd(symbol)>prcd(stack[top]))
push(symbol);
else{
while(prcd(symbol)<=prcd(stack[top]))
{
postfix[j]=pop();
j++;
}
push(symbol);
}
}
}
}
while(stack[top]!='#')
{
postfix[j]=pop();
j++;
}
postfix[j]='';
}
int main() //Start of main() function
{
char infix[20],postfix[20];
printf("Enter the valid infix string:");
gets(infix); //User enters the infix expression
convertip(infix,postfix);
printf("The corresponding postfix string is: ");
puts(postfix); //Program prints the output postfix statement
getche(); //For stopping the execution of program after a keystroke
} //End of main
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.