Question
Create a C code to determine if the equations given to it are valid. The following code gets the equations from the equations.txt file the
Print ERROR ID 0 where 0 means no problems
Print ERROR ID 1 where 1 means variable missing on the left side of the =
Print ERROR ID 2 where 2 means missing =
Print ERROR ID 3 where 3 means mismatched()/{}/[] on the right side of the =
Equations.txt content:
a = ((c+d)*a)/(b/(d-a)) b = 4*[x + 3*(2*x + 1)] c = 5*{3 2*[1 4*(3 22)]} d = 5 + 2*{[3 + (2*x 1) + x] 2} e = 5 + 9 * 3 (5 + 9)*3 f (5 + 9)*3 g = 5 + 2*{[3 + (2*x 1) + x 2} h = 5 + 9 * 3) i = 5 + (9 * 3
Expected output:
***ERROR ID 0 on a = ((c+d)*a)/(b/(d-a))
***ERROR ID 0 on a = ((c+d)*a)/(b/(d-a))
***ERROR ID 0 on c = 5*{3 2*[1 4*(3 22)]}
***ERROR ID 0 on d = 5 + 2*{[3 + (2*x 1) + x] 2}
***ERROR ID 0 on e = 5 + 9 * 3
***ERROR ID 1 on (5 + 9)*3
***ERROR ID 2 on f (5 + 9)*3
***ERROR ID 3 on g = 5 + 2*{[3 + (2*x 1) + x 2}
***ERROR ID 3 on h = 5 + 9 * 3)
***ERROR ID 3 on i = 5 + (9 *3
current code:
// C program to Implement a stack
// using singly linked list
#include
#include
#include
// Declare linked list node
struct Node
{
char data;
struct Node *next;
};
int nodesCount = 0;
// Utility function to add an element `x` to the stack
void push(struct Node **top) // insert at the beginning
{
FILE *myFile;
myFile = fopen(\"equations.txt\", \"r\");
if (myFile == 0)
{
printf(\"file not opened \");
}
else
{
printf(\"file opened \");
}
// allocate a new node in a heap
struct Node *node = NULL;
node = (struct Node *)malloc(sizeof(struct Node));
// check if stack (heap) is full. Then inserting an element would
// lead to stack overflow
if (!node)
{
printf(\"Heap Overflow \");
exit(-1);
}
char equation; // to store the current character in the file
/*
* initialize the count to 1
*/
nodesCount = 0;
while (!feof(myFile))
{
/*
* Use fgetc to read the file by character
*/
equation = (char)fgetc(myFile);
// set data in the allocated node
node->data = equation;
// set the .next pointer of the new node to point to the current
// top node of the list
node->next = *top;
// update top pointer
*top = node;
/*
* Each time a node is added, it should be allocated new memory
*/
node = (struct Node *)malloc(sizeof(struct Node)); // allocate the memory to new node
// increase stack's size by 1
nodesCount += 1;
}
}
// Utility function to check if the stack is empty or not
int isEmpty(struct Node *top)
{
return top == NULL;
}
// Utility function to return the top element of the stack
int peek(struct Node *top)
{
// check for an empty stack
if (!isEmpty(top))
{
return top->data;
}
else
{
printf(\"The stack is empty \");
exit(EXIT_FAILURE);
}
}
// Utility function to pop a top element from the stack
int pop(struct Node **top) // remove at the beginning
{
struct Node *node;
// check for stack underflow
if (*top == NULL)
{
printf(\"Stack Underflow \");
exit(EXIT_FAILURE);
}
// take note of the top node's data
int x = peek(*top);
printf(\"Removing %d \", x);
node = *top;
// update the top pointer to point to the next node
*top = (*top)->next;
// decrease stack's size by 1
nodesCount -= 1;
// free allocated memory
free(node);
return x;
}
// Utility function to return the nodesCount of the stack
int size()
{
return nodesCount;
}
int mainmod(void)
{
struct Node *top = NULL;
push(&top);
if (isEmpty(top))
{
printf(\"The stack is empty \");
}
else
{
printf(\"The stack is not empty \");
}
// since the values read from the equations.txt are pushed into the stack
// they would be in reverse order. To get the actual order, we can copy these
// values into an array of characters and traverse it in reverse order
// get the size of the stack
int stackSize = size();
// allocate memory to array to store the characters
char *correctOrder = (char *)malloc(sizeof(char) * stackSize);
int i = 0; // index pointer for correctOrder array
// copy the elements to the array
struct Node *curr = top; // take a pointer at stack top to traverse
while (curr)
{
correctOrder[i++] = curr->data; // get the current character
curr = curr->next; // go to next node in the stack
}
// print the array in reverse order
for (int i = stackSize-1; i >= 0; i--)
{
printf(\"%c\",correctOrder[i]);
}
return 0;
}
int main(void)
{
mainmod();
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started