Home > Back-end >  Help parentheses matching problem (stack),. The main part of c code can't write, request expert
Help parentheses matching problem (stack),. The main part of c code can't write, request expert

Time:12-11

I have already written status. J h, stack. J h, stack. C, in the main. Part c has not. Through this part of the code is to compare a pair of parentheses, turn your bosses, small white, white, just contact c

Test inputs:

3
([])
((({}))))
([[] () ()]) ()

Test results:
Yes
No
Yes



The code is as follows:
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - status. H

# # ifndef STATUS_H
# define STATUS_H


Enum status {
FAILURE, SUCCESS
};

Typedef enum status status;

Enum Boolean {
FALSE, TRUE,
};

Typedef enum Boolean Boolean;

# endif

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - stack. H
# # ifndef STACK_H
# define STACK_H
# include "status. H"

Typedef void * STACK;

STACK stack_init_default (void);

The Status stack_push (STACK hStack, char value);
The Status stack_pop (STACK hStack);
Char stack_top (STACK hStack, the Status * pStatus);
Boolean stack_empty (STACK hStack);

Void stack_destory (STACK * phStack);

# endif

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - stack. C
#include
#include
# include "stack. H"

Struct stack
{
int size;
Int capacity;
Char * data;
};

Typedef struct stack stack;

STACK stack_init_default (void)
{


Stack * pStack;
PStack=(Stack *) malloc (sizeof (Stack));
If (pStack!=NULL)
{
PStack - & gt; Capacity=8;
PStack - & gt; Size=0;
PStack - & gt; Data=https://bbs.csdn.net/topics/(char *) malloc (sizeof (char) * pStack -> capacity);
If (pStack - & gt; data=https://bbs.csdn.net/topics/=NULL)
{
Free (pStack);
Return NULL;
}
}

Return (STACK) pStack;
}

The Status stack_push (STACK hStack, char value)
{
Stack * pStack=(Stack *) hStack;
Char * temp;
int i;

If (pStack - & gt; Size>=pStack - & gt; Capacity)
{
Temp=(char *) malloc (sizeof (char) * (pStack - & gt; Capacity * 2));
If (temp==NULL)
{
Return FAILURE;
}
for (i=0; i{
\ [I]=pStack - & gt; Data [I];

}
Free (pStack - & gt; The data);
PStack - & gt; data=https://bbs.csdn.net/topics/temp;
PStack - & gt; Capacity *=2;

}

PStack - & gt; Data [pStack - & gt; size]=value;
PStack - & gt; Size++;




Return FAILURE;
}

The Status stack_pop (STACK hStack)
{
Stack * pStack=(Stack *) hStack;
If (stack_empty (hStack))
{
Return FAILURE;
}

PStack - & gt; Size -;
The return SUCCESS.
}

Char stack_top (STACK hStack, the Status * pStatus)
{
Stack * pStack=(Stack *) hStack;
If (stack_empty (hStack))
{
If (pStatus!=NULL)
{
* pStatus=FAILURE;
}
Return the '*';
}

If (pStatus!=NULL)
{
* pStatus=SUCCESS;

}
Return pStack - & gt; Data [pStack - & gt; size - 1].
}


Boolean stack_empty (STACK hStack)
{
Stack * pStack=(Stack *) hStack;
Return (Boolean) (pStack - & gt; Size<=0);
}

Void stack_destory (STACK * phStack)
{
Stack * pStack=(Stack * * phStack;
Free (pStack - & gt; The data);
Free (pStack);
* phStack=NULL;
}

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - main. C
#include
#include
# include "stack. H"


Int test_case_is_good (void);
Void clear_keyboard_buffer (void);

Int main (int arg c, char * argv [])
{
int n;
int i;

The scanf (" % d ", & amp; n);
Clear_keyboard_buffer ();
for (i=0; i{
If (test_case_is_good ())
{
Printf (" Yes \ n ");
}
The else
{
Printf (" \ n ");
}
}
return 0;
}

int test_case_is_good (void)
{
Char c;
The scanf (" % c ", & amp; C);

Int know_answer=0;
While (c! )
='\ n'{
if(! Know_answer)
{
//
}
The scanf (" % c ", & amp; C);
}

return 1;
}


Void clear_keyboard_buffer (void)
{
Char c;
Int noc;
Noc=the scanf (" % c ", & amp; C);
While (noc==1 & amp; & C!!! )
='\ n'{
The scanf (" % c ", & amp; C);
}
}

CodePudding user response:

 
Void Test (void)
{
Char buf [512].
Char stack [512].

Bool run=true;
While (run)
{
Buf [0]=0;

While (buf [0]==0)
{
Gets_s (buf, 512);
}

Int stackLen=0;
Stack [0]=0;

Bool result=true;
Char * PCH=buf;
While (* PCH!=0)
{
If PCH (*=='('
| | * PCH=='['
| | * PCH=='{')
{
PCH stack [stackLen++]=*;
}
Else if PCH (*==') ')
{
If (stackLen & gt; 0 & amp; & Stack [stackLen - 1]=='(')
{
Stack [-- stackLen]=0;
}
The else
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related