0% found this document useful (0 votes)
10 views1 page

5 A

Uploaded by

darshangowda0525
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)
10 views1 page

5 A

Uploaded by

darshangowda0525
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/ 1

#include<stdio.

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

double compute(char symbol, double op1, double op2) {


switch(symbol) {
case '+': return op1 + op2;
case '-': return op1 - op2;
case '*': return op1 * op2;
case '/': return op1 / op2;
case '$':
case '^': return pow(op1, op2);
default: return 0;
}
}

void main() {
double s[20], res, op1, op2;
int top, i;
char postfix[20], symbol;

printf("\nEnter the postfix expression:\n");


fgets(postfix, sizeof(postfix), stdin);

top = -1;

for(i = 0; i < strlen(postfix); i++) {


symbol = postfix[i];
if(isdigit(symbol))
s[++top] = symbol - '0';

else if (symbol == '+' || symbol == '-' || symbol == '*' || symbol == '^' || symbol
== '$') {
if (top<1) {
printf("Error: Insufficient operands\n");
return 1;
}
op2 = s[top--];
opl = s[top--];
res = compute (symbol, opl, op2);
s[++ top] = res;
}
}
if (top == 0) {
res = s[top--];
printf("\n The result is: %f\n", res);
} else {
printf("Error: Too many operands\n");
return 1;
}
return 0;

You might also like