forked from DHEERAJHARODE/Hacktoberfest2024-Open-source-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTo_change_an_Expression_from_Infix_to_Postfix.c
148 lines (128 loc) · 3.63 KB
/
To_change_an_Expression_from_Infix_to_Postfix.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
/*
Problem statement: To change a Expression from Infix to Postfix
using stack in C
Algorithm:
1. At first we'll make simple push and pop function
2. If the leftmost character is an operand, set it as the current output to the Postfix string.
If the scanned character is the operator and the Stack is empty push the operator into the Stack.
3. If the scanned operator has higher priority than the existing priority operator in the Stack,
push it on the Stack.
4. If the scanned operator has lower or equal priority than the existing operator in the Stack,
pop all the Stack operators and after that push the scanned operator into the Stack.
5. If the scanned character is open parenthesis '(', always push it into the Stack.
6. If the scanned character is close parenthesis ')', pop the Stack and print all output string
character until '(' is encountered and discard both the bracket.
7. Repeat all steps from 2 to 8 until the infix expression is scanned.
8. Print the Stack output.
9. Pop and output all characters, including the operator, from the Stack until it is not empty.
*/
#include<stdio.h>
#include<ctype.h> //for alnum
#define max 100
//global variables
char stack [max];
//top is use to navigate in the stack
int top = -1;
void push (char x) //push function
{
if(top == max-1)
{
printf("\nStack Overflow.");
}
else
{
//increasing the top to next location and pushing character
stack[++top] = x ;
}
}
char pop() //pop function
{
char item;
if(top == -1)
{
printf("stack under flow: invalid infix expression");
/* underflow may occur because of invalid expression */
exit(1);
}
else
{
item = stack[top];
top = top-1;
return(item);
}
}
int priority(char x) //priority function
//it defines a level of precedence of a inputed character
{
if(x== '^')
return 3;
else if(x=='*' || x=='/')
return 2;
else if(x=='+' || x=='-')
return 1;
else
return 0;
}
void main()
{
char exp[20];
char*e , x;
printf("Enter an Infix expression: ");
scanf("%s",&exp);
e=exp;
printf("Its Post fix expression is: ");
while(*e!='\0')
//this loop will iterate untill value in *e become NULL
{
if(isalnum(*e))
{
//isalnum checks whether a given element is alphabet or number
printf("%c",*e);
}
else if(*e == '(')
{
//checks if the value on *e is '('
push(*e);
}
else if(*e == ')')
{
//checks if the value on *e is ')'
while((x = pop()) != '(')
printf("%c",x);
}
else
{
/*it means an operator is encountered so we
check its precedence by calling priority function.*/
while(priority(stack[top]) >= priority(*e))
printf("%c ",pop());
push(*e);
}
e++; //incrementing to check next character
} //end of while
while(top != -1)
{
//printing all the remaining elements in the stack
printf("%c ",pop());
}
return 0;
} //end of main
/*
INPUT 1:
Enter an Infix expression: a*(b+c)
OUTPUT 2:
Its Post fix expression is: abc+*
INPUT 2:
Enter an Infix expression: ((A+B)*C-D)*E
OUTPUT 2:
Its Post fix expression is: AB+C* D-E*
Time Complexity : O(n^2)
as expression is iterated two
times simultaneously, firstly for scanning the infix expression
and secondly while poping out of stack.
Space Complexity : O(n)
For storing Infix expression of n literals the space complexity
is O(n) and for stack to hold atmost n literals the space complexity
is O(n), hence
Total space complexity is O(n+n) = O(2n) i.e : O(n)
*/