Question: I'm writing a C program that reads from a file expressions of parenthesis (i.e. {[[()]{()()}()][()]}). There could be up to 25 different expressions in the

I'm writing a C program that reads from a file expressions of parenthesis (i.e. {[[()]{()()}()][()]}). There could be up to 25 different expressions in the file and the program is supposed to read each one and tell you if its balanced or not. The first line of the file is the numer of expressions there are so that should not be read. I had my program working for just entering an expression in manually(so my parenthesis checker is working properly). But once I got to reading the file its now saying the expression is invalid every time. if I could get some help where I went wrong with the file io that would be great. here is my code so far:

#include #include #include

int top = -1; char stack[100];

void push(char); void pop(); void find_top;

void main() { FILE * fp; fp = fopen("expressions.txt", "r"); int i; char exp[100]; while(!feof(fp)) { fgets(a, 100, fp); for(i = 0; exp[i] != '\0'; i++) { if(exp[i] == '(' || exp[i] == '{' ||exp[i] == '[' || ) { push(exp[i]); } else if(exp[i] == ')' ||exp[i] == '}' ||exp[i] == ']' ||) { pop(); } else{ printf("Invalid characters "); exit(0); } } } find_top(); void push(char exp) { stack[top] = a; top++; } void pop() { if(top == -1) { printf("Expression is invalid. "); exit(0); } else top--; } void find_top() { if(top == -1) printf("Expression is balanced. "); else printf("Expression is not balanced. "); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!