-
Notifications
You must be signed in to change notification settings - Fork 1
/
q5a.c
155 lines (134 loc) · 2.7 KB
/
q5a.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Infinix to postfix expression converter
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define SIZE 100 // Maximum size of the stack
// Stack structure
struct Stack
{
int top;
char items[SIZE];
};
// Function to initialize stack
void initStack(struct Stack *s)
{
s->top = -1;
}
// Function to check if stack is full
int isFull(struct Stack *s)
{
return (s->top == SIZE - 1);
}
// Function to check if stack is empty
int isEmpty(struct Stack *s)
{
return (s->top == -1);
}
// Function to push an element into the stack
void push(struct Stack *s, char element)
{
if (isFull(s))
{
printf("Stack Overflow\n");
return;
}
s->items[++(s->top)] = element;
}
// Function to pop an element from the stack
char pop(struct Stack *s)
{
if (isEmpty(s))
{
printf("Stack Underflow\n");
return -1;
}
return s->items[(s->top)--];
}
// Function to peek at the top of the stack
char peek(struct Stack *s)
{
if (isEmpty(s))
{
return -1;
}
return s->items[s->top];
}
// Function to check the precedence of operators
int precedence(char op)
{
switch (op)
{
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
default:
return 0;
}
}
// Function to check if a character is an operator
int isOperator(char ch)
{
return (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '^');
}
// Function to convert infix expression to postfix expression
void infixToPostfix(char infix[], char postfix[])
{
struct Stack s;
initStack(&s);
int i = 0, j = 0;
char ch;
while (infix[i] != '\0')
{
ch = infix[i];
// If the character is an operand, add it to the postfix expression
if (isalnum(ch))
{
postfix[j++] = ch;
}
// If the character is '(', push it onto the stack
else if (ch == '(')
{
push(&s, ch);
}
// If the character is ')', pop and output from the stack until '(' is encountered
else if (ch == ')')
{
while (!isEmpty(&s) && peek(&s) != '(')
{
postfix[j++] = pop(&s);
}
pop(&s); // Remove '(' from the stack
}
// If the character is an operator
else if (isOperator(ch))
{
while (!isEmpty(&s) && precedence(peek(&s)) >= precedence(ch))
{
postfix[j++] = pop(&s);
}
push(&s, ch);
}
i++;
}
// Pop all remaining operators from the stack
while (!isEmpty(&s))
{
postfix[j++] = pop(&s);
}
postfix[j] = '\0'; // Null-terminate the postfix expression
}
// Driver function
int main()
{
char infix[SIZE], postfix[SIZE];
printf("Enter infix expression: ");
scanf("%s", infix);
infixToPostfix(infix, postfix);
printf("Postfix Expression: %s\n", postfix);
return 0;
}