Home > Back-end >  Bosses have a look at me why this program didn't output? And how to change?
Bosses have a look at me why this program didn't output? And how to change?

Time:10-01

# include
# include
# define stack_init_size 100
# define stack_increment 10
# define TRUE 1
# define FALSE 0

Typedef char ElemType;
Typedef struct {
ElemType * base;
ElemType * top;
Int stacksize;
} SqStack;
Void init (SqStack & amp; S)
{
S.b ase=(ElemType *) malloc (stack_init_size * sizeof (ElemType));
If (S.b ase)
{
exit(-1);
}
S.t op=S.b ase;
S.s tacksize=stack_init_size;
}
Void getTop (SqStack S, ElemType & amp; E)
{
If (S.t op==S.b ase)
{
return;
}
E=* (S.t op - 1);
}
Void pop (SqStack & amp; S, ElemType & amp; E)
{
If (S.t op=S.b ase)
{
return;
}
E=* - S.t op;
}
Void push (SqStack & amp; S, ElemType c)
{
If (S.t op - S.b ase==S.s tacksize)
{
S.b ase=(ElemType *) realloc (S.b ase, (S.s tacksize + stack_increment) * sizeof (ElemType));
if(! S.b ase) {
exit(-1);
}
S.t op=S.b ase + S.s tacksize;
S.s tacksize +=stack_increment;
}
* (S.t op++)=c;
}
Int isEmpty (SqStack S)
{
If (S.t op==S.b ase)
{
Return TRUE;
}
The else
{
Return FALSE;
}
}
Int isLeft (char & amp; C)
{
If (c=='(' | | c==' [' | | c=='{')
{
Return TRUE;
}
The else
{
Return FALSE;
}

}
Int isMatch (char & amp; C, char & amp; E)
{
If ((c==') & amp; & E=='(') | | (c=='] '& amp; & E=='[') | | (c=='} '& amp; & amp; e==' {'))
{
Return TRUE;
}
The else
{
Return FALSE;
}
}
Int checkBrackets (char [] s)
{
int i=0;
SqStack S;
Init (S);
ElemType c, e,
While ((c=s [i++])!='\ 0')
{
If (isLeft (c))
{
Push (S, c);
}
The else
{
If (isEmpty (S))
{
return 0;
}
The else
{
GetTop (S, e);
If (isMatch (c, e))
{
Pop (S, e);
}
The else
{
return 0;
}
}
}
}
If (isEmpty (S))
{
return 1;
}
return 0;
}
Int main (void)
{
Char [] s={' [', '(',') ', '] ', '{','} '};
Int m=checkBrackets (s);
If (m==1)
{
Printf (" yes ");
}
The else
{
Printf (" no ");
}
return 0;
}
Topic: 1, the input from the keyboard a includes only (,), [and], a total of 6 {and} character string of brackets, using stack to check its legitimacy, such as: [] {}, [{} ()] legal; [] ({}, {} is illegal, in order to facilitate I use arrays instead of directly input, ask how change can be a legal character input yes, no illegal input

CodePudding user response:

CodePudding user response:

reference 1/f, the wise know already should good karma response:
code wrong
For such data can do
{/}
Only calculate the number is no good

CodePudding user response:

reference 1/f, the wise know already should good karma response:
and so did the statistics of the number of each symbol cycle is very bad

CodePudding user response:

When Posting put code [code] tag inside the
Can retain format and highlight
No one wants to see you now this code

CodePudding user response:

reference 4 floor lin5161678 response:
when Posting put code [code] tag inside the
Can retain format and highlight
Now this code nobody you want to see

Ok, next time

CodePudding user response:

reference 4 floor lin5161678 response:
when Posting put code [code] tag inside the
Can retain format and highlight
Now this code nobody you want to see

Ok, next time

CodePudding user response:

 while ((c=s [i++])!='\ 0') 

S not you save 0
You can't the loop at the end of the reasonable

CodePudding user response:

Legal [{} ()], [] {} is illegal, said very busy, solved?

CodePudding user response:

refer to the eighth floor wise men know already should good karma response:
{} () legal, [] {} is illegal, said very busy, solved?
solution is simple to
As need to solve this problem is the abnormal
Parentheses matching is a basic introduction to application is the stack of exercises

CodePudding user response:

Use an array to design two kinds of logic, beginning elementary arithmetic parentheses first brackets brackets, not for illegal use parentheses to adapt to cuhk parentheses, university of logic or programming logic check order symmetric regardless of bracket type only, write the code above has to fit application scenarios, and free to write in the future.

Look at the busy.

CodePudding user response:

CodePudding user response:

I don't know with dried shrimp test
  • Related