-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproblem34.c
More file actions
100 lines (81 loc) Β· 2.26 KB
/
problem34.c
File metadata and controls
100 lines (81 loc) Β· 2.26 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_LEN 10000
typedef struct Stack {
char *data[MAX_LEN];
int top;
} Stack;
void initStack(Stack *stack) {
stack->top = -1;
}
void push(Stack *stack, const char *str) {
if (stack->top < MAX_LEN - 1) {
stack->data[++(stack->top)] = strdup(str);
} else {
printf("Stack overflow! Too many elements.\n");
exit(1);
}
}
char *pop(Stack *stack) {
if (stack->top >= 0) {
return stack->data[(stack->top)--];
} else {
printf("Stack underflow! Nothing to pop.\n");
exit(1);
}
}
int isOperator(char ch) {
return ch == '+' || ch == '-' || ch == '*' || ch == '/';
}
int main() {
char input[MAX_LEN + 1];
Stack stack;
initStack(&stack);
printf("π Enter postfix expression: ");
if (fgets(input, sizeof(input), stdin) == NULL) {
printf("!! Invalid Input !!\n");
return 1;
}
size_t len = strlen(input);
if (len > 0 && input[len - 1] == '\n') {
input[len - 1] = '\0';
}
for (int i = 0; input[i] != '\0'; i++) {
char ch = input[i];
if (isspace(ch)) continue;
if (isalnum(ch)) {
char operand[2] = {ch, '\0'};
push(&stack, operand);
} else if (isOperator(ch)) {
if (stack.top < 1) {
printf("Not enough operands for operator '%c'.\n", ch);
return 1;
}
char *right = pop(&stack);
char *left = pop(&stack);
size_t size = strlen(left) + strlen(right) + 2;
char *newExpr = (char *)malloc(size);
if (newExpr == NULL) {
printf("Memory allocation failed.\n");
return 1;
}
sprintf(newExpr, "%c%s%s", ch, left, right);
push(&stack, newExpr);
free(left);
free(right);
free(newExpr);
} else {
printf("!! Invalid Input !!\n");
return 1;
}
}
if (stack.top == 0) {
printf("Prefix expression: %s\n", stack.data[0]);
free(stack.data[0]);
} else {
printf("!! Invalid Input: More than one element left in stack.\n");
}
return 0;
}