Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using c languge , Your application should be able to show the following information through a proper menu of the application: 1. Read equations from

Using c languge , Your application should be able to show the following information through a proper menu of the application: 1. Read equations from input file by using array or linked list,White spaces or special characters are ignored. 2. Print equations we read from file 3. Evaluate using Expression tree 4. Print postfix expressions 5. Save to output file (postfix and results) 6. Exit

On this projct i writ the code put it error on case 3 and dont print the right resut evaluation , so pleas solve it errors #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'; } /* Save to output file (postfix and results) */ void saveToOutputFile(LinkedList *list) { // Add code to save postfix expressions and results to an output file } /* Create a new tree node */ TreeNode* createNode(char data) { TreeNode* newNode = (TreeNode*)malloc(sizeof(TreeNode)); newNode->data = data; newNode->left = newNode->right = NULL; return newNode; } /* Build expression tree from postfix expression */ TreeNode* buildExpressionTree(char postfix[]) { struct Stack* stack = createStack(strlen(postfix)); for (int i = 0; postfix[i] != '\0'; i++) { if (isalnum(postfix[i])) { // If it's an operand, create a new node and push its value to the stack TreeNode* operandNode = createNode(postfix[i]); push(stack, operandNode->data); } else if (postfix[i] == ' ') { continue; // Skip spaces } else { // Operator encountered TreeNode* operatorNode = createNode(postfix[i]); // Pop operands from the stack TreeNode* operand2 = pop(stack); TreeNode* operand1 = pop(stack); // Set the operands as the left and right children of the operator node operatorNode->right = operand2; operatorNode->left = operand1; // Push the value of the operator node back to the stack push(stack, operatorNode->data); } } // The final result is at the top of the stack char result = pop(stack); // Clean up free(stack->array); free(stack); // Create a new tree node for the result TreeNode* finalResult = createNode(result); return finalResult; } /* Print infix expression of the expression tree (in-order traversal) */ void printInfix(TreeNode* root) { if (root != NULL) { printInfix(root->left); printf("%c ", root->data); printInfix(root->right); } } /* Evaluate and print results using expression tree */ void evaluateAndPrintResults(LinkedList* list) { ListNode* current = list->head; while (current != NULL) { char infix[100], postfix[100]; strcpy(infix, current->equation); infixToPostfix(infix, postfix); // Build expression tree TreeNode* expressionTree = buildExpressionTree(postfix); // Print the infix expression of the expression tree if it's not NULL if (expressionTree != NULL) { printf("tree expression: "); printInfix(expressionTree); printf(" "); // Evaluate the expression tree and print the result int result = evaluateExpressionTree(expressionTree); printf("Result: %d ", result); } else { printf("Expression tree is NULL. "); } current = current->next; } } /* Evaluate expression tree and return the result */ int evaluateExpressionTree(TreeNode* root) { if (root == NULL) { return 0; // If the tree is empty, return 0 (you can change this behavior based on your requirements) } if (isdigit(root->data)) { return root->data - '0'; // Convert character digit to integer } int leftResult = evaluateExpressionTree(root->left); int rightResult = evaluateExpressionTree(root->right); switch (root->data) { case '+': return leftResult + rightResult; case '-': return leftResult - rightResult; case '*': return leftResult * rightResult; case '/': return leftResult / rightResult; case '%': return leftResult % rightResult; default: return 0; // Handle unknown operators (you can change this behavior based on your requirements) } } /* 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 evaluateAndPrintResults(&equationList); 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

AutoCAD Database Connectivity

Authors: Scott McFarlane

1st Edition

0766816400, 978-0766816404

More Books

Students also viewed these Databases questions

Question

For a given soil, show that Ysat - Ya Ya Ya - Ysat + Yu

Answered: 1 week ago

Question

What are ergonomics?

Answered: 1 week ago

Question

You have

Answered: 1 week ago