-
Notifications
You must be signed in to change notification settings - Fork 0
/
assignment7.c
68 lines (59 loc) · 1.42 KB
/
assignment7.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
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX_SIZE 20
char stack[MAX_SIZE][MAX_SIZE];
int top = -1;
bool isEmpty() {
return top == -1;
}
bool isFull() {
return top == MAX_SIZE - 1;
}
char* push(const char* item) {
if (isFull()) {
printf("Stack is full. Cannot push %s.\n", item);
return NULL;
} else {
char* newItem = item;
strcpy(stack[++top], newItem);
printf("Pushed %s onto the stack.\n", item);
return newItem;
}
}
char* pop() {
if (isEmpty()) {
printf("Stack is empty. Cannot pop.\n");
return NULL;
} else {
char* poppedItem = stack[top];
top--;
return poppedItem;
}
}
void postfixToInfix(const char* postfix ) {
char infix[MAX_SIZE];
for (int i = 0; postfix[i]; i++) {
if (isalpha(postfix[i])) {
char operand[2] = { postfix[i], '\0' };
push(operand);
} else {
char* operand2 = pop();
char* operand1 = pop();
sprintf(infix, "(%s%c%s)", operand1, postfix[i], operand2);
push(infix);
}
}
char* result = pop();
printf("Infix expression:\n");
printf("%s", result);
}
int main() {
char postfix[MAX_SIZE];
printf("Enter a postfix expression: ");
scanf("%s", postfix);
postfixToInfix(postfix);
return 0;
}