-
Notifications
You must be signed in to change notification settings - Fork 0
/
Infix - prefix.c
71 lines (66 loc) · 1.77 KB
/
Infix - prefix.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
#include <stdio.h>
#include<string.h>
#include <ctype.h>
#define SIZE 50
char stack[SIZE];
int top = -1;
void push(char element) // Function to Push the element from the stack
{
stack[++top] = element;
}
char pop() // Function to pop the element from the stack
{
return(stack[top--]);
}
int priority(char element) // Check the Priority of the String
{
switch(element)
{
case '#': return 0;
case ')': return 1;
case '+':
case '-': return 2;
case '*':
case '/': return 3;
}
}
int main()
{
char infix[50], prefix[50], ch, element;
int i = 0, j = 0;
printf("Enter the Infix Expression:\n");
scanf("%s", infix); // Input Infix Expression from the user
push( '#' );
strrev (infix);
while( (ch = infix[i++] ) != '\0' )
{
if ( ch == ')' )
{
push(ch);
else
{
if ( isalnum (ch) ) // Check if Numeric
prefix[j++] = ch;
else
{
if ( ch == '(' )
{
while ( stack[top] != ')' )
prefix[j++] = pop();
element = pop(); // Remove ')'
}
else
{ // Operator
while(priority(stack[top]) >= priority(ch))
prefix[j++] = pop();
push(ch);
}
}
}
}
while(stack[top] != '#') // Pop from stack till empty
prefix[j++] = pop();
prefix[j] = '\0'; // Make prefix as valid string
strrev (prefix);
printf("Prefix Expression: %s", prefix);
}