#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define SIZE 100
int count=0;
typedef struct node{
int value;
struct stack *next;
struct stack *prev;
}stack;
stack *bottom=NULL;
stack *top=NULL;
stack *createnode(int a)
{
stack *newstack;
newstack
=(stack
*)calloc(1,sizeof(stack
)); newstack->value=a;
newstack->prev=NULL;
newstack->next=NULL;
return newstack;
}
int isempty()
{
if(count==0)
{return 1;}
else
{return 0;}
}
int isfull()
{
if(count==SIZE)
{return 1;}
else
{return 0;}
}
void push(int a)
{ if(isfull()==1)
{
printf("overflow condition: stack full"); }
else
{count+=1;
stack *newstack = createnode(a);
if(bottom==NULL)
{
bottom=newstack;
top=bottom;
}
else
{
top->next=newstack;
newstack->prev=top;
top=newstack;
}
}
}
char peek()
{
return top->value;
}
char pop()
{ if(isempty()==1)
{
printf("underflow condition:empty stack"); }
else
{if(count==1)
{ int c;
c=top->value;
top=NULL;
bottom=NULL;
count=count-1;
return c;
}
else
{int c;
c=top->value;
top=top->prev;
top->next=NULL;
count=count-1;
return c;
}
}
}
char plus=43;
char multi=42;
char divi=47;
char power=94;
char left=40;
char right=41;
char subtract=45;
void *postfix_evaluation(char *string,int length)
{ int i; stack operators;
for(i=0;i<length;i++)
{
char c;
c=string[i];
if(c==plus || c==subtract || c==multi || c==divi || c==power)
{
int a =pop();
int b=pop();
if(c==plus)
{
int result=b+a;
push(result);
// printf("%d",result);
}
if(c==subtract)
{
int result=b-a;
push(result);
}
if(c==multi)
{
int result=b*a;
push(result);
}
if(c==divi)
{
int result=b/a;
push(result);
}
if(c==power)
{
push(result);
}
}
else
{ int k=c-'0';
push(k);
// printf("%d",k);
}
/* if(i==length-1)
{
int d=pop();
printf("%d",d);
}
*/
}
int d=pop();
}
// driver code , the main function
int main(void)
{ char inversion[200];
printf("give the postfix expression\n"); postfix_evaluation(inversion,longer);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgP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VlaygpCnsKICAgIHJldHVybiB0b3AtPnZhbHVlOwp9CmNoYXIgcG9wKCkKICAgIHsgICBpZihpc2VtcHR5KCk9PTEpCiAgICAgICAgewogICAgICAgICBwcmludGYoInVuZGVyZmxvdyBjb25kaXRpb246ZW1wdHkgc3RhY2siKTsKICAgICAgICB9CiAgICAgICAgZWxzZQogICAgICAgIHtpZihjb3VudD09MSkKICAgICAgICB7ICAgaW50IGM7CiAgICAgICAgIGM9dG9wLT52YWx1ZTsKICAgICAgICAgdG9wPU5VTEw7CiAgICAgICAgIGJvdHRvbT1OVUxMOwogICAgICAgICBjb3VudD1jb3VudC0xOwogICAgICAgICByZXR1cm4gYzsKCiAgICAgICAgfQogICAgICAgIGVsc2UKICAgICB7aW50IGM7CiAgICAgYz10b3AtPnZhbHVlOwogIHRvcD10b3AtPnByZXY7CiAgdG9wLT5uZXh0PU5VTEw7CiAgY291bnQ9Y291bnQtMTsKICByZXR1cm4gYzsKCiAgICAgfQogICAgICAgfQogICAgfQogICAgY2hhciBwbHVzPTQzOwogICAgY2hhciBtdWx0aT00MjsKICAgIGNoYXIgZGl2aT00NzsKICAgIGNoYXIgcG93ZXI9OTQ7CiAgICBjaGFyIGxlZnQ9NDA7CiAgICBjaGFyIHJpZ2h0PTQxOwogICAgY2hhciBzdWJ0cmFjdD00NTsKdm9pZCAqcG9zdGZpeF9ldmFsdWF0aW9uKGNoYXIgKnN0cmluZyxpbnQgbGVuZ3RoKQp7ICBpbnQgaTsgc3RhY2sgb3BlcmF0b3JzOwogZm9yKGk9MDtpPGxlbmd0aDtpKyspCiB7CiAgY2hhciBjOwogIGM9c3RyaW5nW2ldOwogIGlmKGM9PXBsdXMgfHwgYz09c3VidHJhY3QgfHwgYz09bXVsdGkgfHwgYz09ZGl2aSB8fCBjPT1wb3dlcikKICB7CiAgICAgICAgaW50IGEgPXBvcCgpOwogICAgICAgIGludCBiPXBvcCgpOwogICAgICAgIGlmKGM9PXBsdXMpCiAgICAgICAgewogICAgICAgICAgICAgICAgaW50IHJlc3VsdD1iK2E7CiAgICAgICAgICAgICAgICBwdXNoKHJlc3VsdCk7CiAgICAgICAgICAgICAvLyAgIHByaW50ZigiJWQiLHJlc3VsdCk7CiAgICAgICAgfQogICAgICAgIGlmKGM9PXN1YnRyYWN0KQogICAgICAgIHsKICAgICAgICAgICAgICAgIGludCByZXN1bHQ9Yi1hOwogICAgICAgICAgICAgICAgcHVzaChyZXN1bHQpOwogICAgICAgIH0KICAgICAgICBpZihjPT1tdWx0aSkKICAgICAgICB7CiAgICAgICAgICAgICAgICBpbnQgcmVzdWx0PWIqYTsKICAgICAgICAgICAgICAgIHB1c2gocmVzdWx0KTsKICAgICAgICB9CiAgICAgICAgaWYoYz09ZGl2aSkKICAgICAgICB7CiAgICAgICAgICAgICAgICBpbnQgcmVzdWx0PWIvYTsKICAgICAgICAgICAgICAgIHB1c2gocmVzdWx0KTsKICAgICAgICB9CiAgICAgICAgaWYoYz09cG93ZXIpCiAgICAgICAgewogICAgICAgICAgICAgICAgaW50IHJlc3VsdD1wb3coYixhKTsKICAgICAgICAgICAgICAgIHB1c2gocmVzdWx0KTsKICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGVsc2UKICAgICAgICB7ICAgICBpbnQgaz1jLScwJzsKICAgICAgICAgICAgICBwdXNoKGspOwogICAgICAgICAgLy8gICAgcHJpbnRmKCIlZCIsayk7CiAgICAgICAgfQovKiAgaWYoaT09bGVuZ3RoLTEpCiAgeyAgICAKICAgICAgICBpbnQgZD1wb3AoKTsKICAgICAgICBwcmludGYoIiVkIixkKTsKICB9CiovICAKICB9CiAgaW50IGQ9cG9wKCk7CiAgcHJpbnRmKCIlZCIsZCk7Cn0KCi8vIGRyaXZlciBjb2RlICwgdGhlIG1haW4gZnVuY3Rpb24KCmludCBtYWluKHZvaWQpCnsgICBjaGFyIGludmVyc2lvblsyMDBdOwogcHJpbnRmKCJnaXZlIHRoZSBwb3N0Zml4IGV4cHJlc3Npb25cbiIpOwogc2NhbmYoIiVzIiwmaW52ZXJzaW9uKTsKIGludCBsb25nZXI9c3RybGVuKGludmVyc2lvbik7CiBwb3N0Zml4X2V2YWx1YXRpb24oaW52ZXJzaW9uLGxvbmdlcik7CgogcmV0dXJuIDA7Cn0K