maheswar01.c
Source Code
Printing the code
To print this file, open it on GitHub and click Raw before printing, or use the Download Raw button above and print directly from that page.
c
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define MAX 1000
/* ================= STACK FOR OPERATORS ================= */
typedef struct {
char data[MAX];
int top;
} CharStack;
void initCharStack(CharStack *s) {
s->top = -1;
}
void pushChar(CharStack *s, char c) {
s->data[++s->top] = c;
}
char popChar(CharStack *s) {
return s->data[s->top--];
}
char peekChar(CharStack *s) {
return s->data[s->top];
}
int isEmptyChar(CharStack *s) {
return s->top == -1;
}
/* ================= STACK FOR NUMBERS ================= */
typedef struct {
double data[MAX];
int top;
} DoubleStack;
void initDoubleStack(DoubleStack *s) {
s->top = -1;
}
void pushDouble(DoubleStack *s, double x) {
s->data[++s->top] = x;
}
double popDouble(DoubleStack *s) {
return s->data[s->top--];
}
/* ================= HELPER FUNCTIONS ================= */
int precedence(char op) {
if (op == '+' || op == '-') return 1;
if (op == '*' || op == '/') return 2;
return 0;
}
int isOperator(char c) {
return c == '+' || c == '-' || c == '*' || c == '/';
}
/* ================= INFIX TO POSTFIX ================= */
void infixToPostfix(const char *infix, char *postfix) {
CharStack stack;
initCharStack(&stack);
int i = 0, k = 0;
while (infix[i]) {
if (isspace(infix[i])) {
i++;
continue;
}
if (isdigit(infix[i]) || infix[i] == '.') {
while (isdigit(infix[i]) || infix[i] == '.') {
postfix[k++] = infix[i++];
}
postfix[k++] = ' ';
}
else if (infix[i] == '(') {
pushChar(&stack, infix[i]);
i++;
}
else if (infix[i] == ')') {
while (!isEmptyChar(&stack) && peekChar(&stack) != '(') {
postfix[k++] = popChar(&stack);
postfix[k++] = ' ';
}
popChar(&stack); // remove '('
i++;
}
else if (isOperator(infix[i])) {
while (!isEmptyChar(&stack) &&
precedence(peekChar(&stack)) >= precedence(infix[i])) {
postfix[k++] = popChar(&stack);
postfix[k++] = ' ';
}
pushChar(&stack, infix[i]);
i++;
}
else {
printf("Invalid character: %c\n", infix[i]);
exit(1);
}
}
while (!isEmptyChar(&stack)) {
postfix[k++] = popChar(&stack);
postfix[k++] = ' ';
}
postfix[k] = '\0';
}
/* ================= POSTFIX EVALUATION ================= */
double evaluatePostfix(const char *postfix) {
DoubleStack stack;
initDoubleStack(&stack);
int i = 0;
while (postfix[i]) {
if (isspace(postfix[i])) {
i++;
continue;
}
if (isdigit(postfix[i]) || postfix[i] == '.') {
char number[50];
int k = 0;
while (isdigit(postfix[i]) || postfix[i] == '.') {
number[k++] = postfix[i++];
}
number[k] = '\0';
pushDouble(&stack, atof(number));
}
else if (isOperator(postfix[i])) {
double b = popDouble(&stack);
double a = popDouble(&stack);
switch (postfix[i]) {
case '+': pushDouble(&stack, a + b); break;
case '-': pushDouble(&stack, a - b); break;
case '*': pushDouble(&stack, a * b); break;
case '/':
if (b == 0) {
printf("Error: Division by zero\n");
exit(1);
}
pushDouble(&stack, a / b);
break;
}
i++;
}
}
return popDouble(&stack);
}
/* ================= MAIN ================= */
int main() {
char infix[MAX];
char postfix[MAX];
printf("Enter expression: ");
fgets(infix, MAX, stdin);
infixToPostfix(infix, postfix);
double result = evaluatePostfix(postfix);
printf("Result = %.10g\n", result);
return 0;
}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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185