-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.c
66 lines (66 loc) · 1.56 KB
/
stack.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
#include <stdio.h>
#include <string.h>
struct stack{
char a[50];
int top;
}st;
int openingParantheses() {
if (st.a[st.top] == '(')return 1;
return 0;
}
int isEmpty() {
if (st.top == -1) return 1;
return 0;
}
char stack_top() {
return st.a[st.top];
}
int hasHigherPrecedence(char t,char c) {
if (isEmpty())return 1;
if (t == '/')return 1;
if (t == '*' && c != '/') return 1;
if (t == '+' && c != '/' && c != '*')return 1;
return 0;
}
void pop() {
st.top--;
}
void push(char ch) {
st.a[++st.top] = ch;
}
void infix(char exp[]) {
char temp[50];
int k = 0;
for (int i = 0; i < strlen(exp); i++) {
char ch = exp[i];
if (ch != '/' && ch != '*' && ch != '+' && ch != '-' && ch != '(' && ch != ')') {
temp[k++] = ch;
} else if (ch == '/' || ch == '+' || ch == '*' || ch == '-') {
while (!isEmpty() && hasHigherPrecedence(stack_top(), ch) && !openingParantheses()) {
temp[k++] = stack_top();
pop();
}
push(ch);
} else if (ch == '(')push(ch);
else if (ch == ')') {
while (!isEmpty() && !openingParantheses()) {
temp[k++] = stack_top();
pop();
}
pop();
}
}
while (!isEmpty()) {
char ch = stack_top();
temp[k++] = ch;
pop();
}
temp[k] = '\0';
puts(temp);
}
int main() {
char exp[50];
st.top = -1;
gets(exp);
infix(exp);
}