-
Notifications
You must be signed in to change notification settings - Fork 0
/
DSA_Ques7.c
64 lines (62 loc) · 1.26 KB
/
DSA_Ques7.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
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define SIZE 100
typedef struct InfixToPostfix{
char stack[100];
int top = -1;
} InfixToPostfix;
InfixToPostfix var1;
char pop()
{
if(var1.top == -1)
return -1;
else
return var1.stack[var1.top--];
}
void push(char ch)
{ if(strlen(var1.stack)<SIZE)
var1.stack[++var1.top] = ch;
}
int precedence(char ch)
{
if(ch == '+' || ch == '-')
return 1;
if(ch == '*' || ch == '/' || ch == '%')
return 2;
if(ch == '^')
return 3;
return 0;
}
int main()
{
char *exp, ch, expression[100];
printf("Enter the Infix Expression : ");
scanf("%s",expression);
printf("The Converted Postfix Expression is: ");
exp = expression;
while(*exp != '\0')
{
if(isalnum(*exp))
printf("%c ",*exp);
else if(*exp == '(')
push(*exp);
else if(*exp == ')')
{
while((ch = pop()) != '(')
printf("%c ", ch);
}
else
{
while(precedence(var1.stack[var1.top]) >= precedence(*exp))
printf("%c ",pop());
push(*exp);
}
exp++;
}
while(var1.top != -1)
{
printf("%c ",pop());
}
return 0;
}