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
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
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