-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOperator_precedence_using_Stack_ADT.c
58 lines (56 loc) · 1.05 KB
/
Operator_precedence_using_Stack_ADT.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
// Given an arithmetic expression, find the operator with highest priority using stack.
// (consider only basic arithmetic operators)
#include<stdio.h>
#include<string.h>
char s[20];
int top=-1;
int pri(char c)
{
switch(c)
{
case '+':
case '-':return 1;
break;
case '*':
case '/':return 2;
break;
default: return 0;
}
}
void push(char c)
{
top++;
s[top]=c;
}
char pop()
{
int a=top;
top--;
return s[a];
}
int main()
{
char str[20],temp;
scanf("%[^\n]s",str);
for(int i=0;i<strlen(str);i++)
{
if(str[i]=='+'||str[i]=='-'||str[i]=='*'||str[i]=='/')
{
if(top==-1)
push(str[i]);
else
{
if(pri(s[top])>=pri(str[i]))
{
temp=pop();
push(str[i]);
push(temp);
}
else
push(str[i]);
}
}
}
printf("%c",pop());
return 0;
}