Reputation: 23
I'm trying to do the parentheses balance problem and adjust the following code to work with command-line input (argc/argv), but I can't figure out how to properly push variables to the stack and do comparisons between the stack and parentheses.
balance.c: In function 'push':
balance.c:16:18: error: expected expression before 'char'
len = strlen(char[i]);
^~~~
balance.c:19:9: error: expected expression before 'char'
char[i]=other[i];
^~~~
balance.c:25:22: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
s.stk[s.top] = other;
^
balance.c: In function 'main':
balance.c:55:33: warning: comparison between pointer and integer
if(s.stk[s.top] == "(")
I was hoping to get advice on how to properly format cmd input so I would be able to make comparisons. I've adjusted some of them by using strcmp, but am not sure how to move forward.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 20
struct stack
{
char stk[MAX];
int top;
}s;
void push(char* item)
{
int len;
int i;
len = strlen(char[i]);
char other [len];
for(i=0;i<len;i++)
char[i]=other[i];
if (s.top == (MAX - 1))
printf ("Stack is Full\n");
else
{
s.top = s.top + 1; // Push the char and increment top
s.stk[s.top] = other;
}}
void pop()
{
if (s.top == - 1)
{
printf ("Stack is Empty\n");
}
else
{
s.top = s.top - 1; // Pop the char and decrement top
}}
int main(int argc, char* argv[])
{
int i = 0;
s.top = -1;
for(i = 0;i < argc;i++)
{
if((strcmp(argv[i],"(")==0) || (strcmp(argv[i],"[")==0) || (strcmp(argv[i],"{")==0))
{
push(argv[i]); // Push the open bracket
continue;
}
else if((strcmp(argv[i],")")==0) || (strcmp(argv[i],"]")==0) || (strcmp(argv[i],"}")==0)) // If a closed bracket is encountered
{
if((strcmp(argv[i],")")==0))
{
if(s.stk[s.top] == "(")
{
pop(); // Pop the stack until closed bracket is found
}
else
{
printf("\nUNBALANCED EXPRESSION\n");
break;
}}
if((strcmp(argv[i],"]")==0))
{
if(s.stk[s.top] == "[")
{
pop(); // Pop the stack until closed bracket is found
}
else
{
printf("\nUNBALANCED EXPRESSION\n");
break;
}}
if((strcmp(argv[i],"}")==0))
{
if(s.stk[s.top] == "{")
{
pop(); // Pop the stack until closed bracket is found
}
else
{
printf("\nUNBALANCED EXPRESSION\n");
break;
}}}}
if(s.top == -1)
{
printf("\nBALANCED EXPRESSION\n"); // Finally if the stack is empty, display that the expression is balanced
}}
Upvotes: 0
Views: 103
Reputation: 9629
Problems:
len = strlen(char[i]);
Does not mean nothing. If you want to get the size of item
, you must write:
len = strlen(item);
char other [len];
for(i=0;i<len;i++)
char[i]=other[i];
You try to copy the uninitiated data of other
into...item
?
In that case, you should have written
char other [len+1];
for(i=0;i<len;i++)
other[i] = item[i];
other[len] = '\0'; // do not forget the final character of a NULL terminated string.
or
char other [len+1];
strcpy(other, item);
But, since other
will be use in the stack, so outside of push
, it can't be internal to push
, you have to reserve some memory with malloc
or friend functions. In that case, you don't need len
and a simple
char *other = strdup(item); //( ~ malloc + strcpy in one function)
whould be enough.
(re)But given the stack and the code using it, it seems you only stack single characters. In that case, you could simplify push
to:
void push(char* item)
{
if (s.top == (MAX - 1))
printf ("Stack is Full\n");
else
{
s.top = s.top + 1; // Push the char and increment top
s.stk[s.top] = item[0];
}
}
On one other side, your main function seems complicated using strcmp
where simple char
comparison would suffice:
(strcmp(argv[i],"(")==0)
can be written
( argv[i][0] == '(' )
And you stack character checking is wrong:
if(s.stk[s.top] == "(")
should be writteng
if(s.stk[s.top] == '(')
Moreover, your code could have a real interest using switch/case
:
int main(int argc, char* argv[])
{
int i = 0;
s.top = -1;
for(i = 0;i < argc;i++)
{
switch(argv[i][0]) {
case '(':
case '[':
push(argv[i]); // Push the open bracket
break;
case ')':
if(s.stk[s.top] == '(') // note what happend in s.top is -1?
pop()
else
printf("\nUNBALANCED EXPRESSION\n");
return;
case ']':
if(s.stk[s.top] == ']') // note what happend in s.top is -1?
pop()
else
printf("\nUNBALANCED EXPRESSION\n");
return 0;
}
}
if(s.top == -1)
{
printf("\nBALANCED EXPRESSION\n"); // Finally if the stack is empty, display that the expression is balanced
}
return 0;
}
Upvotes: 1