第五章專案三 括號的匹配

2021-08-09 07:40:34 字數 1803 閱讀 5748

/*

煙台大學計算機學院

檔名稱:main.cpp ti.cpp head.cpp

完成日期:2023年10月10日

問題描述:第五周專案三

假設表示式中允許三種括號:圓括號、方括號和大括號。編寫乙個演算法,判斷表示式中的各種左括號是否與右括號匹配。

例如,輸入2+(3+4)*[2+]-8,輸出匹配正確;輸入2+(3+4*[2)+-8,輸出匹配錯誤

輸入描述:

輸出描述:

*/#include #include #include #include "head.h"

using namespace std;

int main()

': pop(s,c);

if(c!='

}if(stackempty(s)&&d==1)

printf("配對正確!!\n");

else

printf("配對錯誤!!\n");

return 0;

}

#include #include #include "head.h"

void initstack(sqstack *&s)

void destroystack(sqstack *&s)

int stacklength(sqstack *s) //返回棧中元素個數——棧長度

bool stackempty(sqstack *s)

bool push(sqstack *&s,elemtype e)

bool pop(sqstack *&s,elemtype &e)

bool gettop(sqstack *s,elemtype &e)

void dispstack(sqstack *s) //輸出棧

#ifndef list_h_included

#define list_h_included

#define maxsize 100

typedef char elemtype;

typedef struct

sqstack; //順序棧型別定義

void initstack(sqstack *&s); //初始化棧

void destroystack(sqstack *&s); //銷毀棧

bool stackempty(sqstack *s); //棧是否為空

int stacklength(sqstack *s); //返回棧中元素個數——棧長度

bool push(sqstack *&s,elemtype e); //入棧

bool pop(sqstack *&s,elemtype &e); //出棧

bool gettop(sqstack *s,elemtype &e); //取棧頂資料元素

void dispstack(sqstack *s); //輸出棧

#endif

執行結果:

知識點總結:

這題巧妙的運用了順序棧裡的先進後出的原理,完成了左邊和右邊括號的配對。

學習心得:

我覺得棧還可以這樣運用到很多例項上。

第五章 模式匹配

第五章 模式匹配 b 匹配乙個字元邊界 t 匹配乙個普通水平製表字元 bar 匹配的是barrr 5.2 模式匹配操作符 m 用於匹配乙個模式,s 用於將某個符合模式的子字元竄代換為某個子串 每次成功匹配了乙個模式 包括替換中的模式 操作符都會把變數 分別設定為 匹配內容左邊內容,匹配的內容和匹配內...

第五章 體檢專案

using system using system.collections.generic using system.linq using system.threading.tasks using system.windows.forms namespace day05 體檢 管理系統 using ...

第五周專案三(括號的匹配)

問題及 all right reserved.檔名稱 括號的匹配.cpp 完成日期 2015年9月29日 版本號 v1.0 問題描述 假設表示式中允許三種括號 圓括號 方括號和大括號。編寫乙個演算法,判斷表示式中的各種左括號是否與右括號匹配。例如,輸入2 3 4 2 8,輸出匹配正確 輸入2 3 4...