0% found this document useful (0 votes)
4 views5 pages

Postfix Temp

The document contains a C program that evaluates postfix expressions using a stack data structure. It includes functions for pushing and popping elements from the stack, as well as evaluating the expression based on operators and operands. The program handles errors such as stack overflow, underflow, and division by zero.

Uploaded by

nihar.sbmp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

Postfix Temp

The document contains a C program that evaluates postfix expressions using a stack data structure. It includes functions for pushing and popping elements from the stack, as well as evaluating the expression based on operators and operands. The program handles errors such as stack overflow, underflow, and division by zero.

Uploaded by

nihar.sbmp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

#include <stdio.

h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>

#define SIZE 100

int stack[SIZE];
int top = -1;

// Push an element onto the stack


void push(int value) {
if (top == SIZE - 1) {
printf("Stack Overflow!\n");
exit(1);
}
stack[++top] = value;
}

// Pop an element from the stack


int pop() {
if (top < 0) {
printf("Stack Underflow!\n");
exit(1);
}
return stack[top--];
}

// Evaluate the postfix expression


int evaluate_postfix(const char *expression) {
int i = 0;

while (expression[i] != '\0') {


char symbol = expression[i];

// Skip spaces
if (isspace(symbol)) {
i++;
continue;
}

// If it's a number (or negative number)


if (isdigit(symbol) || (symbol == '-' && isdigit(expression[i + 1]))) {
int number = 0;
if (symbol == '-') i++; // Skip minus sign for negative numbers

// Read the number


while (isdigit(expression[i])) {
number = number * 10 + (expression[i] - '0');
i++;
}

if (expression[i-1] == '-') number = -number; // Apply negative sign


push(number);
}
// If it's an operator
else if (symbol == '+' || symbol == '-' || symbol == '*' || symbol == '/'
|| symbol == '^') {
int operand2 = pop();
int operand1 = pop();
int result;

switch (symbol) {
case '+': result = operand1 + operand2; break;
case '-': result = operand1 - operand2; break;
case '*': result = operand1 * operand2; break;
case '/':
if (operand2 == 0) {
printf("Division by zero!\n");
exit(1);
}
result = operand1 / operand2;
break;
case '^': result = (int)pow(operand1, operand2); break;
}
push(result);
}
else {
printf("Invalid character: %c\n", symbol);
exit(1);
}

i++;
}

return pop(); // The result is the only value left in the stack
}

int main() {
char expression[SIZE];
printf("Enter Postfix Expression: ");
fgets(expression, sizeof(expression), stdin);

// Remove newline character if present


expression[strcspn(expression, "\n")] = '\0';

int result = evaluate_postfix(expression);


printf("Result = %d\n", result);

return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#define SIZE 100

int stack[SIZE], top = -1;

void push(int value)


{
if (top == SIZE - 1)
{
printf("Stack Overflow!\n");
exit(1);
}
stack[++top] = value;
}

int pop() {
if (top < 0)
{
printf("Stack Underflow!\n");
exit(1);
}
return stack[top--];
}

int evaluate_postfix(const char *expr)


{
int i = 0;

while (expr[i])
{
if (isspace(expr[i]))
{
i++;
continue;
}

if (isdigit(expr[i]) || (expr[i] == '-' && isdigit(expr[i + 1]))) {


int num = 0;

if (expr[i] == '-')
{
i++;
}

while (isdigit(expr[i]))
{
num = num * 10 + (expr[i++] - '0');
}

if (expr[i-1] == '-') num = -num;

push(num);
}
else if (strchr("+-*/^", expr[i]))
{
int b = pop(), a = pop();
int result;

switch (expr[i])
{
case '+': result = a + b; break;
case '-': result = a - b; break;
case '*': result = a * b; break;
case '/':
if (b == 0)
{
printf("Division by zero!\n");
exit(1);
}
result = a / b;
break;
case '^': result = (int)pow(a, b); break;
default:
printf("Invalid operator: %c\n", expr[i]);
exit(1);
}

push(result);
}
else
{
printf("Invalid character: %c\n", expr[i]);
exit(1);
}

i++;
}

return pop(); // The result is the only value left in the stack
}
int main() {
char expr[SIZE];
printf("Enter Postfix Expression: ");
fgets(expr, SIZE, stdin);

expr[strcspn(expr, "\n")] = '\0';

printf("Result = %d\n", evaluate_postfix(expr));


return 0;
}

You might also like