forked from Prashantsetia/C
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostfix.c
75 lines (74 loc) · 1.49 KB
/
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
74
75
#include <stdio.h>
#include<math.h>
int top=-1;
int pop(int arr[]){
if(top==-1)
printf("Stack underfow\n");
else
return (arr[top--]);
}
void push(int arr[],int a){
if(a==1){
int val;
printf("Enter value ");
scanf(" %d",&val);
arr[++top]=val;
}
else
{
char ch,c,a,b;
a=pop(arr);
b=pop(arr);
printf("Enter the operator ");
scanf(" %c", &ch);
switch(ch){
case '+':
c=b+a;
break;
case '-':
c=b-a;
break;
case '*':
c=b*a;
break;
case '%':
c=b%a;
break;
case '/':
c=b/a;
break;
case '^':
c=pow(b,a);
break;
}
arr[++top]=c;
}
}
void display(int arr[]){
printf("Output is %d \n",arr[top]);
}
void main(){
int loop=0;
int arr[50];
while(loop!=1){
printf("1 for value\t2 for operator\t3 for display\t4 for exit \n");
int choices;
scanf("%d",&choices);
switch(choices){
case 1:
push(arr,choices);
break;
case 2:
push(arr,choices);
break;
case 3:
display(arr);
break;
case 4:
loop=1;
break;
default:
printf("\nEnter the right choices\n");
}
}
}