-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfix.c
98 lines (94 loc) · 1.64 KB
/
infix.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 50
void push(char[],int *,char);
char pop(char[],int *);
int ICP(char);
int ISP(char);
void infixpostfix(char[],char[]);
void main()
{
char infix[MAX],postfix[MAX];
printf("Enter infix expression \n");
scanf("%s",infix);
printf("Entered expression : %s \n",infix);
infixpostfix(infix,postfix);
printf("The postfix string is %s \n",postfix);
}
void infixpostfix(char infix[],char postfix[])
{
int len,i,k,top;
char x,s[MAX];
len = strlen(infix);
infix[len] = ')';
top = i = k = 0;
s[top] = '(';
while(i<=len)
{
x = infix[i];
if(ICP(x) == 8)
x='#';
switch(x)
{
case '#': postfix[k++] = infix[i];
break;
case ')': while(s[top]!='(')
postfix[k++] = pop(s,&top);
pop(s,&top);
if(top == -1)
{
postfix[k]='\0';
return;
}
break;
default : while(ICP(x) < ISP(s[top]))
postfix[k++] = pop(s,&top);
push(s,&top,x);
}
i++;
}
}
int ICP(char c)
{
if((c >= 'A') && (c <= 'Z') || (c >= '0' && c <= '9') || (c >= 'a') && (c<= 'z'))
c = '#';
switch(c)
{
case '+':
case '-': return(1);
case '*':
case '/':
case '%': return(3);
case '$':
case '^': return(6);
case '#': return(8);
case '(': return(9);
case ')': return(0);
default: printf("Error \n");
return (-1);
}
}
int ISP(char c)
{
switch(c)
{
case '+':
case '-': return(1);
case '*':
case '/':
case '%': return(4);
case '$': return(5);
case '(': return(0);
default: printf("error \n");
return(-1);
}
}
void push(char items[],int *top,char e)
{
items[++(*top)] = e;
}
char pop(char items[], int *top)
{
return items[(*top)--];
}