Exemplo n.º 1
0
int char_stack_test( int argc, char ** argv )
{
	char_stack_t * stack = char_stack_init();
	printf("initial char_stack root is empty? %s\n",
		char_stack_is_empty( stack ) ? "yes" : "no");

	printf("stack bfore push %p, val is %c\n",stack,stack->c);
	stack = char_stack_push( stack, 'c');
	stack = char_stack_push( stack, 'd');
	char_stack_dump( stack,stdout );
	printf("stack after push %p, val is %c\n",stack,stack->c);
	stack = char_stack_pop( stack);
	printf("stack after pop %p, popped val is %c\n",stack,stack->c);
	stack = char_stack_pop( stack);
	//printf("pushed a char to %%p, is empty? %s\n",
		//char_stack_is_empty( stack ) ? "yes" : "no");
	char_stack_dump( stack,stdout );
	char_stack_free(stack);
   return 0;
}
Exemplo n.º 2
0
void char_stack_push_string(struct CharStack *stack, char *str)
{
    int str_length = strlen(str);
    if (stack->stack_ptr + str_length >= STACK_SIZE) {
        printf("String \"%s\" is too long to be pushed into stack.\n");
        return;
    }

    int i;
    for(i=0; i < str_length; i++)
        char_stack_push(stack, str[i]);
}
Exemplo n.º 3
0
void checkForBalancedParensBracketsBraces( char * filename )
{
	FILE * fp = fopen(filename,"rb");
	if( !fp )
	{
		fprintf(stderr,"Error opening file %s, %s\n",filename,strerror(errno));
		return;
	}

	int MAX_CHARS = 1024;
	int nCharsRead;
	char buf[MAX_CHARS];
	char * charsRead;
	char lastChar;
	char curChar;

	struct char_stack * cs = char_stack_init();

	int lineno = 1;

   while( (charsRead = fgets(buf,MAX_CHARS,fp)) != NULL )
   {
      nCharsRead = strlen(charsRead);
      for(int i = 0; i < nCharsRead; i++)
      {
         curChar = charsRead[i];
			if(curChar == '\n')
				lineno++;

			switch(curChar)
			{
				case '(':
				case '[':
				case '{':
					cs = char_stack_push( cs,curChar);
					break;

				case ')':
					if( ! is_balanced( cs, '(') )
						printf("Out of balance at line %d, found %c, expected to match %c\n",
							lineno, cs->c, '(');
					cs = char_stack_pop(cs);
					break;

				case ']':
					if( ! is_balanced( cs, '[') )
						printf("Out of balance at line %d, found %c, expected to match %c\n",
							lineno, cs->c, '[');
					cs = char_stack_pop( cs );
					break;
				case '}':
					if( ! is_balanced( cs, '{') )
						printf("Out of balance at line %d, found %c, expected to match %c\n",
							lineno, cs->c, '{');
					cs = char_stack_pop( cs );
					break;

			}
		}
	}

	if( ! char_stack_is_empty(cs) )
	{
		printf("Out of balance at end of source, dumping what's left\n");
		char_stack_dump(cs,stdout);
	}
	//char_stack_free(cs);
}