Evaluating Postfix Expression
CODE
👇
- #include <stdio.h>
- #include <ctype.h>
- #define MAX 20
- typedef struct
- {
- int data[MAX];
- int top;
- }STACK;
- void initstack(STACK *ps)
- {
- ps->top = -1;
- }
- void push(STACK *ps, int num)
- {
- ps->top++;
- ps->data[ps->top]=num;
- }
- int pop(STACK *ps)
- {
- return(ps->data[ps->top--]);
- }
- int isempty(STACK *ps)
- {
- return(ps->top == -1);
- }
- int isfull(STACK *ps)
- {
- return(ps->top == MAX-1);
- }
- void main()
- {
- void Eval(char post[]);
- char post[20];
- printf(">>Evaluating Postfix Expression<<\n");
- printf("Enter The Postfix Expression:\t");
- scanf("%s", post);
- Eval(post);
- }
- void Eval(char post[])
- {
- int value, i, op1, op2;
- STACK s1;
- initstack(&s1);
- for(i=0; post[i]!='\0'; i++)
- {
- if(isalpha(post[i]))
- {
- printf("Enter The Value Of %c: \t", post[i]);
- scanf("%d", &value);
- push(&s1, value);
- }
- else
- {
- op2=pop(&s1);
- op1=pop(&s1);
- switch(post[i])
- {
- case '+':
- push(&s1, op1+op2);
- break;
- case '-':
- push(&s1, op1-op2);
- break;
- case '*':
- push(&s1, op1*op2);
- break;
- case '/':
- push(&s1, op1/op2);
- break;
- case '%':
- push(&s1, op1%op2);
- break;
- default:
- printf("Enter Value The Value Of");
- scanf("%d", &value);
- push(&s1, value);
- break;
- }
- }
- }
- printf("The Result Is: %d \n", pop(&s1));
- }
👉Execute👈
//OUTPUT
/*
>>Evaluating Postfix Expression<<
Enter The Postfix Expression: ABC*+DE/-
Enter The Value Of A: 78
Enter The Value Of B: 14
Enter The Value Of C: 64
Enter The Value Of D: 52
Enter The Value Of E: 43
The Result Is: 973
*/
//ThE ProFessoR
Comments
Post a Comment