-
properly nested
Hey all, Ive been working on this program for a while and keep changing it around. Im trying to have it verify that the user inputted braces are properly nested. I keep coming up with the outcome that they are not, even if they are. any input on where im going wrong is appreciated.
Code:
#include<stdbool.h>
#include<stdio.h>
#define STACK_SIZE 100
char contents[STACK_SIZE];
int top = 0;
void stack_overflow(void)
{
printf("invalid under");
}
void stack_underflow(void)
{
printf("invalid over");
}
void make_empty(void)
{
top = 0;
}
bool is_empty(void)
{
return top==0;
}
bool is_full(void)
{
return top==STACK_SIZE;
}
char push(char ch)
{
if(is_full())
stack_overflow();
else
contents[top++]=ch;
}
char pop(char contents[])
{
if(is_empty())
stack_underflow();
else
return contents[--top];
}
int main(void)
{
char ch,fst,lst;
int i = 0;
printf( "Enter parenthesesor braces:\n" );
while ( ( i < STACK_SIZE ) && ( ( ch = getchar() ) != '\n' ) ) {
contents[i++] = ch;
if(('('==ch)||('{'==ch))
{push(ch);
fst = ch;
}
else if((')'==ch)||('}'==ch))
{
pop(contents);
lst = ch;
}
}
if ( fst == lst )
printf("\nParenthesis/brackets nested properly....");
else printf("\nParenthesis/brackets not nested properly...");
return 0;
}
-
Could you please give a sample input? :)
Do not use global variables , see the last post here
http://cboard.cprogramming.com/c-pro...c-ids-mpi.html
-
sample:
- should be nested properly
- not nested properly
-
You check for overflow, why not check for underflow? :)
You have for example
Code:
char pop(char contents[])
{
if(is_empty())
stack_underflow();
else
return contents[--top];
}
and then when you call pop in main you do not collect the return value.This was made by accident?
EDIT: Moreover, you check only the last character given by the user.Shouldn't you use a loop to test all the data that your stack holds? :)
-
Passing a global variable to the function?
From what i can tell it would never return true as this will never evaluate as true
Where are you checking if they are opposing brackets of equivalent type?
If there are three different brackets how are you storing that information in a single spot in a char? It looks like you are over writing the old value.
-
Yes to the external variables. Unfortunately.
I changed to final statement to
Code:
if ( fst == '(' && lst == ')' || fst == '{' && lst == '}')
printf("\nParenthesis/brackets nested properly....");
else printf("\nParenthesis/brackets not nested properly...");
but now it always runs as true....
-
heres other changes that i made, but still not working:
Code:
#include<stdbool.h>
#include<stdio.h>
#define STACK_SIZE 100
char contents[STACK_SIZE];
int top = 0;
void stack_overflow(void)
{
printf("invalid under");
}
void stack_underflow(void)
{
printf("invalid over");
}
void make_empty(void)
{
top = 0;
}
bool is_empty(void)
{
return top==0;
}
bool is_full(void)
{
return top==STACK_SIZE;
}
int push(int i)
{
if(is_full())
stack_overflow();
else
contents[top++]=i;
}
int pop(void)
{
if(is_empty())
stack_underflow();
else
return contents[--top];
}
int main(void)
{
char ch,fst,lst;
int i = 0;
printf( "Enter parenthesesor braces:\n" );
while ( ( i < STACK_SIZE ) && ( ( ch = getchar() ) != '\n' ) ) {
contents[i++] = ch;
if(('('==ch)||('{'==ch))
{push(i);
fst = contents[i];
}
else if((')'==ch)||('}'==ch))
{
pop();
lst = contents[i];
}
}
if ( fst == '(' && lst == ')' || fst == '{' && lst == '}')
printf("\nParenthesis/brackets nested properly....");
else printf("\nParenthesis/brackets not nested properly...");
return 0;
}
-
look at your push statement i think
does not do what you think it does... just simulate numbers in your head and follow the program this line overwrites what
Code:
contents[i++] = ch;
does
-
I changed three lines and got it to evaluate properly so you are close. When you figure that out then you'll have to figure out why
would evaluate as true :o)
-
Thanks for the encouragement! lol I know why its going to always be true- its because the lst is always going to be a right brace and fst will always be a left brace. Still messing with the push statement.
-
all push needs to do is increase top as long as it hasn't exceeded the limit. The first statement under the while saves the char into contents. By passing the int into the function and then contents you are just saving the number of times the loop has run over the actual parenthesis type you initially saved. So by time you get to comparing fst and lst you will not even have chars in the fst data type. fst will be an integer and last will be a char. Maybe you only need to do top++?
Then you could have two integers NumOfOpen and NumOfClosed that count up when you find each. These numbers also should evaluate as true when compared. After that works you need to maybe use an array to save the parenthesis char types so you can evaluate if they properly closed each other.
Remember your last opening parenthesis type should match your first closing parenthesis type and second to last matching second... Ect
-
Oh and by doing I++ during the first part of the while loop. You are saving char in data space one whole saying the top is one so the first data position would be 0 rather than one. Are you meaning to leave the first space of your array blank permanently? You could I++ at the end of the loop if that wasn't designed with a larger purpose in mind.