-
Notifications
You must be signed in to change notification settings - Fork 1
/
10.infixConv.c
69 lines (64 loc) · 1.39 KB
/
10.infixConv.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
/*
Author : abhijithvijayan
Title : Infix to Postfix Conversion
*/
#include <stdio.h>
#include <ctype.h>
char Ar[50];
int top = -1;
void push(char item)
{
top++;
Ar[top] = item;
}
char pop() //Return the character when operator appears
{
char item;
if (top == -1)
return -1;
item = Ar[top];
top--;
return item;
}
int precedence(char symbol)
{
if (symbol == '(')
return 0;
if (symbol == '+' || symbol == '-')
return 1;
if (symbol == '*' || symbol == '/')
return 2;
}
void main()
{
int i;
char str[20];
char item, s;
printf("Enter the infix expression :: ");
scanf("%s", str);
printf("The postfix expression is :: ");
for (i = 0; str[i] != '\0'; i++)
{ /*isalnum is a function which returns true value if the argument passed is an alphabet or a number.
Else, it returns zero, and the if block is not executed.*/
s = str[i];
if (isalnum(s))
printf("%c", s);
else if (s == '(')
push(s);
else if (s == ')')
{
while ((item = pop()) != '(')
printf("%c", item);
}
else
{
while (precedence(Ar[top]) >= precedence(s))
printf("%c", pop());
push(s);
}
}
while (top != -1)
{
printf("%c", pop());
}
}