-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path004_Postfix.c
73 lines (69 loc) · 1.76 KB
/
004_Postfix.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//Author:Kavya Dhar // © Mr.dhar 25-June-2021
// Purpose ; // Postfix expressions
// 1) Enter postfix Expression along with operation
// 3) Result
// 4) $$ Exit $$
#include<stdio.h> // Pre-Prossive To include standard input and output header files
int pop(); // Funtion declratation
int stack[20]; // Stack size declared
int top = -1; // Checks the stack is overflow or not
int n1,n2,n3,num; // Global Declratation
void push(int x)
{ // Pre-increment
top=top+1;
stack[top]=x;
}
int pop()
{
return stack[top--];
}
int main()
{
char exp[20];
char *e;
printf("Welcome to Postfix Operations");
printf(" Only enter in this e.g: abc+-*/ ") ;
printf("Enter the expression in the postfix :: ");
scanf("%s",exp);
e = exp;
while(*e)
{
if(isdigit(*e))
{
num = *e -'0';
push(num);
}
else
{
n1 = pop();
n2 = pop();
switch(*e)
{
case '+':
{
n3 = n1 + n2;
break;
}
case '-':
{
n3 = n2 - n1;
break;
}
case '*':
{
n3 = n1 * n2;
break;
}
case '/':
{
n3 = n2 / n1;
break;
}
}
push(n3);
}
e++;
}
printf("\nThe result of expression %s = %d\n\n",exp,pop());
return 0;
}