-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram4
More file actions
78 lines (73 loc) · 1.42 KB
/
program4
File metadata and controls
78 lines (73 loc) · 1.42 KB
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
#include<stdio.h>
char stack[50];
int top = -1;
void push(char ch)
{
stack[++top] = ch;
}
char pop()
{
return(stack[top--]);
}
int prcd(char ch)
{
int p;
switch(ch)
{
case '$':
case '^': p = 3;
break;
case '*':
case '/': p = 2;
break;
case '+':
case '-': p = 1;
break;
case '(': p = -1;
break;
}
return p;
}
void conversion(char infix[], char postfix[])
{
int i = 0, p = 0;
char ch;
while((ch = infix[i]) != '\0')
{
switch(ch)
{
default : postfix[p++] = ch;
break;
case '(': push(ch);
break;
case ')': while(top != -1 && stack[top] != '(')
postfix[p++] = pop();
pop();
break;
case '*':
case '/':
case '+':
case '-': while(top != -1 && prcd(stack[top]) >= prcd(ch))
postfix[p++] = pop();
push(ch);
break;
case '$':
case '^': while(top != -1 && prcd(stack[top]) > prcd(ch))
postfix[p++] = pop();
push(ch);
break;
}
i++;
}
while (top != -1)
postfix[p++] = pop();
postfix[p] = '\0';
}
int main()
{
char infix[50], postfix[50];
printf("enter valid infix expression\n");
scanf("%s", infix);
conversion(infix, postfix);
printf("postfix expression= %s\n", postfix);
}