From c6df50662d5d2480c7de53bb3b4fca6efb074410 Mon Sep 17 00:00:00 2001 From: Aadit Agrawal Date: Tue, 3 Sep 2024 00:27:03 +0530 Subject: [PATCH] Upload files to "DS/C/Lab/Week6" --- DS/C/Lab/Week6/InfixToPrefix.c | 91 ++++++++++++++++++++++++++++++++ DS/C/Lab/Week6/PrefixCheck.c | 70 ++++++++++++++++++++++++ DS/C/Lab/Week6/PrefixToInfix.c | 63 ++++++++++++++++++++++ DS/C/Lab/Week6/PrefixToPostfix.c | 63 ++++++++++++++++++++++ 4 files changed, 287 insertions(+) create mode 100644 DS/C/Lab/Week6/InfixToPrefix.c create mode 100644 DS/C/Lab/Week6/PrefixCheck.c create mode 100644 DS/C/Lab/Week6/PrefixToInfix.c create mode 100644 DS/C/Lab/Week6/PrefixToPostfix.c diff --git a/DS/C/Lab/Week6/InfixToPrefix.c b/DS/C/Lab/Week6/InfixToPrefix.c new file mode 100644 index 0000000..a1a2909 --- /dev/null +++ b/DS/C/Lab/Week6/InfixToPrefix.c @@ -0,0 +1,91 @@ +#include +#include + +#define MAX_SIZE 100 + +char stack[MAX_SIZE]; +int top = -1; + +void push(char item) { + if (top >= MAX_SIZE - 1) { + printf("Stack Overflow\n"); + return; + } + stack[++top] = item; +} + +char pop() { + if (top < 0) { + printf("Stack Underflow\n"); + return -1; + } + return stack[top--]; +} + +int precedence(char symbol) { + switch (symbol) { + case '^': return 3; + case '*': + case '/': return 2; + case '+': + case '-': return 1; + default: return 0; + } +} + +void reverse(char *str) { + int len = strlen(str); + int i, j; + char temp; + for (i = 0, j = len - 1; i < j; i++, j--) { + temp = str[i]; + str[i] = str[j]; + str[j] = temp; + } +} + +int isalnum(char c) { + return ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')); +} + +void infixToPrefix(char infix[], char prefix[]) { + char symbol, temp[2]; + int i, j = 0; + + reverse(infix); + + for (i = 0; infix[i] != '\0'; i++) { + symbol = infix[i]; + if (isalnum(symbol)) { + prefix[j++] = symbol; + } else if (symbol == ')') { + push(symbol); + } else if (symbol == '(') { + while ((temp[0] = pop()) != ')') { + prefix[j++] = temp[0]; + } + } else { + while (top > -1 && precedence(stack[top]) > precedence(symbol)) { + prefix[j++] = pop(); + } + push(symbol); + } + } + + while (top > -1) { + prefix[j++] = pop(); + } + prefix[j] = '\0'; + + reverse(prefix); +} + +int main() { + char infix[MAX_SIZE], prefix[MAX_SIZE]; + printf("Enter infix expression: "); + fgets(infix, MAX_SIZE, stdin); + infix[strcspn(infix, "\n")] = 0; // Remove newline if present + infixToPrefix(infix, prefix); + printf("Prefix expression: %s\n", prefix); + return 0; +} diff --git a/DS/C/Lab/Week6/PrefixCheck.c b/DS/C/Lab/Week6/PrefixCheck.c new file mode 100644 index 0000000..dc34168 --- /dev/null +++ b/DS/C/Lab/Week6/PrefixCheck.c @@ -0,0 +1,70 @@ +#include +#include + +#define MAX_SIZE 100 + +int stack[MAX_SIZE]; +int top = -1; + +void push(int item) { + if (top >= MAX_SIZE - 1) { + printf("Stack Overflow\n"); + return; + } + stack[++top] = item; +} + +int pop() { + if (top < 0) { + printf("Stack Underflow\n"); + return -1; + } + return stack[top--]; +} + +int isOperator(char c) { + return (c == '+' || c == '-' || c == '*' || c == '/'); +} + +int isDigit(char c) { + return (c >= '0' && c <= '9'); +} + +int evaluatePrefix(char* expr) { + int len = strlen(expr); + for (int i = len - 1; i >= 0; i--) { + if (isDigit(expr[i])) { + push(expr[i] - '0'); + } else if (isOperator(expr[i])) { + int operand1 = pop(); + int operand2 = pop(); + switch (expr[i]) { + case '+': push(operand1 + operand2); break; + case '-': push(operand1 - operand2); break; + case '*': push(operand1 * operand2); break; + case '/': + if (operand2 != 0) push(operand1 / operand2); + else { + printf("Error: Division by zero\n"); + return -1; + } + break; + } + } + } + return pop(); +} + +int main() { + char expr[MAX_SIZE]; + printf("Enter prefix expression: "); + fgets(expr, MAX_SIZE, stdin); + expr[strcspn(expr, "\n")] = 0; // Remove newline if present + + int result = evaluatePrefix(expr); + if (result != -1) { + printf("Result: %d\n", result); + } + + return 0; +} \ No newline at end of file diff --git a/DS/C/Lab/Week6/PrefixToInfix.c b/DS/C/Lab/Week6/PrefixToInfix.c new file mode 100644 index 0000000..a6a49a9 --- /dev/null +++ b/DS/C/Lab/Week6/PrefixToInfix.c @@ -0,0 +1,63 @@ +#include +#include + +#define MAX_SIZE 100 + +char stack[MAX_SIZE][MAX_SIZE]; +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 == '/'); +} + +int isAlphanumeric(char c) { + return ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')); +} + +void prefixToInfix(char* prefix) { + int len = strlen(prefix); + char temp[MAX_SIZE]; + + for (int i = len - 1; i >= 0; i--) { + if (isAlphanumeric(prefix[i])) { + temp[0] = prefix[i]; + temp[1] = '\0'; + push(temp); + } else if (isOperator(prefix[i])) { + char op1[MAX_SIZE], op2[MAX_SIZE], result[MAX_SIZE]; + strcpy(op1, pop()); + strcpy(op2, pop()); + sprintf(result, "(%s%c%s)", op1, prefix[i], op2); + push(result); + } + } + + printf("Infix expression: %s\n", stack[top]); +} + +int main() { + char prefix[MAX_SIZE]; + printf("Enter prefix expression: "); + fgets(prefix, MAX_SIZE, stdin); + prefix[strcspn(prefix, "\n")] = 0; // Remove newline if present + + prefixToInfix(prefix); + + return 0; +} \ No newline at end of file diff --git a/DS/C/Lab/Week6/PrefixToPostfix.c b/DS/C/Lab/Week6/PrefixToPostfix.c new file mode 100644 index 0000000..e3ec024 --- /dev/null +++ b/DS/C/Lab/Week6/PrefixToPostfix.c @@ -0,0 +1,63 @@ +#include +#include + +#define MAX_SIZE 100 + +char stack[MAX_SIZE][MAX_SIZE]; +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 == '/'); +} + +int isAlphanumeric(char c) { + return ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')); +} + +void prefixToPostfix(char* prefix) { + int len = strlen(prefix); + char temp[MAX_SIZE]; + + for (int i = len - 1; i >= 0; i--) { + if (isAlphanumeric(prefix[i])) { + temp[0] = prefix[i]; + temp[1] = '\0'; + push(temp); + } else if (isOperator(prefix[i])) { + char op1[MAX_SIZE], op2[MAX_SIZE], result[MAX_SIZE]; + strcpy(op1, pop()); + strcpy(op2, pop()); + sprintf(result, "%s%s%c", op1, op2, prefix[i]); + push(result); + } + } + + printf("Postfix expression: %s\n", stack[top]); +} + +int main() { + char prefix[MAX_SIZE]; + printf("Enter prefix expression: "); + fgets(prefix, MAX_SIZE, stdin); + prefix[strcspn(prefix, "\n")] = 0; // Remove newline if present + + prefixToPostfix(prefix); + + return 0; +} \ No newline at end of file