Upload files to "DS/C/Lab/Week5"
This commit is contained in:
parent
0589cce66a
commit
0cdc830d54
4 changed files with 277 additions and 0 deletions
56
DS/C/Lab/Week5/PostfixToInfix.c
Normal file
56
DS/C/Lab/Week5/PostfixToInfix.c
Normal file
|
@ -0,0 +1,56 @@
|
|||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define MAX_SIZE 100
|
||||
#define MAX_EXPR_LEN 200
|
||||
|
||||
char stack[MAX_SIZE][MAX_EXPR_LEN];
|
||||
int top = -1;
|
||||
|
||||
void push(const char* item) {
|
||||
if (top >= MAX_SIZE - 1) {
|
||||
printf("Stack Overflow\n");
|
||||
return;
|
||||
}
|
||||
strcpy(stack[++top], item);
|
||||
}
|
||||
|
||||
char* pop() {
|
||||
if (top < 0) {
|
||||
printf("Stack Underflow\n");
|
||||
return NULL;
|
||||
}
|
||||
return stack[top--];
|
||||
}
|
||||
|
||||
int isOperator(char c) {
|
||||
return (c == '+' || c == '-' || c == '*' || c == '/' || c == '^');
|
||||
}
|
||||
|
||||
void postfixToInfix(const char* postfix) {
|
||||
char op1[MAX_EXPR_LEN], op2[MAX_EXPR_LEN], temp[MAX_EXPR_LEN];
|
||||
|
||||
for (int i = 0; postfix[i] != '\0'; i++) {
|
||||
if (!isOperator(postfix[i])) {
|
||||
temp[0] = postfix[i];
|
||||
temp[1] = '\0';
|
||||
push(temp);
|
||||
} else {
|
||||
strcpy(op2, pop());
|
||||
strcpy(op1, pop());
|
||||
sprintf(temp, "(%s%c%s)", op1, postfix[i], op2);
|
||||
push(temp);
|
||||
}
|
||||
}
|
||||
|
||||
printf("Infix expression: %s\n", stack[top]);
|
||||
}
|
||||
|
||||
int main() {
|
||||
char postfix[MAX_SIZE];
|
||||
printf("Enter postfix expression: ");
|
||||
gets(postfix);
|
||||
postfixToInfix(postfix);
|
||||
return 0;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue