資料結構實驗之棧與佇列四 括號匹配

2021-08-08 19:24:03 字數 1480 閱讀 4376

time limit: 1000ms

memory limit: 65536kb

submit

statistic

problem description

給你一串字元,不超過50個字元,可能包括括號、數字、字母、標點符號、空格,你的任務是檢查這一串字元中的( ) ,[ ],是否匹配。

input

輸入資料有多組,處理到檔案結束。

output

如果匹配就輸出「yes」,不匹配輸出「no」

example input

sin(20+10)

]

example output

yes

no

遇到左括號入棧,遇到右括號和棧頂比較,如果不匹配則不匹配,直接break;如果出現右括號的時候當前棧已經是空的,那麼就不匹配;如果棧不空,

但是沒有別的括號再和棧中的元素匹配,那麼也不匹配。

/***************************本題比較容易忽略的一點************************/

題目中說中間可能會有空格,所以讀入字串的時候用gets();

#include #include typedef char elemtype;

typedef int status;

#define maxsize 100

#define overflow -2

#define another 50

#define true 1

#define false 0

typedef struct sqstack;

status isempty(sqstack &s)

void initstack(sqstack &s)

elemtype gettop(sqstack &s)

void push(sqstack &s, elemtype e)

*s.top++ = e;

}int pop(sqstack &s, elemtype &e)

int match(sqstack &s, char str)

else if(str[i] == ')')

char c = gettop(s);

if(c == '(')

else

} else if(str[i] == ']')

char c = gettop(s);

if(c == '[')

else

} else if(str[i] == '}')

char c = gettop(s);

if(c == '

else

} }if(!isempty(s))

flag = false;

return flag;

}int main()

return 0;

}

資料結構實驗之棧與佇列四 括號匹配

problem description 給你一串字元,不超過50個字元,可能包括括號 數字 字母 標點符號 空格,你的任務是檢查這一串字元中的 是否匹配。input 輸入資料有多組,處理到檔案結束。output 如果匹配就輸出 yes 不匹配輸出 no example input sin 20 10...

資料結構實驗之棧與佇列四 括號匹配

problem description 給你一串字元,不超過50個字元,可能包括括號 數字 字母 標點符號 空格,你的任務是檢查這一串字元中的 是否匹配。input 輸入資料有多組,處理到檔案結束。output 如果匹配就輸出 yes 不匹配輸出 no example input sin 20 10...

資料結構實驗之棧與佇列四 括號匹配

time limit 1000ms memory limit 65536kb submit statistic problem description 給你一串字元,不超過50個字元,可能包括括號 數字 字母 標點符號 空格,你的任務是檢查這一串字元中的 是否匹配。input 輸入資料有多組,處理到...