Code:
- Program is supposed to check for balanced parentheses which are (), {}, [] using a vector
- First read in the number of lines user wishes to test
- Return Yes if balanced, No if unbalanced
- Missing conditions to check for unbalanced parentheses
- Program doesn't catch left parentheses returns Yes
- Pairs wrong parentheses together, (} returns Yes, should be No
- Cases with incorrect output
- (}, (], ))), }}}, ]]], just a space or nothing entered
- Looking for answers to how I can correctly catch the cases I've missed
Code:
#include <stdio.h>
#include <stdlib.h>
#include "char_vector.h"
int main( int argc, char * argv [])
{
int i, num;
char c;
scanf("%d", &num);
scanf("%c", &c);
for(i = 0; i < num; i++)
{
MY_VECTOR vector = my_vector_init_default();
do
{
scanf("%c", &c);
if (c == '(' || c == '[' || c == '{')
{
char_my_vector_push_back(vector, c);
}
if (c == ')' || c == ']' || c == '}')
{
my_vector_pop_back(vector);
}
}while(c != '\n');
if (my_vector_size(vector) == 0)
{
printf( "Yes\n");
}
else
{
printf( "No\n");
}
my_vector_destroy(&vector);
}
return 0;
}