-
Notifications
You must be signed in to change notification settings - Fork 0
/
Prefix - Postfix.c
50 lines (50 loc) · 981 Bytes
/
Prefix - 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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 20
char str[MAX], stack[MAX];
int top = -1;
void push(char c) // to push the character in to the stack
{
stack[++top] = c;
}
char pop()
{
return stack[top--];
}
void pre_post() // function to convert prefix to postfix
{
int n, i, j = 0;
char c[20];
char a;
printf("Enter the prefix expression\n");
gets(str); // used to scan the prefix expression
n = strlen(str);
for (i = 0; i < MAX; i++)
stack[i] = '\0';
printf("Postfix expression is:\t");
for (i = 0; i < n; i++)
{
if (str[i] == '+' || str[i] == '-' || str[i] == '*' || str[i] == '/')
{
push(str[i]);
}
else
{
c[j++] = str[i];
while ((top != -1) && (stack[top] == '#'))
{
a = pop();
c[j++] = pop();
}
push('#');
}
}
c[j] = '\0';
printf("%s", c);
}
int main()
{
pre_post();
return 0;
}