-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPrg 3.cpp
75 lines (66 loc) · 951 Bytes
/
Prg 3.cpp
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
74
75
/**
Lab Program 3
Postfix Evaluation Using Stack
Author SkyKOG
*/
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
#include <process.h>
int stack[20];
int top;
void push(int);
int pop();
void eval(char post[20]);
void main()
{
char post[20];
top=-1;
printf("Enter Postfix Expression : ");
gets(post);
eval(post);
printf("\nThe Evaluated Answer Is : %d",pop());
getch();
}
void push(int ele)
{
stack[++top]=ele;
}
int pop()
{
return (stack[top--]);
}
void eval(char post[20])
{
char ch;
int i,b,a;
for(i=0;post[i];i++)
{
ch=post[i];
if(isdigit(ch))
push(ch-'0');
else
{
b=pop();
a=pop();
switch(ch)
{
case '+':push(a+b);
break;
case '-':push(a-b);
break;
case '*':push(a*b);
break;
case '/':if(b==0)
{
printf("Devide By Zero");
getch();
exit(0);
}
push(a/b);
default:printf("Invalid Operation");
}
}
}
}