Title here
Summary here
isdigit()
is needed to check the numbers, which will be available in <ctype.h>
int stack[100]
: A fixed-size stack to hold the operands and results of operations.int top = -1
: This variable points to the top of the stack.char exp[100]
: Array to store the input postfix expression.char *e
: Pointer used to traverse the exp[]
array.int conv
: Variable used to hold the converted integer value from the character.int n1, n2, n3
: Variables to hold the popped values from the stack and the result of operations.int stack[100]
and int top = -1
to manage stack.main
function Declaring char exp[100]
and char *e
to hold the prefix expression and pointer to traverse it.scanf("%s", exp)
e = exp
int conv
to hold converted value of the character in *e
int n1, n2, n3
to hold the values in the popped values and new result.while
loop to traverse through the exp
array while incrementing e
*e == '\0'
, So runs when while ( *e != '\0' )
*e
is a number by passing to isdigit()
from <ctype.h>
int
by subtracting its ASCII value with 48
as number 0
has value of 48
, then storing it in conv
conv
to the stack
.int
from the stack has to be popped and operated on. (there will be no (
or )
in postfix evaluation)n1
and n2
char
and direct operation cannot happen.*e
using switch case
or if else
and perform the relevant operation on n2
and n1
. Store result in n3
n3
is pushed into the stack.Once the iteration of the exp[]
is done, the stack
will have the last value which will be the result. It can be accessed as stack[top]
as top will be 0
or by using pop()
x = stack[++top]
top == -1
and return -1
stack[top--]
n1, n2, n3
integers and the *e
character.*e
with operators and performs the appropriate operation.n3
, n3 = n2 + n1
etcn3
is pushed into the stack#include <stdio.h>
#include <ctype.h>
int stack[100];
int top = -1;
void push(int x);
int pop();
void eval(int n1, int n2, int n3, char x );
int main()
{
char exp[100];
char *e;
printf("Enter the Postfix Expression: \n");
scanf("%s", exp);
e = exp;
int conv;
int n1, n2, n3;
while ( *e != '\0')
{
if ( isdigit(*e) )
{
conv = *e - 48;
push(conv);
}
else
{
n1 = pop();
n2 = pop();
eval(n1, n2, n3, *e);
}
e++;
}
printf("\nThe Result of the expression %s is %d\n", exp, pop());
return 0;
}
void push(int x)
{
stack[++top] = x;
}
int pop()
{
return stack[top--];
}
void eval(int n1, int n2, int n3, char x )
{
if (x == '+')
n3 = n2 + n1;
else if (x == '-')
n3 = n2 - n1;
else if (x == '*')
n3 = n2 * n1;
else
n3 = n2 / n1;
push(n3);
}
#include <stdio.h>
#include <ctype.h>
int stack[100];
int top = -1;
void push(int x);
int pop();
void eval(int n1, int n2, int *n3, char x);
int main() {
char exp[100];
char *e;
printf("Enter the Postfix Expression: \n");
scanf("%s", exp); // Read the input postfix expression
e = exp; // Pointer to traverse the expression
int conv;
int n1, n2, n3;
while (*e != '\0') { // Loop until the end of the expression
if (isdigit(*e)) { // Check if the character is a digit
conv = *e - '0'; // Convert char to int
push(conv); // Push the integer onto the stack
}
else { // If the character is an operator
n1 = pop(); // Pop first operand
n2 = pop(); // Pop second operand
eval(n1, n2, &n3, *e); // Perform the operation
push(n3); // Push the result back onto the stack
}
e++; // Move to the next character in the expression
}
printf("\nThe Result of the expression %s is %d\n", exp, pop()); // The final result
return 0;
}
// Function to push an integer onto the stack
void push(int x) {
stack[++top] = x; // Increment top and store the value
}
// Function to pop an integer from the stack
int pop() {
return stack[top--]; // Return the value at top and decrement top
}
// Function to evaluate an operation and store the result in n3
void eval(int n1, int n2, int *n3, char x) {
switch (x) {
case '+':
*n3 = n2 + n1; // Addition
break;
case '-':
*n3 = n2 - n1; // Subtraction
break;
case '*':
*n3 = n2 * n1; // Multiplication
break;
case '/':
*n3 = n2 / n1; // Division
break;
default:
printf("Unknown operator %c\n", x); // Handle invalid operators
break;
}
}