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

2021-08-08 20:53:59 字數 1166 閱讀 7349

problem description

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

input

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

output

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

example input

sin(20+10)

] example output

yes

no

思路:遇到左括號均進棧,右括號進行判斷

不匹配有三種情況:1.右括號與左括號不匹配 2.右括號多餘(棧空了) 3. 左括號多餘(棧非空)

#include 

#include

#define stackmax 100000

#define stacknum 100

#define ok 1

#define error 0

#define overflow -2

#define true 1

#define false 0

typedef int status;

typedef char selemtype;

typedef struct

sqstack;

int inistack(sqstack &s)

int push(sqstack &s, selemtype e)

*s.top=e;

s.top++;

return0;}

int pop(sqstack &s, selemtype &e)

status isempty(sqstack s)

int main()

else}}

else

if(s[i]=='}')

else}}

else

if(s[i]==']')

else}}

}if(!isempty(s))//遍歷完畢後,若棧非空:左括號多餘

if(flag)

printf("yes\n");

else

printf("no\n");

}return

0;}

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

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

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

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

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

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