From fd97f54f1dd765799a8935ba0369cf812584578e Mon Sep 17 00:00:00 2001 From: Mansi-Tanwar <148472134+Mansi-Tanwar@users.noreply.github.com> Date: Sat, 2 Nov 2024 17:09:27 +0530 Subject: [PATCH 01/10] Create code.c --- Postfix to Infix/code.c | 95 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 Postfix to Infix/code.c diff --git a/Postfix to Infix/code.c b/Postfix to Infix/code.c new file mode 100644 index 00000000..feeadea0 --- /dev/null +++ b/Postfix to Infix/code.c @@ -0,0 +1,95 @@ +#include +#include +#include +#include + +// Define the maximum size for the stack and expression +#define MAX 100 + +// Stack structure to store strings (expressions) +struct Stack { + int top; + char* items[MAX]; +}; + +// Function to initialize the stack +void initStack(struct Stack* stack) { + stack->top = -1; +} + +// Function to push an item onto the stack +void push(struct Stack* stack, char* str) { + if (stack->top >= MAX - 1) { + printf("Stack overflow\n"); + return; + } + stack->items[++(stack->top)] = strdup(str); // strdup duplicates the string +} + +// Function to pop an item from the stack +char* pop(struct Stack* stack) { + if (stack->top == -1) { + printf("Stack underflow\n"); + return NULL; + } + return stack->items[(stack->top)--]; +} + +// Function to check if a character is an operator +int isOperator(char ch) { + return (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '^'); +} + +// Function to convert a Postfix expression to Infix +void postfixToInfix(char* postfix) { + struct Stack stack; + initStack(&stack); + + int length = strlen(postfix); + + // Traverse the Postfix expression from left to right + for (int i = 0; i < length; i++) { + // If the character is an operand, push it onto the stack + if (isalnum(postfix[i])) { + char operand[2] = {postfix[i], '\0'}; // Create a string for the operand + push(&stack, operand); + } + // If the character is an operator + else if (isOperator(postfix[i])) { + // Pop the top two operands from the stack + char* operand1 = pop(&stack); + char* operand2 = pop(&stack); + + // Create a new string: (operand2 operator operand1) + char* expression = (char*)malloc(strlen(operand1) + strlen(operand2) + 4); + sprintf(expression, "(%s%c%s)", operand2, postfix[i], operand1); + + // Push the resulting expression back onto the stack + push(&stack, expression); + + // Free dynamically allocated memory + free(operand1); + free(operand2); + } + } + + // The final result is the remaining element in the stack + char* result = pop(&stack); + printf("Infix Expression: %s\n", result); + + // Free the final result memory + free(result); +} + +int main() { + char postfix[MAX]; + + // Input the Postfix expression + printf("Enter a Postfix expression: "); + scanf("%s", postfix); + + // Convert and display the Infix expression + postfixToInfix(postfix); + + return 0; +} From cb57a9eb9853145e271cab77f144b76c7a616ecf Mon Sep 17 00:00:00 2001 From: Mansi-Tanwar <148472134+Mansi-Tanwar@users.noreply.github.com> Date: Sat, 2 Nov 2024 17:13:05 +0530 Subject: [PATCH 02/10] Create readme.md --- Postfix to Infix/readme.md | 41 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 Postfix to Infix/readme.md diff --git a/Postfix to Infix/readme.md b/Postfix to Infix/readme.md new file mode 100644 index 00000000..037470c8 --- /dev/null +++ b/Postfix to Infix/readme.md @@ -0,0 +1,41 @@ +# Postfix to Infix Conversion + +## Description: +This project provides a tool for converting arithmetic expressions from Postfix notation (Reverse Polish Notation) to Infix notation. Postfix notation places operators after their operands, while Infix notation places operators between operands with parentheses to ensure the correct precedence. This conversion is useful for readability and in situations where expressions need to be evaluated based on operator precedence. + +## Problem Definition: +Given an arithmetic expression in Postfix form (e.g., `A B + C *`), the objective is to convert it to Infix form (e.g., `(A + B) * C`) using an algorithm that handles operator precedence and parenthesis placement correctly. + +## Key Elements: +- **Operands:** Variables or numbers. +- **Operators:** (`+`, `-`, `*`, `/`, `^`). + +The output of converting a postfix expression to an infix expression should: +1. Correctly place operators between operands. +2. Include parentheses to maintain the correct precedence of operations, avoiding ambiguity. + +## Algorithm +1. **Initialize a Stack:** Create an empty stack to store operands and intermediate expressions. +2. **Read the Postfix Expression:** Start reading the postfix expression from left to right. +3. **Process Each Symbol:** + - **If the Symbol is an Operand:** + - Push it onto the stack as a string. + - **If the Symbol is an Operator:** + - Pop the top two elements from the stack. Let’s call them `operand1` and `operand2`. + - Form a new infix expression in the format `(operand2 operator operand1)` (since postfix order is reversed). + - Push this new expression (with parentheses) back onto the stack. +4. **Repeat:** Continue processing each symbol in the postfix expression until the entire expression is parsed. +5. **Final Result:** The stack will contain one element at the end, which is the required infix expression. + +## Example +For the Postfix expression: `A B + C *` +- **Step 1:** `A` is an operand, push it → Stack: `[A]` +- **Step 2:** `B` is an operand, push it → Stack: `[A, B]` +- **Step 3:** `+` is an operator, pop `B` and `A`, form `(A + B)`, push it → Stack: `[(A + B)]` +- **Step 4:** `C` is an operand, push it → Stack: `[(A + B), C]` +- **Step 5:** `*` is an operator, pop `C` and `(A + B)`, form `((A + B) * C)`, push it → Stack: `[((A + B) * C)]` + +**Final Infix output:** `((A + B) * C)` + +## Time Complexity +The algorithm performs a single scan of the postfix expression, making it **O(n)** in time complexity, where `n` is the number of characters in the input expression. Each operand and operator is pushed and popped from the stack at most once, ensuring linear complexity. From 81c376b4caeca6f4e4a4d4bbf1f24f89762b6ca1 Mon Sep 17 00:00:00 2001 From: Mansi-Tanwar <148472134+Mansi-Tanwar@users.noreply.github.com> Date: Sat, 2 Nov 2024 17:19:21 +0530 Subject: [PATCH 03/10] Create readme.md --- Postfix to Infix/conversion/readme.md | 41 +++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 Postfix to Infix/conversion/readme.md diff --git a/Postfix to Infix/conversion/readme.md b/Postfix to Infix/conversion/readme.md new file mode 100644 index 00000000..037470c8 --- /dev/null +++ b/Postfix to Infix/conversion/readme.md @@ -0,0 +1,41 @@ +# Postfix to Infix Conversion + +## Description: +This project provides a tool for converting arithmetic expressions from Postfix notation (Reverse Polish Notation) to Infix notation. Postfix notation places operators after their operands, while Infix notation places operators between operands with parentheses to ensure the correct precedence. This conversion is useful for readability and in situations where expressions need to be evaluated based on operator precedence. + +## Problem Definition: +Given an arithmetic expression in Postfix form (e.g., `A B + C *`), the objective is to convert it to Infix form (e.g., `(A + B) * C`) using an algorithm that handles operator precedence and parenthesis placement correctly. + +## Key Elements: +- **Operands:** Variables or numbers. +- **Operators:** (`+`, `-`, `*`, `/`, `^`). + +The output of converting a postfix expression to an infix expression should: +1. Correctly place operators between operands. +2. Include parentheses to maintain the correct precedence of operations, avoiding ambiguity. + +## Algorithm +1. **Initialize a Stack:** Create an empty stack to store operands and intermediate expressions. +2. **Read the Postfix Expression:** Start reading the postfix expression from left to right. +3. **Process Each Symbol:** + - **If the Symbol is an Operand:** + - Push it onto the stack as a string. + - **If the Symbol is an Operator:** + - Pop the top two elements from the stack. Let’s call them `operand1` and `operand2`. + - Form a new infix expression in the format `(operand2 operator operand1)` (since postfix order is reversed). + - Push this new expression (with parentheses) back onto the stack. +4. **Repeat:** Continue processing each symbol in the postfix expression until the entire expression is parsed. +5. **Final Result:** The stack will contain one element at the end, which is the required infix expression. + +## Example +For the Postfix expression: `A B + C *` +- **Step 1:** `A` is an operand, push it → Stack: `[A]` +- **Step 2:** `B` is an operand, push it → Stack: `[A, B]` +- **Step 3:** `+` is an operator, pop `B` and `A`, form `(A + B)`, push it → Stack: `[(A + B)]` +- **Step 4:** `C` is an operand, push it → Stack: `[(A + B), C]` +- **Step 5:** `*` is an operator, pop `C` and `(A + B)`, form `((A + B) * C)`, push it → Stack: `[((A + B) * C)]` + +**Final Infix output:** `((A + B) * C)` + +## Time Complexity +The algorithm performs a single scan of the postfix expression, making it **O(n)** in time complexity, where `n` is the number of characters in the input expression. Each operand and operator is pushed and popped from the stack at most once, ensuring linear complexity. From c9eaff0db04d12f344bcdeb5d664f7e88f9e9fc6 Mon Sep 17 00:00:00 2001 From: Mansi-Tanwar <148472134+Mansi-Tanwar@users.noreply.github.com> Date: Sat, 2 Nov 2024 17:20:16 +0530 Subject: [PATCH 04/10] Create code.c --- Postfix to Infix/conversion/code.c | 95 ++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 Postfix to Infix/conversion/code.c diff --git a/Postfix to Infix/conversion/code.c b/Postfix to Infix/conversion/code.c new file mode 100644 index 00000000..feeadea0 --- /dev/null +++ b/Postfix to Infix/conversion/code.c @@ -0,0 +1,95 @@ +#include +#include +#include +#include + +// Define the maximum size for the stack and expression +#define MAX 100 + +// Stack structure to store strings (expressions) +struct Stack { + int top; + char* items[MAX]; +}; + +// Function to initialize the stack +void initStack(struct Stack* stack) { + stack->top = -1; +} + +// Function to push an item onto the stack +void push(struct Stack* stack, char* str) { + if (stack->top >= MAX - 1) { + printf("Stack overflow\n"); + return; + } + stack->items[++(stack->top)] = strdup(str); // strdup duplicates the string +} + +// Function to pop an item from the stack +char* pop(struct Stack* stack) { + if (stack->top == -1) { + printf("Stack underflow\n"); + return NULL; + } + return stack->items[(stack->top)--]; +} + +// Function to check if a character is an operator +int isOperator(char ch) { + return (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '^'); +} + +// Function to convert a Postfix expression to Infix +void postfixToInfix(char* postfix) { + struct Stack stack; + initStack(&stack); + + int length = strlen(postfix); + + // Traverse the Postfix expression from left to right + for (int i = 0; i < length; i++) { + // If the character is an operand, push it onto the stack + if (isalnum(postfix[i])) { + char operand[2] = {postfix[i], '\0'}; // Create a string for the operand + push(&stack, operand); + } + // If the character is an operator + else if (isOperator(postfix[i])) { + // Pop the top two operands from the stack + char* operand1 = pop(&stack); + char* operand2 = pop(&stack); + + // Create a new string: (operand2 operator operand1) + char* expression = (char*)malloc(strlen(operand1) + strlen(operand2) + 4); + sprintf(expression, "(%s%c%s)", operand2, postfix[i], operand1); + + // Push the resulting expression back onto the stack + push(&stack, expression); + + // Free dynamically allocated memory + free(operand1); + free(operand2); + } + } + + // The final result is the remaining element in the stack + char* result = pop(&stack); + printf("Infix Expression: %s\n", result); + + // Free the final result memory + free(result); +} + +int main() { + char postfix[MAX]; + + // Input the Postfix expression + printf("Enter a Postfix expression: "); + scanf("%s", postfix); + + // Convert and display the Infix expression + postfixToInfix(postfix); + + return 0; +} From ec02b4c313c16fd819096d4f3a37592a12794be1 Mon Sep 17 00:00:00 2001 From: Mansi-Tanwar <148472134+Mansi-Tanwar@users.noreply.github.com> Date: Sat, 2 Nov 2024 17:20:40 +0530 Subject: [PATCH 05/10] Delete Postfix to Infix/code.c --- Postfix to Infix/code.c | 95 ----------------------------------------- 1 file changed, 95 deletions(-) delete mode 100644 Postfix to Infix/code.c diff --git a/Postfix to Infix/code.c b/Postfix to Infix/code.c deleted file mode 100644 index feeadea0..00000000 --- a/Postfix to Infix/code.c +++ /dev/null @@ -1,95 +0,0 @@ -#include -#include -#include -#include - -// Define the maximum size for the stack and expression -#define MAX 100 - -// Stack structure to store strings (expressions) -struct Stack { - int top; - char* items[MAX]; -}; - -// Function to initialize the stack -void initStack(struct Stack* stack) { - stack->top = -1; -} - -// Function to push an item onto the stack -void push(struct Stack* stack, char* str) { - if (stack->top >= MAX - 1) { - printf("Stack overflow\n"); - return; - } - stack->items[++(stack->top)] = strdup(str); // strdup duplicates the string -} - -// Function to pop an item from the stack -char* pop(struct Stack* stack) { - if (stack->top == -1) { - printf("Stack underflow\n"); - return NULL; - } - return stack->items[(stack->top)--]; -} - -// Function to check if a character is an operator -int isOperator(char ch) { - return (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '^'); -} - -// Function to convert a Postfix expression to Infix -void postfixToInfix(char* postfix) { - struct Stack stack; - initStack(&stack); - - int length = strlen(postfix); - - // Traverse the Postfix expression from left to right - for (int i = 0; i < length; i++) { - // If the character is an operand, push it onto the stack - if (isalnum(postfix[i])) { - char operand[2] = {postfix[i], '\0'}; // Create a string for the operand - push(&stack, operand); - } - // If the character is an operator - else if (isOperator(postfix[i])) { - // Pop the top two operands from the stack - char* operand1 = pop(&stack); - char* operand2 = pop(&stack); - - // Create a new string: (operand2 operator operand1) - char* expression = (char*)malloc(strlen(operand1) + strlen(operand2) + 4); - sprintf(expression, "(%s%c%s)", operand2, postfix[i], operand1); - - // Push the resulting expression back onto the stack - push(&stack, expression); - - // Free dynamically allocated memory - free(operand1); - free(operand2); - } - } - - // The final result is the remaining element in the stack - char* result = pop(&stack); - printf("Infix Expression: %s\n", result); - - // Free the final result memory - free(result); -} - -int main() { - char postfix[MAX]; - - // Input the Postfix expression - printf("Enter a Postfix expression: "); - scanf("%s", postfix); - - // Convert and display the Infix expression - postfixToInfix(postfix); - - return 0; -} From 305bca0272b111edc849ac8339517f09aed29b0b Mon Sep 17 00:00:00 2001 From: Mansi-Tanwar <148472134+Mansi-Tanwar@users.noreply.github.com> Date: Sat, 2 Nov 2024 17:20:57 +0530 Subject: [PATCH 06/10] Delete Postfix to Infix/readme.md --- Postfix to Infix/readme.md | 41 -------------------------------------- 1 file changed, 41 deletions(-) delete mode 100644 Postfix to Infix/readme.md diff --git a/Postfix to Infix/readme.md b/Postfix to Infix/readme.md deleted file mode 100644 index 037470c8..00000000 --- a/Postfix to Infix/readme.md +++ /dev/null @@ -1,41 +0,0 @@ -# Postfix to Infix Conversion - -## Description: -This project provides a tool for converting arithmetic expressions from Postfix notation (Reverse Polish Notation) to Infix notation. Postfix notation places operators after their operands, while Infix notation places operators between operands with parentheses to ensure the correct precedence. This conversion is useful for readability and in situations where expressions need to be evaluated based on operator precedence. - -## Problem Definition: -Given an arithmetic expression in Postfix form (e.g., `A B + C *`), the objective is to convert it to Infix form (e.g., `(A + B) * C`) using an algorithm that handles operator precedence and parenthesis placement correctly. - -## Key Elements: -- **Operands:** Variables or numbers. -- **Operators:** (`+`, `-`, `*`, `/`, `^`). - -The output of converting a postfix expression to an infix expression should: -1. Correctly place operators between operands. -2. Include parentheses to maintain the correct precedence of operations, avoiding ambiguity. - -## Algorithm -1. **Initialize a Stack:** Create an empty stack to store operands and intermediate expressions. -2. **Read the Postfix Expression:** Start reading the postfix expression from left to right. -3. **Process Each Symbol:** - - **If the Symbol is an Operand:** - - Push it onto the stack as a string. - - **If the Symbol is an Operator:** - - Pop the top two elements from the stack. Let’s call them `operand1` and `operand2`. - - Form a new infix expression in the format `(operand2 operator operand1)` (since postfix order is reversed). - - Push this new expression (with parentheses) back onto the stack. -4. **Repeat:** Continue processing each symbol in the postfix expression until the entire expression is parsed. -5. **Final Result:** The stack will contain one element at the end, which is the required infix expression. - -## Example -For the Postfix expression: `A B + C *` -- **Step 1:** `A` is an operand, push it → Stack: `[A]` -- **Step 2:** `B` is an operand, push it → Stack: `[A, B]` -- **Step 3:** `+` is an operator, pop `B` and `A`, form `(A + B)`, push it → Stack: `[(A + B)]` -- **Step 4:** `C` is an operand, push it → Stack: `[(A + B), C]` -- **Step 5:** `*` is an operator, pop `C` and `(A + B)`, form `((A + B) * C)`, push it → Stack: `[((A + B) * C)]` - -**Final Infix output:** `((A + B) * C)` - -## Time Complexity -The algorithm performs a single scan of the postfix expression, making it **O(n)** in time complexity, where `n` is the number of characters in the input expression. Each operand and operator is pushed and popped from the stack at most once, ensuring linear complexity. From 40af2430c0e9eedbfa1df524ae50ba2c738214e5 Mon Sep 17 00:00:00 2001 From: Mansi-Tanwar <148472134+Mansi-Tanwar@users.noreply.github.com> Date: Sat, 2 Nov 2024 17:25:40 +0530 Subject: [PATCH 07/10] Create code.c --- Postfix to Infix/evaluation/code.c | 90 ++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 Postfix to Infix/evaluation/code.c diff --git a/Postfix to Infix/evaluation/code.c b/Postfix to Infix/evaluation/code.c new file mode 100644 index 00000000..849e7752 --- /dev/null +++ b/Postfix to Infix/evaluation/code.c @@ -0,0 +1,90 @@ +#include +#include +#include + +#define MAX 100 // Maximum size of the stack + +// Stack structure for integer values +struct Stack { + int top; + int items[MAX]; +}; + +// Function to initialize the stack +void initStack(struct Stack *s) { + s->top = -1; +} + +// Function to push an integer onto the stack +void push(struct Stack *s, int value) { + if (s->top == MAX - 1) { + printf("Stack Overflow!\n"); + return; + } + s->items[++(s->top)] = value; +} + +// Function to pop an integer from the stack +int pop(struct Stack *s) { + if (s->top == -1) { + printf("Stack Underflow!\n"); + return -1; + } + return s->items[(s->top)--]; +} + +// Function to evaluate the postfix expression +int evaluatePostfix(char *postfix) { + struct Stack s; + initStack(&s); + int i = 0; + + while (postfix[i] != '\0') { + char ch = postfix[i]; + + // If the character is an operand (digit), push it to the stack + if (isdigit(ch)) { + push(&s, ch - '0'); // Convert character to integer and push + } + // If the character is an operator, pop two elements and apply the operation + else if (ch == '+' || ch == '-' || ch == '*' || ch == '/') { + int operand2 = pop(&s); + int operand1 = pop(&s); + + // Perform the operation and push the result back to the stack + switch (ch) { + case '+': push(&s, operand1 + operand2); break; + case '-': push(&s, operand1 - operand2); break; + case '*': push(&s, operand1 * operand2); break; + case '/': + if (operand2 != 0) { + push(&s, operand1 / operand2); + } else { + printf("Division by zero error!\n"); + return -1; // Return an error code + } + break; + } + } + i++; + } + + // The final result is the only element left in the stack + return pop(&s); +} + +int main() { + char postfix[MAX]; + + // Input postfix expression + printf("Enter a postfix expression: "); + scanf("%s", postfix); + + // Evaluate the postfix expression + int result = evaluatePostfix(postfix); + if (result != -1) { + printf("Result of evaluation: %d\n", result); + } + + return 0; +} From 7681da679e69c55f68861a3b76153c121576f221 Mon Sep 17 00:00:00 2001 From: Mansi-Tanwar <148472134+Mansi-Tanwar@users.noreply.github.com> Date: Sat, 2 Nov 2024 17:28:43 +0530 Subject: [PATCH 08/10] Create readme.md --- Postfix to Infix/evaluation/readme.md | 61 +++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 Postfix to Infix/evaluation/readme.md diff --git a/Postfix to Infix/evaluation/readme.md b/Postfix to Infix/evaluation/readme.md new file mode 100644 index 00000000..d847653d --- /dev/null +++ b/Postfix to Infix/evaluation/readme.md @@ -0,0 +1,61 @@ +# Postfix Expression Evaluation + +## Description: +This project implements a tool for evaluating arithmetic expressions written in Postfix notation (Reverse Polish Notation). Postfix notation is a mathematical notation in which every operator follows all of its operands, eliminating the need for parentheses to dictate the order of operations. + +## Problem Definition: +Given an arithmetic expression in Postfix form (e.g., `23 45 * 9 -`), the objective is to evaluate the expression and return the result using an algorithm that efficiently manages the operands and operators using a stack data structure. + +## Key Elements: +- **Operands:** Numeric values. +- **Operators:** `+`, `-`, `*`, `/`. + +The output of evaluating a Postfix expression should: +1. Correctly apply operators to their respective operands. +2. Handle arithmetic operations including addition, subtraction, multiplication, and division. +3. Manage errors, such as division by zero, appropriately. + +## Algorithm +1. **Initialize a Stack:** Create an empty stack to store integer values. +2. **Read the Postfix Expression:** Start reading the postfix expression from left to right. +3. **Process Each Symbol:** + - **If the Symbol is an Operand:** + - Convert it to an integer and push it onto the stack. + - **If the Symbol is an Operator:** + - Pop the top two operands from the stack. + - Perform the arithmetic operation corresponding to the operator. + - Push the result back onto the stack. +4. **Repeat:** Continue this process until all symbols in the postfix expression are processed. +5. **Final Result:** The stack will contain one element at the end, which is the result of the expression. + +## Example +For the Postfix expression: `23 45 * 9 -` +- **Step 1:** Push 2 → Stack: [2] +- **Step 2:** Push 3 → Stack: [2, 3] +- **Step 3:** `*` is an operator, pop 3 and 2, compute 2 * 3 = 6, push the result → Stack: [6] +- **Step 4:** Push 4 → Stack: [6, 4] +- **Step 5:** Push 5 → Stack: [6, 4, 5] +- **Step 6:** `-` is an operator, pop 5 and 4, compute 4 - 5 = -1, push the result → Stack: [6, -1] +- **Step 7:** Push 9 → Stack: [6, -1, 9] +- **Step 8:** `-` is an operator, pop 9 and -1, compute -1 - 9 = -10, push the result → Stack: [6, -10] +- **Step 9:** `*` is an operator, pop -10 and 6, compute 6 * -10 = -60, push the result → Stack: [-60] + +**Final result:** `-60` + +## Time Complexity +The algorithm performs a single scan of the postfix expression, making it **O(n)** in time complexity, where n is the number of characters in the input expression. Each operand and operator is pushed and popped from the stack at most once, ensuring linear complexity. + +## Usage +1. Compile the program using a C compiler. +2. Run the executable and input a valid postfix expression when prompted. +3. The program will output the result of the evaluation. + +## Example Input +``` +Enter a postfix expression: 23 45 * 9 - +``` + +## Example Output +``` +Result of evaluation: -60 +``` From 682900db56cc33d61d588aca2f200caa4a362a81 Mon Sep 17 00:00:00 2001 From: Pankaj Kumar Bind <73558583+pankaj-bind@users.noreply.github.com> Date: Mon, 4 Nov 2024 12:50:50 +0530 Subject: [PATCH 09/10] Delete Postfix to Infix/evaluation/code.c --- Postfix to Infix/evaluation/code.c | 90 ------------------------------ 1 file changed, 90 deletions(-) delete mode 100644 Postfix to Infix/evaluation/code.c diff --git a/Postfix to Infix/evaluation/code.c b/Postfix to Infix/evaluation/code.c deleted file mode 100644 index 849e7752..00000000 --- a/Postfix to Infix/evaluation/code.c +++ /dev/null @@ -1,90 +0,0 @@ -#include -#include -#include - -#define MAX 100 // Maximum size of the stack - -// Stack structure for integer values -struct Stack { - int top; - int items[MAX]; -}; - -// Function to initialize the stack -void initStack(struct Stack *s) { - s->top = -1; -} - -// Function to push an integer onto the stack -void push(struct Stack *s, int value) { - if (s->top == MAX - 1) { - printf("Stack Overflow!\n"); - return; - } - s->items[++(s->top)] = value; -} - -// Function to pop an integer from the stack -int pop(struct Stack *s) { - if (s->top == -1) { - printf("Stack Underflow!\n"); - return -1; - } - return s->items[(s->top)--]; -} - -// Function to evaluate the postfix expression -int evaluatePostfix(char *postfix) { - struct Stack s; - initStack(&s); - int i = 0; - - while (postfix[i] != '\0') { - char ch = postfix[i]; - - // If the character is an operand (digit), push it to the stack - if (isdigit(ch)) { - push(&s, ch - '0'); // Convert character to integer and push - } - // If the character is an operator, pop two elements and apply the operation - else if (ch == '+' || ch == '-' || ch == '*' || ch == '/') { - int operand2 = pop(&s); - int operand1 = pop(&s); - - // Perform the operation and push the result back to the stack - switch (ch) { - case '+': push(&s, operand1 + operand2); break; - case '-': push(&s, operand1 - operand2); break; - case '*': push(&s, operand1 * operand2); break; - case '/': - if (operand2 != 0) { - push(&s, operand1 / operand2); - } else { - printf("Division by zero error!\n"); - return -1; // Return an error code - } - break; - } - } - i++; - } - - // The final result is the only element left in the stack - return pop(&s); -} - -int main() { - char postfix[MAX]; - - // Input postfix expression - printf("Enter a postfix expression: "); - scanf("%s", postfix); - - // Evaluate the postfix expression - int result = evaluatePostfix(postfix); - if (result != -1) { - printf("Result of evaluation: %d\n", result); - } - - return 0; -} From 44768026cfeb08f29e5637573fb42a8d1fd90df0 Mon Sep 17 00:00:00 2001 From: Pankaj Kumar Bind <73558583+pankaj-bind@users.noreply.github.com> Date: Mon, 4 Nov 2024 12:51:04 +0530 Subject: [PATCH 10/10] Delete Postfix to Infix/evaluation/readme.md --- Postfix to Infix/evaluation/readme.md | 61 --------------------------- 1 file changed, 61 deletions(-) delete mode 100644 Postfix to Infix/evaluation/readme.md diff --git a/Postfix to Infix/evaluation/readme.md b/Postfix to Infix/evaluation/readme.md deleted file mode 100644 index d847653d..00000000 --- a/Postfix to Infix/evaluation/readme.md +++ /dev/null @@ -1,61 +0,0 @@ -# Postfix Expression Evaluation - -## Description: -This project implements a tool for evaluating arithmetic expressions written in Postfix notation (Reverse Polish Notation). Postfix notation is a mathematical notation in which every operator follows all of its operands, eliminating the need for parentheses to dictate the order of operations. - -## Problem Definition: -Given an arithmetic expression in Postfix form (e.g., `23 45 * 9 -`), the objective is to evaluate the expression and return the result using an algorithm that efficiently manages the operands and operators using a stack data structure. - -## Key Elements: -- **Operands:** Numeric values. -- **Operators:** `+`, `-`, `*`, `/`. - -The output of evaluating a Postfix expression should: -1. Correctly apply operators to their respective operands. -2. Handle arithmetic operations including addition, subtraction, multiplication, and division. -3. Manage errors, such as division by zero, appropriately. - -## Algorithm -1. **Initialize a Stack:** Create an empty stack to store integer values. -2. **Read the Postfix Expression:** Start reading the postfix expression from left to right. -3. **Process Each Symbol:** - - **If the Symbol is an Operand:** - - Convert it to an integer and push it onto the stack. - - **If the Symbol is an Operator:** - - Pop the top two operands from the stack. - - Perform the arithmetic operation corresponding to the operator. - - Push the result back onto the stack. -4. **Repeat:** Continue this process until all symbols in the postfix expression are processed. -5. **Final Result:** The stack will contain one element at the end, which is the result of the expression. - -## Example -For the Postfix expression: `23 45 * 9 -` -- **Step 1:** Push 2 → Stack: [2] -- **Step 2:** Push 3 → Stack: [2, 3] -- **Step 3:** `*` is an operator, pop 3 and 2, compute 2 * 3 = 6, push the result → Stack: [6] -- **Step 4:** Push 4 → Stack: [6, 4] -- **Step 5:** Push 5 → Stack: [6, 4, 5] -- **Step 6:** `-` is an operator, pop 5 and 4, compute 4 - 5 = -1, push the result → Stack: [6, -1] -- **Step 7:** Push 9 → Stack: [6, -1, 9] -- **Step 8:** `-` is an operator, pop 9 and -1, compute -1 - 9 = -10, push the result → Stack: [6, -10] -- **Step 9:** `*` is an operator, pop -10 and 6, compute 6 * -10 = -60, push the result → Stack: [-60] - -**Final result:** `-60` - -## Time Complexity -The algorithm performs a single scan of the postfix expression, making it **O(n)** in time complexity, where n is the number of characters in the input expression. Each operand and operator is pushed and popped from the stack at most once, ensuring linear complexity. - -## Usage -1. Compile the program using a C compiler. -2. Run the executable and input a valid postfix expression when prompted. -3. The program will output the result of the evaluation. - -## Example Input -``` -Enter a postfix expression: 23 45 * 9 - -``` - -## Example Output -``` -Result of evaluation: -60 -```