forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3273df2
commit 053e2da
Showing
1 changed file
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
//Postfix Evaluation | ||
#include<stdio.h> | ||
#include<string.h> | ||
#include<ctype.h> | ||
|
||
struct stack | ||
{ | ||
char A[100]; | ||
int Top; | ||
}; | ||
|
||
void push(struct stack *ps, char x) | ||
{ | ||
if(ps -> Top == 99) | ||
printf("\nStack is Full\n"); | ||
else | ||
{ | ||
ps -> Top = ps -> Top + 1; | ||
ps -> A[ps -> Top] = x; | ||
} | ||
} | ||
|
||
int evaluate(int op1, int op2, char c) | ||
{ | ||
if(c == '+') | ||
return op1 + op2; | ||
else if(c == '-') | ||
return op1 - op2; | ||
else if(c == '*') | ||
return op1 * op2; | ||
else if(c == '/') | ||
return op1 / op2; | ||
} | ||
|
||
void print(struct stack s) | ||
{ | ||
int i; | ||
for(i = 0 ; i <= s.Top ; i++) | ||
printf("%d\t", s.A[i]); | ||
} | ||
|
||
int pop(struct stack *ps) | ||
{ | ||
int x; | ||
if(ps -> Top == -1) | ||
printf("Stack is empty!Element cant be deleted\n"); | ||
else | ||
{ | ||
x = ps -> A[ps -> Top]; | ||
ps -> Top = ps -> Top - 1; | ||
return x; | ||
} | ||
} | ||
|
||
int main() | ||
{ | ||
int op1, op2, x, i; | ||
char optr; | ||
struct stack s; | ||
s.Top = -1; | ||
printf("Enter the postfix exp to be evaluated \n"); | ||
gets(s.A); | ||
for(i = 0; i < strlen(s.A); i++) | ||
{ | ||
if(isdigit(s.A[i])) | ||
push(&s, s.A[i] - '0'); | ||
else if(s.A[i] == '+' || s.A[i] == '-' || s.A[i] == '*' || s.A[i] == '/') | ||
{ | ||
op1 = pop(&s); | ||
op2 = pop(&s); | ||
optr = s.A[i]; | ||
x = evaluate(op2, op1, optr); | ||
push(&s, x); | ||
} | ||
} | ||
|
||
printf("The result is : "); | ||
print(s); | ||
printf("\n"); | ||
} | ||
|
||
/*Input - Output Sample | ||
Enter the postfix exp to be evaluated | ||
2 3 1 * + 9 - | ||
The result is : -4 | ||
*/ |