Hey guys, I'm new here.. So I want to apologize beforehand if my question seems confusing or if I accidentally do something completely wrong.. :/
Anyway, I'm doing a parentheses checker for my programming class. To be quite honest, the teacher doesn't have much to say so I pretty much do everything out of the book or on my own.
However, I can't seem to get this problem to work. It's supposed to take the file from the command line and check if the parentheses within each line is correctly put. However, I can't seem to compile it.. What am I doing wrong??
There are 3 layers to this program: Main/application, the interface, and the primitives..
Here is my code for main and the interface. I don't think I need to post up the code for my primitives.. If I do, please let me know!
Code:
/******************/
/* main.c */
/******************/
#include <stdio.h>
#include <stdlib.h>
#include "globals.h"
#include "stack.h"
#define ERR_PUSH
#define ERR_POP
#define ERR_MATCH
#define ERR_EOL
char matching_symbol( char c );
int main( int argc, char *argv[] ){
char c;
char *ptr;
char buffer[BUFSIZ];
bool error;
stack S;
init_stack(&S);
printf( "? " );
while( gets( buffer ) != NULL && buffer[0] != '\0' ){
error = FALSE;
for( ptr = buffer; *ptr != '\0' && error == FALSE; ptr++ ){
switch( *ptr ){
case '(':
case '[':
case '{':
if( push_char( &S, *ptr ) == ERROR ){
error = TRUE;
printf( ERR_PUSH, ptr - buffer + 1 );
}
break;
case ')':
case ']':
case '}':
if( pop_char( &S, &c ) == ERROR ){
error = TRUE;
printf( ERR_POP, ptr - buffer + 1 );
break;
}
if( *ptr != matching_symbol( c ) ){
error = TRUE;
printf( ERR_MATCH, *ptr, ptr - buffer + 1, matching_symbol( c ) );
}
break;
}
}
if( error == TRUE ){
while( empty_stack( &S ) == FALSE ){
pop_char( &S, &c );
}
}
else if( empty_stack( &S ) == TRUE ){
printf( "Valid input.\n" );
}
else{
printf( ERR_EOL );
while( empty_stack( &S ) == FALSE ){
pop_char( &S, &c );
printf( "%c ", matching_symbol( c ) );
}
printf( "\n" );
}
printf( "? " );
}
}
char matching_symbol( char c ){
switch ( c ){
case '(': return ')';
case ')': return '(';
case '[': return ']';
case ']': return '[';
case '{': return '}';
case '}': return '{';
}
return 0;
}
Code:
/*********************/
/* stackinterface.c */
/*********************/
#include <stdlib.h>
#include "stackinterface.h"
#include "globals.h"
/* define push_char */
extern status push_char( stack *p_S, char c ){
char *p_c = (char *) malloc(sizeof(char));
if( p_c == NULL )
return( ERROR );
*p_c = c;
if( push( p_S, p_c ) == ERROR ){
free( p_c );
return( ERROR );
}
return( OK );
}
/* define pop_char */
extern status pop_char( stack *p_S, char *p_c ){
char *p_data;
if( pop( p_S, &p_data ) == ERROR )
return( ERROR );
*p_c = *p_data;
free( p_data );
return( OK );
}
/* define top_char */
extern status top_char( stack *p_S, char *p_c ){
char *p_data;
if( top( p_S, &p_data ) == ERROR )
return( ERROR );
*p_c = *p_data;
return( OK );
}
It keeps saying there is an error with: ERR_PUSH, ERR_POP, ERR_MATCH, and ERR_EOL..
Please save me! Ahhh. Thanks so much.