-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostfix.h
More file actions
47 lines (32 loc) · 939 Bytes
/
Postfix.h
File metadata and controls
47 lines (32 loc) · 939 Bytes
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
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define Max 30
int postfix_top = -1;
char postfix_stack[Max];
int pri(char c) {
if (c == '^') return 9;
if (c == '/' || c == '*') return 7;
if (c == '+' || c == '-') return 5;
}
void infix_to_postfix(char *infix, char *postfix) {
int i,k, j = 0;
char ch;
for (i = 0; infix[i] != '\0'; i++) {
ch = infix[i];
if (isalpha(ch)) {
postfix[j++] = ch;
}
else
{
for (k=postfix_top ;k>=0 && pri(ch) <= pri(postfix_stack[postfix_top]);k--) {
postfix[j++] = postfix_stack[postfix_top--];
}
postfix_stack[++postfix_top] = ch;
}
}
while (postfix_top > -1) {
postfix[j++] = postfix_stack[postfix_top--];
}
postfix[j] = '\0';
}