我已經寫好了status.h, stack.h, stack.c, 在 main.c 有一部分不會.通過這部分代碼來進行比對是否是配對的括號。求助各位大佬。小白特白,剛剛接觸c
測驗輸入:
3
([])
(([{}])))
([()[]()])()
測驗結果:
yes
no
yes
代碼如下:
----------------------------------------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);
Status stack_push(STACK hStack, char value);
Status stack_pop(STACK hStack);
char stack_top(STACK hStack, Status* pStatus);
Boolean stack_empty(STACK hStack);
void stack_destory(STACK* phStack);
#endif
----------------------------------------------------stack.c
#include <stdio.h>
#include <stdlib.h>
#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->capacity = 8;
pStack->size = 0;
pStack->data = (char*) malloc(sizeof(char) * pStack->capacity);
if (pStack->data=https://bbs.csdn.net/topics/=NULL)
{
free(pStack);
return NULL;
}
}
return (STACK)pStack;
}
Status stack_push(STACK hStack, char value)
{
Stack* pStack = (Stack*)hStack;
char* temp;
int i;
if(pStack->size>=pStack->capacity)
{
temp = (char*)malloc(sizeof(char)*(pStack->capacity *2));
if (temp == NULL)
{
return FAILURE;
}
for (i=0;i<pStack->size;i++)
{
temp[i]=pStack->data[i];
}
free(pStack->data);
pStack->data=https://bbs.csdn.net/topics/temp;
pStack->capacity *=2;
}
pStack->data[pStack->size]=value;
pStack->size++;
return FAILURE;
}
Status stack_pop(STACK hStack)
{
Stack* pStack = (Stack*)hStack;
if(stack_empty(hStack))
{
return FAILURE;
}
pStack->size--;
return SUCCESS;
}
char stack_top(STACK hStack, Status* pStatus)
{
Stack* pStack = (Stack*)hStack;
if(stack_empty(hStack))
{
if(pStatus != NULL)
{
*pStatus=FAILURE;
}
return '*';
}
if(pStatus != NULL)
{
*pStatus = SUCCESS;
}
return pStack->data[pStack->size-1];
}
Boolean stack_empty(STACK hStack)
{
Stack* pStack = (Stack*)hStack;
return (Boolean)(pStack->size<=0);
}
void stack_destory(STACK* phStack)
{
Stack* pStack = (Stack*)*phStack;
free(pStack->data);
free(pStack);
*phStack=NULL;
}
-----------------------------------------------main.c
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
int test_case_is_good(void);
void clear_keyboard_buffer(void);
int main(int argc, char* argv[])
{
int n;
int i;
scanf("%d",&n);
clear_keyboard_buffer();
for (i=0;i<n;i++)
{
if(test_case_is_good())
{
printf("Yes\n");
}
else
{
printf("No\n");
}
}
return 0;
}
int test_case_is_good(void)
{
char c;
scanf("%c",&c);
int know_answer =0;
while(c!='\n' )
{
if(!know_answer)
{
//
}
scanf("%c",&c);
}
return 1;
}
void clear_keyboard_buffer(void)
{
char c;
int noc;
noc = scanf("%c", &c);
while( noc==1 && c!= '\n')
{
scanf("%c", &c);
}
}
uj5u.com熱心網友回復:
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 == '{')
{
stack[stackLen++] = *pch;
}
else if (*pch == ')')
{
if (stackLen > 0 && stack[stackLen - 1] == '(')
{
stack[--stackLen] = 0;
}
else
{
result = false;
break;
}
}
else if (*pch == ']')
{
if (stackLen > 0 && stack[stackLen - 1] == '[')
{
stack[--stackLen] = 0;
}
else
{
result = false;
break;
}
}
else if (*pch == '}')
{
if (stackLen > 0 && stack[stackLen - 1] == '{')
{
stack[--stackLen] = 0;
}
else
{
result = false;
break;
}
}
else
{
result = false;
break;
}
++pch;
}
result &= stackLen == 0;
printf(result ? "yes\r\n" : "no\r\n");
printf("繼續測驗嗎?輸入y/n:");
char input = 0;
scanf_s("%c", &input, sizeof(char));
run = input == 'y';
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/232764.html
標籤:C語言
下一篇:QCefView初始化錯誤
