Write a C program that evaluates a postfix expression, such as 6 2 + 5 * 8 4 / -
ID: 3703539 • Letter: W
Question
Write a C program that evaluates a postfix expression, such as 6 2 + 5 * 8 4 / -The program should read a postfix expression consisting of single digits and operators into a character array. Using the stack functions implemented earlier in this project (project-1), the program should scan the expression and evaluate it. The algorithm can be as follows:
1) Append the null character ('') to the end of the postfix expression. When the null character is encountered, no further processing is necessary.
2) While '' has not been encountered, read the expression from left to right.
If the current character is a digit, push its integer value onto the stack (the integer value of a digit character is its value in the computer’s character set minus the value of '0' in the computer’s character set). Otherwise, if the current character is an operator, Pop the two top elements of the stack into variables x and y. Calculate y operator x. Push the result of the calculation onto the stack. 3) When the null character is encountered in the expression, pop the top value of the stack. that will be a result of the postfix expression.
Write a C program that evaluates a postfix expression, such as 6 2 + 5 * 8 4 / -
The program should read a postfix expression consisting of single digits and operators into a character array. Using the stack functions implemented earlier in this project (project-1), the program should scan the expression and evaluate it. The algorithm can be as follows:
1) Append the null character ('') to the end of the postfix expression. When the null character is encountered, no further processing is necessary.
2) While '' has not been encountered, read the expression from left to right.
If the current character is a digit, push its integer value onto the stack (the integer value of a digit character is its value in the computer’s character set minus the value of '0' in the computer’s character set). Otherwise, if the current character is an operator, Pop the two top elements of the stack into variables x and y. Calculate y operator x. Push the result of the calculation onto the stack. 3) When the null character is encountered in the expression, pop the top value of the stack. that will be a result of the postfix expression.
Write a C program that evaluates a postfix expression, such as 6 2 + 5 * 8 4 / -
The program should read a postfix expression consisting of single digits and operators into a character array. Using the stack functions implemented earlier in this project (project-1), the program should scan the expression and evaluate it. The algorithm can be as follows:
1) Append the null character ('') to the end of the postfix expression. When the null character is encountered, no further processing is necessary.
2) While '' has not been encountered, read the expression from left to right.
If the current character is a digit, push its integer value onto the stack (the integer value of a digit character is its value in the computer’s character set minus the value of '0' in the computer’s character set). Otherwise, if the current character is an operator, Pop the two top elements of the stack into variables x and y. Calculate y operator x. Push the result of the calculation onto the stack. 3) When the null character is encountered in the expression, pop the top value of the stack. that will be a result of the postfix expression.
Explanation / Answer
#include<stdio.h>
//ctype is used to know isdigit() method
#include<ctype.h>
int stack[20];
int top = -1;
void push(int x)
{
stack[++top] = x;
}
int pop()
{
return stack[top--];
}
void main()
{
char exp[] = "62+5*84/-";
char *e;
int n1,n2,n3,num;
e = exp;
while(*e != '')
{
if(isdigit(*e))
{
num = *e - 48; //coverting char to int
push(num);
}
else
{
n1 = pop();
n2 = pop();
switch(*e)
{
case '+':
{
n3 = n1 + n2;
break;
}
case '-':
{
n3 = n2 - n1;
break;
}
case '*':
{
n3 = n1 * n2;
break;
}
case '/':
{
n3 = n2 / n1;
break;
}
}
push(n3);
}
e++;
}
printf("The result of expression %s = %d",exp,pop());
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.