-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProg6.c
156 lines (135 loc) · 3.97 KB
/
Prog6.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
156
/* ---------------PROBLEM STATEMENT ---------------
Implement the conversion of infix notation to postfix notation
*/
#include <stdio.h>
#include <string.h>
#define SIZE 100
int check_priority(char c){
if(c == '^'){
return 3;
}
else if(c == '*' || c == '/'){
return 2;
}
else if(c == '+' || c == '-'){
return 1;
}
return -1;
}
int isOperator(char c){
if(c == '+' || c == '-' || c == '*' || c == '/' || c == '^'){
return 1;
}
return 0;
}
void infixToPostfix(char* infix){
char postfix[SIZE] = "";
char stack[SIZE] = "";
int top = -1;
for(int i = 0 ; infix[i] != '\0' ; i++){
if(infix[i]==' ')
continue;
else if(infix[i] == '('){
top++;
stack[top] = infix[i];
}
// Add any alphabets or no. to postfix
else if(isOperator(infix[i]) == 0 && infix[i]!='(' && infix[i]!=')'){
int len = strlen(postfix);
postfix[len] = infix[i];
postfix[len + 1] = '\0';
}
// check for closing paranthesis
else if(infix[i] == ')'){
while (top >= 0 && stack[top] != '(') {
int len = strlen(postfix);
postfix[len] = stack[top];
postfix[len + 1] = '\0';
top--;
}
if (top >= 0 && stack[top] != '(') {
printf("\n***INVALID***\n");
return;
} else {
top--;
}
}
// check for operator
else if(isOperator(infix[i])){
// poping operator from stack till precedences of operator in stack is >= precedence of operator in infix.
while (top >= 0 && check_priority(stack[top]) >= check_priority(infix[i])) {
int len = strlen(postfix);
postfix[len] = stack[top];
postfix[len + 1] = '\0';
top--;
}
stack[++top] = infix[i];
}
}
// Empty the stack
while (top >= 0) {
int len = strlen(postfix);
postfix[len] = stack[top];
postfix[len + 1] = '\0';
top--;
}
printf("Postfix Expression : %s\n", postfix);
}
int isEmptyExpression(char *infix) {
for (int i = 0; infix[i] != '\0'; i++) {
if (infix[i] != ' ' && infix[i] != '\n') {
return 0; // The expression is not empty
}
}
return 1; // The expression is empty
}
int check_paranthesis(char *infix){
char ps[SIZE];
int top = -1;
for(int i = 0 ; infix[i] != '\0' ; i++){
if(infix[i] == '('){
ps[++top] = infix[i];
}
else if(infix[i] == ')'){
if(top == -1){
return 1; // The expression is wrong
}
top--;
}
}
if(top != -1){
return 1; // The expression is wrong
}
return 0; // The expression is correct
}
int main() {
char infix[SIZE];
printf("Enter Infix Expression : ");
fgets(infix, SIZE, stdin); // read string
// Remove the newline character added by fgets
infix[strcspn(infix, "\n")] = '\0';
if (isEmptyExpression(infix)) {
printf("Invalid! The input expression is empty.\n");
return 1; // Exit the program with an error code
}
// Check for input without operators
int hasOperators = 0;
for (int i = 0; infix[i] != '\0'; i++) {
if (isOperator(infix[i])) {
hasOperators = 1;
break;
}
}
if (!hasOperators) {
printf("Invalid! The input expression does not contain any operators.\n");
return 1; // Exit the program with an error code
}
// check for valid parentheses
if(check_paranthesis(infix)){
printf("Invalid! The input expression is Not Correct.\n");
return 1; // Exit the program with an error code
}
// Call the infixToPostfix function here
infixToPostfix(infix);
return 0;
}