Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Complete the code and write the fenctions required to build the Efalotion Experation Tree in this code and write the necessary finctions to achieve the

Complete the code and write the fenctions required to build the Efalotion Experation Tree in this code and write the necessary finctions to achieve the third and fifth options in C so that you will add the finctions to this code

#include #include #include // Define the structure for the linked list node typedef struct ListNode { char equation[100]; struct ListNode *next; } ListNode; // Define the structure for the linked list typedef struct { ListNode *head; } LinkedList; /* Stack structure */ struct Stack { int top; unsigned capacity; char* array; }; // Define the structure for the expression tree node typedef struct TreeNode { char data; struct TreeNode* left; struct TreeNode* right; } TreeNode; // Function to read equations from a file and store them in a linked list void readEquationsFromFile(LinkedList *list, const char *filename) { FILE *file = fopen(filename, "r"); if (file == NULL) { printf("Error opening file %s. ", filename); return; } char equation[100]; while (fgets(equation, sizeof(equation), file) != NULL) { // Ignore empty lines if (strcmp(equation, " ") == 0 || strcmp(equation, " ") == 0) { continue; } // Remove unwanted characters from the equation char sanitizedEquation[100]; int j = 0; for (int i = 0; equation[i] != '\0'; i++) { if (isalnum(equation[i]) || equation[i] == '+' || equation[i] == '-' || equation[i] == '*' || equation[i] == '/' || equation[i] == '%' || equation[i] == '(' || equation[i] == ')') { sanitizedEquation[j++] = equation[i]; } } sanitizedEquation[j] = '\0'; // Add the sanitized equation to the end of the linked list ListNode *newNode = (ListNode *)malloc(sizeof(ListNode)); strcpy(newNode->equation, sanitizedEquation); newNode->next = NULL; if (list->head == NULL) { // If the list is empty, set the new node as the head list->head = newNode; } else { // Find the last node and append the new node ListNode *current = list->head; while (current->next != NULL) { current = current->next; } current->next = newNode; } } printf("The file has been read successfully. "); fclose(file); } // Function to print equations from the linked list void printEquations(LinkedList *list) { ListNode *current = list->head; while (current != NULL) { printf("Equation: %s ", current->equation); current = current->next; } } /* Create a new stack */ struct Stack* createStack(unsigned capacity) { struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack)); stack->capacity = capacity; stack->top = -1; stack->array = (char*)malloc(stack->capacity * sizeof(char)); return stack; } /* Add an element to the stack */ void push(struct Stack* stack, char item) { stack->array[++stack->top] = item; } /* Extract an element from the stack and return its value */ char pop(struct Stack* stack) { if (isEmpty(stack)) return '\0'; return stack->array[stack->top--]; } /* Peek at the top of the stack without extracting the element */ char top(struct Stack* stack) { if (isEmpty(stack)) return '\0'; return stack->array[stack->top]; } /* Check if the stack is empty */ int isEmpty(struct Stack* stack) { return stack->top == -1; } /* Get the priority of an operator */ int precedence(char x) { if (x == '-' || x == '+') { return 0; } if (x == '*' || x == '/' || x == '%') { return 1; } if (x == '^') { return 2; } return -1; } /* Convert infix expression to postfix */ void infixToPostfix(char equation[], char result[]) { int index = 0; struct Stack* stack = createStack(strlen(equation)); for (int i = 0; i < strlen(equation); i++) { if ((equation[i] == '+' || equation[i] == '-') && (i == 0 || equation[i - 1] == '(' || equation[i - 1] == '+' || equation[i - 1] == '-' || equation[i - 1] == '*' || equation[i - 1] == '/' || equation[i - 1] == '%' || equation[i - 1] == '^')) { // Handle positive/negative sign at the beginning of a number result[index] = equation[i]; index++; // Check if the next character is a digit if (i < strlen(equation) - 1 && isdigit(equation[i + 1])) { // Append the digit to the result result[index] = equation[i + 1]; index++; // Move to the next character i++; } result[index] = ' '; index++; } else if (equation[i] >= '0' && equation[i] <= '9') { // Process digits result[index] = equation[i]; index++; if (i < strlen(equation) - 1 && (equation[i + 1] < '0' || equation[i + 1] > '9')) { result[index] = ' '; index++; } if (i == strlen(equation) - 1) { result[index] = ' '; index++; } } else if (equation[i] == '+' || equation[i] == '-' || equation[i] == '*' || equation[i] == '/' || equation[i] == '%' || equation[i] == '^') { // Process operators if (isEmpty(stack) || stack->array[stack->top] == '(' || stack->array[stack->top] == '[' || stack->array[stack->top] == '{') { push(stack, equation[i]); } else { while (!isEmpty(stack) && precedence(equation[i]) <= precedence(top(stack))) { result[index] = pop(stack); index++; result[index] = ' '; index++; } push(stack, equation[i]); } } else if (equation[i] == '(' || equation[i] == '[' || equation[i] == '{') { // Process opening parentheses push(stack, equation[i]); } else if (equation[i] == ')' || equation[i] == ']' || equation[i] == '}') { // Process closing parentheses while (!isEmpty(stack) && stack->array[stack->top] != '(' && stack->array[stack->top] != '[' && stack->array[stack->top] != '{') { result[index] = pop(stack); index++; result[index] = ' '; index++; } if (!isEmpty(stack) && stack->array[stack->top] != '(' && stack->array[stack->top] != '[' && stack->array[stack->top] != '{') { return; // Error: mismatched parentheses } else { pop(stack); } } } // Process any remaining operators in the stack while (!isEmpty(stack)) { result[index] = pop(stack); index++; result[index] = ' '; index++; } result[index] = '\0'; } /* Evaluate equations and print results */ void evaluateAndPrintResults(LinkedList *list) { // Add code to evaluate equations and print results } /* Save to output file (postfix and results) */ void saveToOutputFile(LinkedList *list) { // Add code to save postfix expressions and results to an output file } /* Main function */ int main() { int choice; LinkedList equationList; equationList.head = NULL; do { printf(" Menu: "); printf("1. Read equations from file "); printf("2. Print equations "); printf("3. Evaluate using Expression tree "); printf("4. Print postfix expressions "); printf("5. Save to output file (postfix and results) "); printf("6. Exit "); printf("Enter your choice: "); scanf("%d", &choice); switch (choice) { case 1: readEquationsFromFile(&equationList, "Input.txt"); break; case 2: printEquations(&equationList); break; case 3: // Add code to evaluate equations ; break; case 4: // Add code to print postfix expressions { ListNode *current = equationList.head; while (current != NULL) { char infix[100], postfix[100]; strcpy(infix, current->equation); infixToPostfix(infix, postfix); printf("Infix: %s, Postfix: %s ", infix, postfix); current = current->next; } } break; case 5: // Add code to save to output file saveToOutputFile(&equationList); break; case 6: printf("Exiting the program. "); break; default: printf("Invalid choice. Please enter a valid option. "); } } while (choice != 6); // Add code to free allocated memory for the linked list nodes return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Transactions On Large Scale Data And Knowledge Centered Systems Xxviii Special Issue On Database And Expert Systems Applications Lncs 9940

Authors: Abdelkader Hameurlain ,Josef Kung ,Roland Wagner ,Qimin Chen

1st Edition

3662534541, 978-3662534540

More Books

Students also viewed these Databases questions

Question

Find the derivative. f(x) 8 3 4 mix X O 4 x32 4 x32 3 -4x - x2

Answered: 1 week ago

Question

describe the main employment rights as stated in the law

Answered: 1 week ago